Skip to content

Commit b15db80

Browse files
committed
Merge branch 'package-init-empty' of https://github.com/ddunbar/swift-package-manager
2 parents 9b1f8a7 + c508f63 commit b15db80

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ public struct SwiftPackageTool: SwiftTool {
243243
print("")
244244
print("COMMANDS:")
245245
print(" init [--type <type>] Initialize a new package")
246-
print(" (type: library|executable|system-module)")
246+
print(" (type: empty|library|executable|system-module)")
247247
print(" fetch Fetch package dependencies")
248248
print(" update Update package dependencies")
249249
print(" generate-xcodeproj [--output <path>] Generates an Xcode project")
250250
print(" show-dependencies [--format <format>] Print the resolved dependency graph")
251-
print(" (format: text|dot|json)")
251+
print(" (format: text|dot|json)")
252252
print(" dump-package [--input <path>] Print parsed Package.swift as JSON")
253253
print("")
254254
print("OPTIONS:")

Sources/Commands/init.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ final class InitPackage {
117117
}
118118
print("Creating \(sources.relative(to: rootd).asString)/")
119119
try makeDirectories(sources)
120-
120+
121+
if mode == .empty {
122+
return
123+
}
124+
121125
let sourceFileName = (mode == .executable) ? "main.swift" : "\(typeName).swift"
122126
let sourceFile = sources.appending(RelativePath(sourceFileName))
123127

@@ -129,8 +133,8 @@ final class InitPackage {
129133
stream <<< "}\n"
130134
case .executable:
131135
stream <<< "print(\"Hello, world!\")\n"
132-
case .systemModule:
133-
break
136+
case .systemModule, .empty:
137+
fatalError("invalid")
134138
}
135139
}
136140
}
@@ -210,10 +214,12 @@ final class InitPackage {
210214

211215
/// Represents a package type for the purposes of initialization.
212216
enum InitMode: CustomStringConvertible {
213-
case library, executable, systemModule
217+
case empty, library, executable, systemModule
214218

215219
init(_ rawValue: String) throws {
216220
switch rawValue.lowercased() {
221+
case "empty":
222+
self = .empty
217223
case "library":
218224
self = .library
219225
case "executable":
@@ -227,6 +233,7 @@ enum InitMode: CustomStringConvertible {
227233

228234
var description: String {
229235
switch self {
236+
case .empty: return "empty"
230237
case .library: return "library"
231238
case .executable: return "executable"
232239
case .systemModule: return "system-module"

Tests/CommandsTests/PackageToolTests.swift

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,54 @@ final class PackageToolTests: XCTestCase {
8282
XCTAssertEqual(resolveSymlinks(AbsolutePath(path)), resolveSymlinks(packageRoot))
8383
}
8484
}
85-
85+
86+
func testInitEmpty() throws {
87+
mktmpdir { tmpPath in
88+
var fs = localFileSystem
89+
let path = tmpPath.appending(component: "Foo")
90+
try fs.createDirectory(path)
91+
_ = try execute(["-C", path.asString, "init", "--type", "empty"])
92+
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
93+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), [])
94+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")), [])
95+
}
96+
}
97+
98+
func testInitExecutable() throws {
99+
mktmpdir { tmpPath in
100+
var fs = localFileSystem
101+
let path = tmpPath.appending(component: "Foo")
102+
try fs.createDirectory(path)
103+
_ = try execute(["-C", path.asString, "init", "--type", "executable"])
104+
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
105+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), ["main.swift"])
106+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Tests")), [])
107+
}
108+
}
109+
110+
func testInitLibrary() throws {
111+
mktmpdir { tmpPath in
112+
var fs = localFileSystem
113+
let path = tmpPath.appending(component: "Foo")
114+
try fs.createDirectory(path)
115+
_ = try execute(["-C", path.asString, "init"])
116+
XCTAssert(fs.exists(path.appending(component: "Package.swift")))
117+
XCTAssertEqual(try fs.getDirectoryContents(path.appending(component: "Sources")), ["Foo.swift"])
118+
XCTAssertEqual(
119+
try fs.getDirectoryContents(path.appending(component: "Tests")).sorted(),
120+
["FooTests", "LinuxMain.swift"])
121+
}
122+
}
123+
86124
static var allTests = [
87125
("testUsage", testUsage),
88126
("testVersion", testVersion),
89127
("testFetch", testFetch),
90128
("testUpdate", testUpdate),
91129
("testDumpPackage", testDumpPackage),
92130
("testShowDependencies", testShowDependencies),
131+
("testInitEmpty", testInitEmpty),
132+
("testInitExecutable", testInitExecutable),
133+
("testInitLibrary", testInitLibrary),
93134
]
94135
}

0 commit comments

Comments
 (0)