From 9956b423927e4ecddfe1e48cb432a4766a8040db Mon Sep 17 00:00:00 2001 From: David Majda Date: Tue, 11 Oct 2016 11:33:20 +0200 Subject: [PATCH] Switch from Make to Gulp The core of the transition is getting rid of Makefile and replacing it with gulpfile.js. The rest is details (fixing dependencies, changing all references to "make", etc.). Target/task names mostly stay the same, so in most cases "gulp foo" does what "make foo" did before. The only exceptions are "make browser" and "make browserclean", which are now "gulp browser:build" and "gulp browser:clean" (it feels more systematic). Functionality is mostly unchanged (modulo Gulp clutter in the console), but there are two small exceptions: gulp spec The reporter now displays just dots while previously it displayed spec descriptions. There is also a deprecation warning (most likely because I used an old version of gulp-jasmine in order to support Jasmine 1.x). I kept these issues unfixed because I plan to switch to Mocha soon (#409). gulp browser:build The copyright header is now added manually both to the development and minified build. Before, it was added only to the development build and the minified build relied on preserving it using "uglify --comments". This was broken since switching to //-style comments. There is now also an empty line between the header and the source code. Fixes #444. --- .travis.yml | 2 + CONTRIBUTING.md | 4 +- Makefile | 99 ----------------------------------------- benchmark/README.md | 2 +- gulpfile.js | 105 ++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 +++++-- spec/README.md | 2 +- tools/impact | 2 +- 8 files changed, 123 insertions(+), 108 deletions(-) delete mode 100644 Makefile create mode 100644 gulpfile.js diff --git a/.travis.yml b/.travis.yml index 264e3ca..efb43b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,8 @@ node_js: - "4.0" - "5.0" - "6.0" +before_script: + - npm install -g gulp cache: directories: - node_modules diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 9046955..07718c3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -46,8 +46,8 @@ see good examples. When appropriate, add documentation and tests. -Before submitting, make sure your change passes the specs (`make spec`) and -ESLint checks (`make lint`). +Before submitting, make sure your change passes the specs (`gulp spec`) and +ESLint checks (`gulp lint`). [issues]: https://github.com/pegjs/pegjs/issues [issues-search-bugs]: https://github.com/pegjs/pegjs/issues?q=is%3Aopen+is%3Aissue+label%3ABug diff --git a/Makefile b/Makefile deleted file mode 100644 index 56cdd35..0000000 --- a/Makefile +++ /dev/null @@ -1,99 +0,0 @@ -# ===== Variables ===== - -PEGJS_VERSION = `cat $(VERSION_FILE)` - -# ===== Directories ===== - -SRC_DIR = src -LIB_DIR = lib -BIN_DIR = bin -BROWSER_DIR = browser -SPEC_DIR = spec -BENCHMARK_DIR = benchmark -NODE_MODULES_DIR = node_modules -NODE_MODULES_BIN_DIR = $(NODE_MODULES_DIR)/.bin - -# ===== Files ===== - -MAIN_FILE = $(LIB_DIR)/peg.js - -PARSER_SRC_FILE = $(SRC_DIR)/parser.pegjs -PARSER_OUT_FILE = $(LIB_DIR)/parser.js - -BROWSER_FILE_DEV = $(BROWSER_DIR)/peg.js -BROWSER_FILE_MIN = $(BROWSER_DIR)/peg.min.js - -SPEC_SERVER_FILE = $(SPEC_DIR)/server -BENCHMARK_SERVER_FILE = $(BENCHMARK_DIR)/server - -VERSION_FILE = VERSION - -# ===== Executables ===== - -ESLINT = $(NODE_MODULES_BIN_DIR)/eslint -BROWSERIFY = $(NODE_MODULES_BIN_DIR)/browserify -UGLIFYJS = $(NODE_MODULES_BIN_DIR)/uglifyjs -JASMINE_NODE = $(NODE_MODULES_BIN_DIR)/jasmine-node -PEGJS = $(BIN_DIR)/pegjs -BENCHMARK_RUN = $(BENCHMARK_DIR)/run - -# ===== Targets ===== - -# Default target -all: browser - -# Generate the grammar parser -parser: - $(PEGJS) -o $(PARSER_OUT_FILE) $(PARSER_SRC_FILE) - -# Build the browser version of the library -browser: - mkdir -p $(BROWSER_DIR) - - rm -f $(BROWSER_FILE_DEV) - rm -f $(BROWSER_FILE_MIN) - - echo "// PEG.js $(PEGJS_VERSION)" >> $(BROWSER_FILE_DEV) - echo '//' >> $(BROWSER_FILE_DEV) - echo '// http://pegjs.org/' >> $(BROWSER_FILE_DEV) - echo '//' >> $(BROWSER_FILE_DEV) - echo '// Copyright (c) 2010-2016 David Majda' >> $(BROWSER_FILE_DEV) - echo '// Licensed under the MIT license.' >> $(BROWSER_FILE_DEV) - - $(BROWSERIFY) \ - --standalone peg \ - --transform [ babelify --presets [ es2015 ] --compact false ] \ - $(MAIN_FILE) >> $(BROWSER_FILE_DEV) - - $(UGLIFYJS) \ - --mangle \ - --compress warnings=false \ - --comments /Copyright/ \ - -o $(BROWSER_FILE_MIN) \ - $(BROWSER_FILE_DEV) - -# Remove browser version of the library (created by "browser") -browserclean: - rm -rf $(BROWSER_DIR) - -# Run the spec suite -spec: - $(JASMINE_NODE) --verbose $(SPEC_DIR) - -# Run the benchmark suite -benchmark: - $(BENCHMARK_RUN) - -# Run ESLint on the source -lint: - $(ESLINT) \ - `find $(LIB_DIR) -name '*.js' -and -not -path '$(PARSER_OUT_FILE)'` \ - `find $(SPEC_DIR) -name '*.js' -and -not -path '$(SPEC_DIR)/vendor/*'` \ - $(SPEC_SERVER_FILE) \ - $(BENCHMARK_DIR)/*.js \ - $(BENCHMARK_RUN) \ - $(BENCHMARK_SERVER_FILE) \ - $(PEGJS) - -.PHONY: all parser browser browserclean spec benchmark lint -.SILENT: all parser browser browserclean spec benchmark lint diff --git a/benchmark/README.md b/benchmark/README.md index da334a0..d53d7ac 100644 --- a/benchmark/README.md +++ b/benchmark/README.md @@ -20,7 +20,7 @@ All commands in the following steps need to be executed in PEG.js root directory 2. Execute the benchmark suite: ```console - $ make benchmark + $ gulp benchmark ``` 3. Wait for results. diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..96e776e --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,105 @@ +"use strict"; + +/* eslint-env node */ + +let babelify = require("babelify"); +let browserify = require("browserify"); +let buffer = require("vinyl-buffer"); +let del = require("del"); +let eslint = require("gulp-eslint"); +let gulp = require("gulp"); +let header = require("gulp-header"); +let jasmine = require("gulp-jasmine"); +let package_ = require("./package"); +let peg = require("./lib/peg"); +let rename = require("gulp-rename"); +let source = require("vinyl-source-stream"); +let spawn = require("child_process").spawn; +let transform = require("gulp-transform"); +let uglify = require("gulp-uglify"); + +const HEADER = [ + "// PEG.js " + package_.version, + "//", + "// http://pegjs.org/", + "//", + "// Copyright (c) 2010-2016 David Majda", + "// Licensed under the MIT License.", + "" +].map(line => `${line}\n`).join(""); + +const JS_FILES = [ + "lib/**/*.js", + "!lib/parser.js", + "spec/**/*.js", + "spec/server", + "!spec/vendor/**/*", + "benchmark/**/*.js", + "benchmark/run", + "benchmark/server", + "!benchmark/vendor/**/*", + "bin/pegjs", + "gulpfile.js" +]; + +const SPEC_FILES = [ + "spec/**/*.js", + "!spec/vendor/**/*" +]; + +function generate(contents) { + return peg.generate(contents.toString(), { + output: "source", + format: "commonjs" + }); +} + +// Run ESLint on all JavaScript files. +gulp.task("lint", () => + gulp.src(JS_FILES) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failAfterError()) +); + +// Run specs. +gulp.task("spec", () => + gulp.src(SPEC_FILES) + .pipe(jasmine()) +); + +// Run benchmarks. +gulp.task("benchmark", () => + spawn("benchmark/run", { stdio: "inherit" }) +); + +// Create the browser build. +gulp.task("browser:build", () => + browserify("lib/peg.js", { standalone: "peg" }) + .transform(babelify, { presets: "es2015", compact: false }) + .bundle() + .pipe(source("peg.js")) + .pipe(header(HEADER)) + .pipe(gulp.dest("browser")) + .pipe(rename({ suffix: ".min" })) + .pipe(buffer()) + .pipe(uglify()) + .pipe(header(HEADER)) + .pipe(gulp.dest("browser")) +); + +// Delete the browser build. +gulp.task("browser:clean", () => + del("browser") +); + +// Generate the grammar parser. +gulp.task("parser", () => + gulp.src("src/parser.pegjs") + .pipe(transform(generate)) + .pipe(rename({ extname: ".js" })) + .pipe(gulp.dest("lib")) +); + +// Default task. +gulp.task("default", ["lint", "spec"]); diff --git a/package.json b/package.json index e8a2787..01b9894 100644 --- a/package.json +++ b/package.json @@ -42,19 +42,26 @@ "bin": "bin/pegjs", "repository": "pegjs/pegjs", "scripts": { - "test": "make lint && make spec" + "test": "gulp" }, "devDependencies": { "babel-preset-es2015": "6.14.0", "babelify": "7.3.0", "browserify": "13.1.0", - "eslint": "3.7.1", + "del": "2.2.2", "eslint-config-dmajda": "1.0.0", "express": "4.14.0", "glob": "7.0.6", - "jasmine-node": "1.14.5", + "gulp": "3.9.1", + "gulp-eslint": "3.0.1", + "gulp-header": "1.8.8", + "gulp-jasmine": "0.2.0", + "gulp-rename": "1.2.2", + "gulp-transform": "1.0.8", + "gulp-uglify": "2.0.0", "morgan": "1.7.0", - "uglify-js": "2.7.0" + "vinyl-buffer": "1.0.0", + "vinyl-source-stream": "1.1.0" }, "engines": { "node": ">=4" diff --git a/spec/README.md b/spec/README.md index 71c8cd9..b9d96ff 100644 --- a/spec/README.md +++ b/spec/README.md @@ -19,7 +19,7 @@ All commands in the following steps need to be executed in PEG.js root directory 2. Execute the spec suite: ```console - $ make spec + $ gulp spec ``` 3. Watch the specs pass (or fail). diff --git a/tools/impact b/tools/impact index da2b0e9..6163130 100755 --- a/tools/impact +++ b/tools/impact @@ -13,7 +13,7 @@ prepare() { } run_benchmark() { - echo $(make benchmark | awk 'BEGIN { FS = " *│ *" } /Total/ { split($5, a, " "); print a[1] }') + echo $(gulp benchmark | awk 'BEGIN { FS = " *│ *" } /Total/ { split($5, a, " "); print a[1] }') } measure_speed() {