Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify apkSorter using compare-func package #788

Merged
merged 1 commit into from
Jul 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions bin/templates/cordova/lib/builders/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(),
erisu marked this conversation as resolved.
Show resolved Hide resolved

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;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/builders/ProjectBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down