tzh
2024-08-22 c7d0944258c7d0943aa7b2211498fd612971ce27
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2013 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 com.example.android.samples.build
 
import freemarker.ext.dom.NodeModel
import groovy.transform.Canonical
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.file.FileTree
 
/**
 * Gradle extension that holds properties for sample generation.
 *
 * The sample generator needs a number of properties whose values can be
 * inferred by convention from a smaller number of initial properties.
 * This class defines fields for the initial properties, and getter
 * methods for the inferred properties. It also defines a small number
 * of convenience methods for setting up template-generation tasks.
 */
@Canonical
class SampleGenProperties {
    /**
     * The Gradle project that this extension is being applied to.
     */
    Project project
 
    /**
     *  Directory where the top-level sample project lives
     */
    def targetProjectPath
 
    /**
     * Relative path to samples/common directory
     */
    def pathToSamplesCommon
 
    /**
     * Relative path to build directory (platform/developers/build)
     */
    def pathToBuild
 
    /**
     * Java package name for the root package of this sample.
     */
    String targetSamplePackage
 
    /**
     *
     * @return The path to the sample project (as opposed to the top-level project, which
     *         what is that even for anyway?)
     */
    String targetSamplePath() {
        return "${targetProjectPath}/${targetSampleModule()}"
    }
 
 
 
    /**
     *
     * @return The path that contains common files -- can be cleaned without harming
     *         the sample
     */
    String targetCommonPath() {
        return "${targetSamplePath()}/src/common/java/com/example/android/common"
    }
 
    /**
     *
     * @return The path that contains template files -- can be cleaned without harming
     *         the sample
     */
    String targetTemplatePath() {
        return "${targetSamplePath()}/src/template"
    }
 
    /**
     * The name of this sample (and also of the corresponding .iml file)
     */
    String targetSampleName() {
        return project.file(targetProjectPath).getName()
    }
 
    /**
     * The name of the main module in the sample project
     */
    String targetSampleModule() {
        return "Application"
    }
 
    /**
     * The path to the template parameters file
     */
    String templateXml() {
        return "${targetProjectPath}/template-params.xml"
    }
 
    /**
     * Transforms a package name into a java-style OS dependent path
     * @param pkg cccc
     * @return The java-style path to the package's code
     */
    String packageAsPath(String pkg) {
        return pkg.replaceAll(/\./, File.separator)
    }
 
    /**
     * Transforms a path into a java-style package name
     * @param path The java-style path to the package's code
     * @return Name of the package to transform
     */
    String pathAsPackage(String path) {
        return path.replaceAll(File.separator, /\./)
    }
 
    /**
     * Returns the path to the common/build/templates directory
     */
    String templatesRoot() {
        return "${targetProjectPath}/${pathToBuild}/templates"
    }
 
 
    /**
     * Returns the path to common/src/java
     */
    String commonSourceRoot() {
        return "${targetProjectPath}/${pathToSamplesCommon}/src/java/com/example/android/common"
    }
 
    /**
     * Returns the path to the template include directory
     */
    String templatesInclude() {
        return "${templatesRoot()}/include"
    }
 
    /**
     * Returns the output file that will be generated for a particular
     * input, by replacing generic pathnames with project-specific pathnames
     * and dropping the .ftl extension from freemarker files.
     *
     * @param relativeInputPath Input file as a relative path from the template directory
     * @return Relative output file path
     */
    String getOutputForInput(String relativeInputPath) {
        String outputPath = relativeInputPath
        outputPath = outputPath.replaceAll('_PROJECT_', targetSampleName())
        outputPath = outputPath.replaceAll('_MODULE_', targetSampleModule())
        outputPath = outputPath.replaceAll('_PACKAGE_', packageAsPath(targetSamplePackage))
 
        // This is kind of a hack; IntelliJ picks up any and all subdirectories named .idea, so
        // named them ._IDE_ instead. TODO: remove when generating .idea projects is no longer necessary.
        outputPath = outputPath.replaceAll('_IDE_', "idea")
        outputPath = outputPath.replaceAll(/\.ftl$/, '')
 
        // Any file beginning with a dot won't get picked up, so rename them as necessary here.
        outputPath = outputPath.replaceAll('gitignore', '.gitignore')
        return outputPath
    }
 
