2020-04-24 19:29:53 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2020-04-24 19:29:53 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {join} from 'path';
|
|
|
|
|
|
|
|
import {getRepoBaseDir} from '../../utils/config';
|
2020-05-20 17:19:17 -04:00
|
|
|
import {error} from '../../utils/console';
|
2020-04-24 19:29:53 -04:00
|
|
|
|
|
|
|
import {Formatter} from './base-formatter';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Formatter for running buildifier against bazel related files.
|
|
|
|
*/
|
|
|
|
export class Buildifier extends Formatter {
|
|
|
|
name = 'buildifier';
|
|
|
|
|
|
|
|
binaryFilePath = join(getRepoBaseDir(), 'node_modules/.bin/buildifier');
|
|
|
|
|
|
|
|
defaultFileMatcher = ['**/*.bzl', '**/BUILD.bazel', '**/WORKSPACE', '**/BUILD'];
|
|
|
|
|
|
|
|
actions = {
|
|
|
|
check: {
|
|
|
|
commandFlags: `${BAZEL_WARNING_FLAG} --lint=warn --mode=check --format=json`,
|
|
|
|
callback:
|
|
|
|
(_: string, code: number, stdout: string) => {
|
|
|
|
return code !== 0 || !JSON.parse(stdout)['success'];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
format: {
|
|
|
|
commandFlags: `${BAZEL_WARNING_FLAG} --lint=fix --mode=fix`,
|
|
|
|
callback:
|
|
|
|
(file: string, code: number, _: string, stderr: string) => {
|
|
|
|
if (code !== 0) {
|
2020-05-20 17:19:17 -04:00
|
|
|
error(`Error running buildifier on: ${file}`);
|
|
|
|
error(stderr);
|
|
|
|
error();
|
2020-04-24 19:29:53 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// The warning flag for buildifier copied from angular/angular's usage.
|
|
|
|
const BAZEL_WARNING_FLAG = `--warnings=attr-cfg,attr-license,attr-non-empty,attr-output-default,` +
|
|
|
|
`attr-single-file,constant-glob,ctx-args,depset-iteration,depset-union,dict-concatenation,` +
|
|
|
|
`duplicated-name,filetype,git-repository,http-archive,integer-division,load,load-on-top,` +
|
|
|
|
`native-build,native-package,output-group,package-name,package-on-top,positional-args,` +
|
|
|
|
`redefined-variable,repository-name,same-origin-load,string-iteration,unused-variable`;
|