[ADF-579] - move license check in webpack (#1859)

* move license check in webpack

* fix exclusion check header

* different headers
This commit is contained in:
Eugenio Romano
2017-05-05 18:11:36 +02:00
committed by Eugenio Romano
parent 774017ac62
commit 80d67212b7
8 changed files with 196 additions and 32 deletions

View File

@@ -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);
};

View File

@@ -29,6 +29,13 @@ module.exports = {
'vendor': './app/vendor.ts', 'vendor': './app/vendor.ts',
'app': './app/main.ts' 'app': './app/main.ts'
}, },
resolveLoader: {
alias: {
"license-check": path.resolve(__dirname, "./loaders/license-check")
}
},
module: { module: {
rules: [ rules: [
{ {
@@ -84,6 +91,17 @@ module.exports = {
{ {
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/, test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'file-loader?name=assets/[name].[hash].[ext]' 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/],
} }
] ]
}, },

View File

@@ -4,14 +4,13 @@
"version": "1.4.0", "version": "1.4.0",
"author": "Alfresco Software, Ltd.", "author": "Alfresco Software, Ltd.",
"scripts": { "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", "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", "test": "rimraf coverage && karma start --single-run",
"clean": "npm run clean-build && rimraf dist node_modules typings dist", "clean": "npm run clean-build && rimraf dist node_modules typings dist",
"clean-build": "rimraf 'app/{,**/}**.js' 'app/{,**/}**.js.map' 'app/{,**/}**.d.ts'", "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", "server-versions": "rimraf versions.json && npm list --depth=0 --json=true --prod=true > versions.json || exit 0",
"aws": "node app.js", "aws": "node app.js"
"licensecheck": "license-check"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -118,7 +117,6 @@
"karma-remap-istanbul": "^0.6.0", "karma-remap-istanbul": "^0.6.0",
"karma-sourcemap-loader": "^0.3.7", "karma-sourcemap-loader": "^0.3.7",
"karma-webpack": "^2.0.2", "karma-webpack": "^2.0.2",
"license-check": "1.1.5",
"null-loader": "^0.1.1", "null-loader": "^0.1.1",
"to-string-loader": "^1.1.4", "to-string-loader": "^1.1.4",
"raw-loader": "^0.5.1", "raw-loader": "^0.5.1",
@@ -134,15 +132,5 @@
"webpack": "^2.2.1", "webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0", "webpack-dev-server": "^2.3.0",
"webpack-merge": "^2.6.1" "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
} }
} }

View File

@@ -1,14 +1,16 @@
@license /*!
Copyright 2016 Alfresco Software, Ltd. * @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. * Licensed under the Apache License, Version 2.0 (the "License");
You may obtain a copy of the License at * 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 *
* 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, * Unless required by applicable law or agreed to in writing, software
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
limitations under the License. * See the License for the specific language governing permissions and
* limitations under the License.
*/

View File

@@ -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.

View File

@@ -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);
};

View File

@@ -7,7 +7,8 @@ module.exports = {
resolveLoader: { resolveLoader: {
alias: { 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$/, test: /\.css$/,
loader: ['to-string-loader', 'css-loader'], loader: ['to-string-loader', 'css-loader'],
exclude: [/node_modules/, /bundles/, /dist/, /demo/] 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)$/, test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
@@ -95,7 +105,7 @@ module.exports = {
plugins: [ plugins: [
new webpack.NoEmitOnErrorsPlugin(), 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 // Workaround for angular/angular#11580
new webpack.ContextReplacementPlugin( new webpack.ContextReplacementPlugin(

View File

@@ -47,7 +47,6 @@ done
cd "$DIR/../ng2-components/" cd "$DIR/../ng2-components/"
npm install package-json-merge -g npm install package-json-merge -g
npm install rimraf -g npm install rimraf -g
npm install license-check -g
npm run pkg-build npm run pkg-build
npm install && npm run build || exit 1 npm install && npm run build || exit 1
@@ -55,7 +54,6 @@ for PACKAGE in ${projects[@]}
do do
DESTDIR="$DIR/../ng2-components/${PACKAGE}" DESTDIR="$DIR/../ng2-components/${PACKAGE}"
cd $DESTDIR cd $DESTDIR
npm run license-check || exit 1
if $RUN_TEST == true; then if $RUN_TEST == true; then
test_project $PACKAGE test_project $PACKAGE
fi fi