From 80d67212b780a517816ca9f17723099e4904d6d8 Mon Sep 17 00:00:00 2001 From: Eugenio Romano Date: Fri, 5 May 2017 18:11:36 +0200 Subject: [PATCH] [ADF-579] - move license check in webpack (#1859) * move license check in webpack * fix exclusion check header * different headers --- .../config/loaders/license-check.js | 67 +++++++++++++++++++ demo-shell-ng2/config/webpack.common.js | 18 +++++ demo-shell-ng2/package.json | 16 +---- .../config/assets/license_header.txt | 30 +++++---- .../config/assets/license_header_add.txt | 14 ++++ .../config/custom-loaders/license-check.js | 67 +++++++++++++++++++ ng2-components/config/webpack.common.js | 14 +++- scripts/npm-build-all.sh | 2 - 8 files changed, 196 insertions(+), 32 deletions(-) create mode 100644 demo-shell-ng2/config/loaders/license-check.js create mode 100644 ng2-components/config/assets/license_header_add.txt create mode 100644 ng2-components/config/custom-loaders/license-check.js diff --git a/demo-shell-ng2/config/loaders/license-check.js b/demo-shell-ng2/config/loaders/license-check.js new file mode 100644 index 0000000000..6a15ac03e2 --- /dev/null +++ b/demo-shell-ng2/config/loaders/license-check.js @@ -0,0 +1,67 @@ +var path = require('path'); +var loaderUtils = require('loader-utils'); +var fs = require('fs'); + +var licenseFileUtf8Store = undefined; + +function readLicenseHeaderFile(licenseFilePath) { + if (licenseFileUtf8Store) { + return licenseFileUtf8Store; + } + + if (fs.existsSync(licenseFilePath)) { + licenseFileUtf8Store = fs.readFileSync(licenseFilePath, 'utf8').split(/\r?\n/); + return licenseFileUtf8Store; + } + + throw new Error('The license header file path is wrong ' + licenseFilePath); +} + +function isFileEmpty(fileContents) { + return fileContents.toString('utf8').trim() === ''; +} + +function readCurrentFile(fileContent) { + return fileContent.toString('utf8').split(/\r?\n/); +} + +function isLicenseHeaderPresent(currentFileContent, licenseFilePath) { + if (!isFileEmpty(currentFileContent)) { + var currentFileUtf8 = readCurrentFile(currentFileContent), + licenseFileUtf8 = readLicenseHeaderFile(licenseFilePath); + skipStrict = 0; + + if(currentFileUtf8[0] === '"use strict";' ) { + skipStrict = 1; + } + + for (var i = skipStrict; i < licenseFileUtf8.length; i++) { + if (currentFileUtf8[i + skipStrict] !== licenseFileUtf8[i]) { + return false; + } + } + } + return true; +} + +function report(hasHeader, emitter, filename) { + if (hasHeader) return; + emitter('Missing license header file : ' + filename); +} + +function licenseCheck(webpackInstance, input, options) { + var isLicensePresent = isLicenseHeaderPresent(input, options.licenseFile); + + var emitter = options.emitErrors ? webpackInstance.emitError : webpackInstance.emitWarning; + + report(isLicensePresent, emitter, webpackInstance.resourcePath); +} + +module.exports = function(input, map) { + this.cacheable && this.cacheable(); + var callback = this.async(); + + var options = loaderUtils.getOptions(this); + licenseCheck(this, input, options); + callback(null, input, map); +}; diff --git a/demo-shell-ng2/config/webpack.common.js b/demo-shell-ng2/config/webpack.common.js index 27f34660ec..03cfd6fd61 100644 --- a/demo-shell-ng2/config/webpack.common.js +++ b/demo-shell-ng2/config/webpack.common.js @@ -29,6 +29,13 @@ module.exports = { 'vendor': './app/vendor.ts', 'app': './app/main.ts' }, + + resolveLoader: { + alias: { + "license-check": path.resolve(__dirname, "./loaders/license-check") + } + }, + module: { rules: [ { @@ -84,6 +91,17 @@ module.exports = { { test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/, loader: 'file-loader?name=assets/[name].[hash].[ext]' + }, + { + enforce: 'pre', + test: /\.ts$/, + loader: 'license-check', + include: helpers.root('app'), + options: { + emitErrors: true, + licenseFile: path.resolve(__dirname, '../assets/license_header.txt') + }, + exclude: [/node_modules/, /bundles/, /dist/, /demo/], } ] }, diff --git a/demo-shell-ng2/package.json b/demo-shell-ng2/package.json index fbcdf0ce3f..c1f7effc54 100644 --- a/demo-shell-ng2/package.json +++ b/demo-shell-ng2/package.json @@ -4,14 +4,13 @@ "version": "1.4.0", "author": "Alfresco Software, Ltd.", "scripts": { - "build": "rimraf dist && npm run licensecheck && webpack --config config/webpack.prod.js --progress --profile --bail", + "build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail", "start": "npm run server-versions && node --max_old_space_size=4096 node_modules/webpack-dev-server/bin/webpack-dev-server.js --progress", "test": "rimraf coverage && karma start --single-run", "clean": "npm run clean-build && rimraf dist node_modules typings dist", "clean-build": "rimraf 'app/{,**/}**.js' 'app/{,**/}**.js.map' 'app/{,**/}**.d.ts'", "server-versions": "rimraf versions.json && npm list --depth=0 --json=true --prod=true > versions.json || exit 0", - "aws": "node app.js", - "licensecheck": "license-check" + "aws": "node app.js" }, "repository": { "type": "git", @@ -118,7 +117,6 @@ "karma-remap-istanbul": "^0.6.0", "karma-sourcemap-loader": "^0.3.7", "karma-webpack": "^2.0.2", - "license-check": "1.1.5", "null-loader": "^0.1.1", "to-string-loader": "^1.1.4", "raw-loader": "^0.5.1", @@ -134,15 +132,5 @@ "webpack": "^2.2.1", "webpack-dev-server": "^2.3.0", "webpack-merge": "^2.6.1" - }, - "license-check-config": { - "src": [ - "./app/**/*.js", - "!./app/js/*.js" - ], - "path": "assets/license_header.txt", - "blocking": true, - "logInfo": false, - "logError": true } } diff --git a/ng2-components/config/assets/license_header.txt b/ng2-components/config/assets/license_header.txt index b38baa9716..58ad8b656b 100644 --- a/ng2-components/config/assets/license_header.txt +++ b/ng2-components/config/assets/license_header.txt @@ -1,14 +1,16 @@ -@license -Copyright 2016 Alfresco Software, Ltd. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. +/*! + * @license + * Copyright 2016 Alfresco Software, Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ diff --git a/ng2-components/config/assets/license_header_add.txt b/ng2-components/config/assets/license_header_add.txt new file mode 100644 index 0000000000..b38baa9716 --- /dev/null +++ b/ng2-components/config/assets/license_header_add.txt @@ -0,0 +1,14 @@ +@license +Copyright 2016 Alfresco Software, Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/ng2-components/config/custom-loaders/license-check.js b/ng2-components/config/custom-loaders/license-check.js new file mode 100644 index 0000000000..6a15ac03e2 --- /dev/null +++ b/ng2-components/config/custom-loaders/license-check.js @@ -0,0 +1,67 @@ +var path = require('path'); +var loaderUtils = require('loader-utils'); +var fs = require('fs'); + +var licenseFileUtf8Store = undefined; + +function readLicenseHeaderFile(licenseFilePath) { + if (licenseFileUtf8Store) { + return licenseFileUtf8Store; + } + + if (fs.existsSync(licenseFilePath)) { + licenseFileUtf8Store = fs.readFileSync(licenseFilePath, 'utf8').split(/\r?\n/); + return licenseFileUtf8Store; + } + + throw new Error('The license header file path is wrong ' + licenseFilePath); +} + +function isFileEmpty(fileContents) { + return fileContents.toString('utf8').trim() === ''; +} + +function readCurrentFile(fileContent) { + return fileContent.toString('utf8').split(/\r?\n/); +} + +function isLicenseHeaderPresent(currentFileContent, licenseFilePath) { + if (!isFileEmpty(currentFileContent)) { + var currentFileUtf8 = readCurrentFile(currentFileContent), + licenseFileUtf8 = readLicenseHeaderFile(licenseFilePath); + skipStrict = 0; + + if(currentFileUtf8[0] === '"use strict";' ) { + skipStrict = 1; + } + + for (var i = skipStrict; i < licenseFileUtf8.length; i++) { + if (currentFileUtf8[i + skipStrict] !== licenseFileUtf8[i]) { + return false; + } + } + } + return true; +} + +function report(hasHeader, emitter, filename) { + if (hasHeader) return; + emitter('Missing license header file : ' + filename); +} + +function licenseCheck(webpackInstance, input, options) { + var isLicensePresent = isLicenseHeaderPresent(input, options.licenseFile); + + var emitter = options.emitErrors ? webpackInstance.emitError : webpackInstance.emitWarning; + + report(isLicensePresent, emitter, webpackInstance.resourcePath); +} + +module.exports = function(input, map) { + this.cacheable && this.cacheable(); + var callback = this.async(); + + var options = loaderUtils.getOptions(this); + licenseCheck(this, input, options); + callback(null, input, map); +}; diff --git a/ng2-components/config/webpack.common.js b/ng2-components/config/webpack.common.js index 8c2a2805bf..80773b1f73 100644 --- a/ng2-components/config/webpack.common.js +++ b/ng2-components/config/webpack.common.js @@ -7,7 +7,8 @@ module.exports = { resolveLoader: { alias: { - "file-multi-loader": path.resolve(__dirname, "./custom-loaders/file-loader-multi") + "file-multi-loader": path.resolve(__dirname, "./custom-loaders/file-loader-multi"), + "license-check": path.resolve(__dirname, "./custom-loaders/license-check") } }, @@ -65,6 +66,15 @@ module.exports = { test: /\.css$/, loader: ['to-string-loader', 'css-loader'], exclude: [/node_modules/, /bundles/, /dist/, /demo/] + },{ + enforce: 'pre', + test: /\.ts$/, + loader: 'license-check', + options: { + emitErrors: true, + licenseFile: path.resolve(__dirname, './assets/license_header.txt') + }, + exclude: [/node_modules/, /bundles/, /dist/, /demo/, /rendering-queue.services.ts/ ], }, { test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/, @@ -95,7 +105,7 @@ module.exports = { plugins: [ new webpack.NoEmitOnErrorsPlugin(), - new webpack.BannerPlugin(fs.readFileSync(path.resolve(__dirname, './assets/license_header.txt'), 'utf8')), + new webpack.BannerPlugin(fs.readFileSync(path.resolve(__dirname, './assets/license_header_add.txt'), 'utf8')), // Workaround for angular/angular#11580 new webpack.ContextReplacementPlugin( diff --git a/scripts/npm-build-all.sh b/scripts/npm-build-all.sh index 09710aa168..fa837b6c87 100755 --- a/scripts/npm-build-all.sh +++ b/scripts/npm-build-all.sh @@ -47,7 +47,6 @@ done cd "$DIR/../ng2-components/" npm install package-json-merge -g npm install rimraf -g -npm install license-check -g npm run pkg-build npm install && npm run build || exit 1 @@ -55,7 +54,6 @@ for PACKAGE in ${projects[@]} do DESTDIR="$DIR/../ng2-components/${PACKAGE}" cd $DESTDIR - npm run license-check || exit 1 if $RUN_TEST == true; then test_project $PACKAGE fi