Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
Added task running from files
Browse files Browse the repository at this point in the history
🎨 🚧 Added task running from files
🚧 TODO: Add deps
  • Loading branch information
Gum-Joe committed Apr 6, 2016
1 parent 08d869e commit e28a647
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 29 deletions.
4 changes: 2 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ if (typeof taskrun === 'undefined') {
}
var ser = new app.Server('./servertest', { quite: false })
ser.nameServer('buildtest')
ser.loadConfig(`buildup.json`, { file: true, type: 'json' })
ser.loadPlugins()
//ser.loadConfig(`buildup.json`, { file: true, type: 'json' })
//ser.loadPlugins()
ser.runTask(taskrun)
8 changes: 8 additions & 0 deletions buildupfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Sample buildup
//const tools = require('buildup').tools;
module.exports = (buildup) => {
buildup.task('test', function (done) {
buildup.logger.info('Task ran');
done();
})
}
34 changes: 34 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// File for the api
'use strict'
/**
* Module dependencies
*/
const Log = require('./logger').Logger;
/**
* Vars
*/
let app = module.exports = {};
app.that = this;
app.that.tasks = {};

/**
* Task loader api
* @param name {String} name of task
* @param deps {Array} or {Function} deps for task
* @param method {Function} Method
*/
app.taskloader = (name, deps, method) => {
if (typeof deps === 'function') {
app.that.tasks[name] = { method: deps }
}
}

/**
* API
*/
app.api = () => {
return {
logger: new Log('task'),
task: app.taskloader
};
}
16 changes: 16 additions & 0 deletions lib/callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Callback stuff
'use strict'
/**
* Vars
*/
let app = module.exports = {};

