You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2019/07/18 10:00:03 UTC

[cordova-android] branch master updated: Simplify apkSorter using compare-func package (#788)

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

raphinesse 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 b3b8690  Simplify apkSorter using compare-func package (#788)
b3b8690 is described below

commit b3b8690bbdfc9c7eddce21c45e770918dd0ea8b7
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Thu Jul 18 11:59:54 2019 +0200

    Simplify apkSorter using compare-func package (#788)
---
 .../cordova/lib/builders/ProjectBuilder.js         | 29 ++++++++--------------
 package.json                                       |  1 +
 spec/unit/builders/ProjectBuilder.spec.js          |  2 +-
 3 files changed, 13 insertions(+), 19 deletions(-)

diff --git a/bin/templates/cordova/lib/builders/ProjectBuilder.js b/bin/templates/cordova/lib/builders/ProjectBuilder.js
index e7dd8f0..f2a0066 100644
--- a/bin/templates/cordova/lib/builders/ProjectBuilder.js
+++ b/bin/templates/cordova/lib/builders/ProjectBuilder.js
@@ -27,6 +27,7 @@ var spawn = require('cordova-common').superspawn.spawn;
 var events = require('cordova-common').events;
 var CordovaError = require('cordova-common').CordovaError;
 var check_reqs = require('../check_reqs');
+const compareFunc = require('compare-func');
 
 const MARKER = 'YOUR CHANGES WILL BE ERASED!';
 const SIGNING_PROPERTIES = '-signing.properties';
@@ -300,27 +301,19 @@ class ProjectBuilder {
 
 module.exports = ProjectBuilder;
 
-function apkSorter (fileA, fileB) {
-    const archSpecificRE = /-x86|-arm/;
+const apkSorter = compareFunc([
+    // Sort arch specific builds after generic ones
+    apkPath => /-x86|-arm/.test(apkPath),
 
-    const unsignedRE = /-unsigned/;
+    // Sort unsigned builds after signed ones
+    apkPath => /-unsigned/.test(apkPath),
 
-    // De-prioritize arch-specific builds & unsigned builds
-    const lower = (fileName) => {
-        return archSpecificRE.exec(fileName)
-            ? -2
-            : unsignedRE.exec(fileName)
-                ? -1
-                : 0;
-    };
+    // Sort by file modification time, latest first
+    apkPath => -fs.statSync(apkPath).mtime.getTime(),
 
-    const lowerDiff = lower(fileB) - lower(fileA);
-
-    if (lowerDiff !== 0) return lowerDiff;
-
-    var timeDiff = fs.statSync(fileB).mtime - fs.statSync(fileA).mtime;
-    return timeDiff === 0 ? fileA.length - fileB.length : timeDiff;
-}
+    // Sort by file name length, ascending
+    'length'
+]);
 
 function findOutputApksHelper (dir, build_type, arch) {
     var shellSilent = shell.config.silent;
diff --git a/package.json b/package.json
index 998c6d7..6b0a947 100644
--- a/package.json
+++ b/package.json
@@ -30,6 +30,7 @@
   "license": "Apache-2.0",
   "dependencies": {
     "android-versions": "^1.3.0",
+    "compare-func": "^1.3.2",
     "cordova-common": "^3.1.0",
     "nopt": "^4.0.1",
     "properties-parser": "^0.3.1",
diff --git a/spec/unit/builders/ProjectBuilder.spec.js b/spec/unit/builders/ProjectBuilder.spec.js
index 0f71381..6dfd05f 100644
--- a/spec/unit/builders/ProjectBuilder.spec.js
+++ b/spec/unit/builders/ProjectBuilder.spec.js
@@ -252,7 +252,7 @@ describe('ProjectBuilder', () => {
 
             const fsSpy = jasmine.createSpyObj('fs', ['statSync']);
             fsSpy.statSync.and.callFake(filename => {
-                return { mtime: APKs[filename].getTime() };
+                return { mtime: APKs[filename] };
             });
             ProjectBuilder.__set__('fs', fsSpy);
 


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