Skip to content

Commit 2c2c964

Browse files
committed
revert: "fix: build plugins using same gradle config structure as apps (#5671)"
This reverts commit 40e459a.
1 parent 9191d12 commit 2c2c964

File tree

3 files changed

+122
-177
lines changed

3 files changed

+122
-177
lines changed

lib/services/android-plugin-build-service.ts

+78
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,40 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
144144
return promise;
145145
}
146146

147+
private getIncludeGradleCompileDependenciesScope(
148+
includeGradleFileContent: string
149+
): Array<string> {
150+
const indexOfDependenciesScope = includeGradleFileContent.indexOf(
151+
"dependencies"
152+
);
153+
const result: Array<string> = [];
154+
155+
if (indexOfDependenciesScope === -1) {
156+
return result;
157+
}
158+
159+
const indexOfRepositoriesScope = includeGradleFileContent.indexOf(
160+
"repositories"
161+
);
162+
163+
let repositoriesScope = "";
164+
if (indexOfRepositoriesScope >= 0) {
165+
repositoriesScope = this.getScope(
166+
"repositories",
167+
includeGradleFileContent
168+
);
169+
result.push(repositoriesScope);
170+
}
171+
172+
const dependenciesScope = this.getScope(
173+
"dependencies",
174+
includeGradleFileContent
175+
);
176+
result.push(dependenciesScope);
177+
178+
return result;
179+
}
180+
147181
private getScope(scopeName: string, content: string): string {
148182
const indexOfScopeName = content.indexOf(scopeName);
149183
const openingBracket = "{";
@@ -377,13 +411,18 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
377411
const settingsGradlePath = path.join(pluginTempDir, "settings.gradle");
378412

379413
this.$fs.copyFile(allGradleTemplateFiles, pluginTempDir);
414+
this.addCompileDependencies(platformsAndroidDirPath, buildGradlePath);
380415
const runtimeGradleVersions = await this.getRuntimeGradleVersions(
381416
projectDir
382417
);
383418
this.replaceGradleVersion(
384419
pluginTempDir,
385420
runtimeGradleVersions.gradleVersion
386421
);
422+
this.replaceGradleAndroidPluginVersion(
423+
buildGradlePath,
424+
runtimeGradleVersions.gradleAndroidPluginVersion
425+
);
387426
this.replaceFileContent(buildGradlePath, "{{pluginName}}", pluginName);
388427
this.replaceFileContent(settingsGradlePath, "{{pluginName}}", pluginName);
389428
}
@@ -606,6 +645,22 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
606645
);
607646
}
608647

