CompilerChef.java revision 1bbaf7cdf7f9d93ae09365192abb2288cf0dfb41
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14package android.databinding.tool; 15 16import android.databinding.tool.store.ResourceBundle; 17import android.databinding.tool.util.L; 18import android.databinding.tool.writer.DataBinderWriter; 19import android.databinding.tool.writer.JavaFileWriter; 20 21/** 22 * Chef class for compiler. 23 * 24 * Different build systems can initiate a version of this to handle their work 25 */ 26public class CompilerChef { 27 private JavaFileWriter mFileWriter; 28 private ResourceBundle mResourceBundle; 29 private DataBinder mDataBinder; 30 31 private CompilerChef() { 32 } 33 34 public static CompilerChef createChef(ResourceBundle bundle, JavaFileWriter fileWriter) { 35 CompilerChef chef = new CompilerChef(); 36 37 chef.mResourceBundle = bundle; 38 chef.mFileWriter = fileWriter; 39 chef.mResourceBundle.validateMultiResLayouts(); 40 return chef; 41 } 42 43 public ResourceBundle getResourceBundle() { 44 return mResourceBundle; 45 } 46 47 public void ensureDataBinder() { 48 if (mDataBinder == null) { 49 mDataBinder = new DataBinder(mResourceBundle); 50 mDataBinder.setFileWriter(mFileWriter); 51 } 52 } 53 54 public boolean hasAnythingToGenerate() { 55 L.d("checking if we have anything to generate. bundle size: %s", 56 mResourceBundle == null ? -1 : mResourceBundle.getLayoutBundles().size()); 57 return mResourceBundle != null && mResourceBundle.getLayoutBundles().size() > 0; 58 } 59 60 public void writeDbrFile(int minSdk) { 61 ensureDataBinder(); 62 final String pkg = "android.databinding"; 63 DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(), 64 "DataBinderMapper", mDataBinder.getLayoutBinders(), minSdk); 65 mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write()); 66 } 67 68 /** 69 * Adds variables to list of Bindables. 70 */ 71 public void addBRVariables(BindableHolder bindables) { 72 ensureDataBinder(); 73 for (LayoutBinder layoutBinder : mDataBinder.mLayoutBinders) { 74 for (String variableName : layoutBinder.getUserDefinedVariables().keySet()) { 75 bindables.addVariable(variableName, layoutBinder.getClassName()); 76 } 77 } 78 } 79 80 public void writeViewBinderInterfaces(boolean isLibrary) { 81 ensureDataBinder(); 82 mDataBinder.writerBaseClasses(isLibrary); 83 } 84 85 public void writeViewBinders(int minSdk) { 86 ensureDataBinder(); 87 mDataBinder.writeBinders(minSdk); 88 } 89 90 public interface BindableHolder { 91 void addVariable(String variableName, String containingClassName); 92 } 93} 94