diff --git a/bin/lib/create.js b/bin/lib/create.js index f99d1da9..8a908276 100755 --- a/bin/lib/create.js +++ b/bin/lib/create.js @@ -106,6 +106,50 @@ function copyScripts(projectPath) { shell.cp(path.join(ROOT, 'bin', 'lib', 'android_sdk_version.js'), path.join(projectPath, 'cordova', 'lib', 'android_sdk_version.js')); } +/** + * Test whether a package name is acceptable for use as an android project. + * Returns a promise, fulfilled if the package name is acceptable; rejected + * otherwise. + */ +function validatePackageName(package_name) { + //Make the package conform to Java package types + if (!/[a-zA-Z0-9_]+\.[a-zA-Z0-9_](.[a-zA-Z0-9_])*/.test(package_name)) { + return Q.reject('Package name must look like: com.company.Name'); + } + + //Enforce underscore limitation + if (/[_]+[a-zA-Z0-9_]*/.test(package_name)) { + return Q.reject("Package name can't begin with an underscore"); + } + + //Class is a reserved word + if(/[C|c]+lass+[\s|\.]/.test(package_name) && !/[a-zA-Z0-9_]+[C|c]+lass/.test(package_name)) + { + return Q.reject('class is a reserved word'); + } + + return Q.resolve(); +} + +/** + * Test whether a project name is acceptable for use as an android class. + * Returns a promise, fulfilled if the project name is acceptable; rejected + * otherwise. + */ +function validateProjectName(project_name) { + //Enforce stupid name error + if (project_name === 'CordovaActivity') { + return Q.reject('Project name cannot be CordovaActivity'); + } + + //Classes in Java don't begin with numbers + if (/^[0-9]/.test(project_name)) { + return Q.reject('Project name must not begin with a number'); + } + + return Q.resolve(); +} + /** * $ create [options] * @@ -146,34 +190,14 @@ exports.createProject = function(project_path, package_name, project_name, proje } //Make the package conform to Java package types - if (!/[a-zA-Z0-9_]+\.[a-zA-Z0-9_](.[a-zA-Z0-9_])*/.test(package_name)) { - return Q.reject('Package name must look like: com.company.Name'); - } - - //Enforce underscore limitation - if (/[_]+[a-zA-Z0-9_]*/.test(package_name)) { - return Q.reject("Package name can't begin with an underscore"); - } - - //Enforce stupid name error - if (project_name === 'CordovaActivity') { - return Q.reject('Project name cannot be CordovaActivity'); - } - - //Classes in Java don't begin with numbers - if (/[0-9]+[a-zA-Z0-9]/.test(project_name)) { - return Q.reject('Project name must not begin with a number'); - } - - //Class is a reserved word - if(/[C|c]+lass+[\s|\.]/.test(package_name) && !/[a-zA-Z0-9_]+[C|c]+lass/.test(package_name)) - { - return Q.reject('class is a reserved word'); - } - - // Check that requirements are met and proper targets are installed - return check_reqs.run() + return validatePackageName(package_name) .then(function() { + validateProjectName(project_name); + }) + // Check that requirements are met and proper targets are installed + .then(function() { + check_reqs.run(); + }).then(function() { // Log the given values for the project console.log('Creating Cordova project for the Android platform:'); console.log('\tPath: ' + project_path);