@@ -312,31 +312,36 @@ extension SwiftPackageTool {
312
312
throw InternalError ( " Could not find config path " )
313
313
}
314
314
315
+ let templatePath : AbsolutePath
315
316
if let path = try ? AbsolutePath ( validating: url) {
316
- if localFileSystem. exists ( path) {
317
- let currentTemplateLocation = path
318
- let templateDirectory = configPath. appending ( components: " templates " , " new-package " , url)
319
-
320
- guard localFileSystem. exists ( currentTemplateLocation) && localFileSystem. isDirectory ( currentTemplateLocation) else {
321
- throw StringError ( " Could not find template: \( url) " )
322
- }
323
-
324
- try localFileSystem. copy ( from: currentTemplateLocation, to: templateDirectory)
317
+ guard localFileSystem. exists ( path) else {
318
+ throw StringError ( " Could not find template: \( path) " )
319
+ }
320
+
321
+ guard localFileSystem. isDirectory ( path) else {
322
+ throw StringError ( " \( path) is not a valid directory " )
325
323
}
324
+
325
+ templatePath = configPath. appending ( components: " templates " , " new-package " , path. basename)
326
+ try localFileSystem. copy ( from: path, to: templatePath)
326
327
} else {
327
- let templatePath : AbsolutePath
328
328
let provider = GitRepositoryProvider ( )
329
329
330
330
if let templateName = name {
331
331
templatePath = configPath. appending ( components: " templates " , " new-package " , templateName)
332
- } else if let templateName = url. split ( separator: " / " ) . last {
333
- templatePath = configPath. appending ( components: " templates " , " new-package " , String ( templateName. replacingOccurrences ( of : " .git " , with : " " ) ) )
332
+ } else if let templateName = url. split ( separator: " / " ) . last? . replacingOccurrences ( of : " .git " , with : " " ) . flatMap ( { String ( $0 ) } ) {
333
+ templatePath = configPath. appending ( components: " templates " , " new-package " , String ( templateName) )
334
334
} else {
335
335
throw InternalError ( " Could not determine template name " )
336
336
}
337
337
338
338
try provider. fetch ( repository: RepositorySpecifier ( url: url) , to: templatePath, mirror: false )
339
339
}
340
+
341
+ guard localFileSystem. exists ( templatePath. appending ( component: " Package.swift " ) ) else {
342
+ try localFileSystem. removeFileTree ( templatePath)
343
+ throw StringError ( " \( templatePath) is not a valid template directory, missing 'Package.swift' " )
344
+ }
340
345
}
341
346
}
342
347
@@ -362,11 +367,12 @@ extension SwiftPackageTool {
362
367
let provider = GitRepositoryProvider ( )
363
368
let templatePath = configPath. appending ( components: " templates " , " new-package " , templateName)
364
369
365
- if provider. isValidDirectory ( templatePath. pathString) {
366
- try provider. pull ( templatePath. pathString)
367
- } else {
370
+ guard provider. isValidDirectory ( templatePath. pathString) else {
368
371
throw StringError ( " Template: \( templateName) is not a git repo, and therefore could not be updated " )
369
372
}
373
+
374
+ try provider. pull ( templatePath. pathString)
375
+
370
376
}
371
377
}
372
378
0 commit comments