You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by er...@apache.org on 2021/08/13 03:08:25 UTC

[cordova-android] branch master updated: fix: display project name in Android Studio (#1214)

This is an automated email from the ASF dual-hosted git repository.

erisu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git


The following commit(s) were added to refs/heads/master by this push:
     new 09c7523  fix: display project name in Android Studio (#1214)
09c7523 is described below

commit 09c75237d9348a944b4946ccb31d2dee90330ce4
Author: Alexis THOMAS <at...@users.noreply.github.com>
AuthorDate: Fri Aug 13 05:08:18 2021 +0200

    fix: display project name in Android Studio (#1214)
---
 lib/builders/ProjectBuilder.js | 18 +++++++++++++-----
 lib/create.js                  | 15 ---------------
 lib/prepare.js                 |  5 +++++
 spec/unit/create.spec.js       | 21 ---------------------
 4 files changed, 18 insertions(+), 41 deletions(-)

diff --git a/lib/builders/ProjectBuilder.js b/lib/builders/ProjectBuilder.js
index 1dcaaf7..f92e358 100644
--- a/lib/builders/ProjectBuilder.js
+++ b/lib/builders/ProjectBuilder.js
@@ -183,21 +183,29 @@ class ProjectBuilder {
                 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
         var settingsGradlePaths = subProjects.map(function (p) {
             var realDir = p.replace(/[/\\]/g, ':');
-            var libName = realDir.replace(name + '-', '');
+            var libName = realDir.replace(projectName + '-', '');
             var str = 'include ":' + libName + '"\n';
-            if (realDir.indexOf(name + '-') !== -1) {
+            if (realDir.indexOf(projectName + '-') !== -1) {
                 str += 'project(":' + libName + '").projectDir = new File("' + p + '")\n';
             }
             return str;
         });
 
+        // Update subprojects within settings.gradle.
         fs.writeFileSync(path.join(this.root, 'settings.gradle'),
             '// 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.
         var buildGradle = fs.readFileSync(path.join(this.root, 'app', 'build.gradle'), 'utf8');
@@ -214,7 +222,7 @@ class ProjectBuilder {
         };
         subProjects.forEach(function (p) {
             events.emit('log', 'Subproject Path: ' + p);
-            var libName = p.replace(/[/\\]/g, ':').replace(name + '-', '');
+            var libName = p.replace(/[/\\]/g, ':').replace(projectName + '-', '');
             if (libName !== 'app') {
                 depsList += '    implementation(project(path: ":' + libName + '"))';
                 insertExclude(p);
diff --git a/lib/create.js b/lib/create.js
index 2818264..e8388d9 100755
--- a/lib/create.js
+++ b/lib/create.js
@@ -37,7 +37,6 @@ 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');
@@ -177,19 +176,6 @@ function validateProjectName (project_name) {
 }
 
 /**
- * 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.
  *
  * @param   {String}  project_path  Path to the new Cordova android project.
@@ -286,7 +272,6 @@ 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/lib/prepare.js b/lib/prepare.js
index e927dff..9e4f50c 100644
--- a/lib/prepare.js
+++ b/lib/prepare.js
@@ -273,6 +273,11 @@ function updateProjectAccordingTo (platformConfig, locations) {
     fs.writeFileSync(locations.strings, strings.write({ indent: 4 }), 'utf-8');
     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
     var androidPkgName = (platformConfig.android_packageName() || platformConfig.packageName()).replace(/-/g, '_');
 
diff --git a/spec/unit/create.spec.js b/spec/unit/create.spec.js
index 4e5a5a7..a8fb5a9 100644
--- a/spec/unit/create.spec.js
+++ b/spec/unit/create.spec.js
@@ -132,7 +132,6 @@ 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');
@@ -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);
-        });
-    });
 });

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org