summaryrefslogtreecommitdiff
path: root/gradlePlugin/src/main/java/android/databinding/tool/DataBindingExcludeGeneratedTask.java
blob: f311513be56740c64919a1e27cfd989919ba2436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.databinding.tool;

import com.google.common.base.Preconditions;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.bundling.Jar;

import android.databinding.tool.processing.Scope;
import android.databinding.tool.util.L;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Task to exclude generated classes from the Jar task of a library project
 */
public class DataBindingExcludeGeneratedTask extends DefaultTask {
    private String appPackage;
    private String infoClassQualifiedName;
    @Input
    private File generatedClassListFile;
    private boolean isLibrary;

    private org.gradle.api.tasks.bundling.Jar packageTask;
    private final String EXCLUDE_PATTERN = "android/databinding/layouts/*.*";

    public void setAppPackage(String appPackage) {
        this.appPackage = appPackage;
    }

    public void setInfoClassQualifiedName(String infoClassQualifiedName) {
        this.infoClassQualifiedName = infoClassQualifiedName;
    }

    public void setLibrary(boolean isLibrary) {
        this.isLibrary = isLibrary;
    }

    public void setPackageTask(Jar packageTask) {
        this.packageTask = packageTask;
    }

    public void setGeneratedClassListFile(File generatedClassListFile) {
        this.generatedClassListFile = generatedClassListFile;
    }

    public String getAppPackage() {
        return appPackage;
    }

    public String getInfoClassQualifiedName() {
        return infoClassQualifiedName;
    }

    public File getGeneratedClassListFile() {
        return generatedClassListFile;
    }

    @TaskAction
    public void excludeGenerated() {
        L.d("Excluding generated classes from jar. Is library ? %s", isLibrary);
        String appPkgAsClass = appPackage.replace('.', '/');
        String infoClassAsClass = infoClassQualifiedName.replace('.', '/');
        exclude(infoClassAsClass + ".class");
        exclude(EXCLUDE_PATTERN);
        if (isLibrary) {
            exclude(appPkgAsClass + "/BR.*");
            exclude("android/databinding/DynamicUtil.class");
            List<String> generatedClasses = readGeneratedClasses();
            for (String klass : generatedClasses) {
                exclude(klass.replace('.', '/') + ".class");
            }
        }
        Scope.assertNoError();
        L.d("Excluding generated classes from library jar is done.");
    }

    private void exclude(String pattern) {
        L.d("exclude %s", pattern);
        packageTask.exclude(pattern);
    }

    private List<String> readGeneratedClasses() {
        Preconditions.checkNotNull(generatedClassListFile, "Data binding exclude generated task"
                + " is not configured properly");
        Preconditions.checkArgument(generatedClassListFile.exists(),
                "Generated class list does not exist %s", generatedClassListFile.getAbsolutePath());
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(generatedClassListFile);
            return IOUtils.readLines(fis);
        } catch (FileNotFoundException e) {
            L.e(e, "Unable to read generated class list from %s",
                    generatedClassListFile.getAbsoluteFile());
        } catch (IOException e) {
            L.e(e, "Unexpected exception while reading %s",
                    generatedClassListFile.getAbsoluteFile());
        } finally {
            IOUtils.closeQuietly(fis);
        }
        Preconditions.checkState(false, "Could not read data binding generated class list");
        return null;
    }
}