Skip to content

Commit

Permalink
refator: get_target method
Browse files Browse the repository at this point in the history
  • Loading branch information
erisu committed Jan 23, 2020
1 parent 6ffb57f commit be8ce35
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 40 deletions.
65 changes: 26 additions & 39 deletions bin/templates/cordova/lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ var fs = require('fs');
var os = require('os');
var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..');
var PROJECT_ROOT = path.join(__dirname, '..', '..');
var events = require('cordova-common').events;
var CordovaError = require('cordova-common').CordovaError;
var ConfigParser = require('cordova-common').ConfigParser;
const { CordovaError, ConfigParser, events } = require('cordova-common');
var android_sdk = require('./android_sdk');
const { createEditor } = require('properties-parser');

function forgivingWhichSync (cmd) {
try {
Expand All @@ -53,46 +52,34 @@ module.exports.isDarwin = function () {
* @returns {string} The android target in format "android-${target}"
*/
module.exports.get_target = function () {
function extractFromFile (filePath) {
var target = shelljs.grep(/\btarget=/, filePath);
if (!target) {
return null;
}
return target.split('=')[1].trim();
}
const projectPropertiesPaths = [
path.join(REPO_ROOT, 'framework', 'project.properties'),
path.join(PROJECT_ROOT, 'project.properties')
];

var target = null;
var repo_file = path.join(REPO_ROOT, 'framework', 'project.properties');
var project_file = path.join(PROJECT_ROOT, 'project.properties');
var config_file = path.join(REPO_ROOT, 'config.xml');

// First get the desired target API from the framework. This will be treated as our
// minimum required target API.
if (fs.existsSync(repo_file)) {
target = extractFromFile(repo_file);
} else if (fs.existsSync(project_file)) {
// if no target found, we're probably in a project and project.properties is in PROJECT_ROOT.
target = extractFromFile(project_file);
}
// Get the minimum required target API from the framework.
let target = projectPropertiesPaths.filter(filePath => fs.existsSync(filePath))
.map(filePath => createEditor(filePath).get('target'))
.pop();

if (target === null) {
throw new Error('We could not locate the "project.properties" at either ' + repo_file + ' or ' + project_file + '.');
if (!target) {
throw new Error(`We could not locate the target from the "project.properties" at either "${projectPropertiesPaths.join('", "')}".`);
}

var minimumAPI = parseInt(target.split('-')[1]);

// Next, check the app config.xml file for the desired targetSdkVersion and use it if
// found and valid.
if (fs.existsSync(config_file)) {
// Find the desired android target from the project config
var configParser = new ConfigParser(config_file);
var desiredAPI = parseInt(configParser.getPreference('android-targetSdkVersion', 'android'));
if (!isNaN(desiredAPI)) {
if (desiredAPI >= minimumAPI) {
target = 'android-' + desiredAPI;
} else {
events.emit('warn', 'android-targetSdkVersion must be greater than or equal to ' + minimumAPI + '.');
}
// If the repo config.xml file exists, find the desired targetSdkVersion.
const configFile = path.join(REPO_ROOT, 'config.xml');
if (!fs.existsSync(configFile)) return target;

const configParser = new ConfigParser(configFile);
const desiredAPI = parseInt(configParser.getPreference('android-targetSdkVersion', 'android'), 10);

if (!isNaN(desiredAPI)) {
const minimumAPI = parseInt(target.split('-').pop(), 10);

if (desiredAPI >= minimumAPI) {
target = `android-${desiredAPI}`;
} else {
events.emit('warn', `android-targetSdkVersion must be greater than or equal to ${minimumAPI}.`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/check_reqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var path = require('path');
var events = require('cordova-common').events;

// This should match /bin/templates/project/build.gradle
const DEFAULT_TARGET_API = 28;
const DEFAULT_TARGET_API = 29;

describe('check_reqs', function () {
var original_env;
Expand Down

0 comments on commit be8ce35

Please sign in to comment.