Skip to content

Commit

Permalink
Merge pull request #8 from apache/master
Browse files Browse the repository at this point in the history
refactor(run)!: cleanup run method (apache#1266)
  • Loading branch information
sakibguy committed Jul 13, 2021
2 parents a579a98 + 53d60dd commit 04ec650
Show file tree
Hide file tree
Showing 107 changed files with 334 additions and 295 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bin/templates/project/assets/www/cordova.js
templates/project/assets/www/cordova.js
test/android/app
test/androidx/app
2 changes: 1 addition & 1 deletion .ratignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
*.properties
bin
templates
gen
proguard-project.txt
spec
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 14 additions & 22 deletions bin/templates/cordova/Api.js → lib/Api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,18 @@
under the License.
*/

/**
* @todo update coho to update this line.
* @todo use `package.json` instead but first
* figure out how this fit in with the platform-centered workflow structure.
* This workflow would not have the `package.json` file.
*/
// Coho updates this line
const VERSION = '10.0.0-dev';

var path = require('path');

var AndroidProject = require('./lib/AndroidProject');
var AndroidProject = require('./AndroidProject');
var PluginManager = require('cordova-common').PluginManager;

var CordovaLogger = require('cordova-common').CordovaLogger;
var selfEvents = require('cordova-common').events;
var ConfigParser = require('cordova-common').ConfigParser;
const prepare = require('./lib/prepare').prepare;
const prepare = require('./prepare').prepare;

var PLATFORM = 'android';
const VERSION = require('../package').version;

function setupEvents (externalEventEmitter) {
if (externalEventEmitter) {
Expand Down Expand Up @@ -66,7 +58,7 @@ function setupEvents (externalEventEmitter) {
class Api {
constructor (platform, platformRootDir, events) {
this.platform = PLATFORM;
this.root = path.resolve(__dirname, '..');
this.root = platformRootDir;

setupEvents(events);

Expand All @@ -86,7 +78,7 @@ class Api {
javaSrc: path.join(appMain, 'java')
};

this._builder = require('./lib/builders/builders').getBuilder(this.root);
this._builder = require('./builders/builders').getBuilder(this.root);
}

/**
Expand Down Expand Up @@ -249,8 +241,8 @@ class Api {
build (buildOptions) {
var self = this;

return require('./lib/check_reqs').run().then(function () {
return require('./lib/build').run.call(self, buildOptions);
return require('./check_reqs').run().then(function () {
return require('./build').run.call(self, buildOptions);
}).then(function (buildResults) {
// Cast build result to array of build artifacts
return buildResults.paths.map(function (apkPath) {
Expand Down Expand Up @@ -278,8 +270,8 @@ class Api {
*/
run (runOptions) {
var self = this;
return require('./lib/check_reqs').run().then(function () {
return require('./lib/run').run.call(self, runOptions);
return require('./check_reqs').run().then(function () {
return require('./run').run.call(self, runOptions);
});
}

Expand All @@ -297,10 +289,10 @@ class Api {
cleanOptions = {};
}

return require('./lib/check_reqs').run().then(function () {
return require('./lib/build').runClean.call(self, cleanOptions);
return require('./check_reqs').run().then(function () {
return require('./build').runClean.call(self, cleanOptions);
}).then(function () {
return require('./lib/prepare').clean.call(self, cleanOptions);
return require('./prepare').clean.call(self, cleanOptions);
});
}

Expand All @@ -313,7 +305,7 @@ class Api {
* objects for current platform.
*/
requirements () {
return require('./lib/check_reqs').check_all();
return require('./check_reqs').check_all(this.root);
}

/**
Expand All @@ -338,7 +330,7 @@ class Api {
events = setupEvents(events);
var result;
try {
result = require('../../lib/create').create(destination, config, options, events).then(function (destination) {
result = require('./create').create(destination, config, options, events).then(function (destination) {
return new Api(PLATFORM, destination, events);
});
} catch (e) {
Expand Down
File renamed without changes.
File renamed without changes.
11 changes: 7 additions & 4 deletions bin/templates/cordova/lib/build.js → lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
var path = require('path');
var fs = require('fs');
var nopt = require('nopt');
const untildify = require('untildify');

var Adb = require('./Adb');

Expand Down Expand Up @@ -82,9 +83,7 @@ function parseOpts (options, resolvedTarget, projectRoot) {
if (config.android && config.android[ret.buildType]) {
var androidInfo = config.android[ret.buildType];
if (androidInfo.keystore && !packageArgs.keystore) {
if (androidInfo.keystore.substr(0, 1) === '~') {
androidInfo.keystore = process.env.HOME + androidInfo.keystore.substr(1);
}
androidInfo.keystore = untildify(androidInfo.keystore);
packageArgs.keystore = path.resolve(path.dirname(buildConfig), androidInfo.keystore);
events.emit('log', 'Reading the keystore from: ' + packageArgs.keystore);
}
Expand Down Expand Up @@ -130,7 +129,11 @@ function parseOpts (options, resolvedTarget, projectRoot) {
ret.packageType = packageArgs.packageType;
}
} else {
ret.packageType = PackageType.APK;
if (ret.buildType === 'release') {
ret.packageType = PackageType.BUNDLE;
} else {
ret.packageType = PackageType.APK;
}
}

return ret;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function findOutputFiles (bundleType, buildType, { arch } = {}) {

class ProjectBuilder {
constructor (rootDirectory) {
this.root = rootDirectory || path.resolve(__dirname, '../../..');
this.root = rootDirectory;
this.apkDir = path.join(this.root, 'app', 'build', 'outputs', 'apk');
this.aabDir = path.join(this.root, 'app', 'build', 'outputs', 'bundle');
}
Expand Down Expand Up @@ -160,7 +160,7 @@ class ProjectBuilder {
// Makes the project buildable, minus the gradle wrapper.
prepBuildFiles () {
// Update the version of build.gradle in each dependent library.
var pluginBuildGradle = path.join(this.root, 'cordova', 'lib', 'plugin-build.gradle');
var pluginBuildGradle = path.join(__dirname, 'plugin-build.gradle');
var propertiesObj = this.readProjectProperties();
var subProjects = propertiesObj.libs;

Expand Down Expand Up @@ -310,7 +310,7 @@ class ProjectBuilder {
if (error.toString().includes('failed to find target with hash string')) {
// Add hint from check_android_target to error message
try {
await check_reqs.check_android_target();
await check_reqs.check_android_target(this.root);
} catch (checkAndroidTargetError) {
error.message += '\n' + checkAndroidTargetError.message;
}
Expand Down
File renamed without changes.
File renamed without changes.
27 changes: 17 additions & 10 deletions bin/templates/cordova/lib/check_reqs.js → lib/check_reqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ var path = require('path');
var fs = require('fs-extra');
const { forgivingWhichSync, isWindows, isDarwin } = require('./utils');
const java = require('./env/java');
var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..');
const { CordovaError, ConfigParser, events } = require('cordova-common');
var android_sdk = require('./android_sdk');
const { SDK_VERSION } = require('./gradle-config-defaults');
Expand All @@ -32,10 +31,11 @@ const { SDK_VERSION } = require('./gradle-config-defaults');
Object.assign(module.exports, { isWindows, isDarwin });

/**
* @param {string} projectRoot
* @returns {string} The android target in format "android-${target}"
*/
module.exports.get_target = function () {
const userTargetSdkVersion = getUserTargetSdkVersion();
module.exports.get_target = function (projectRoot) {
const userTargetSdkVersion = getUserTargetSdkVersion(projectRoot);

if (userTargetSdkVersion && userTargetSdkVersion < SDK_VERSION) {
events.emit('warn', `android-targetSdkVersion should be greater than or equal to ${SDK_VERSION}.`);
Expand All @@ -44,10 +44,16 @@ module.exports.get_target = function () {
return `android-${Math.max(userTargetSdkVersion, SDK_VERSION)}`;
};

/** @returns {number} target sdk or 0 if undefined */
function getUserTargetSdkVersion () {
/**
* @param {string} projectRoot
* @returns {number} target sdk or 0 if undefined
*/
function getUserTargetSdkVersion (projectRoot) {
// If the repo config.xml file exists, find the desired targetSdkVersion.
const configFile = path.join(REPO_ROOT, 'config.xml');
// We need to use the cordova project's config.xml here, since the platform
// project's config.xml does not yet have the user's preferences when this
// function is called during `Api.createPlatform`.
const configFile = path.join(projectRoot, '../../config.xml');
if (!fs.existsSync(configFile)) return 0;

const configParser = new ConfigParser(configFile);
Expand Down Expand Up @@ -235,13 +241,13 @@ module.exports.check_android = function () {
});
};

module.exports.check_android_target = function () {
module.exports.check_android_target = function (projectRoot) {
// valid_target can look like:
// android-19
// android-L
// Google Inc.:Google APIs:20
// Google Inc.:Glass Development Kit Preview:20
var desired_api_level = module.exports.get_target();
var desired_api_level = module.exports.get_target(projectRoot);
return android_sdk.list_targets().then(function (targets) {
if (targets.indexOf(desired_api_level) >= 0) {
return targets;
Expand Down Expand Up @@ -286,9 +292,10 @@ var Requirement = function (id, name, version, installed) {
* Methods that runs all checks one by one and returns a result of checks
* as an array of Requirement objects. This method intended to be used by cordova-lib check_reqs method
*
* @param {string} projectRoot
* @return Promise<Requirement[]> Array of requirements. Due to implementation, promise is always fulfilled.
*/
module.exports.check_all = function () {
module.exports.check_all = function (projectRoot) {
var requirements = [
new Requirement('java', 'Java JDK'),
new Requirement('androidSdk', 'Android SDK'),
Expand All @@ -299,7 +306,7 @@ module.exports.check_all = function () {
var checkFns = [
this.check_java,
this.check_android,
this.check_android_target,
this.check_android_target.bind(this, projectRoot),
this.check_gradle
];

Expand Down
File renamed without changes.
28 changes: 12 additions & 16 deletions bin/lib/create.js → lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

var path = require('path');
var fs = require('fs-extra');
var utils = require('../templates/cordova/lib/utils');
var check_reqs = require('./../templates/cordova/lib/check_reqs');
var ROOT = path.join(__dirname, '..', '..');
var utils = require('./utils');
var check_reqs = require('./check_reqs');
var ROOT = path.join(__dirname, '..');
const { createEditor } = require('properties-parser');

var CordovaError = require('cordova-common').CordovaError;
var AndroidManifest = require('../templates/cordova/lib/AndroidManifest');
var AndroidManifest = require('./AndroidManifest');

// Export all helper functions, and make sure internally within this module, we
// reference these methods via the `exports` object - this helps with testing
Expand All @@ -45,7 +45,7 @@ function getFrameworkDir (projectPath, shared) {

function copyJsAndLibrary (projectPath, shared, projectName, targetAPI) {
var nestedCordovaLibPath = getFrameworkDir(projectPath, false);
var srcCordovaJsPath = path.join(ROOT, 'bin', 'templates', 'project', 'assets', 'www', 'cordova.js');
var srcCordovaJsPath = path.join(ROOT, 'templates', 'project', 'assets', 'www', 'cordova.js');
var app_path = path.join(projectPath, 'app', 'src', 'main');
const platform_www = path.join(projectPath, 'platform_www');

Expand Down Expand Up @@ -89,7 +89,7 @@ function extractSubProjectPaths (data) {

function writeProjectProperties (projectPath, target_api) {
var dstPath = path.join(projectPath, 'project.properties');
var templatePath = path.join(ROOT, 'bin', 'templates', 'project', 'project.properties');
var templatePath = path.join(ROOT, 'templates', 'project', 'project.properties');
var srcPath = fs.existsSync(dstPath) ? dstPath : templatePath;

var data = fs.readFileSync(srcPath, 'utf8');
Expand All @@ -113,12 +113,12 @@ function writeProjectProperties (projectPath, target_api) {

// This makes no sense, what if you're building with a different build system?
function prepBuildFiles (projectPath) {
var buildModule = require('../templates/cordova/lib/builders/builders');
var buildModule = require('./builders/builders');
buildModule.getBuilder(projectPath).prepBuildFiles();
}

function copyBuildRules (projectPath, isLegacy) {
var srcDir = path.join(ROOT, 'bin', 'templates', 'project');
var srcDir = path.join(ROOT, 'templates', 'project');

if (isLegacy) {
// The project's build.gradle is identical to the earlier build.gradle, so it should still work
Expand All @@ -134,16 +134,12 @@ function copyBuildRules (projectPath, isLegacy) {
}

function copyScripts (projectPath) {
var bin = path.join(ROOT, 'bin');
var srcScriptsDir = path.join(bin, 'templates', 'cordova');
var srcScriptsDir = path.join(ROOT, 'templates', 'cordova');
var destScriptsDir = path.join(projectPath, 'cordova');
// Delete old scripts directory if this is an update.
fs.removeSync(destScriptsDir);
// Copy in the new ones.
fs.copySync(srcScriptsDir, destScriptsDir);

const nodeModulesDir = path.join(ROOT, 'node_modules');
if (fs.existsSync(nodeModulesDir)) fs.copySync(nodeModulesDir, path.join(destScriptsDir, 'node_modules'));
}

/**
Expand Down Expand Up @@ -230,7 +226,7 @@ exports.create = function (project_path, config, options, events) {
? config.name().replace(/[^\w.]/g, '_') : 'CordovaExample';

var safe_activity_name = config.android_activityName() || options.activityName || 'MainActivity';
var target_api = check_reqs.get_target();
var target_api = check_reqs.get_target(project_path);

// Make the package conform to Java package types
return exports.validatePackageName(package_name)
Expand All @@ -247,7 +243,7 @@ exports.create = function (project_path, config, options, events) {

events.emit('verbose', 'Copying android template project to ' + project_path);

var project_template_dir = options.customTemplate || path.join(ROOT, 'bin', 'templates', 'project');
var project_template_dir = options.customTemplate || path.join(ROOT, 'templates', 'project');
var app_path = path.join(project_path, 'app', 'src', 'main');

// copy project template
Expand Down Expand Up @@ -300,7 +296,7 @@ exports.create = function (project_path, config, options, events) {
};

function generateDoneMessage (type, link) {
var pkg = require('../../package');
var pkg = require('../package');
var msg = 'Android project ' + (type === 'update' ? 'updated ' : 'created ') + 'with ' + pkg.name + '@' + pkg.version;
if (link) {
msg += ' and has a linked CordovaLib';
Expand Down

0 comments on commit 04ec650

Please sign in to comment.