@@ -43,8 +43,6 @@ public final class InitPackage {
43
43
case empty = " empty "
44
44
case library = " library "
45
45
case executable = " executable "
46
- case systemModule = " system-module "
47
- case manifest = " manifest "
48
46
case `extension` = " extension "
49
47
50
48
public var description : String {
@@ -114,15 +112,8 @@ public final class InitPackage {
114
112
// FIXME: We should form everything we want to write, then validate that
115
113
// none of it exists, and then act.
116
114
try writeManifestFile ( )
117
-
118
- if packageType == . manifest {
119
- return
120
- }
121
-
122
- try writeREADMEFile ( )
123
115
try writeGitIgnore ( )
124
116
try writeSources ( )
125
- try writeModuleMap ( )
126
117
try writeTests ( )
127
118
}
128
119
@@ -177,51 +168,43 @@ public final class InitPackage {
177
168
""" )
178
169
}
179
170
180
- if packageType == . library || packageType == . manifest {
171
+ if packageType == . library {
181
172
pkgParams. append ( """
182
173
products: [
183
- // Products define the executables and libraries a package produces, and make them visible to other packages.
174
+ // Products define the executables and libraries a package produces, making them visible to other packages.
184
175
.library(
185
176
name: " \( pkgname) " ,
186
177
targets: [ " \( pkgname) " ]),
187
178
]
188
179
""" )
189
180
}
190
181
191
- pkgParams. append ( """
192
- dependencies: [
193
- // Dependencies declare other packages that this package depends on.
194
- // .package(url: /* package url */, from: " 1.0.0 " ),
195
- ]
196
- """ )
197
-
198
- if packageType == . library || packageType == . executable || packageType == . manifest {
182
+ if packageType == . library || packageType == . executable {
199
183
var param = " "
200
184
201
185
param += """
202
186
targets: [
203
- // Targets are the basic building blocks of a package. A target can define a module or a test suite.
204
- // Targets can depend on other targets in this package, and on products in packages this package depends on .
187
+ // Targets are the basic building blocks of a package, defining a module or a test suite.
188
+ // Targets can depend on other targets in this package and products from dependencies .
205
189
206
190
"""
207
191
if packageType == . executable {
208
192
param += """
209
193
.executableTarget(
194
+ name: " \( pkgname) " ,
195
+ path: " Sources " ),
196
+ ]
210
197
"""
211
198
} else {
212
199
param += """
213
200
.target(
201
+ name: " \( pkgname) " ,
202
+ .testTarget(
203
+ name: " \( pkgname) Tests " ,
204
+ dependencies: [ " \( pkgname) " ]),
205
+ ]
214
206
"""
215
207
}
216
- param += """
217
-
218
- name: " \( pkgname) " ,
219
- dependencies: []),
220
- .testTarget(
221
- name: " \( pkgname) Tests " ,
222
- dependencies: [ " \( pkgname) " ]),
223
- ]
224
- """
225
208
226
209
pkgParams. append ( param)
227
210
}
@@ -242,23 +225,10 @@ public final class InitPackage {
242
225
)
243
226
}
244
227
245
- private func writeREADMEFile( ) throws {
246
- let readme = destinationPath. appending ( component: " README.md " )
247
- guard self . fileSystem. exists ( readme) == false else {
228
+ private func writeGitIgnore( ) throws {
229
+ guard packageType != . empty else {
248
230
return
249
231
}
250
-
251
- try writePackageFile ( readme) { stream in
252
- stream <<< """
253
- # \( pkgname)
254
-
255
- A description of this package.
256
-
257
- """
258
- }
259
- }
260
-
261
- private func writeGitIgnore( ) throws {
262
232
let gitignore = destinationPath. appending ( component: " .gitignore " )
263
233
guard self . fileSystem. exists ( gitignore) == false else {
264
234
return
@@ -281,21 +251,20 @@ public final class InitPackage {
281
251
}
282
252
283
253
private func writeSources( ) throws {
284
- if packageType == . systemModule || packageType == . manifest {
254
+ if packageType == . empty {
285
255
return
286
256
}
257
+
287
258
let sources = destinationPath. appending ( component: " Sources " )
288
259
guard self . fileSystem. exists ( sources) == false else {
289
260
return
290
261
}
291
262
progressReporter ? ( " Creating \( sources. relative ( to: destinationPath) ) / " )
292
263
try makeDirectories ( sources)
293
264
294
- if packageType == . empty {
295
- return
296
- }
297
-
298
- let moduleDir = sources. appending ( component: " \( pkgname) " )
265
+ let moduleDir = packageType == . executable
266
+ ? sources
267
+ : sources. appending ( component: " \( pkgname) " )
299
268
try makeDirectories ( moduleDir)
300
269
301
270
let sourceFileName = " \( typeName) .swift "
@@ -305,27 +274,19 @@ public final class InitPackage {
305
274
switch packageType {
306
275
case . library:
307
276
content = """
308
- public struct \( typeName) {
309
- public private(set) var text = " Hello, World! "
310
-
311
- public init() {
312
- }
313
- }
277
+ // The Swift Programming Language
278
+ // https://docs.swift.org/swift-book
314
279
315
280
"""
316
281
case . executable:
317
282
content = """
318
- @main
319
- struct \( typeName) {
320
- private(set) var text = " Hello, World! "
283
+ // The Swift Programming Language
284
+ // https://docs.swift.org/swift-book
321
285
322
- static func main() {
323
- print( \( typeName) ().text)
324
- }
325
- }
286
+ print( " Hello, world! " )
326
287
327
288
"""
328
- case . systemModule , . empty, . manifest , . `extension`:
289
+ case . empty, . `extension`:
329
290
throw InternalError ( " invalid packageType \( packageType) " )
330
291
}
331
292
@@ -334,43 +295,18 @@ public final class InitPackage {
334
295
}
335
296
}
336
297
337
- private func writeModuleMap( ) throws {
338
- if packageType != . systemModule {
339
- return
340
- }
341
- let modulemap = destinationPath. appending ( component: " module.modulemap " )
342
- guard self . fileSystem. exists ( modulemap) == false else {
343
- return
344
- }
345
-
346
- try writePackageFile ( modulemap) { stream in
347
- stream <<< """
348
- module \( moduleName) [system] {
349
- header " /usr/include/ \( moduleName) .h "
350
- link " \( moduleName) "
351
- export *
352
- }
353
-
354
- """
355
- }
356
- }
357
-
358
298
private func writeTests( ) throws {
359
- if packageType == . systemModule {
360
- return
299
+ switch packageType {
300
+ case . empty, . executable, . `extension`: return
301
+ default : break
361
302
}
362
303
let tests = destinationPath. appending ( component: " Tests " )
363
304
guard self . fileSystem. exists ( tests) == false else {
364
305
return
365
306
}
366
307
progressReporter ? ( " Creating \( tests. relative ( to: destinationPath) ) / " )
367
308
try makeDirectories ( tests)
368
-
369
- switch packageType {
370
- case . systemModule, . empty, . manifest, . `extension`: break
371
- case . library, . executable:
372
- try writeTestFileStubs ( testsPath: tests)
373
- }
309
+ try writeTestFileStubs ( testsPath: tests)
374
310
}
375
311
376
312
private func writeLibraryTestsFile( _ path: AbsolutePath ) throws {
@@ -381,29 +317,11 @@ public final class InitPackage {
381
317
382
318
final class \( moduleName) Tests: XCTestCase {
383
319
func testExample() throws {
384
- // This is an example of a functional test case.
385
- // Use XCTAssert and related functions to verify your tests produce the correct
386
- // results.
387
- XCTAssertEqual( \( typeName) ().text, " Hello, World! " )
388
- }
389
- }
390
-
391
- """
392
- }
393
- }
320
+ // XCTest Documenation
321
+ // https://developer.apple.com/documentation/xctest
394
322
395
- private func writeExecutableTestsFile( _ path: AbsolutePath ) throws {
396
- try writePackageFile ( path) { stream in
397
- stream <<< """
398
- import XCTest
399
- @testable import \( moduleName)
400
-
401
- final class \( moduleName) Tests: XCTestCase {
402
- func testExample() throws {
403
- // This is an example of a functional test case.
404
- // Use XCTAssert and related functions to verify your tests produce the correct
405
- // results.
406
- XCTAssertEqual( \( typeName) ().text, " Hello, World! " )
323
+ // Defining Test Cases and Test Methods
324
+ // https://developer.apple.com/documentation/xctest/defining_test_cases_and_test_methods
407
325
}
408
326
}
409
327
@@ -418,11 +336,9 @@ public final class InitPackage {
418
336
419
337
let testClassFile = try AbsolutePath ( validating: " \( moduleName) Tests.swift " , relativeTo: testModule)
420
338
switch packageType {
421
- case . systemModule , . empty, . manifest , . `extension`: break
339
+ case . empty, . `extension`, . executable : break
422
340
case . library:
423
341
try writeLibraryTestsFile ( testClassFile)
424
- case . executable:
425
- try writeExecutableTestsFile ( testClassFile)
426
342
}
427
343
}
428
344
}
0 commit comments