/* webfan (C) Till Wehowski, Webfan.de - All rights reserved. */
var project = {};
var idGenerator, idGen;
var fs = require('fs');
var DB = 'apc';
var api_url = '';
var doInstall = false;
var _taskGroup = false;
var sqlTask = frdl.task().ql;
var cliTask = frdl.task().cli;
exports.finalize = function(){
var CLI = this;
if(!!doInstall){
try{
frdl.c('CREATE DATABASE ' + DB);
}catch(err){
//console.warn(err);
}
/*
*/
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects (parent, id, vendor, packagename, type, tag, install_secret, password, password_alg, uid, organisation, title, dir, file)', function(err, data){
if(err){
// frdl.alert.error(err);
return;
}
// console.dir(data);
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects ADD PRIMARY KEY ( id )');
frdl.c('ALTER TABLE ' + DB + '.projects ADD UNIQUE KEY ( vendor, packagename )');
frdl.c('ALTER TABLE ' + DB + '.projects ADD UNIQUE KEY ( dir )');
frdl.c('ALTER TABLE ' + DB + '.projects ADD UNIQUE KEY ( file )');
}catch(err){
//console.warn(err);
}
/*
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.packages (vendor, packagename, version_latest, last_time_fetched_composer, composer)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.packages ADD PRIMARY KEY ( vendor, packagename )');
}catch(err){
//console.warn(err);
}
*/
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects_required_php_version (projectID, version)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects_required_php_version ADD PRIMARY KEY ( projectID )');
}catch(err){
// console.warn(err);
}
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects_required_php_packages (projectID, vendor, packagename, version)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects_required_php_packages ADD PRIMARY KEY ( projectID, vendor, packagename )');
}catch(err){
//console.warn(err);
}
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects_required_php_ext (projectID, ext, version)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects_required_php_ext ADD PRIMARY KEY ( projectID, ext )');
}catch(err){
//console.warn(err);
}
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects_required_widget (projectID, widget)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects_required_widget ADD PRIMARY KEY ( projectID, widget )');
}catch(err){
//console.warn(err);
}
try{
frdl.c('CREATE TABLE IF NOT EXISTS ' + DB + '.projects_required_npm (projectID, package)', function(err, data){
if(err){
return;
}
}, true, CLI);
frdl.c('ALTER TABLE ' + DB + '.projects_required_npm ADD PRIMARY KEY ( projectID, package )');
}catch(err){
//console.warn(err);
}
// webfan.$Async(function(){
// frdl.sql.query('commit');
// },100);
}
idGenerator = new idGen(doInstall);
CLI.finalState.add('cmd.createproject.mkdir web');
CLI.finalState.add('cmd.createproject.mkdir packages');
CLI.finalState.add('cmd.createproject.create-composer-file');
CLI.once('cmd.finalized', function(){
frdl.sql.query('commit');
});
var taskGroup = (false===_taskGroup) ? ('createproject '+frdl.Guid.newGuid()) : _taskGroup;
var group = frdl.task(taskGroup);
group.pause();
frdl.task(taskGroup, 'schedule', (function(err, data){
// dummy to hold queue
}), 0) ;
var newID = idGenerator.generate('urn:webfan:wpjct:'+location.host,'-v2', '-');
newID.then(function(result){
project.id = result;
CLI.log('Create new project with #ID: '+project.id);
var dir = ('undefined'!==typeof CLI.data.DIRECTORIES[0]) ? CLI.data.DIRECTORIES[0] : '1.3.6.1.4.1.37553.8.1.8.8.5.65.8' + frdl.DIRECTORY_SEPARATOR + 'projects' + frdl.DIRECTORY_SEPARATOR ;
dir += project.id + frdl.DIRECTORY_SEPARATOR;
project.file = frdl.fs5.polyfill.getFiler().pathToFilesystemURL(dir) + '#' + project.id + '.'+project.type;
project.dir=dir;
frdl.fs5.polyfill.getFiler().cd(frdl.fs5.polyfill.getFiler().fs.root);
fs.mkdir(dir, '0755', function(err,dirEntry) {
if(err){
CLI.error(err);
return;
}else{
CLI.log('mkdir ' + dir);
}
// console.dir(project); uid, organisation, title, dir, file
var sqlquery = "INSERT INTO " + DB + ".projects SET parent="+frdl.ql_escape_string(project.parent)+", "
+ " id="+frdl.ql_escape_string(project.id)+", "
+ " vendor="+frdl.ql_escape_string(project.vendor)+", "
+ " packagename="+frdl.ql_escape_string(project.packagename)+", "
+ " type="+frdl.ql_escape_string(project.type )+", "
+ " tag="+frdl.ql_escape_string(project.tag )+", "
+ " install_secret='', "
+ " password='', "
+ " password_alg='', "
+ " uid='', "
+ " organisation='', "
+ " title="+frdl.ql_escape_string(project.title )+", "
+ " dir="+frdl.ql_escape_string(project.dir)+", "
+ " file="+frdl.ql_escape_string(project.file)+" "
+ "";
frdl.sql.query(sqlquery, function(err, result){
if(err){
CLI.error(err+ ' ' +sqlquery);
CLI.emit('cmd.finalized', CLI.data.data);
return;
}
frdl.task(taskGroup, 'schedule', (function(err, data){
fs.mkdir(dir + 'packages' + frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) {
if(err){
CLI.error(err);
return;
}
CLI.emit('cmd.createproject.mkdir web', data);
});
}), 5);
frdl.task(taskGroup, 'schedule', (function(err, data){
fs.mkdir(dir + 'web' + frdl.DIRECTORY_SEPARATOR, '0755', function(err,dirEntry) {
if(err){
CLI.error(err);
return;
}
CLI.emit('cmd.createproject.mkdir packages', data);
});
}), 5);
frdl.task(taskGroup, 'schedule', new cliTask('pm create-composer-file --projectID='+frdl.ql_escape_string(project.id), function(err, data){
if(err){
CLI.error(err);
return;
}
CLI.emit('cmd.createproject.create-composer-file', data);
}, 5));
group.resume();
CLI.data.data = project;
CLI.emit('cmd.finalized', CLI.data.data);
});
});
});
};
exports.prepare = function(_CLI){
/*
_CLI.finalState.add('parse_second_arg');
var parse_second_arg = function(value){
action = value;
_CLI.emit('parse_second_arg', action);
};
// _CLI.finalState.add('op.mkdir success');
// _CLI.finalState.add('op.dir');
CLI.positionalSwitches[1] = parse_second_arg;
*/
_CLI.finalState.add('op.vendor');
_CLI.finalState.add('op.packagename');
_CLI.finalState.add('op.type');
_CLI.finalState.add('op.title');
_CLI.finalState.add('cmd.createproject.idGenerator');
_CLI.finalState.add('cmd.finalized');
_CLI.addOption('install', 'Install apc application data', function(name, value) {
doInstall=value;
}, ' [SWITCH]', '-i')
.addOption('type', 'Set project type', function(name, value) {
project.type=value;
_CLI.emit('op.type', value);
}, ' [PROJECTTYPE]')
.filter('PROJECTTYPE', function(value) {
return (-1!==['wpjct', 'apc'].indexOf(value) ) ? value : undefined;
})
/*
.addOption('type', 'Set project type', function(name, value) {
project.type=value;
_CLI.emit('op.type', value);
}, ' [TEXT]')
*/
.addOption('title', 'Add project title', function(name, value) {
project.title=value;
_CLI.emit('op.title', value);
}, ' [TEXT]')
.addOption('parent', 'Add project parentID', function(name, value) {
project.parent=value;
}, ' [TEXT]')
.addOption('api_url', 'Add project api_url', function(name, value) {
api_url=value;
}, ' [TEXT]')
.addOption('db', 'Add project DBNAME', function(name, value) {
DB=value;
}, ' [TEXT]')
.addOption('vendor', 'Add project vendor', function(name, value) {
project.vendor=value;
_CLI.emit('op.vendor', value);
}, ' [TEXT]')
.addOption('packagename', 'Add project packagename', function(name, value) {
project.packagename=value;
_CLI.emit('op.packagename', value);
}, ' [TEXT]')
.addOption('tag', 'Set project tag', function(name, value) {
project.tag=value;
}, ' [TEXT]')
;
require('16:webfan/webfat/js/id-generator', function(idgen){
idGen = idgen;
_CLI.emit('cmd.createproject.idGenerator', idGen);
});
};
|