fix: display project name in Android Studio (#1214)

This commit is contained in:
Alexis THOMAS 2021-08-13 05:08:18 +02:00 committed by GitHub
parent b5e79b5a4d
commit 09c75237d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 41 deletions

View File

@ -183,21 +183,29 @@ class ProjectBuilder {
checkAndCopy(subProjects[i], this.root); checkAndCopy(subProjects[i], this.root);
} }
} }
var name = this.extractRealProjectNameFromManifest(); var projectName = this.extractRealProjectNameFromManifest();
// Remove the proj.id/name- prefix from projects: https://issues.apache.org/jira/browse/CB-9149 // Remove the proj.id/name- prefix from projects: https://issues.apache.org/jira/browse/CB-9149
var settingsGradlePaths = subProjects.map(function (p) { var settingsGradlePaths = subProjects.map(function (p) {
var realDir = p.replace(/[/\\]/g, ':'); var realDir = p.replace(/[/\\]/g, ':');
var libName = realDir.replace(name + '-', ''); var libName = realDir.replace(projectName + '-', '');
var str = 'include ":' + libName + '"\n'; var str = 'include ":' + libName + '"\n';
if (realDir.indexOf(name + '-') !== -1) { if (realDir.indexOf(projectName + '-') !== -1) {
str += 'project(":' + libName + '").projectDir = new File("' + p + '")\n'; str += 'project(":' + libName + '").projectDir = new File("' + p + '")\n';
} }
return str; return str;
}); });
// Update subprojects within settings.gradle.
fs.writeFileSync(path.join(this.root, 'settings.gradle'), fs.writeFileSync(path.join(this.root, 'settings.gradle'),
'// GENERATED FILE - DO NOT EDIT\n' + '// GENERATED FILE - DO NOT EDIT\n' +
'include ":"\n' + settingsGradlePaths.join('')); 'apply from: "cdv-gradle-name.gradle"\n' +
'include ":"\n' +
settingsGradlePaths.join(''));
// Touch empty cdv-gradle-name.gradle file if missing.
if (!fs.pathExistsSync(path.join(this.root, 'cdv-gradle-name.gradle'))) {
fs.writeFileSync(path.join(this.root, 'cdv-gradle-name.gradle'), '');
}
// Update dependencies within build.gradle. // Update dependencies within build.gradle.
var buildGradle = fs.readFileSync(path.join(this.root, 'app', 'build.gradle'), 'utf8'); var buildGradle = fs.readFileSync(path.join(this.root, 'app', 'build.gradle'), 'utf8');
@ -214,7 +222,7 @@ class ProjectBuilder {
}; };
subProjects.forEach(function (p) { subProjects.forEach(function (p) {
events.emit('log', 'Subproject Path: ' + p); events.emit('log', 'Subproject Path: ' + p);
var libName = p.replace(/[/\\]/g, ':').replace(name + '-', ''); var libName = p.replace(/[/\\]/g, ':').replace(projectName + '-', '');
if (libName !== 'app') { if (libName !== 'app') {
depsList += ' implementation(project(path: ":' + libName + '"))'; depsList += ' implementation(project(path: ":' + libName + '"))';
insertExclude(p); insertExclude(p);

View File

@ -37,7 +37,6 @@ exports.copyScripts = copyScripts;
exports.copyBuildRules = copyBuildRules; exports.copyBuildRules = copyBuildRules;
exports.writeProjectProperties = writeProjectProperties; exports.writeProjectProperties = writeProjectProperties;
exports.prepBuildFiles = prepBuildFiles; exports.prepBuildFiles = prepBuildFiles;
exports.writeNameForAndroidStudio = writeNameForAndroidStudio;
function getFrameworkDir (projectPath, shared) { function getFrameworkDir (projectPath, shared) {
return shared ? path.join(ROOT, 'framework') : path.join(projectPath, 'CordovaLib'); return shared ? path.join(ROOT, 'framework') : path.join(projectPath, 'CordovaLib');
@ -176,19 +175,6 @@ function validateProjectName (project_name) {
return Promise.resolve(); return Promise.resolve();
} }
/**
* Write the name of the app in "platforms/android/.idea/.name" so that Android Studio can show that name in the
* project listing. This is helpful to quickly look in the Android Studio listing if there are so many projects in
* Android Studio.
*
* https://github.com/apache/cordova-android/issues/1172
*/
function writeNameForAndroidStudio (project_path, project_name) {
const ideaPath = path.join(project_path, '.idea');
fs.ensureDirSync(ideaPath);
fs.writeFileSync(path.join(ideaPath, '.name'), project_name);
}
/** /**
* Creates an android application with the given options. * Creates an android application with the given options.
* *
@ -286,7 +272,6 @@ exports.create = function (project_path, config, options, events) {
// Link it to local android install. // Link it to local android install.
exports.writeProjectProperties(project_path, target_api); exports.writeProjectProperties(project_path, target_api);
exports.prepBuildFiles(project_path); exports.prepBuildFiles(project_path);
exports.writeNameForAndroidStudio(project_path, project_name);
events.emit('log', generateDoneMessage('create', options.link)); events.emit('log', generateDoneMessage('create', options.link));
}).then(() => project_path); }).then(() => project_path);
}; };

View File

@ -273,6 +273,11 @@ function updateProjectAccordingTo (platformConfig, locations) {
fs.writeFileSync(locations.strings, strings.write({ indent: 4 }), 'utf-8'); fs.writeFileSync(locations.strings, strings.write({ indent: 4 }), 'utf-8');
events.emit('verbose', 'Wrote out android application name "' + name + '" to ' + locations.strings); events.emit('verbose', 'Wrote out android application name "' + name + '" to ' + locations.strings);
// Update app name for gradle project
fs.writeFileSync(path.join(locations.root, 'cdv-gradle-name.gradle'),
'// GENERATED FILE - DO NOT EDIT\n' +
'rootProject.name = "' + name.replace(/[/\\:<>"?*|]/g, '_') + '"\n');
// Java packages cannot support dashes // Java packages cannot support dashes
var androidPkgName = (platformConfig.android_packageName() || platformConfig.packageName()).replace(/-/g, '_'); var androidPkgName = (platformConfig.android_packageName() || platformConfig.packageName()).replace(/-/g, '_');

View File

@ -132,7 +132,6 @@ describe('create', function () {
spyOn(create, 'copyBuildRules'); spyOn(create, 'copyBuildRules');
spyOn(create, 'writeProjectProperties'); spyOn(create, 'writeProjectProperties');
spyOn(create, 'prepBuildFiles'); spyOn(create, 'prepBuildFiles');
spyOn(create, 'writeNameForAndroidStudio');
revert_manifest_mock = create.__set__('AndroidManifest', Manifest_mock); revert_manifest_mock = create.__set__('AndroidManifest', Manifest_mock);
spyOn(fs, 'existsSync').and.returnValue(false); spyOn(fs, 'existsSync').and.returnValue(false);
spyOn(fs, 'copySync'); spyOn(fs, 'copySync');
@ -301,24 +300,4 @@ describe('create', function () {
}); });
}); });
}); });
describe('writeNameForAndroidStudio', () => {
const project_path = path.join('some', 'path');
const appName = 'Test Cordova';
beforeEach(function () {
spyOn(fs, 'ensureDirSync');
spyOn(fs, 'writeFileSync');
});
it('should call ensureDirSync with path', () => {
create.writeNameForAndroidStudio(project_path, appName);
expect(fs.ensureDirSync).toHaveBeenCalledWith(path.join(project_path, '.idea'));
});
it('should call writeFileSync with content', () => {
create.writeNameForAndroidStudio(project_path, appName);
expect(fs.writeFileSync).toHaveBeenCalledWith(path.join(project_path, '.idea', '.name'), appName);
});
});
}); });