feat: support adding project build script dependencies

This commit is contained in:
Erisu
2026-05-12 13:02:42 +09:00
parent a1f75a3e95
commit 8d20f842a3
5 changed files with 56 additions and 2 deletions
+14
View File
@@ -124,6 +124,20 @@ class AndroidProject {
this._dirty = true;
}
addAppBuildScriptDependency (parentDir, value) {
const parentProjectFile = path.resolve(parentDir, 'project.properties');
const parentProperties = this._getPropertiesFile(parentProjectFile);
addToPropertyList(parentProperties, 'cordova.appbuildscript.dependency', value);
this._dirty = true;
}
removeAppBuildScriptDependency (parentDir, value) {
const parentProjectFile = path.resolve(parentDir, 'project.properties');
const parentProperties = this._getPropertiesFile(parentProjectFile);
removeFromPropertyList(parentProperties, 'cordova.appbuildscript.dependency', value);
this._dirty = true;
}
addSystemLibrary (parentDir, value) {
const parentProjectFile = path.resolve(parentDir, 'project.properties');
const parentProperties = this._getPropertiesFile(parentProjectFile);
+15
View File
@@ -153,6 +153,7 @@ class ProjectBuilder {
return {
libs: findAllUniq(data, /^\s*android\.library\.reference\.\d+=(.*)(?:\s|$)/mg),
gradleIncludes: findAllUniq(data, /^\s*cordova\.gradle\.include\.\d+=(.*)(?:\s|$)/mg),
appBuildScriptDependencies: findAllUniq(data, /^\s*cordova\.appbuildscript\.dependency\.\d+=((?!.*\().*)(?:\s|$)/mg),
systemLibs: findAllUniq(data, /^\s*cordova\.system\.library\.\d+=((?!.*\().*)(?:\s|$)/mg),
bomPlatforms: findAllUniq(data, /^\s*cordova\.system\.library\.\d+=platform\((?:'|")(.*)(?:'|")\)/mg)
};
@@ -280,6 +281,20 @@ class ProjectBuilder {
includeList += 'apply from: "../' + includePath + '"\n';
});
buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + includeList + '$2');
// Injecting buildscript dependencies
if (propertiesObj.appBuildScriptDependencies.length > 0) {
const buildScriptClasspaths = propertiesObj.appBuildScriptDependencies.map(d => `classpath "${d}"`);
buildGradle = buildGradle.replace(
/^([ \t]*)(\/\/ APP-BUILDSCRIPT-DEPENDENCIES START)[\s\S]*(\/\/ APP-BUILDSCRIPT-DEPENDENCIES END)/m,
(match, indentLevel, startLine, endLine) => [
startLine,
...buildScriptClasspaths,
endLine
].map(l => `${indentLevel}${l}`).join('\n')
);
}
// This needs to be stored in the app gradle, not the root grade
fs.writeFileSync(path.join(this.root, 'app', 'build.gradle'), buildGradle);
}
+10 -2
View File
@@ -83,12 +83,16 @@ const handlers = {
copyNewFile(plugin.dir, src, project.projectDir, subRelativeDir, !!(options && options.link));
subDir = path.resolve(project.projectDir, subRelativeDir);
} else {
obj.type = 'sys';
if (obj.type !== 'appBuildScriptDependency') {
obj.type = 'sys';
}
subDir = src;
}
if (obj.type === 'gradleReference') {
project.addGradleReference(parentDir, subDir);
} else if (obj.type === 'appBuildScriptDependency') {
project.addAppBuildScriptDependency(parentDir, subDir);
} else if (obj.type === 'sys') {
project.addSystemLibrary(parentDir, subDir);
} else {
@@ -113,12 +117,16 @@ const handlers = {
fs.rmdirSync(parDir);
}
} else {
obj.type = 'sys';
if (obj.type !== 'appBuildScriptDependency') {
obj.type = 'sys';
}
subDir = src;
}
if (obj.type === 'gradleReference') {
project.removeGradleReference(parentDir, subDir);
} else if (obj.type === 'appBuildScriptDependency') {
project.removeAppBuildScriptDependency(parentDir, subDir);
} else if (obj.type === 'sys') {
project.removeSystemLibrary(parentDir, subDir);
} else {
+14
View File
@@ -197,6 +197,7 @@ describe('android project handler', function () {
spyOn(dummyProject, 'addSystemLibrary');
spyOn(dummyProject, 'addSubProject');
spyOn(dummyProject, 'addGradleReference');
spyOn(dummyProject, 'addAppBuildScriptDependency');
common.__set__('copyNewFile', copyNewFileSpy);
});
@@ -241,6 +242,12 @@ describe('android project handler', function () {
expect(copyNewFileSpy).toHaveBeenCalledWith(dummyPluginInfo.dir, framework.src, dummyProject.projectDir, someString, false);
expect(dummyProject.addGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
});
it('Test#013 : should install app buildscript dependency using project.addAppBuildScriptDependency', () => {
const framework = { src: 'plugin-dependency', type: 'appBuildScriptDependency' };
android.framework.install(framework, dummyPluginInfo, dummyProject);
expect(dummyProject.addAppBuildScriptDependency).toHaveBeenCalledWith(dummyProject.projectDir, someString);
});
});
describe('of <js-module> elements', function () {
@@ -402,6 +409,7 @@ describe('android project handler', function () {
spyOn(dummyProject, 'removeSystemLibrary');
spyOn(dummyProject, 'removeSubProject');
spyOn(dummyProject, 'removeGradleReference');
spyOn(dummyProject, 'removeAppBuildScriptDependency');
});
it('Test#020 : should throw if framework doesn\'t have "src" attribute', function () {
@@ -433,6 +441,12 @@ describe('android project handler', function () {
expect(rmSyncSpy).toHaveBeenCalledWith(someString, { recursive: true, force: true });
expect(dummyProject.removeGradleReference).toHaveBeenCalledWith(dummyProject.projectDir, someString);
});
it('Test#025 : should uninstall app buildscript dependency using project.removeAppBuildScriptDependency', () => {
const framework = { src: 'plugin-dependency', type: 'appBuildScriptDependency' };
android.framework.uninstall(framework, dummyPluginInfo, dummyProject);
expect(dummyProject.removeAppBuildScriptDependency).toHaveBeenCalledWith(dummyProject.projectDir, someString);
});
});
describe('of <js-module> elements', function () {
+3
View File
@@ -59,6 +59,9 @@ buildscript {
println "Adding classpath: ${gradlePluginGoogleServicesClassPath}"
classpath gradlePluginGoogleServicesClassPath
}
// APP-BUILDSCRIPT-DEPENDENCIES START
// APP-BUILDSCRIPT-DEPENDENCIES END
}
}