let instance;
const cliArgs = require('./cli-args.js');
const path = require('path');
const _ = require('lodash');
const git = require(path.resolve(__dirname,'..','utils','git.js'));
const bitrise = require(path.resolve(__dirname,'..','utils','bitrise.js'));
const jira = require(path.resolve(__dirname,'..','utils','jira.js'));
const updates = require(path.resolve(__dirname,'..','utils','checkUpdates.js'));
const exec = require('child_process').exec;
/**
* This is the workhorse of utility. Called by global @see <startJobrunner>
* @global
* @alias module:jobrunner
* @property instance {instance} Singleton of this app to hold determined settings and values.
* @property build_params {instance.build_params} At completion, contains all values for submission to Bitrise job.
* @property local_repo_path {instance.local_repo_path} Immediately assigned at instantiation and resolves to current working directory $cwd/repo
* @constructor
*/
module.exports = (()=>{
if (_.isNil(instance)){
instance = this;
instance.build_params = [];
whoami((username)=>{
instance.whoami=username;
ensureBranch();
setArgs();
})
}
/**
* Ensures we have a -b <branch> given as argument for continued processing.
* Fixes #1
*/
function ensureBranch(){
let userSuppliedBranch = _.filter(process.argv,arg =>{
if (arg.toUpperCase() === '-B' || arg.toUpperCase() === '--BRANCH' ){
return arg;
}
});
let assignOrUseBranch = function () {
if (userSuppliedBranch.length === 0) {
process.argv.push("-b");
process.argv.push("##USE_SETTINGS_DEFAULT_BRANCH##");
}
}
return assignOrUseBranch();
}
/**
* Parses arguments given by command line for continued processing.
*/
function setArgs(){
cliArgs.processArguments(process.argv);
}
/**
* Helper function to return locally executing workstation user name for clearly identifying runner of any given
* build at Bitrise.
* @property callback At completion of execution, provides results back to callee as string.
*/
function whoami(callback){
exec('whoami',(err,stdout,stderr)=>{
if (err) callback("Anonymous");
if (stderr) callback("Anonymous")
let username = stdout.toString().replace(/(\r\n|\n|\r)/gm,"");
callback(username);
})
}
/**
* Process build starting point
* @global
* @alias jobrunner:processBuild
* @returns null
*/
let processBuild = () =>{
updates.checkJobrunner(path.resolve(process.argv[1],'..','..'))
.then(()=>{
cliArgs.buildSettingExist(instance,()=>{
git.getRepoInformation(instance).then((err)=>{
if (err) throw new Error(err);
git.nextVersionTag(instance).then(()=>{
bitrise.setBuildParams(instance)
.then(()=>{
jira.getJiraIds(instance,()=>{
bitrise.getEnvironments(instance)
.then(()=>{
bitrise.processBuild(instance)
.then(()=>{
return instance;
})
})
})
})
})
}).catch((err)=>{
console.log('Encountered error',err);
process.exit(1);
})
})
})
}
return {
processBuild: processBuild,
}
})();