Skip to content
This repository
Browse code

Add "dist" Jakefile task that prepares the distribution files

  • Loading branch information...
commit 69044e9d0bc8852fc47d99f200498ebe751bc1dc 1 parent 827a5ac
David Majda authored April 02, 2011
1  .gitignore
... ...
@@ -1 +1,2 @@
1 1
 lib/*
  2
+dist/*
114  Jakefile
... ...
@@ -1,11 +1,16 @@
1 1
 var sys          = require("sys");
2 2
 var fs           = require("fs");
  3
+var path         = require("path");
3 4
 var childProcess = require("child_process");
4 5
 
5 6
 /* Relative paths are here because of use in |require|. */
6  
-var SRC_DIR = "./src";
7  
-var LIB_DIR = "./lib";
8  
-var BIN_DIR = "./bin";
  7
+var SRC_DIR       = "./src";
  8
+var LIB_DIR       = "./lib";
  9
+var BIN_DIR       = "./bin";
  10
+var EXAMPLES_DIR  = "./examples";
  11
+var DIST_DIR      = "./dist";
  12
+var DIST_WEB_DIR  = "./dist/web";
  13
+var DIST_NODE_DIR = "./dist/node";
9 14
 
10 15
 var PEGJS = BIN_DIR + "/pegjs";
11 16
 
@@ -26,6 +31,57 @@ function abort(message) {
26 31
   exitFailure();
27 32
 }
28 33
 
  34
+function mkdirUnlessExists(dir) {
  35
+  try {
  36
+    fs.statSync(dir);
  37
+  } catch (e) {
  38
+    fs.mkdirSync(dir, 0755);
  39
+  }
  40
+}
  41
+
  42
+function copyFile(src, dest) {
  43
+  fs.writeFileSync(dest, fs.readFileSync(src));
  44
+}
  45
+
  46
+function copyDir(src, dest) {
  47
+  mkdirUnlessExists(dest);
  48
+
  49
+  fs.readdirSync(src).every(function(file) {
  50
+    var srcFile  = src  + "/" + file;
  51
+    var destFile = dest + "/" + file;
  52
+
  53
+    var stats = fs.statSync(srcFile);
  54
+    if (stats.isDirectory()) {
  55
+      copyDir(srcFile, destFile);
  56
+    } else {
  57
+      copyFile(srcFile, destFile);
  58
+      fs.chmodSync(destFile, stats.mode);
  59
+    }
  60
+
  61
+    return true;
  62
+  });
  63
+}
  64
+
  65
+function preprocess(file) {
  66
+  var input = fs.readFileSync(file, "utf8").trim();
  67
+  return input.split("\n").map(function(line) {
  68
+    var matches = /^\s*\/\/\s*@include\s*"([^"]*)"\s*$/.exec(line);
  69
+    if (matches !== null) {
  70
+      var includedFile = SRC_DIR + "/" + matches[1];
  71
+
  72
+      try {
  73
+        fs.statSync(includedFile);
  74
+      } catch (e) {
  75
+        abort("Included file \"" + includedFile + "\" does not exist.");
  76
+      }
  77
+
  78
+      return preprocess(includedFile);
  79
+    } else {
  80
+      return line;
  81
+    }
  82
+  }).join("\n").replace("@VERSION", PEGJS_VERSION);
  83
+}
  84
+
29 85
 desc("Generate the grammar parser");
30 86
 task("parser", [], function() {
31 87
   var PEG = require(PEGJS_OUT_FILE);
@@ -46,34 +102,36 @@ task("parser", [], function() {
46 102
 
47 103
 desc("Build the peg.js file");
48 104
 task("build", [], function() {
49  
-  function preprocess(file) {
50  
-    var input = fs.readFileSync(file, "utf8").trim();
51  
-    return input.split("\n").map(function(line) {
52  
-      var matches = /^\s*\/\/\s*@include\s*"([^"]*)"\s*$/.exec(line);
53  
-      if (matches !== null) {
54  
-        var includedFile = SRC_DIR + "/" + matches[1];
55  
-
56  
-        try {
57  
-          fs.statSync(includedFile);
58  
-        } catch (e) {
59  
-          abort("Included file \"" + includedFile + "\" does not exist.");
60  
-        }
61  
-
62  
-        return preprocess(includedFile);
63  
-      } else {
64  
-        return line;
65  
-      }
66  
-    }).join("\n").replace("@VERSION", PEGJS_VERSION);
67  
-  }
68  
-
69  
-  try {
70  
-    fs.statSync(LIB_DIR);
71  
-  } catch (e) {
72  
-    fs.mkdirSync(LIB_DIR, 0755);
73  
-  }
  105
+  mkdirUnlessExists(LIB_DIR);
74 106
   fs.writeFileSync(PEGJS_OUT_FILE, preprocess(PEGJS_SRC_FILE), "utf8");
75 107
 });
76 108
 
  109
+desc("Prepare the distribution files");
  110
+task("dist", ["build"], function() {
  111
+  mkdirUnlessExists(DIST_DIR);
  112
+
  113
+  /* Web */
  114
+
  115
+  mkdirUnlessExists(DIST_WEB_DIR);
  116
+
  117
+  copyFile(PEGJS_OUT_FILE, DIST_WEB_DIR + "/peg-" + PEGJS_VERSION + ".js");
  118
+
  119
+  /* Node.js */
  120
+
  121
+  mkdirUnlessExists(DIST_NODE_DIR);
  122
+
  123
+  copyDir(LIB_DIR,      DIST_NODE_DIR + "/lib");
  124
+  copyDir(BIN_DIR,      DIST_NODE_DIR + "/bin");
  125
+  copyDir(EXAMPLES_DIR, DIST_NODE_DIR + "/examples");
  126
+
  127
+  copyFile("CHANGELOG", DIST_NODE_DIR + "/CHANGELOG");
  128
+  copyFile("LICENSE",   DIST_NODE_DIR + "/LICENSE");
  129
+  copyFile("README.md", DIST_NODE_DIR + "/README.md");
  130
+  copyFile("VERSION",   DIST_NODE_DIR + "/VERSION");
  131
+
  132
+  fs.writeFileSync(DIST_NODE_DIR + "/package.json", preprocess("package.json"), "utf8");
  133
+});
  134
+
77 135
 desc("Run the test suite");
78 136
 task("test", [], function() {
79 137
   var process = childProcess.spawn("test/run", [], { customFds: [0, 1, 2] });
2  package.json
... ...
@@ -1,7 +1,7 @@
1 1
 {
2 2
   "name": "pegjs",
3 3
   "description": "Parser generator for JavaScript",
4  
-  "version": "0.6.0pre",
  4
+  "version": "@VERSION",
5 5
   "author": "David Majda <david@majda.cz> (http://majda.cz/)",
6 6
   "homepage": "http://pegjs.majda.cz/",
7 7
   "repository": "git://github.com/dmajda/pegjs.git",

0 notes on commit 69044e9

Please sign in to comment.
Something went wrong with that request. Please try again.