Skip to content

Commit

Permalink
feat: smarter path default for vknabel/vscode-apple-swift-format#17
Browse files Browse the repository at this point in the history
  • Loading branch information
vknabel committed Dec 12, 2022
1 parent 61221fc commit 70ed02e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## Unreleased
## 1.8.0

- removed: `swiftlint.forceExcludePaths` as it didn't work. Use `excluded` in your `.swiftlint` config instead. #39
- Added: `swiftlint.path` can now be an array of strings and defaults to `[/usr/bin/env, swiftlint]` [vknabel/vscode-apple-swift-format#17](https://github.com/vknabel/vscode-apple-swift-format/issues/17)

## 1.7.3

Expand Down
27 changes: 23 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/vknabel/vscode-swiftlint"
},
"version": "1.7.3",
"version": "1.8.0",
"license": "MIT",
"author": {
"name": "Valentin Knabel",
Expand Down Expand Up @@ -60,10 +60,29 @@
"description": "Only use SwiftLint when a config exists."
},
"swiftlint.path": {
"type": "string",
"default": "/usr/local/bin/swiftlint",
"description": "The location of your globally installed SwiftLint.",
"scope": "machine"
"scope": "machine",
"default": [
"/usr/bin/env",
"swiftlint"
],
"oneOf": [
{
"type": "string",
"default": "/usr/local/bin/swiftlint"
},
{
"type": "array",
"minItems": 1,
"default": [
"/usr/bin/env",
"swiftlint"
],
"items": {
"type": "string"
}
}
]
},
"swiftlint.additionalParameters": {
"type": "array",
Expand Down
23 changes: 15 additions & 8 deletions src/Current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Current {
onlyEnableWithConfig(): boolean;
affectsConfiguration(changeEvent: vscode.ConfigurationChangeEvent): boolean;

swiftLintPath(uri: vscode.Uri): string | null;
swiftLintPath(uri: vscode.Uri): string[] | null;
toolchainPath(): string | undefined;
additionalParameters(): string[];
resetSwiftLintPath(): void;
Expand Down Expand Up @@ -113,7 +113,7 @@ export function prodEnvironment(): Current {
const fullPath = workspace ? join(workspace!.uri.path, path) : path;

if (existsSync(fullPath)) {
return absolutePath(fullPath);
return [absolutePath(fullPath)];
}
}

Expand Down Expand Up @@ -160,12 +160,19 @@ export function prodEnvironment(): Current {
};
}

const fallbackGlobalSwiftFormatPath = () =>
absolutePath(
vscode.workspace
.getConfiguration()
.get("swiftlint.path", "/usr/local/bin/swiftlint")
);
const fallbackGlobalSwiftFormatPath = (): string[] => {
const defaultPath = ["/usr/bin/env", "swiftlint"];
const path = vscode.workspace
.getConfiguration()
.get("swiftlint.path", defaultPath);
if (typeof path === "string") {
return [absolutePath(path)];
} else if (Array.isArray(path) && path.length > 0) {
return [absolutePath(path[0]), ...path.slice(1)];
} else {
return defaultPath;
}
};

const Current = prodEnvironment();
export default Current as Current;
4 changes: 3 additions & 1 deletion src/UserInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export async function handleFormatError(error: any, uri: vscode.Uri) {
}
} else if (error.code === "ENOENT") {
const selection = await Current.editor.showErrorMessage(
`Could not find SwiftLint: ${Current.config.swiftLintPath(uri)}`,
`Could not find SwiftLint: ${
Current.config.swiftLintPath(uri)?.join(" ") ?? "null"
}`,
FormatErrorInteraction.reset,
FormatErrorInteraction.configure
);
Expand Down
6 changes: 3 additions & 3 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,12 @@ function execSwiftlint(request: {
Object.keys(filesEnv)
.map((env) => `${env}='${filesEnv[env]}'`)
.join(" "),
`'${swiftLintPath}' `,
`'${swiftLintPath.join(" ")}' `,
swiftLintArgs?.map((arg) => `'${arg}'`).join(" ")
);
const exec = execShell(
swiftLintPath,
swiftLintArgs,
swiftLintPath[0],
[...swiftLintPath.slice(1), ...swiftLintArgs],
{
encoding: "utf8",
maxBuffer: 20 * 1024 * 1024,
Expand Down

0 comments on commit 70ed02e

Please sign in to comment.