Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buildifier Json File Configuration #357

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@
"markdownDescription": "The name of the Buildifier executable. This may be an absolute path, or a simple name that will be searched for on the system path. Paths starting with `@` are interpreted as Bazel targets and are run via `bazel run`. If empty, \"buildifier\" on the system path will be used.\n\nBuildifier can be downloaded from https://github.com/bazelbuild/buildtools/releases.",
"scope": "machine-overridable"
},
"bazel.buildifierConfigJsonPath": {
"type": "string",
"default": "",
"markdownDescription": "The path of the Buildifier config json file, ex: `.buildifier.json`. This may be an absolute path, or a relative path within the workspace. If this option is unset, `buildifier` will automatically look for a `.buildifier.json` file in the workspace.",
"scope": "machine-overridable"
},
"bazel.buildifierFixOnFormat": {
"type": "boolean",
"default": false,
Expand Down Expand Up @@ -461,4 +467,4 @@
"vscode-uri": "^3.0.2",
"which": "^4.0.0"
}
}
}
16 changes: 16 additions & 0 deletions src/buildifier/buildifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,18 @@ export function getDefaultBuildifierExecutablePath(): string {
return buildifierExecutable;
}

/**
* Gets the path to the buildifier json configuration file specified by the
* workspace configuration, if present.
*
* @returns The path to the buildifier json configuration file specified in the
* workspace configuration, or an empty string if not present.
*/
export function getDefaultBuildifierJsonConfigPath(): string {
const bazelConfig = vscode.workspace.getConfiguration("bazel");
return bazelConfig.get<string>("buildifierConfigJsonPath");
}

/**
* Executes buildifier with the given file content and arguments.
*
Expand All @@ -191,6 +203,10 @@ export function executeBuildifier(
return new Promise((resolve, reject) => {
// Determine the executable
let executable = getDefaultBuildifierExecutablePath();
const buildifierConfigJsonPath = getDefaultBuildifierJsonConfigPath();
if (buildifierConfigJsonPath.length !== 0) {
cameron-martin marked this conversation as resolved.
Show resolved Hide resolved
args = ["--config", buildifierConfigJsonPath, ...args];
}
// Paths starting with an `@` are referring to Bazel targets
if (executable.startsWith("@")) {
const targetName = executable;
Expand Down