Skip to content
This repository has been archived by the owner on Aug 15, 2018. It is now read-only.

Commit

Permalink
done with lots of improvements and docs. close #17 close #20 close #23
Browse files Browse the repository at this point in the history
…close #8
  • Loading branch information
yyfrankyy committed Jun 9, 2011
1 parent 863b375 commit be7cfa6
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1,111 deletions.
15 changes: 10 additions & 5 deletions bin/spm-autocompletion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@
# @create: 06/08/2011 08:17:16 PM CST

_spm() {
local spm cur action modules actions
local spm cur action modules actions opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
spm="${COMP_WORDS[0]}"
action="${COMP_WORDS[1]}"
actions="transport help"
actions="build transport help --help"

modules=`ls modules`
case "${action}" in
transport)
COMPREPLY=( $(compgen -W "${modules}" -- ${cur}) )
return 0
"build" | "transport")
if [[ ${cur} == -* ]]; then
opts="-f --force"
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
else
COMPREPLY=( $(compgen -W "${modules}" -- ${cur}) )
fi
return 0;
;;
esac

Expand Down
29 changes: 23 additions & 6 deletions lib/spm/spm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@ var actions = {

transport: function() {
var args = [].slice.call(arguments);
if (args[0] === 'all' || args.length === 0) {
if (args.length === 0) {
transport.buildAll();
}

else {
if (['--force', '-f'].indexOf(args[0]) > -1) {
transport.FORCE_MODE = true;
args.shift();
}
args.forEach(transport.build);
}
},

help: function() {
console.log('Usage: spm [options] [action] [action-options]');
console.log(' [module] [modules] ..');
console.log('Usage: spm [action] [action-options] [module] [module] ..');
console.log('');
console.log('try this:');
console.log(' spm transport jquery');
console.log('');
console.log(' spm build jquery');
console.log('');
console.log('you can use or spm help for showing this help again');
}

};
Expand All @@ -44,8 +49,20 @@ if (!action) {
return;
}

for (var i in actions) {
// for more interactive actions
var commands = {
build: actions.transport,
transport: actions.transport,
help: actions.help,
'--help': actions.help
};

for (var i in commands) {
if (i === action) {
actions[i].apply(this, argv);
commands[i].apply(this, argv);
return;
}
}

console.log('action %s not recognized.', action);
actions.help();
77 changes: 45 additions & 32 deletions lib/spm/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ var fs = require('fs'),
const PLACEHOLDER = '/*{{code}}*/';
const MODULES_DIR = 'modules';
const DEFAULT_VERSION = '1.0.0';
const VERSION_REGEXP = /^\d+(\.\d+){2}(\.\d+)?$/;

// in force mode, we will update all infomation, code.
exports.FORCE_MODE = false;

// meta data stored meta infomation of all modules
var metaData = [];

function parseTemplate(text, callback) {
var comments, template;
Expand All @@ -37,6 +44,7 @@ function parseTemplate(text, callback) {
config = JSON.parse(config);

for (var i in config) {
// priority: transport.js > package.json
if (i in meta) continue;
meta[i] = config[i];
}
Expand Down Expand Up @@ -71,14 +79,24 @@ function prepareDir(name, version) {
return verDir;
}

// transport result
// only publish version can be transported.
// or in force_mode
function verifyVersion(version) {
if (exports.FORCE_MODE || VERSION_REGEXP.test(version))
return true;
console.warn('%s is not a valid publish version.', version);
console.warn('we use http://semver.org/ for validating version number');
console.warn('you can use force mode to force build this unstable version');
return false;
}

// writing transported result
function handleResult(meta, tmpl, data, suffix) {
data = tmpl.split(PLACEHOLDER).join(data);
var dir = prepareDir(meta.name, meta.version),
name = meta.filename || meta.name;

// FIXME
// 先如此处理depends
// 因为uglifyjs的parser会丢失注释
// 暂时没找到从ast重新输出正确包含依赖结构的方法
var deps = extract.getAstDependencies(jsp(tmpl));
Expand All @@ -90,8 +108,7 @@ function handleResult(meta, tmpl, data, suffix) {
fs.writeFileSync(outputPath, data);
}

var metaData = [];

// update meta infomation for webAPI
function updateMeta(meta, min) {
var exists = false,
stat = fs.statSync(min);
Expand All @@ -109,73 +126,71 @@ function updateMeta(meta, min) {
webAPI.generate(metaData);
}

// entrance for building
function _build(tmpl) {
// transporting
function transport(tmpl) {

tmpl = fs.readFileSync(tmpl).toString();

parseTemplate(tmpl, function(o) {
var meta = o.meta, stat;
var meta = o.meta, stat, tmpl;

meta.tags = meta.tags ? meta.tags.split(/\s*,\s*/) : (meta.keywords || []);

meta.name = meta.name.toLowerCase();

tmpl = o.template;

// unstable version or invalid version number
if (!verifyVersion(meta.version))
return;

var srcSuffix = '-debug.js', minSuffix = '.js',
name = meta.filename || meta.name;

var dir = prepareDir(meta.name, meta.version),
src = path.join(dir, name + srcSuffix),
min = path.join(dir, name + minSuffix);

if (path.existsSync(src)) {
console.warn('%s already exists, ignore.', src);
console.warn('Update version information in the transport.js ');
console.warn('if you want to upgrade to a new version.');

if (!exports.FORCE_MODE && path.existsSync(src)) {
console.warn('%s already exists, ignore', src);
console.warn('update version information in the transport.js');
console.warn('or use force mode to update');
updateMeta(meta, min);

return;
}

if (exports.FORCE_MODE) {
console.warn('we are using force mode');
}

// use given source file
if (meta.src) {
util.readFromPath(meta.src, function(data) {
handleResult(meta, tmpl, data, srcSuffix);

stat = fs.statSync(src);

console.info(
'done building %s source code. size: %s',
meta.name, stat.size);
'built %s source code. size: %s',
meta.name, fs.statSync(src).size);

// minify
if (!meta.min) {
console.warn(
'%s has no minified source, use uglify to minify.',
meta.name
);
'there is no minified link at %s, minifing by UglifyJS',
meta.name);
var source = fs.readFileSync(src).toString();
fs.writeFileSync(min, pro.gen_code(jsp(source)));

updateMeta(meta, min);

console.info(
'done building %s minified code, size: %s',
'%s is minified, size: %s',
meta.name, stat.size);
}
});
}

// use given minified file
if (meta.min) {
util.readFromPath(meta.min, function(data) {
handleResult(meta, tmpl, data, minSuffix);

updateMeta(meta, min);

console.info(
'done building %s minified code. size: %s',
'built %s minified code. size: %s',
meta.name, meta.size);
});
}
Expand All @@ -186,9 +201,7 @@ function build(mod) {
var tmpl = path.join(MODULES_DIR, mod, 'transport.js');
if (path.existsSync(tmpl)) {
console.info('start building %s..', mod);
console.info('found %s', tmpl);
console.info('building %s', mod);
_build(tmpl);
transport(tmpl);
} else {
console.warn('%s does not exist, ignore.', tmpl);
}
Expand Down
Loading

0 comments on commit be7cfa6

Please sign in to comment.