2013-08-13 08:07:23 +08:00
#!/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 shell = require ( 'shelljs' ) ,
2013-09-27 23:48:55 +08:00
child _process = require ( 'child_process' ) ,
Q = require ( 'q' ) ,
2013-08-13 08:07:23 +08:00
path = require ( 'path' ) ,
fs = require ( 'fs' ) ,
ROOT = path . join ( _ _dirname , '..' , '..' ) ;
// Get valid target from framework/project.properties
module . exports . get _target = function ( ) {
if ( fs . existsSync ( path . join ( ROOT , 'framework' , 'project.properties' ) ) ) {
var target = shell . grep ( /target=android-[\d+]/ , path . join ( ROOT , 'framework' , 'project.properties' ) ) ;
return target . split ( '=' ) [ 1 ] . replace ( '\n' , '' ) . replace ( '\r' , '' ) . replace ( ' ' , '' ) ;
} else if ( fs . existsSync ( path . join ( ROOT , 'project.properties' ) ) ) {
// if no target found, we're probably in a project and project.properties is in ROOT.
2013-11-21 01:35:23 +08:00
// this is called on the project itself, and can support Google APIs AND Vanilla Android
var target = shell . grep ( /target=android-[\d+]/ , path . join ( ROOT , 'project.properties' ) ) ||
shell . grep ( /target=Google Inc.:Google APIs:[\d+]/ , path . join ( ROOT , 'project.properties' ) ) ;
return target . split ( '=' ) [ 1 ] . replace ( '\n' , '' ) . replace ( '\r' , '' ) ;
2013-08-13 08:07:23 +08:00
}
}
2013-09-27 23:48:55 +08:00
// Returns a promise.
2013-08-13 08:07:23 +08:00
module . exports . check _ant = function ( ) {
2013-09-27 23:48:55 +08:00
var d = Q . defer ( ) ;
child _process . exec ( 'ant -version' , function ( err , stdout , stderr ) {
if ( err ) d . reject ( new Error ( 'ERROR : executing command \'ant\', make sure you have ant installed and added to your path.' ) ) ;
else d . resolve ( ) ;
} ) ;
return d . promise ;
2013-08-13 08:07:23 +08:00
}
2013-09-27 23:48:55 +08:00
// Returns a promise.
2013-08-13 08:07:23 +08:00
module . exports . check _java = function ( ) {
2013-11-28 05:59:20 +08:00
var d = Q . defer ( ) ;
child _process . exec ( 'java -version' , function ( err , stdout , stderr ) {
if ( err ) {
var msg =
'Failed to run \'java -version\', make sure your java environment is set up\n' +
'including JDK and JRE.\n' +
'Your JAVA_HOME variable is ' + process . env . JAVA _HOME + '\n' ;
d . reject ( new Error ( msg + err ) ) ;
}
else d . resolve ( ) ;
} ) ;
return d . promise ;
2013-08-13 08:07:23 +08:00
}
2013-09-27 23:48:55 +08:00
// Returns a promise.
2013-08-13 08:07:23 +08:00
module . exports . check _android = function ( ) {
var valid _target = this . get _target ( ) ;
2013-09-27 23:48:55 +08:00
var d = Q . defer ( ) ;
child _process . exec ( 'android list targets' , function ( err , stdout , stderr ) {
if ( err ) d . reject ( stderr ) ;
else d . resolve ( stdout ) ;
} ) ;
2013-08-13 08:07:23 +08:00
2013-09-27 23:48:55 +08:00
return d . promise . then ( function ( output ) {
if ( ! output . match ( valid _target ) ) {
return Q . reject ( new Error ( 'Please install Android target ' + valid _target . split ( '-' ) [ 1 ] + ' (the Android newest SDK). Make sure you have the latest Android tools installed as well. Run \"android\" from your command-line to install/update any missing SDKs or tools.' ) ) ;
}
return Q ( ) ;
} , function ( stderr ) {
2014-06-22 05:43:30 +08:00
if ( stderr . match ( /command\snot\sfound/ ) || stderr . match ( /is not recognized as an internal or external command/ ) ) {
2013-09-27 23:48:55 +08:00
return Q . reject ( new Error ( 'The command \"android\" failed. Make sure you have the latest Android SDK installed, and the \"android\" command (inside the tools/ folder) is added to your path.' ) ) ;
} else {
2014-07-01 02:29:53 +08:00
return Q . reject ( new Error ( 'An error occurred while listing Android targets. Error: ' + stderr ) ) ;
2013-09-27 23:48:55 +08:00
}
} ) ;
2013-08-13 08:07:23 +08:00
}
2013-09-27 23:48:55 +08:00
// Returns a promise.
2013-08-13 08:07:23 +08:00
module . exports . run = function ( ) {
2013-11-30 03:39:09 +08:00
return Q . all ( [ this . check _ant ( ) , this . check _java ( ) , this . check _android ( ) ] ) ;
2013-09-14 09:38:46 +08:00
}
2013-10-18 21:59:42 +08:00