2016-11-07 07:15:16 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-08 09:29:34 +00:00
|
|
|
namespace api\module;
|
2016-11-07 07:15:16 +00:00
|
|
|
|
2016-11-08 08:54:59 +00:00
|
|
|
use \error\core\Error;
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
class release{
|
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* Releases a project
|
2016-11-07 07:15:16 +00:00
|
|
|
*
|
2017-12-10 22:20:06 +00:00
|
|
|
* @project<String> Project's name
|
|
|
|
* @step<id> [OPT] Step to run
|
2016-11-07 07:15:16 +00:00
|
|
|
*
|
|
|
|
*/
|
2017-12-10 22:35:06 +00:00
|
|
|
public function get($args){
|
2016-11-07 07:15:16 +00:00
|
|
|
extract($args);
|
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* (1) Load projects' configuration
|
|
|
|
---------------------------------------------------------*/
|
2016-11-07 07:15:16 +00:00
|
|
|
/* (1) Fetch file */
|
|
|
|
$fc = file_get_contents(__CONFIG__.'/projects.json');
|
|
|
|
if( !$fc )
|
2017-12-10 22:20:06 +00:00
|
|
|
return [ 'error' => new Error(Err::UnreachableResource) ];
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
/* (2) Checks json format */
|
|
|
|
$js = json_decode( $fc, true );
|
|
|
|
|
|
|
|
if( !$js )
|
2017-12-10 22:20:06 +00:00
|
|
|
return [ 'error' => new Error(Err::ParsingFailed) ];
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* (2) Checks @project argument and files
|
|
|
|
---------------------------------------------------------*/
|
2016-11-07 07:15:16 +00:00
|
|
|
/* (1) Checks @project */
|
|
|
|
if( !isset($js[$project]) )
|
2017-12-10 22:20:06 +00:00
|
|
|
return [ 'error' => new Error(Err::UnreachableResource) ];
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
/* (2) Checks @project's folder */
|
2017-12-10 22:20:06 +00:00
|
|
|
if( !is_dir($js[$project]['dir']) )
|
|
|
|
return [ 'error' => new Error(Err::UnreachableResource) ];
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
/* (3) Checks @git directory */
|
2017-12-10 22:20:06 +00:00
|
|
|
if( !is_dir($js[$project]['dir']) )
|
|
|
|
return [ 'error' => new Error(Err::UnreachableResource) ];
|
2016-11-07 07:15:16 +00:00
|
|
|
|
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* (3) Launch script
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) 'cd' to project dir */
|
|
|
|
chdir($js[$project]['dir']);
|
2016-11-07 17:17:06 +00:00
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* (2) If given step, only do this one */
|
|
|
|
if( !is_null($step) ){
|
2016-11-07 17:17:06 +00:00
|
|
|
|
2017-12-10 22:20:06 +00:00
|
|
|
/* (2.1) If step does not exist */
|
|
|
|
if( !isset($js[$project]['cmd'][$step]) )
|
|
|
|
return [ 'error' => new Error(Err::ModuleError, 'step out of bounds') ];
|
|
|
|
|
|
|
|
/* (2.2) Execute step */
|
|
|
|
exec($js[$project]['cmd'][$step], $stdout);
|
|
|
|
|
|
|
|
/* (2.3) Dispatch output */
|
|
|
|
return [ $js[$project]['cmd'][$step] => $stdout ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Execute each step one by one */
|
|
|
|
$stdout = [];
|
|
|
|
|
|
|
|
foreach($js[$project]['cmd'] as $step=>$cmd){
|
|
|
|
|
|
|
|
/* (3.1) Pre-create stdout */
|
|
|
|
$stdout[$cmd] = '';
|
|
|
|
|
|
|
|
/* (3.2) Execute step */
|
|
|
|
exec($cmd, $stdout[$cmd]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Dispatch stdout */
|
|
|
|
return [ 'stdout' => $stdout ];
|
2016-11-07 17:17:06 +00:00
|
|
|
|
2016-11-07 07:15:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|