2013-09-24 05:01:03 +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 .
* /
2013-10-19 06:56:42 +08:00
var shell = require ( 'shelljs' ) ,
child _process = require ( 'child_process' ) ,
Q = require ( 'q' ) ;
2013-09-24 05:01:03 +08:00
get _highest _sdk = function ( results ) {
var reg = /\d+/ ;
var apiLevels = [ ] ;
for ( var i = 0 ; i < results . length ; i ++ ) {
apiLevels [ i ] = parseInt ( results [ i ] . match ( reg ) [ 0 ] ) ;
}
apiLevels . sort ( function ( a , b ) { return b - a } ) ;
console . log ( apiLevels [ 0 ] ) ;
}
get _sdks = function ( ) {
2013-10-19 06:56:42 +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-09-24 05:01:03 +08:00
2013-10-19 06:56:42 +08:00
return d . promise . then ( function ( output ) {
2013-09-24 05:01:03 +08:00
var reg = /android-\d+/gi ;
2013-10-19 06:56:42 +08:00
var results = output . match ( reg ) ;
2013-09-24 05:01:03 +08:00
if ( results . length === 0 ) {
2013-10-19 06:56:42 +08:00
return Q . reject ( new Error ( 'No android sdks installed.' ) ) ;
2013-09-24 05:01:03 +08:00
} else {
get _highest _sdk ( results ) ;
}
2013-10-19 06:56:42 +08:00
return Q ( ) ;
} , function ( stderr ) {
2014-08-18 19:53:07 +08:00
if ( stderr . match ( /command\snot\sfound/ ) || stderr . match ( /'android' is not recognized/ ) ) {
2013-10-19 06:56:42 +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 {
return Q . reject ( new Error ( 'An error occurred while listing Android targets' ) ) ;
}
} ) ;
2013-09-24 05:01:03 +08:00
}
module . exports . run = function ( ) {
2013-10-19 06:56:42 +08:00
return Q . all ( [ get _sdks ( ) ] ) ;
2013-09-24 05:01:03 +08:00
}