tgomadev
Version:
A package to expediate and simplify tgoma game development
275 lines (229 loc) • 8.48 kB
JavaScript
var promisify = require("promisify-node")
var fse = require("fs-extra")
var fsp = promisify("fs-extra")
var git = require("nodegit");
var Promise = require("promise")
var path = require("path");
var packPath = path.join.bind(path, __dirname+"/.."); // the path of the tgoma dev directory
var home = process.env.HOME || process.env.USERPROFILE;
console.log("home", home);
var sshPublicKey = home+"/.ssh/id_rsa.pub";
var sshPrivateKey = home+"/.ssh/id_rsa";
var traducer = "git@traducer"
var templates = {
basic:{
repo:"gameTemplates",
host:traducer,
branch:"stripped"
},
freeball:{
repo:"gameTemplates",
host:traducer,
branch:"p2-freeball"
}
}
var opts = {
fetchOpts: {
callbacks: {
certificateCheck: function() {
return 1;
},
credentials: function(url, userName) {
return git.Cred.sshKeyNew(
userName,
sshPublicKey,
sshPrivateKey,
"");
}
}
}
};
var errAt = function(at) {
return function(err){
console.log("Error raised at %s : %s", at , err)
}
};
var cloneError = function(err) {
console.log("cloneError", err)
};
var checkoutError = function(err) {
console.log("checkoutError", err)
};
var plantError = function(err) {
console.log("plantError", err)
};
function pullOneTemplate(template){
var thost = templates[template].host;
var trepo = templates[template].repo;
var tbranch = templates[template].branch;
var remoteBranch = "remotes/origin/"+tbranch;
var trefname = "refs/heads/"+tbranch;
var repoCacheDir = "repoCache/"+trepo;
var templateCacheDir = "templateCache/"+template;
fse.ensureDirSync(repoCacheDir);
fse.ensureDirSync(templateCacheDir);
fse.emptyDirSync(templateCacheDir)
//fse.emptyDirSync(repoCacheDir)
var repo
var remote
return ensureRepositoryClone(thost+":"+trepo, repoCacheDir)
.then(function(_repo){
repo = _repo;
return ensureBranchFetch(_repo, tbranch)
})
.then(function(){
console.log("Checkout Branch")
return repo.checkoutBranch(tbranch, {checkoutStrategy: git.Checkout.STRATEGY.FORCE})
})
.then(function(repo){
console.log("copy from %s to %s", repoCacheDir, templateCacheDir)
fsp.copySync(packPath(repoCacheDir), packPath(templateCacheDir))
})
.then(function(){
return ensureToolsCopied(templateCacheDir)
})
.catch(function(err){
console.log("final catch: ", err)
});
}
function pullTemplates(){
var pullChain
fse.emptyDirSync("../repoCache/")
for(var template in templates){
console.log("Collecting template:", template);
if(pullChain == undefined){
pullChain = pullOneTemplate(template)
}else{
pullChain.then(
function(){
console.log("Proceeding to template: ", template)
return pullOneTemplate(template)
}
)
}
}
pullChain.catch(
errAt("Final template finish")
).then(function(){
console.log("Done")
})
}
function ensureToolsCopied(directory){
return new Promise(
function(fulfill, reject){
console.log("copying tools to "+directory+"/sfgametools")
fse.copy(packPath("./sfgametools"), packPath(directory+"/sfgametools"), function(err){
if (err){
reject(err)
} else {
fulfill()
}
})
// fse.stat("sfgametools", function(stat, err){
// if(err){
// fulfill();
// }else{
// fse.copy(packPath("./sfgametools"), directory+"/sfgametools", function(err){
// if (err){
// reject(err)
// } else {
// fulfill()
// }
// })
// }
// })
}
)
}
function ensureBranchFetch(repo, tbranch){
console.log("enusreBranchFetch")
var trefname = "refs/heads/"+tbranch;
var branchEnsuredPromise = new Promise(
function(fulfill, reject){ //call fulfull with the promised branch oid
repo.getBranch(tbranch).then(
function(_branch){
console.log("Found Branch: ", tbranch);
fulfill();
},
function(err){
repo.getRemote("origin")
.then(function(_remote){
remote = _remote
return git.Remote.addFetch(repo, 'origin', trefname)
})
.then(function(_fetch){
console.log("add fetch status: ", _fetch)
return repo.fetch("origin", opts.fetchOpts)
})
.then (function(){
var found = false;
return repo.fetchheadForeach(function(refname, remoteUrl, oid, isMerge) {
console.log("refname : %s , remoteUrl: %s ", refname, remoteUrl)
if(refname == trefname){
console.log("found correct ref, attempt branch create")
found = true;
return repo.getCommit(oid)
.then(function(commit){
return repo.createBranch(tbranch, commit, true)
}).catch(errAt("repo.createBranch"))
.then(function(commit){
console.log("created branch: ", tbranch)
fulfill();
})
}})
.then(function(){
if(!found){
reject("Could not find remote fetch head");
}
})
}).catch(function(err){
reject("unable to create local branch for fetch head, Err: ", err);
})
}
).catch(errAt("recoverBranch Double failure"))
}
)
return branchEnsuredPromise
}
/*
try to open repository at lpath, it it exists return it otherwise clone the remote
*/
function ensureRepositoryClone(rname, lpath){
console.log("assured repository - name: %s , path: %s", rname, lpath)
var openOrClonedPromise = new Promise(
function(fulfill, reject){
git.Repository.open(packPath(lpath)).then(
function(repo){
console.log("repo accessible - pass")
fulfill(repo)
},
function(err){
console.log("repo clear and clone")
fse.emptyDirSync(packPath(lpath))
git.Clone(rname, packPath(lpath), opts)
.then(
function(repository) {
console.log("clone complete return with callback")
fulfill(repository)
},function(err){
errAt("clone of ensured clone: ")(err);
reject(err)
})
})
}
)
return openOrClonedPromise
}
function main(){
console.log("copying core tgoma components...")
ensureRepositoryClone("git@traducer:tgomaGameDevCore", "core")
.then(function(){
return ensureRepositoryClone("git@traducer:sfgametools", "sfgametools")
})
.then(function(){
pullTemplates();
})
.catch(errAt("Main")
)
}
module.exports = main