/**
* Done fun
* @param err {Error} The error
*/
app.done = (err) => {
if (typeof err !== 'undefined') {
throw err;
}
}
36 changes: 36 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Loads from file
'use strict'
/**
* Module dependencies
*/
const Log = require('./logger').Logger;
const fs = require('fs');
const path = require('path');
const api = require('./api');
let ENV = process.env;
let PWD = process.cwd();
let logger = new Log('buildup')
/**
* Vars
*/
let app = module.exports = {};
/**
* Load file
*
*/
app.loadFromFile = () => {
logger.debug('Loading from file...')
let file;
const done = this.async;
fs.stat(path.join(PWD, 'buildupfile.js'), function (err, stst) {
if (err) {
logger.err(
err,
'the fact we couldn\'t find a \'buildupfile.js\' in the current directory',
'To create a new file, run \'buildup init\''
)
}
})
file = require(path.join(PWD, 'buildupfile.js'))(api.api())
return api.that;
}
100 changes: 73 additions & 27 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const fs = require('fs');
const path = require('path');
const YAML = require('yaml-js');
const Log = require('./logger.js').Logger;
const loadFromFile = require('./loader.js').loadFromFile;
const done = require('./callback.js').done;
let logger;
let that = this;
/**
Expand Down Expand Up @@ -152,39 +154,83 @@ Server.prototype.loadPlugins = () => {
Server.prototype.runTask = function (task) {
// Check type
logger.info(`Running task \'${task}\'...`)
if (typeof task !== 'array' && typeof task !== 'string') {
logger.err(
new TypeError(`Task param was not an array or string in function Server.runTask(), but a ${typeof task}!`),
'the params supplied to Server.runTask() or buildup',
'Please change the params, or file an issue at https://github.com/Gum-Joe/buildup'
)
} else {
// Load task
let taskconfig;
if (that.config.tasks.hasOwnProperty(task)) {
logger.debug('Found the correct task!')
taskconfig = that.config.tasks[task]
logger.info(`Executing task \'${task}\'...`)
that.tasklist[task].function({ logger: logger, config: that.config.tasks[task] })
if (typeof that.config === 'undefined') {
const tasks = loadFromFile().tasks;
if (typeof task !== 'array' && typeof task !== 'string') {
logger.err(
new TypeError(`Task param was not an array or string in function Server.runTask(), but a ${typeof task}!`),
'the params supplied to Server.runTask() or buildup',
'Please change the params, or file an issue at https://github.com/Gum-Joe/buildup'
)
} else {
logger.debug(`Finding task with { type: \'${task}\' }...`)
for (var i in that.config.tasks) {
if (that.config.tasks[i].hasOwnProperty('type') && that.config.tasks[i].type === task) {
taskconfig = that.config.tasks[i];
if (!tasks.hasOwnProperty(task)) {
logger.info(`Task ${task} was not found!`)
logger.err(
new ReferenceError(
`Could not find task ${task}!`,
`the task supplied`,
`Please check that the task exists`
)
)
} else {
if (typeof deps === 'array') {
for (var i = 0; i < deps.length; i++) {
if (!tasks.hasOwnProperty(deps[i])) {
logger.info(`Task ${deps[i]} was not found!`)
logger.err(
new ReferenceError(
`Could not find task ${deps[i]}!`,
`the task supplied`,
`Please check that the task exists`
)
)
} else {
// Run
tasks[deps[i]].method(done);
}
}
tasks[task].method(done)
} else {
// Run it
tasks[task].method(done)
}
}
logger.info(`Executing task \'${task}\'...`)
}
// If no config, error
if (!taskconfig) {
logger.info(`Task ${task} was not found!`)
} else {
if (typeof task !== 'array' && typeof task !== 'string') {
logger.err(
new ReferenceError(
`Could not find task ${task}!`,
`the task supplied`,
`Please check that the task exists`
)
new TypeError(`Task param was not an array or string in function Server.runTask(), but a ${typeof task}!`),
'the params supplied to Server.runTask() or buildup',
'Please change the params, or file an issue at https://github.com/Gum-Joe/buildup'
)
} else {
// Load task
let taskconfig;
if (that.config.tasks.hasOwnProperty(task)) {
logger.debug('Found the correct task!')
taskconfig = that.config.tasks[task]
logger.info(`Executing task \'${task}\'...`)
that.tasklist[task].function({ logger: logger, config: that.config.tasks[task] })
} else {
logger.debug(`Finding task with { type: \'${task}\' }...`)
for (var i in that.config.tasks) {
if (that.config.tasks[i].hasOwnProperty('type') && that.config.tasks[i].type === task) {
taskconfig = that.config.tasks[i];
}
}
logger.info(`Executing task \'${task}\'...`)
}
// If no config, error
if (!taskconfig) {
logger.info(`Task ${task} was not found!`)
logger.err(
new ReferenceError(
`Could not find task ${task}!`,
`the task supplied`,
`Please check that the task exists`
)
)
}
}
}
};
Expand Down
40 changes: 40 additions & 0 deletions servertest/buildupfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Sample buildup
const git = require('buildup-git');
const tools = require('buildup').tools;
const coffee= require('buildup-coffee');
const npm = require('buildup-npm')
const include = require('buildup-include');
module.exports = (buildup) => {
buildup.task('clone', function () {
return git.clone('https://github.com', {
dir: './repo'
});
})
buildup.task('install', ['clone'], function (done) {
npm.install()
done()
})
buildup.task('build:js', ['install'], function (done) {
buildup.src('./src', '.coffee')
.pipe(coffee.compile({
out: './lib'
}))
done()
})
buildup.task('build:cpp', ['build:js'], function (done) {
include([
'https://github.com/Gum-Joe/jakhu-tester/master.zip'
])
buildup.src('./cpp', '.cpp')
.pipe(buildup.compile({
compiler: 'autodetect',
out: './build',
nodeGyp: true
}))
done()
})
buildup.task('build', ['build:cpp'], function (done) {
buildup.logger.info('Built!')
done();
})
}

0 comments on commit e28a647

Please sign in to comment.