648+
private replaceGradleAndroidPluginVersion(
649+
buildGradlePath: string,
650+
version: string
651+
): void {
652+
const gradleAndroidPluginVersionPlaceholder =
653+
"{{runtimeAndroidPluginVersion}}";
654+
const gradleAndroidPluginVersion =
655+
version || AndroidBuildDefaults.GradleAndroidPluginVersion;
656+
657+
this.replaceFileContent(
658+
buildGradlePath,
659+
gradleAndroidPluginVersionPlaceholder,
660+
gradleAndroidPluginVersion
661+
);
662+
}
663+
609664
private replaceFileContent(
610665
filePath: string,
611666
content: string,
@@ -617,6 +672,29 @@ export class AndroidPluginBuildService implements IAndroidPluginBuildService {
617672
this.$fs.writeFile(filePath, replacedFileContent);
618673
}
619674

675+
private addCompileDependencies(
676+
platformsAndroidDirPath: string,
677+
buildGradlePath: string
678+
): void {
679+
const includeGradlePath = path.join(
680+
platformsAndroidDirPath,
681+
INCLUDE_GRADLE_NAME
682+
);
683+
if (this.$fs.exists(includeGradlePath)) {
684+
const includeGradleContent = this.$fs.readText(includeGradlePath);
685+
const compileDependencies = this.getIncludeGradleCompileDependenciesScope(
686+
includeGradleContent
687+
);
688+
689+
if (compileDependencies.length) {
690+
this.$fs.appendFile(
691+
buildGradlePath,
692+
"\n" + compileDependencies.join("\n")
693+
);
694+
}
695+
}
696+
}
697+
620698
private copyAar(
621699
shortPluginName: string,
622700
pluginTempDir: string,

vendor/gradle-plugin/build.gradle

+28-176
Original file line numberDiff line numberDiff line change
@@ -7,72 +7,38 @@ apply plugin: 'com.android.library'
77
apply plugin: 'kotlin-android'
88
apply plugin: 'kotlin-parcelize'
99

10-
def loadPropertyFile = { path ->
11-
try {
12-
if(project.hasProperty("loadedProperties_${path}")) {
13-
logger.info "\t + gradle properties already loaded. SKIPPING"
14-
} else {
15-
logger.info "\t + trying to load gradle properties from \"$path\""
16-
17-
Properties properties = new Properties()
18-
properties.load(new FileInputStream("$path"))
19-
properties.each { prop ->
20-
logger.info "\t + [$path] setting ${prop.key} = ${prop.value}"
21-
project.ext.set(prop.key, prop.value)
22-
}
23-
project.ext.set("loadedProperties_${path}", true)
24-
25-
outLogger.withStyle(Style.SuccessHeader).println "\t + loaded gradle properties from \"$path\""
26-
}
27-
} catch(Exception ex) {
28-
logger.warn "\t + failed to load gradle properties from \"$path\". Error is: ${ex.getMessage()}"
29-
}
30-
}
31-
3210
buildscript {
33-
def GRADLE_PROPERTIES_FILENAME = "gradle.properties"
34-
35-
def getFile = { dir, filename ->
36-
File file = new File("$dir$File.separator$filename")
37-
file?.exists() ? file : null
11+
def getDepPlatformDir = { dep ->
12+
file("${project.ext.USER_PROJECT_ROOT}/${project.ext.PLATFORMS_ANDROID}/${dep.directory}/$PLATFORMS_ANDROID")
3813
}
14+
def computeKotlinVersion = { -> project.hasProperty("kotlinVersion") ? kotlinVersion : "1.6.0" }
15+
def kotlinVersion = computeKotlinVersion()
16+
repositories {
17+
google()
18+
jcenter()
19+
}
20+
dependencies {
21+
classpath 'com.android.tools.build:gradle:{{runtimeAndroidPluginVersion}}'
22+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
3923

40-
def getPropertyFile = { dir ->
41-
return getFile(dir, GRADLE_PROPERTIES_FILENAME)
24+
// NOTE: Do not place your application dependencies here; they belong
25+
// in the individual module build.gradle files
4226
}
43-
def getUserProperties = { dir ->
44-
def file = getPropertyFile(dir)
45-
if (!file) {
46-
return null
47-
}
4827

49-
Properties properties = new Properties()
50-
properties.load(file.newInputStream())
28+
// Set up styled logger
29+
project.ext.getDepPlatformDir = getDepPlatformDir
30+
project.ext.outLogger = services.get(StyledTextOutputFactory).create("colouredOutputLogger")
5131

52-
return properties
53-
}
54-
def loadPropertyFile = { path ->
55-
try {
56-
if(project.hasProperty("loadedProperties_${path}")) {
57-
logger.info "\t + gradle properties already loaded. SKIPPING"
58-
} else {
59-
logger.info "\t + trying to load gradle properties from \"$path\""
60-
61-
Properties properties = new Properties()
62-
properties.load(new FileInputStream("$path"))
63-
properties.each { prop ->
64-
logger.info "\t + [$path] setting ${prop.key} = ${prop.value}"
65-
project.ext.set(prop.key, prop.value)
66-
}
67-
project.ext.set("loadedProperties_${path}", true)
68-
69-
outLogger.withStyle(Style.SuccessHeader).println "\t + loaded gradle properties from \"$path\""
70-
}
71-
} catch(Exception ex) {
72-
logger.warn "\t + failed to load gradle properties from \"$path\". Error is: ${ex.getMessage()}"
73-
}
74-
}
75-
def getAppPath = { ->
32+
project.ext.USER_PROJECT_ROOT = "$rootDir/../../.."
33+
project.ext.PLATFORMS_ANDROID = "platforms/android"
34+
project.ext.PLUGIN_NAME = "{{pluginName}}"
35+
36+
// the build script will not work with previous versions of the CLI (3.1 or earlier)
37+
def dependenciesJson = file("${project.ext.USER_PROJECT_ROOT}/${project.ext.PLATFORMS_ANDROID}/dependencies.json")
38+
def appDependencies = new JsonSlurper().parseText(dependenciesJson.text)
39+
def pluginData = appDependencies.find { it.name == project.ext.PLUGIN_NAME }
40+
project.ext.nativescriptDependencies = appDependencies.findAll{pluginData.dependencies.contains(it.name)}
41+
project.ext.getAppPath = { ->
7642
def relativePathToApp = "app"
7743
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
7844
def nsConfig
@@ -93,7 +59,8 @@ buildscript {
9359

9460
return project.ext.appPath
9561
}
96-
def getAppResourcesPath = { ->
62+
63+
project.ext.getAppResourcesPath = { ->
9764
def relativePathToAppResources
9865
def absolutePathToAppResources
9966
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
@@ -120,95 +87,6 @@ buildscript {
12087
return absolutePathToAppResources
12188
}
12289

123-
def initialize = { ->
124-
// set up our logger
125-
project.ext.outLogger = services.get(StyledTextOutputFactory).create("colouredOutputLogger")
126-
outLogger.withStyle(Style.SuccessHeader).println "\t ~initialize"
127-
128-
129-
project.ext.USER_PROJECT_ROOT = "$rootDir/../../.."
130-
project.ext.PLATFORMS_ANDROID = "platforms/android"
131-
project.ext.PLUGIN_NAME = "{{pluginName}}"
132-
133-
def userDir = "$USER_PROJECT_ROOT"
134-
rootProject.ext.userDefinedGradleProperties = getUserProperties("${getAppResourcesPath()}/Android")
135-
136-
loadPropertyFile("$USER_PROJECT_ROOT/${project.ext.PLATFORMS_ANDROID}/gradle.properties")
137-
loadPropertyFile("$USER_PROJECT_ROOT/${project.ext.PLATFORMS_ANDROID}/additional_gradle.properties")
138-
139-
if (rootProject.hasProperty("userDefinedGradleProperties")) {
140-
rootProject.ext.userDefinedGradleProperties.each { entry ->
141-
def propertyName = entry.getKey()
142-
def propertyValue = entry.getValue()
143-
project.ext.set(propertyName, propertyValue)
144-
}
145-
}
146-
147-
def getDepPlatformDir = { dep ->
148-
file("${project.ext.USER_PROJECT_ROOT}/${project.ext.PLATFORMS_ANDROID}/${dep.directory}/$PLATFORMS_ANDROID")
149-
}
150-
151-
// Set up styled logger
152-
project.ext.getDepPlatformDir = getDepPlatformDir
153-
project.ext.outLogger = services.get(StyledTextOutputFactory).create("colouredOutputLogger")
154-
155-
156-
// the build script will not work with previous versions of the CLI (3.1 or earlier)
157-
def dependenciesJson = file("${project.ext.USER_PROJECT_ROOT}/${project.ext.PLATFORMS_ANDROID}/dependencies.json")
158-
def appDependencies = new JsonSlurper().parseText(dependenciesJson.text)
159-
def pluginData = appDependencies.find { it.name == project.ext.PLUGIN_NAME }
160-
project.ext.nativescriptDependencies = appDependencies.findAll{pluginData.dependencies.contains(it.name)}.plus([pluginData])
161-
162-
project.ext.getAppResourcesPath = { ->
163-
def relativePathToAppResources
164-
def absolutePathToAppResources
165-
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
166-
def nsConfig
167-
168-
if (nsConfigFile.exists()) {
169-
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
170-
}
171-
172-
if (project.hasProperty("appResourcesPath")) {
173-
// when appResourcesPath is passed through -PappResourcesPath=/path/to/App_Resources
174-
// the path could be relative or absolute - either case will work
175-
relativePathToAppResources = appResourcesPath
176-
absolutePathToAppResources = Paths.get(USER_PROJECT_ROOT).resolve(relativePathToAppResources).toAbsolutePath()
177-
} else if (nsConfig != null && nsConfig.appResourcesPath != null) {
178-
relativePathToAppResources = nsConfig.appResourcesPath
179-
absolutePathToAppResources = Paths.get(USER_PROJECT_ROOT).resolve(relativePathToAppResources).toAbsolutePath()
180-
} else {
181-
absolutePathToAppResources = "${getAppPath()}/App_Resources"
182-
}
183-
184-
project.ext.appResourcesPath = absolutePathToAppResources
185-
186-
return absolutePathToAppResources
187-
}
188-
189-
190-
project.ext.getAppPath = { ->
191-
def relativePathToApp = "app"
192-
def nsConfigFile = file("$USER_PROJECT_ROOT/nsconfig.json")
193-
def nsConfig
194-
195-
if (nsConfigFile.exists()) {
196-
nsConfig = new JsonSlurper().parseText(nsConfigFile.getText("UTF-8"))
197-
}
198-
199-
if (project.hasProperty("appPath")) {
200-
// when appPath is passed through -PappPath=/path/to/app
201-
// the path could be relative or absolute - either case will work
202-
relativePathToApp = appPath
203-
} else if (nsConfig != null && nsConfig.appPath != null) {
204-
relativePathToApp = nsConfig.appPath
205-
}
206-
207-
project.ext.appPath = Paths.get(USER_PROJECT_ROOT).resolve(relativePathToApp).toAbsolutePath()
208-
209-
return project.ext.appPath
210-
}
211-
}
21290
def applyBuildScriptConfigurations = { ->
21391
def absolutePathToAppResources = getAppResourcesPath()
21492
def pathToBuildScriptGradle = "$absolutePathToAppResources/Android/buildscript.gradle"
@@ -234,25 +112,8 @@ buildscript {
234112
apply from: pathToPluginBuildScriptGradle, to: buildscript
235113
}
236114
}
237-
238-
initialize()
239115
applyBuildScriptConfigurations()
240116

241-
def computeKotlinVersion = { -> project.hasProperty("kotlinVersion") ? kotlinVersion : "${ns_default_kotlin_version}" }
242-
def computeBuildToolsVersion = { -> project.hasProperty("androidBuildToolsVersion") ? androidBuildToolsVersion : "${NS_DEFAULT_ANDROID_BUILD_TOOLS_VERSION}" }
243-
def kotlinVersion = computeKotlinVersion()
244-
def androidBuildToolsVersion = computeBuildToolsVersion()
245-
246-
repositories {
247-
google()
248-
mavenCentral()
249-
}
250-
dependencies {
251-
classpath "com.android.tools.build:gradle:$androidBuildToolsVersion"
252-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
253-
classpath "org.codehaus.groovy:groovy-all:3.0.8"
254-
}
255-
256117
}
257118

258119
def pluginDependencies
@@ -295,7 +156,6 @@ android {
295156
nativescriptDependencies.each { dep ->
296157
def includeGradlePath = "${getDepPlatformDir(dep)}/include.gradle"
297158
if (file(includeGradlePath).exists()) {
298-
outLogger.withStyle(Style.SuccessHeader).println "\t + applying plugin include.gradle from dependency ${includeGradlePath}"
299159
apply from: includeGradlePath
300160
}
301161
}
@@ -311,10 +171,6 @@ android {
311171
versionCode 1
312172
versionName "1.0"
313173
}
314-
lintOptions {
315-
checkReleaseBuilds false
316-
abortOnError false
317-
}
318174
}
319175

320176

@@ -331,13 +187,9 @@ def applyBeforePluginGradleConfiguration() {
331187
task addDependenciesFromNativeScriptPlugins {
332188
nativescriptDependencies.each { dep ->
333189
def aarFiles = fileTree(dir: getDepPlatformDir(dep), include: ["**/*.aar"])
334-
def currentDirname = file(project.buildscript.sourceFile).getParentFile().getName()
335190
aarFiles.each { aarFile ->
336191
def length = aarFile.name.length() - 4
337192
def fileName = aarFile.name[0..<length]
338-
if(fileName == currentDirname) {
339-
return
340-
}
341193
outLogger.withStyle(Style.SuccessHeader).println "\t + adding aar plugin dependency: " + aarFile.getAbsolutePath()
342194
project.dependencies.add("implementation", [name: fileName, ext: "aar"])
343195
}

vendor/gradle-plugin/gradle.properties

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
# Nativescript CLI plugin build gradle properties
1+
# Project-wide Gradle settings.
2+
3+
# IDE (e.g. Android Studio) users:
4+
# Gradle settings configured through the IDE *will override*
5+
# any settings specified in this file.
6+
7+
# For more details on how to configure your build environment visit
8+
# http://www.gradle.org/docs/current/userguide/build_environment.html
9+
10+
# When configured, Gradle will run in incubating parallel mode.
11+
# This option should only be used with decoupled projects. More details, visit
12+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13+
#org.gradle.parallel=true
14+
15+
# Specifies the JVM arguments used for the daemon process.
16+
# The setting is particularly useful for tweaking memory settings.
217
org.gradle.jvmargs=-Xmx16384M
318

419
android.enableJetifier=true

0 commit comments

Comments
 (0)