Skip to content

Commit 8748e9e

Browse files
committed
Init template cleanup
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 #6144 rdar://98999734
1 parent e1102e4 commit 8748e9e

File tree

8 files changed

+107
-226
lines changed

8 files changed

+107
-226
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Swift Next
1717
}
1818
```
1919

20-
2120
Swift 5.9
2221
-----------
2322

@@ -29,6 +28,9 @@ Swift 5.9
2928

3029
Support for building plugin dependencies for the host when cross-compiling.
3130

31+
* [#6144]
32+
33+
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.
3234

3335
Swift 5.8
3436
-----------

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(validating: "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)