diff --git a/bin/lib/create.js b/bin/lib/create.js index 4b8eb922..96d74377 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -36,6 +36,7 @@ exports.copyScripts = copyScripts; exports.copyBuildRules = copyBuildRules; exports.writeProjectProperties = writeProjectProperties; exports.prepBuildFiles = prepBuildFiles; +exports.writeNameForAndroidStudio = writeNameForAndroidStudio; function getFrameworkDir (projectPath, shared) { return shared ? path.join(ROOT, 'framework') : path.join(projectPath, 'CordovaLib'); @@ -197,6 +198,19 @@ function validateProjectName (project_name) { 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. * @@ -294,6 +308,7 @@ exports.create = function (project_path, config, options, events) { // Link it to local android install. exports.writeProjectProperties(project_path, target_api); exports.prepBuildFiles(project_path); + exports.writeNameForAndroidStudio(project_path, project_name); events.emit('log', generateDoneMessage('create', options.link)); }).then(() => project_path); }; diff --git a/spec/unit/create.spec.js b/spec/unit/create.spec.js index 621a5314..dbe4c3ef 100644 --- a/spec/unit/create.spec.js +++ b/spec/unit/create.spec.js @@ -132,6 +132,7 @@ describe('create', function () { spyOn(create, 'copyBuildRules'); spyOn(create, 'writeProjectProperties'); spyOn(create, 'prepBuildFiles'); + spyOn(create, 'writeNameForAndroidStudio'); revert_manifest_mock = create.__set__('AndroidManifest', Manifest_mock); spyOn(fs, 'existsSync').and.returnValue(false); spyOn(fs, 'copySync'); @@ -300,4 +301,24 @@ 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); + }); + }); });