CompilerChef.java revision dea555cf42dc3583604699c8c018d22681f56166
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() {
61        ensureDataBinder();
62        final String pkg = "android.databinding";
63        DataBinderWriter dbr = new DataBinderWriter(pkg, mResourceBundle.getAppPackage(),
64                "GeneratedDataBinderRenderer", mDataBinder.getLayoutBinders());
65        if (dbr.getLayoutBinders().size() > 0) {
66            mFileWriter.writeToFile(pkg + "." + dbr.getClassName(), dbr.write());
67        }
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() {
88        ensureDataBinder();
89        mDataBinder.writeBinders();
90    }
91
92    public interface BindableHolder {
93        void addVariable(String variableName, String containingClassName);
94    }
95}
96