Skip to content

Commit

Permalink
Restore binary bin/android_sdk_version for CLI compatibility
Browse files Browse the repository at this point in the history
This is used in CLI to implement an Android SDK version check for plugins.
See https://cordova.apache.org/docs/en/latest/plugin_ref/spec.html#engines-and-engine
  • Loading branch information
raphinesse committed Jul 6, 2021
1 parent 1425b84 commit c8c7b1a
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bin/templates/cordova/android_sdk_version
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

var android_sdk = require('./lib/android_sdk');

android_sdk.print_newest_available_sdk_target().catch(err => {
console.error(err);
process.exit(2);
});
27 changes: 27 additions & 0 deletions bin/templates/cordova/lib/android_sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@

const execa = require('execa');

var suffix_number_regex = /(\d+)$/;
// Used for sorting Android targets, example strings to sort:
// android-19
// android-L
// Google Inc.:Google APIs:20
// Google Inc.:Glass Development Kit Preview:20
// The idea is to sort based on largest "suffix" number - meaning the bigger
// the number at the end, the more recent the target, the closer to the
// start of the array.
function sort_by_largest_numerical_suffix (a, b) {
let suffix_a = a.match(suffix_number_regex);
let suffix_b = b.match(suffix_number_regex);
// If no number is detected (eg: preview version like android-R),
// designate a suffix of 0 so it gets moved to the end
suffix_a = suffix_a || ['0', '0'];
suffix_b = suffix_b || ['0', '0'];
// Return < zero, or > zero, based on which suffix is larger.
return (parseInt(suffix_a[1]) > parseInt(suffix_b[1]) ? -1 : 1);
}

module.exports.print_newest_available_sdk_target = function () {
return module.exports.list_targets().then(function (targets) {
targets.sort(sort_by_largest_numerical_suffix);
console.log(targets[0]);
});
};

// Versions should not be represented as float, so we disable quote-props here
/* eslint-disable quote-props */
module.exports.version_string_to_api_level = {
Expand Down
35 changes: 35 additions & 0 deletions spec/unit/android_sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,41 @@ describe('android_sdk', () => {
android_sdk.__set__('execa', execaSpy);
});

describe('sort_by_largest_numerical_suffix', () => {
it('should return the newest version first', () => {
const ids = ['android-P', 'android-24', 'android-19', 'android-27', 'android-23'];
const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19', 'android-P'];
expect(ids.sort(android_sdk.__get__('sort_by_largest_numerical_suffix'))).toEqual(sortedIds);
});

describe('should return release version over preview versions', () => {
it('Test #001', () => {
const ids = ['android-27', 'android-P'];
expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(-1);
});

it('Test #002', () => {
const ids = ['android-P', 'android-27'];
expect(android_sdk.__get__('sort_by_largest_numerical_suffix')(ids[0], ids[1])).toBe(1);
});
});
});

describe('print_newest_available_sdk_target', () => {
it('should log the newest version', () => {
const sortedIds = ['android-27', 'android-24', 'android-23', 'android-19'];

spyOn(android_sdk, 'list_targets').and.returnValue(Promise.resolve(sortedIds));
spyOn(sortedIds, 'sort');
spyOn(console, 'log');

return android_sdk.print_newest_available_sdk_target().then(() => {
expect(sortedIds.sort).toHaveBeenCalledWith(android_sdk.__get__('sort_by_largest_numerical_suffix'));
expect(console.log).toHaveBeenCalledWith(sortedIds[0]);
});
});
});

describe('list_targets_with_avdmanager', () => {
it('should parse and return results from `avdmanager list target` command', () => {
const testTargets = fs.readFileSync(path.join('spec', 'fixtures', 'sdk25.3-avdmanager_list_target.txt'), 'utf-8');
Expand Down

0 comments on commit c8c7b1a

Please sign in to comment.