Skip to content

Commit 2bcbdf0

Browse files
SimplyDannyvknabel
authored andcommitted
Build package only when required by configuration
1 parent 356f914 commit 2bcbdf0

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let package = Package(
3939
.package(url: "https://github.com/orta/PackageConfig.git", from: "0.0.1"),
4040
// Dev deps
4141
.package(url: "https://github.com/orta/Komondor.git", from: "0.0.1"),
42-
+ .package(url: "https://github.com/apple/swift-format.git", branch:("release/5.8")),
42+
+ .package(url: "https://github.com/apple/swift-format.git", from: "601.0.0"),
4343
],
4444
targets: [...]
4545
)
@@ -52,11 +52,17 @@ let package = Package(
5252
| Config | Type | Default | Description | |
5353
| ------------------------------------------------ | ---------- | ------------------- | ------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------- |
5454
| `apple-swift-format.enable` | `Bool` | `true` | Whether apple/swift-format should actually do something. | |
55-
| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses a apple/swift-format as SwiftPM dependency. | |
55+
| `apple-swift-format.onlyEnableOnSwiftPMProjects` | `Bool` | `false` | Requires and uses an apple/swift-format as SwiftPM dependency. This will cause the extension to build the Swift package upon first launch. | |
5656
| `apple-swift-format.onlyEnableWithConfig` | `Bool` | `false` | Only format if config present. | |
5757
| `apple-swift-format.path` | `[String] \| String` | platform dependent | `swift-format` | The location of the globally installed SwiftFormat (resolved with the current path if only a filename). |
5858
| `apple-swift-format.configSearchPaths` | `[String]` | `[".swift-format"]` | Possible paths for apple/swift-format config. | |
5959

60+
Note that when `apple-swift-format.onlyEnableOnSwiftPMProjects` is enabled, the extension will only run `swift-format`
61+
executables built as part of the Swift package open in the workspace. It will try to build the binary once on first
62+
launch. If the build fails, the extension will not fall back to a globally installed `swift-format`. If you prefer a
63+
locally built `swift-format`, but want to skip the automatic initial build, let `apple-swift-format.path` point to the
64+
local executable you have built manually or by other means independent of the extension.
65+
6066
## FAQs
6167

6268
### How do I enable formatting on type?

src/Current.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ export function prodEnvironment(): Current {
8484
}
8585

8686
// Support running from Swift PM projects
87-
let possibleLocalPaths = glob.sync(
88-
"**/.build/{release,debug}/swift-format",
89-
{ maxDepth: 5 },
90-
);
91-
for (const path of possibleLocalPaths) {
92-
const fullPath = paths.resolve(workspace.uri.fsPath, path);
93-
94-
if (existsSync(fullPath)) {
95-
return [absolutePath(fullPath)];
87+
if (Current.config.onlyEnableOnSwiftPMProjects()) {
88+
const possibleLocalPaths = glob.sync(
89+
"**/.build/{release,debug}/swift-format",
90+
{ maxDepth: 5 },
91+
);
92+
for (const path of possibleLocalPaths) {
93+
const fullPath = paths.resolve(workspace.uri.fsPath, path);
94+
if (existsSync(fullPath)) {
95+
return [absolutePath(fullPath)];
96+
}
9697
}
9798
}
9899

@@ -114,11 +115,7 @@ export function prodEnvironment(): Current {
114115
}
115116

116117
const fallbackGlobalSwiftFormatPath = (): string[] | null => {
117-
if (
118-
vscode.workspace
119-
.getConfiguration()
120-
.get("apple-swift-format.onlyEnableOnSwiftPMProjects", false)
121-
) {
118+
if (Current.config.onlyEnableOnSwiftPMProjects()) {
122119
return null;
123120
}
124121
var path = vscode.workspace

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export function activate(context: vscode.ExtensionContext) {
3535
}
3636

3737
async function buildSwiftFormatIfNeeded() {
38+
if (!Current.config.onlyEnableOnSwiftPMProjects()) {
39+
return;
40+
}
3841
const manifests = await vscode.workspace.findFiles(
3942
"**/Package.swift",
4043
"**/.build/**",

0 commit comments

Comments
 (0)