Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 149 lines (133 sloc) 4.689 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
var fs = require('fs');
var assert = require('assert');
var childProcess = require('child_process');
var pkg = require('./package.json');

var SRC_DIR = './src';
var DIST_DIR = './dist';
var LIB_DIR = './lib';
var TEMPLATES_DIR = './templates';

var PEGJS_FILE = SRC_DIR + '/deku.pegjs';
var GENERATED_PARSER_FILE = DIST_DIR + '/generated.parser.js';
var DEKU_FILE = DIST_DIR + '/deku-' + pkg.version + '.js';
var DEKU_MIN_FILE = DIST_DIR + '/deku-' + pkg.version + '.min.js';
var DEKU_RUNTIME_FILE = DIST_DIR + '/deku.runtime-' + pkg.version + '.js';
var DEKU_RUNTIME_MIN_FILE = DIST_DIR + '/deku.runtime-' + pkg.version + '.min.js';

var PRELUDE_FILE = LIB_DIR + '/internal/prelude.js';
var PARSER_FILE = LIB_DIR + '/internal/parser.js';
var COMPILER_FILE = LIB_DIR + '/internal/compiler.js';
var CORE_FILE = LIB_DIR + '/internal/core.js';
var API_FILE = LIB_DIR + '/api.js';

var HEADER_TEMPLATE_FILE = TEMPLATES_DIR + '/header.deku';
var deku_TEMPLATE_FILE = TEMPLATES_DIR + '/deku.deku';
var deku_RUNTIME_TEMPLATE_FILE = TEMPLATES_DIR + '/deku.runtime.deku';

var mkdirUnlessExists = function (dir) {
  try {
    fs.statSync(dir);
  } catch (e) {
    fs.mkdirSync(dir, 0755);
  }
};

var cleanDir = function (dir) {
  fs.readdirSync(dir).every(function (file) {
    var file = dir + '/' + file;
    var stats = fs.statSync(file);
    if (stats.isDirectory()) {
      removeDir(file);
    } else {
      fs.unlinkSync(file);
    }
    return true;
  });
};

var removeDir = function (dir) {
  fs.readdirSync(dir).every(function (file) {
    var file = dir + '/' + file;
    var stats = fs.statSync(file);
    if (stats.isDirectory()) {
      removeDir(file);
    } else {
      fs.unlinkSync(file);
    }
    return true;
  });
  fs.rmdirSync(dir);
};

task('clean', function () {
  mkdirUnlessExists(DIST_DIR);
  cleanDir(DIST_DIR);
});

task('makeParser', ['clean'], function () {
  var process = childProcess.spawn('pegjs', ['-e', 'var parser', PEGJS_FILE, GENERATED_PARSER_FILE]);
  process.on('exit', function () {
    var content = fs.readFileSync(PARSER_FILE, 'utf-8');
    var generatedContent = fs.readFileSync(GENERATED_PARSER_FILE, 'utf-8');
    content = content.replace(/(\/\/ BEGIN PARSER\n)[\s\S]*?(\/\/ END PARSER)/g, '$1' + generatedContent + '$2');
    fs.writeFileSync(PARSER_FILE, content, 'utf-8');
    fs.unlinkSync(GENERATED_PARSER_FILE);
    complete();
  });
}, {async: true});

task('updateVersion', ['clean'], function () {
  var content = fs.readFileSync(API_FILE, 'utf-8');
  content = content.replace(/(deku.version = ').+?(';)/g, '$1' + pkg.version + '$2');
  fs.writeFileSync(API_FILE, content, 'utf-8');
});

task('test', ['makeParser', 'updateVersion'], function () {
  var process = childProcess.execFile('./test/spec/run.js', function (error, stdout, stderr) {
    console.log(stdout);
    console.error(stderr);
    assert.ifError(error);
  });
  process.on('exit', function () {
    complete();
  });
}, {async: true});

task('test-compiler', [], function () {
  var process = childProcess.execFile('./bin/deku', ['./test/spec/simple.deku'], function (error, stdout, stderr) {
    assert.ifError(error);
  });
  process.on('exit', function () {
    complete();
  });
}, {async: true});

task('build', ['test', 'test-compiler'], function () {
  var deku =require('deku');
  var options = {
    templates: {
      header: fs.readFileSync(HEADER_TEMPLATE_FILE, 'utf-8')
    }
  };
  var template = deku.prepare(fs.readFileSync(deku_TEMPLATE_FILE, 'utf-8'), options);
  var data = {
    preludeName: PRELUDE_FILE,
    prelude: fs.readFileSync(PRELUDE_FILE, 'utf-8'),
    parserName: PARSER_FILE,
    parser: fs.readFileSync(PARSER_FILE, 'utf-8'),
    compilerName: COMPILER_FILE,
    compiler: fs.readFileSync(COMPILER_FILE, 'utf-8'),
    coreName: CORE_FILE,
    core: fs.readFileSync(CORE_FILE, 'utf-8'),
    apiName: API_FILE,
    api: fs.readFileSync(API_FILE, 'utf-8'),
    pkg: pkg
  };
  var content = template.render(data);
  fs.writeFileSync(DEKU_FILE, content, 'utf-8');
  template = deku.prepare(fs.readFileSync(deku_RUNTIME_TEMPLATE_FILE, 'utf-8'), options);
  content = template.render(data);
  fs.writeFileSync(DEKU_RUNTIME_FILE, content, 'utf-8');
});

task('minify', ['build'], function () {
  var uglify = function(dest, src) {
    return childProcess.spawn('uglifyjs', ['-o', dest, src]);
  }
  var process = uglify(DEKU_MIN_FILE, DEKU_FILE);
  process.on('exit', function () {
    var process = uglify(DEKU_RUNTIME_MIN_FILE, DEKU_RUNTIME_FILE);
    process.on('exit', function () { complete(); });
  });
}, {async: true});

task('dist', ['minify'], function () {
  console.log('dist task done.');
});

task('default', ['dist']);
Something went wrong with that request. Please try again.