From 082fd1fc6fb44187fed45cb6a0677261ba963b85 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 18 Jun 2021 13:38:58 +0100 Subject: [PATCH] Move build and package directory config into new config.js Make the destination directories for certain build/package/release steps more easily (and centrally) configurable. This only deals with building *_compressed* files; blockly_uncompressed.js and the various msg/js/*.js files are not affected by this commit. --- scripts/gulpfiles/build_tasks.js | 10 ++-- scripts/gulpfiles/config.js | 22 ++++++++ scripts/gulpfiles/package_tasks.js | 91 +++++++++++++++--------------- scripts/gulpfiles/release_tasks.js | 27 +++++---- 4 files changed, 89 insertions(+), 61 deletions(-) create mode 100644 scripts/gulpfiles/config.js diff --git a/scripts/gulpfiles/build_tasks.js b/scripts/gulpfiles/build_tasks.js index 6553e8872..5b196d7ac 100644 --- a/scripts/gulpfiles/build_tasks.js +++ b/scripts/gulpfiles/build_tasks.js @@ -21,8 +21,8 @@ var through2 = require('through2'); var closureCompiler = require('google-closure-compiler').gulp(); var closureDeps = require('google-closure-deps'); var argv = require('yargs').argv; -var { getPackageJson } = require('./helper_tasks'); - +var {BUILD_DIR} = require('./config'); +var {getPackageJson} = require('./helper_tasks'); //////////////////////////////////////////////////////////// // Build // @@ -217,7 +217,7 @@ function buildCompressed() { })) .pipe( gulp.sourcemaps.write('.', {includeContent: false, sourceRoot: './'})) - .pipe(gulp.dest('./')); + .pipe(gulp.dest(BUILD_DIR)); }; /** @@ -242,7 +242,7 @@ function buildBlocks() { includeContent: false, sourceRoot: './' })) - .pipe(gulp.dest('./')); + .pipe(gulp.dest(BUILD_DIR)); }; /** @@ -268,7 +268,7 @@ function buildGenerator(language, namespace) { includeContent: false, sourceRoot: './' })) - .pipe(gulp.dest('./')); + .pipe(gulp.dest(BUILD_DIR)); }; /** diff --git a/scripts/gulpfiles/config.js b/scripts/gulpfiles/config.js new file mode 100644 index 000000000..370b5a79b --- /dev/null +++ b/scripts/gulpfiles/config.js @@ -0,0 +1,22 @@ +/** + * @license + * Copyright 2021 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @fileoverview Common configuration for Gulp scripts. + */ + +var path = require('path'); + +// Paths are all relative to the repository root. Do not include +// trailing slash. +module.exports = { + // Directory to write compiled output to. + BUILD_DIR: '.', + + // Directory in which to assemble (and from which to publish) the + // blockly npm package. + RELEASE_DIR: 'dist', +}; diff --git a/scripts/gulpfiles/package_tasks.js b/scripts/gulpfiles/package_tasks.js index bf79b7a6e..f1dfd121a 100644 --- a/scripts/gulpfiles/package_tasks.js +++ b/scripts/gulpfiles/package_tasks.js @@ -17,13 +17,11 @@ gulp.umd = require('gulp-umd'); var path = require('path'); var fs = require('fs'); -var { getPackageJson } = require('./helper_tasks'); - -const blocklyRoot = '../../'; - -// The destination path where all the NPM distribution files will go. -const packageDistribution = 'dist'; +var {getPackageJson} = require('./helper_tasks'); +var {BUILD_DIR, RELEASE_DIR} = require('./config'); +// Path to template files for gulp-umd. +const TEMPLATE_DIR = 'scripts/package/templates'; /** * A helper method for wrapping a file into a Universal Module Definition. @@ -35,7 +33,7 @@ function packageUMD(namespace, dependencies) { dependencies: function () { return dependencies; }, namespace: function () { return namespace; }, exports: function () { return namespace; }, - template: path.join(__dirname, `${blocklyRoot}/scripts/package/templates/umd.template`) + template: path.join(TEMPLATE_DIR, 'umd.template') }); }; @@ -49,26 +47,26 @@ function packageCommonJS(namespace, dependencies) { dependencies: function () { return dependencies; }, namespace: function () { return namespace; }, exports: function () { return namespace; }, - template: path.join(__dirname, `${blocklyRoot}/scripts/package/templates/node.template`) + template: path.join(TEMPLATE_DIR, 'node.template') }); }; /** - * This task copies source files into the distribution directory. + * This task copies source files into the release directory. */ function packageSources() { return gulp.src(['core/**/**.js', 'blocks/**.js', 'generators/**/**.js'], {base: '.'}) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** - * This task copies the compressed files and their source maps into the - * distribution directory. + * This task copies the compressed files and their source maps into + * the release directory. */ function packageCompressed() { - return gulp.src('*_compressed.js?(.map)') - .pipe(gulp.dest(packageDistribution)); + return gulp.src('*_compressed.js?(.map)', {cwd: BUILD_DIR}) + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -83,7 +81,7 @@ function packageBlockly() { cjs: './blockly_compressed', }])) .pipe(gulp.rename('blockly.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -98,7 +96,7 @@ function packageBlocks() { cjs: './blocks_compressed', }])) .pipe(gulp.rename('blocks.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -115,7 +113,7 @@ function packageIndex() { cjs: './node', }])) .pipe(gulp.rename('index.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -147,7 +145,7 @@ function packageBrowser() { cjs: './javascript', }])) .pipe(gulp.rename('browser.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -166,7 +164,7 @@ function packageCore() { cjs: './blockly', }])) .pipe(gulp.rename('core-browser.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -205,7 +203,7 @@ function packageNode() { cjs: './dart', }])) .pipe(gulp.rename('node.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -224,7 +222,7 @@ function packageNodeCore() { cjs: './blockly', }])) .pipe(gulp.rename('core.js')) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -245,7 +243,7 @@ function packageGenerator(file, rename, namespace) { cjs: `./${file}`, }])) .pipe(gulp.rename(rename)) - .pipe(gulp.dest(packageDistribution)); + .pipe(gulp.dest(RELEASE_DIR)); }; /** @@ -303,7 +301,7 @@ function packageLocales() { amd: '../core', cjs: '../core', }])) - .pipe(gulp.dest(`${packageDistribution}/msg`)); + .pipe(gulp.dest(`${RELEASE_DIR}/msg`)); }; /** @@ -314,60 +312,63 @@ function packageLocales() { */ function packageUMDBundle() { var srcs = [ - 'blockly_compressed.js', + `${BUILD_DIR}/blockly_compressed.js`, 'msg/js/en.js', - 'blocks_compressed.js', - 'javascript_compressed.js' + `${BUILD_DIR}/blocks_compressed.js`, + `${BUILD_DIR}/javascript_compressed.js`, ]; return gulp.src(srcs) - .pipe(gulp.concat('blockly.min.js')) - .pipe(gulp.dest(`${packageDistribution}`)) + .pipe(gulp.concat('blockly.min.js')) + .pipe(gulp.dest(`${RELEASE_DIR}`)); }; /** - * This task copies all the media/* files into the distribution directory. + * This task copies all the media/* files into the release directory. */ function packageMedia() { - return gulp.src('./media/*') - .pipe(gulp.dest(`${packageDistribution}/media`)); + return gulp.src('media/*') + .pipe(gulp.dest(`${RELEASE_DIR}/media`)); }; /** - * This task copies the package.json file into the distribution directory. + * This task copies the package.json file into the release directory. */ function packageJSON(cb) { const packageJson = getPackageJson(); const json = Object.assign({}, packageJson); delete json['scripts']; - if (!fs.existsSync(packageDistribution)) { - fs.mkdirSync(packageDistribution); + if (!fs.existsSync(RELEASE_DIR)) { + fs.mkdirSync(RELEASE_DIR); } - fs.writeFileSync(`${packageDistribution}/package.json`, + fs.writeFileSync(`${RELEASE_DIR}/package.json`, JSON.stringify(json, null, 2)); cb(); }; /** - * This task copies the scripts/package/README.md file into the distribution directory. - * This file is what developers will see at https://www.npmjs.com/package/blockly. + * This task copies the scripts/package/README.md file into the + * release directory. This file is what developers will see at + * https://www.npmjs.com/package/blockly . */ function packageReadme() { - return gulp.src('./scripts/package/README.md') - .pipe(gulp.dest(`${packageDistribution}`)); + return gulp.src('scripts/package/README.md') + .pipe(gulp.dest(`${RELEASE_DIR}`)); }; /** - * This task copies the typings/blockly.d.ts TypeScript definition file into the - * distribution directory. - * The bundled declaration file is referenced in package.json in the types property. + * This task copies the typings/blockly.d.ts TypeScript definition + * file into the release directory. The bundled declaration file is + * referenced in package.json in the types property. */ function packageDTS() { - return gulp.src(['./typings/*.d.ts', './typings/msg/*.d.ts'], {base: './typings'}) - .pipe(gulp.dest(`${packageDistribution}`)); + return gulp.src(['typings/*.d.ts', 'typings/msg/*.d.ts'], + {base: './typings'}) + .pipe(gulp.dest(`${RELEASE_DIR}`)); }; /** - * This task prepares the NPM distribution files under the /dist directory. + * This task prepares the files to be included in the NPM by copying + * them into the release directory. */ const package = gulp.parallel( packageIndex, diff --git a/scripts/gulpfiles/release_tasks.js b/scripts/gulpfiles/release_tasks.js index 5e14fc08c..e98639b48 100644 --- a/scripts/gulpfiles/release_tasks.js +++ b/scripts/gulpfiles/release_tasks.js @@ -17,9 +17,9 @@ var typings = require('./typings'); var buildTasks = require('./build_tasks'); var gitTasks = require('./git_tasks'); var packageTasks = require('./package_tasks'); -var { getPackageJson } = require('./helper_tasks'); +var {getPackageJson} = require('./helper_tasks'); +var {RELEASE_DIR} = require('./config'); -const RELEASE_DIR = 'dist'; // Gets the current major version. function getMajorVersion() { @@ -80,20 +80,25 @@ function checkBranch(done) { } -// Sanity check that the dist folder exists, and that certain files are in the dist folder. -function checkDist(done) { - const sanityFiles = ['blockly_compressed.js', 'blocks_compressed.js', 'core', 'blocks', 'generators']; - // Check that dist exists. +// Sanity check that the RELASE_DIR directory exists, and that certain +// files are in it. +function checkReleaseDir(done) { + const sanityFiles = ['blockly_compressed.js', 'blocks_compressed.js', + 'core', 'blocks', 'generators']; + // Check that directory exists. if (fs.existsSync(RELEASE_DIR)) { - // Sanity check that certain files exist in dist. + // Sanity check that certain files exist in RELASE_DIR. sanityFiles.forEach((fileName) => { if (!fs.existsSync(`${RELEASE_DIR}/${fileName}`)) { - done(new Error(`Your dist folder does not contain:${fileName}`)); + done(new Error( + `Your ${RELEASE_DIR} directory does not contain ${fileName}`)); + return; } }); done(); } else { - done(new Error('The dist directory does not exist. Is packageTasks.package being run?')); + done(new Error(`The ${RELEASE_DIR} directory does not exist. ` + + 'Has packageTasks.package been run?')); } } @@ -148,7 +153,7 @@ function updateBetaVersion(done) { const publish = gulp.series( packageTasks.package, checkBranch, - checkDist, + checkReleaseDir, loginAndPublish ); @@ -158,7 +163,7 @@ const publishBeta = gulp.series( buildTasks.build, packageTasks.package, checkBranch, - checkDist, + checkReleaseDir, loginAndPublishBeta );