Skip to content

Commit 0b01411

Browse files
committed
Init template cleanup (swiftlang#6144)
Improve the `swift package init` experience using the following criteria: - Simplify the templates to include only the minimum necessary to get started without making assumptions. - Use documentation links instead of template content to teach people about Swift. - Improve the "front door" experience with the help usage. Detailed list of changes: - Remove the `system-module` template. - Remove the `manifest` template - Add a new `tool` template, an executable that uses ArgumentParser a manifest instead. - Remove README generation. - Don't generate empty directories. - Don't emit empty dependencies array in manifests. - Update `library` template content: - Remove struct definition, use a simple public function instead. - Include documentation links for TSPL and the Standard Library. - Include documentation links for XCTest in the generated test case. - Update `executable` template content - Use simpler top-level code instead of an `@main` struct. - Remove executable tests. - Put sources directly in `./Sources`. - Clean up and align `type` argument help text - Update Usage.md documentation - Update CHANGELOG to reflect init template changes in swiftlang#6144 rdar://98999734
1 parent 597b99f commit 0b01411

File tree

8 files changed

+108
-230
lines changed

8 files changed

+108
-230
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ Swift Next
77

88
In packages that specify resources using a future tools version, the generated resource bundle accessor will import `Foundation.Bundle` for its own implementation only. _Clients_ of such packages therefore no longer silently import `Foundation`, preventing inadvertent use of Foundation extensions to standard library APIs, which helps to avoid unexpected code size increases.
99

10-
1110
Swift 5.8
1211
-----------
1312

13+
* [#6144]
14+
15+
Remove the `system-module` and `manifest` templates and clean up the remaining `empty`, `library`, and `executable` templates so they include the minimum information needed to get started, with links to documentation in the generated library, executable, and test content.
16+
1417
* [#5949]
1518

1619
New `--pkg-config-path` option on `build`, `test`, and `run` commands has been

Documentation/Usage.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ initialize it as a package that builds a system module:
158158
example$ cd ..
159159
$ mkdir Clibgit
160160
$ cd Clibgit
161-
Clibgit$ swift package init --type system-module
161+
Clibgit$ swift package init --type empty
162162

163-
This creates `Package.swift` and `module.modulemap` files in the directory.
163+
This creates a `Package.swift` file in the directory.
164164
Edit `Package.swift` and add `pkgConfig` parameter:
165165

166166
```swift
@@ -181,7 +181,7 @@ parameter you can pass the path of a directory containing the library using the
181181

182182
example$ swift build -Xlinker -L/usr/local/lib/
183183

184-
Edit `module.modulemap` so it consists of the following:
184+
Create a `module.modulemap` file so it consists of the following:
185185

186186
module Clibgit [system] {
187187
header "/usr/local/include/git2.h"
@@ -258,10 +258,10 @@ initialize it as a package that builds a system module:
258258
example$ cd ..
259259
$ mkdir CJPEG
260260
$ cd CJPEG
261-
CJPEG$ swift package init --type system-module
261+
CJPEG$ swift package init --type empty
262262

263-
This creates `Package.swift` and `module.modulemap` files in the directory.
264-
Edit `module.modulemap` so it consists of the following:
263+
This creates `Package.swift` file in the directory.
264+
Create a `module.modulemap` file so it consists of the following:
265265

266266
module CJPEG [system] {
267267
header "shim.h"

IntegrationTests/Tests/IntegrationTests/BasicTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ final class BasicTests: XCTestCase {
234234
try localFileSystem.createDirectory(packagePath)
235235
try sh(swiftPackage, "--package-path", packagePath, "init", "--type", "executable")
236236
// delete any files generated
237-
for entry in try localFileSystem.getDirectoryContents(packagePath.appending(components: "Sources", "secho")) {
238-
try localFileSystem.removeFileTree(packagePath.appending(components: "Sources", "secho", entry))
237+
for entry in try localFileSystem.getDirectoryContents(packagePath.appending(components: "Sources")) {
238+
try localFileSystem.removeFileTree(packagePath.appending(components: "Sources", entry))
239239
}
240240
try localFileSystem.writeFileContents(
241-
packagePath.appending(components: "Sources", "secho", "main.swift"),
241+
packagePath.appending(components: "Sources", "secho.swift"),
242242
bytes: ByteString(encodingAsUTF8: """
243243
import Foundation
244244
print(CommandLine.arguments.dropFirst().joined(separator: " "))

IntegrationTests/Tests/IntegrationTests/SwiftPMTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ final class SwiftPMTests: XCTestCase {
5959
try localFileSystem.createDirectory(packagePath)
6060
try sh(swiftPackage, "--package-path", packagePath, "init", "--type", "executable")
6161
// delete any files generated
62-
for entry in try localFileSystem.getDirectoryContents(packagePath.appending(components: "Sources", "foo")) {
63-
try localFileSystem.removeFileTree(packagePath.appending(components: "Sources", "foo", entry))
62+
for entry in try localFileSystem.getDirectoryContents(packagePath.appending(components: "Sources")) {
63+
try localFileSystem.removeFileTree(packagePath.appending(components: "Sources", entry))
6464
}
65-
try localFileSystem.writeFileContents(AbsolutePath("Sources/foo/main.m", relativeTo: packagePath)) {
65+
try localFileSystem.writeFileContents(AbsolutePath(validating: "Sources/main.m", relativeTo: packagePath)) {
6666
$0 <<< "int main() {}"
6767
}
6868
let archs = ["x86_64", "arm64"]

Sources/Commands/PackageTools/Init.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ extension SwiftPackageTool {
2525

2626
@Option(
2727
name: .customLong("type"),
28-
help: ArgumentHelp("Package type: empty | library | executable | system-module | manifest", discussion: """
29-
empty - Create an empty package
30-
library - Create a package that contains a library
31-
executable - Create a package that contains a binary executable
32-
system-module - Create a package that contains a system module
33-
manifest - Create a Package.swift file
28+
help: ArgumentHelp("Package type:", discussion: """
29+
library - A package with a library.
30+
executable - A package with an executable.
31+
tool - A package with an executable that uses
32+
Swift Argument Parser. Use this template if you
33+
plan to have a rich set of command-line arguments.
34+
empty - An empty package with a Package.swift manifest.
3435
"""))
3536
var initMode: InitPackage.PackageType = .library
3637

0 commit comments

Comments
 (0)