    /**
     * Returns the tree(s) where the templates to be processed live. The template
     * input paths that are passed to
     * {@link SampleGenProperties#getOutputForInput(java.lang.String) getOutputForInput}
     * are relative to the dir element in each tree.
     */
    FileTree[] templates() {
        def result = []
        def xmlFile = project.file(templateXml())
        if (xmlFile.exists()) {
            def xml = new XmlSlurper().parse(xmlFile)
            xml.template.each { template ->
                result.add(project.fileTree(dir: "${templatesRoot()}/${template.@src}"))
            }
        } else {
            result.add(project.fileTree(dir: "${templatesRoot()}/create"))
        }
        return result;
    }
 
    /**
     * Path(s) of the common directories to copy over to the sample project.
     */
    FileTree[] common() {
        def result = []
        def xmlFile = project.file(templateXml())
        if (xmlFile.exists()) {
            def xml = new XmlSlurper().parse(xmlFile)
            xml.common.each { common ->
                println "Adding common/${common.@src} from ${commonSourceRoot()}"
                result.add(project.fileTree (
                        dir: "${commonSourceRoot()}",
                        include: "${common.@src}/**/*"
                ))
            }
        }
        return result
    }
 
    /**
     * Returns the hash to supply to the freemarker template processor.
     * This is loaded from the file specified by {@link SampleGenProperties#templateXml()}
     * if such a file exists, or synthesized with some default parameters if it does not.
     * In addition, some data about the current project is added to the "meta" key of the
     * hash.
     *
     * @return The hash to supply to freemarker
     */
    Map templateParams() {
        Map result = new HashMap();
 
        def xmlFile = project.file(templateXml())
        if (xmlFile.exists()) {
            // Parse the xml into Freemarker's DOM structure
            def params = freemarker.ext.dom.NodeModel.parse(xmlFile)
 
            // Move to the <sample> node and stuff that in our map
            def sampleNode = (NodeModel)params.exec(['/sample'])
            result.put("sample", sampleNode)
        } else {
            // Fake data for use on creation
            result.put("sample", [
                    name:targetSampleName(),
                    package:targetSamplePackage,
                    minSdk:4
            ])
        }
 
        // Extra data that some templates find useful
        result.put("meta", [
                root: targetProjectPath,
                module: targetSampleModule(),
                common: pathToSamplesCommon,
                build: pathToBuild,
        ])
        return result
    }
 
 
 
    /**
     * Generate default values for properties that can be inferred from an existing
     * generated project, unless those properties have already been
     * explicitly specified.
     */
    void getRefreshProperties() {
        if (!this.targetProjectPath) {
            this.targetProjectPath = project.projectDir
        }
        def xmlFile = project.file(templateXml())
        if (xmlFile.exists()) {
            println "Template XML: $xmlFile"
            def xml = new XmlSlurper().parse(xmlFile)
            this.targetSamplePackage = xml.package.toString()
            println "Target Package: $targetSamplePackage"
        }
    }
 
    /**
     * Generate default values for creation properties, unless those properties
     * have already been explicitly specified. This method will attempt to get
     * these properties interactively from the user if necessary.
     */
    void getCreationProperties() {
        def calledFrom = project.hasProperty('calledFrom') ? new File(project.calledFrom)
                : project.projectDir
        calledFrom = calledFrom.getCanonicalPath()
        println('\n\n\nReady to create project...')
 
        if (project.hasProperty('pathToSamplesCommon')) {
            this.pathToSamplesCommon = project.pathToSamplesCommon
        } else {
            throw new GradleException (
                    'create task requires project property pathToSamplesCommon')
        }
 
 
        if (project.hasProperty('pathToBuild')) {
            this.pathToBuild = project.pathToBuild
        } else {
            throw new GradleException ('create task requires project property pathToBuild')
        }
 
        if (!this.targetProjectPath) {
            if (project.hasProperty('out')) {
                this.targetProjectPath = project.out
            } else {
                this.targetProjectPath = System.console().readLine(
                        "\noutput directory [$calledFrom]:")
                if (this.targetProjectPath.length() <= 0) {
                    this.targetProjectPath = calledFrom
                }
            }
        }
 
        if (!this.targetSamplePackage) {
            def defaultPackage = "com.example.android." +
                    this.targetSampleName().toLowerCase()
            this.targetSamplePackage = System.console().readLine(
                    "\nsample package name[$defaultPackage]:")
            if (this.targetSamplePackage.length() <= 0) {
                this.targetSamplePackage = defaultPackage
            }
        }
    }
 
}