CompilerChef.java revision b6887f1479c3ecec38a7989748ef33de1fbcd973
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
21import java.util.Set;
22
23/**
24 * Chef class for compiler.
25 *
26 * Different build systems can initiate a version of this to handle their work
27 */
28public class CompilerChef {
29    private JavaFileWriter mFileWriter;
30    private ResourceBundle mResourceBundle;
31    private DataBinder mDataBinder;
32
33    private CompilerChef() {
34    }
35
36    public static CompilerChef createChef(ResourceBundle bundle, JavaFileWriter fileWriter) {
37        CompilerChef chef = new CompilerChef();
38
39        chef.mResourceBundle = bundle;
40        chef.mFileWriter = fileWriter;
41        chef.mResourceBundle.validateMultiResLayouts();
42        return chef;
43    }
44
45    public ResourceBundle getResourceBundle() {
46        return mResourceBundle;
47    }
48
49    public void ensureDataBinder() {
50        if (mDataBinder == null) {
51            mDataBinder = new DataBinder(mResourceBundle);
52            mDataBinder.setFileWriter(mFileWriter);
53        }
54    }
55
56    public boolean hasAnythingToGenerate() {
57        L.d("checking if we have anything to generate. bundle size: %s",
58                mResourceBundle == null ? -1 : mResourceBundle.getLayoutBundles().size());
59        return mResourceBundle != null && mResourceBundle.getLayoutBundles().size() > 0;
60    }
61
62    public void writeDbrFile(int minSdk) {
63        ensureDataBinder();
64        final String pkg = "android.databinding";
65        DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(),
66                "DataBinderMapper", mDataBinder.getLayoutBinders(), minSdk);
67        mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write());
68    }
69
70    /**
71     * Adds variables to list of Bindables.
72     */
73    public void addBRVariables(BindableHolder bindables) {
74        ensureDataBinder();
75        for (LayoutBinder layoutBinder : mDataBinder.mLayoutBinders) {
76            for (String variableName : layoutBinder.getUserDefinedVariables().keySet()) {
77                bindables.addVariable(variableName, layoutBinder.getClassName());
78            }
79        }
80    }
81
82    public void writeViewBinderInterfaces(boolean isLibrary) {
83        ensureDataBinder();
84        mDataBinder.writerBaseClasses(isLibrary);
85    }
86
87    public void writeViewBinders(int minSdk) {
88        ensureDataBinder();
89        mDataBinder.writeBinders(minSdk);
90    }
91
92    public Set<String> getWrittenClassNames() {
93        ensureDataBinder();
94        return mDataBinder.getWrittenClassNames();
95    }
96
97    public interface BindableHolder {
98        void addVariable(String variableName, String containingClassName);
99    }
100}
101