[build] update notice generation (#138860)

* [build] update notice generation

* skip shared_built_assets

* mostly revert globs
This commit is contained in:
Jonathan Budzenski 2022-08-17 07:23:10 -04:00 committed by GitHub
parent 4e7f8b2176
commit f6012e9d6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 37 deletions

View file

@ -253,6 +253,32 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Portions of this code are licensed under the following license:
For license information please see https://edge.fullstory.com/s/fs.js.LICENSE.txt
---
This code includes a copy of the `normalize-path`
https://github.com/jonschlinkert/normalize-path/blob/52c3a95ebebc2d98c1ad7606cbafa7e658656899/index.js
The MIT License (MIT)
Copyright (c) 2014-2018, Jon Schlinkert.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
---
This product bundles bootstrap@3.3.6 which is available under a
"MIT" license.

View file

@ -83,9 +83,14 @@ export async function buildDistributables(log: ToolingLog, options: BuildOptions
await run(Tasks.CreatePackageJson);
await run(Tasks.InstallDependencies);
await run(Tasks.GeneratePackagesOptimizedAssets);
await run(Tasks.DeleteBazelPackagesFromBuildRoot);
// Run on all source files
// **/packages need to be read
// before DeleteBazelPackagesFromBuildRoot
await run(Tasks.CreateNoticeFile);
await run(Tasks.CreateXPackNoticeFile);
await run(Tasks.DeleteBazelPackagesFromBuildRoot);
await run(Tasks.UpdateLicenseFile);
await run(Tasks.RemovePackageJsonDeps);
await run(Tasks.CleanPackageManagerRelatedFiles);

View file

@ -6,7 +6,10 @@
* Side Public License, v 1.
*/
import vfs from 'vinyl-fs';
import { readFile } from 'fs/promises';
import { relative } from 'path';
import globby from 'globby';
import { ToolingLog } from '@kbn/tooling-log';
const NOTICE_COMMENT_RE = /\/\*[\s\n\*]*@notice([\w\W]+?)\*\//g;
@ -30,45 +33,37 @@ interface Options {
* into the repository.
*/
export async function generateNoticeFromSource({ productName, directory, log }: Options) {
const globs = ['**/*.{js,less,css,ts,tsx}'];
const select = [
'**/*.{js,mjs,scss,css,ts,tsx}',
'!{node_modules,build,dist,data,built_assets,shared_built_assets}/**',
'!packages/*/{node_modules,build,dist}/**',
'!src/plugins/*/{node_modules,build,dist}/**',
'!x-pack/{node_modules,build,dist,data}/**',
'!x-pack/packages/*/{node_modules,build,dist}/**',
'!x-pack/plugins/**/{node_modules,build,dist}/**',
'!**/target/**',
];
const options = {
cwd: directory,
nodir: true,
ignore: [
'{node_modules,build,dist,data,built_assets}/**',
'packages/*/{node_modules,build,dist}/**',
'src/plugins/*/{node_modules,build,dist}/**',
'x-pack/{node_modules,build,dist,data}/**',
'x-pack/packages/*/{node_modules,build,dist}/**',
'x-pack/plugins/**/{node_modules,build,dist}/**',
'**/target/**',
],
};
log.debug('vfs.src globs', globs);
log.debug('vfs.src options', options);
log.info(`Searching ${directory} for multi-line comments starting with @notice`);
const files = vfs.src(globs, options);
const noticeComments: string[] = [];
await new Promise((resolve, reject) => {
files
.on('data', (file) => {
log.verbose(`Checking for @notice comments in ${file.relative}`);
const source = file.contents.toString('utf8');
let match;
while ((match = NOTICE_COMMENT_RE.exec(source)) !== null) {
log.info(`Found @notice comment in ${file.relative}`);
if (!noticeComments.includes(match[1])) {
noticeComments.push(match[1]);
}
}
})
.on('error', reject)
.on('end', resolve);
const files = globby.stream(select, {
cwd: directory,
followSymbolicLinks: false,
absolute: true,
});
const noticeComments: string[] = [];
for await (const file of files) {
const source = await readFile(file, 'utf-8');
const relativeFile = relative(directory, file.toString());
log.verbose(`Checking for @notice comments in ${relativeFile}`);
let match;
while ((match = NOTICE_COMMENT_RE.exec(source)) !== null) {
log.info(`Found @notice comment in ${relativeFile}`);
if (!noticeComments.includes(match[1])) {
noticeComments.push(match[1]);
}
}
}
let noticeText = '';
noticeText += `${productName}\n`;