DataBindingExcludeGeneratedTask.java revision b6887f1479c3ecec38a7989748ef33de1fbcd973
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.databinding.tool;
18
19import com.google.common.base.Preconditions;
20
21import org.apache.commons.io.FileUtils;
22import org.apache.commons.io.IOUtils;
23import org.gradle.api.DefaultTask;
24import org.gradle.api.tasks.Input;
25import org.gradle.api.tasks.TaskAction;
26import org.gradle.api.tasks.bundling.Jar;
27
28import android.databinding.tool.util.L;
29
30import java.io.File;
31import java.io.FileInputStream;
32import java.io.FileNotFoundException;
33import java.io.IOException;
34import java.util.List;
35
36/**
37 * Task to exclude generated classes from the Jar task of a library project
38 */
39public class DataBindingExcludeGeneratedTask extends DefaultTask {
40    private String appPackage;
41    private String infoClassQualifiedName;
42    @Input
43    private File generatedClassListFile;
44    private boolean isLibrary;
45
46    private org.gradle.api.tasks.bundling.Jar packageTask;
47    private final String EXCLUDE_PATTERN = "android/databinding/layouts/*.*";
48
49    public void setAppPackage(String appPackage) {
50        this.appPackage = appPackage;
51    }
52
53    public void setInfoClassQualifiedName(String infoClassQualifiedName) {
54        this.infoClassQualifiedName = infoClassQualifiedName;
55    }
56
57    public void setLibrary(boolean isLibrary) {
58        this.isLibrary = isLibrary;
59    }
60
61    public void setPackageTask(Jar packageTask) {
62        this.packageTask = packageTask;
63    }
64
65    public void setGeneratedClassListFile(File generatedClassListFile) {
66        this.generatedClassListFile = generatedClassListFile;
67    }
68
69    public String getAppPackage() {
70        return appPackage;
71    }
72
73    public String getInfoClassQualifiedName() {
74        return infoClassQualifiedName;
75    }
76
77    public File getGeneratedClassListFile() {
78        return generatedClassListFile;
79    }
80
81    @TaskAction
82    public void excludeGenerated() {
83        L.d("Excluding generated classes from jar. Is library ? %s", isLibrary);
84        String appPkgAsClass = appPackage.replace('.', '/');
85        String infoClassAsClass = infoClassQualifiedName.replace('.', '/');
86        exclude(infoClassAsClass + ".class");
87        exclude(EXCLUDE_PATTERN);
88        if (isLibrary) {
89            exclude(appPkgAsClass + "/BR.*");
90            List<String> generatedClasses = readGeneratedClasses();
91            for (String klass : generatedClasses) {
92                exclude(klass.replace('.', '/') + ".class");
93            }
94        }
95        L.d("Excluding generated classes from library jar is done.");
96    }
97
98    private void exclude(String pattern) {
99        L.d("exclude %s", pattern);
100        packageTask.exclude(pattern);
101    }
102
103    private List<String> readGeneratedClasses() {
104        Preconditions.checkNotNull(generatedClassListFile, "Data binding exclude generated task"
105                + " is not configured properly");
106        Preconditions.checkArgument(generatedClassListFile.exists(),
107                "Generated class list does not exist %s", generatedClassListFile.getAbsolutePath());
108        FileInputStream fis = null;
109        try {
110            fis = new FileInputStream(generatedClassListFile);
111            return IOUtils.readLines(fis);
112        } catch (FileNotFoundException e) {
113            L.e(e, "Unable to read generated class list from %s",
114                    generatedClassListFile.getAbsoluteFile());
115        } catch (IOException e) {
116            L.e(e, "Unexpected exception while reading %s",
117                    generatedClassListFile.getAbsoluteFile());
118        } finally {
119            IOUtils.closeQuietly(fis);
120        }
121        Preconditions.checkState(false, "Could not read data binding generated class list");
122        return null;
123    }
124}
125