Skip to content

Commit a8e8a4f

Browse files
authored
Add new manifest option to swift package init (#2106)
This will just generate a manifest in the current directory. The contents will be the same as for initializing a library package. That means the contents will likely not match the filesystem layout, but I think that is OK, this will still make it easier to add a manifest to an existing project. We can improve it in the future and make the generated content based on what is actually there on the filesystem. rdar://problem/50347943
1 parent 1e512e7 commit a8e8a4f

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

Sources/Workspace/InitPackage.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public final class InitPackage {
2222
case library = "library"
2323
case executable = "executable"
2424
case systemModule = "system-module"
25+
case manifest = "manifest"
2526

2627
public var description: String {
2728
return rawValue
@@ -63,6 +64,11 @@ public final class InitPackage {
6364
// FIXME: We should form everything we want to write, then validate that
6465
// none of it exists, and then act.
6566
try writeManifestFile()
67+
68+
if packageType == .manifest {
69+
return
70+
}
71+
6672
try writeREADMEFile()
6773
try writeGitIgnore()
6874
try writeSources()
@@ -96,7 +102,7 @@ public final class InitPackage {
96102
name: "\(pkgname)"
97103
""")
98104

99-
if packageType == .library {
105+
if packageType == .library || packageType == .manifest {
100106
pkgParams.append("""
101107
products: [
102108
// Products define the executables and libraries produced by a package, and make them visible to other packages.
@@ -114,7 +120,7 @@ public final class InitPackage {
114120
]
115121
""")
116122

117-
if packageType == .library || packageType == .executable {
123+
if packageType == .library || packageType == .executable || packageType == .manifest {
118124
pkgParams.append("""
119125
targets: [
120126
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -176,7 +182,7 @@ public final class InitPackage {
176182
}
177183

178184
private func writeSources() throws {
179-
if packageType == .systemModule {
185+
if packageType == .systemModule || packageType == .manifest {
180186
return
181187
}
182188
let sources = destinationPath.appending(component: "Sources")
@@ -210,7 +216,7 @@ public final class InitPackage {
210216
print("Hello, world!")
211217
212218
"""
213-
case .systemModule, .empty:
219+
case .systemModule, .empty, .manifest:
214220
fatalError("invalid")
215221
}
216222
}
@@ -249,7 +255,7 @@ public final class InitPackage {
249255
try makeDirectories(tests)
250256

251257
switch packageType {
252-
case .systemModule, .empty: break
258+
case .systemModule, .empty, .manifest: break
253259
case .library, .executable:
254260
try writeLinuxMain(testsPath: tests)
255261
try writeTestFileStubs(testsPath: tests)
@@ -356,7 +362,7 @@ public final class InitPackage {
356362

357363
let testClassFile = testModule.appending(RelativePath("\(moduleName)Tests.swift"))
358364
switch packageType {
359-
case .systemModule, .empty: break
365+
case .systemModule, .empty, .manifest: break
360366
case .library:
361367
try writeLibraryTestsFile(testClassFile)
362368
case .executable:

Tests/WorkspaceTests/InitTests.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ class InitTests: XCTestCase {
164164
XCTAssert(fs.exists(path.appending(component: "module.modulemap")))
165165
}
166166
}
167+
168+
func testInitManifest() throws {
169+
mktmpdir { tmpPath in
170+
let fs = localFileSystem
171+
let path = tmpPath.appending(component: "Foo")
172+
let name = path.basename
173+
try fs.createDirectory(path)
174+
175+
// Create the package
176+
let initPackage = try InitPackage(name: name, destinationPath: path, packageType: InitPackage.PackageType.manifest)
177+
var progressMessages = [String]()
178+
initPackage.progressReporter = { message in
179+
progressMessages.append(message)
180+
}
181+
try initPackage.writePackageStructure()
182+
183+
// Not picky about the specific progress messages, just checking that we got some.
184+
XCTAssert(progressMessages.count > 0)
185+
186+
// Verify basic file system content that we expect in the package
187+
let manifest = path.appending(component: "Package.swift")
188+
XCTAssertTrue(fs.exists(manifest))
189+
let manifestContents = try localFileSystem.readFileContents(manifest).description
190+
let version = "\(InitPackage.newPackageToolsVersion.major).\(InitPackage.newPackageToolsVersion.minor)"
191+
XCTAssertTrue(manifestContents.hasPrefix("// swift-tools-version:\(version)\n"))
192+
}
193+
}
167194

168195
// MARK: Special case testing
169196

Tests/WorkspaceTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extension InitTests {
66
// `swift test --generate-linuxmain`
77
// to regenerate.
88
static let __allTests__InitTests = [
9+
("testInitManifest", testInitManifest),
910
("testInitPackageEmpty", testInitPackageEmpty),
1011
("testInitPackageExecutable", testInitPackageExecutable),
1112
("testInitPackageLibrary", testInitPackageLibrary),

0 commit comments

Comments
 (0)