Skip to content

Commit 01967cc

Browse files
author
Miguel Perez
committed
Removed getSwiftPMDefaultTemplate
- Fixed some typos - add-template is now more robust when determining if we have a path or web url - rough implementation of update-template
1 parent 667022a commit 01967cc

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extension SwiftPackageTool {
7777
if let index = subCommands.firstIndex(where: { $0 == Init.self }) {
7878
subCommands.insert(Create.self, at: index + 1)
7979
subCommands.insert(AddTemplate.self, at: index + 2)
80+
subCommands.insert(UpdateTemplate.self, at: index + 3)
8081
}
8182
}
8283

@@ -223,7 +224,7 @@ extension SwiftPackageTool {
223224

224225
func run(_ swiftTool: SwiftTool) throws {
225226
guard let cwd = localFileSystem.currentWorkingDirectory else {
226-
throw InternalError("Could not find the current working directroy.")
227+
throw InternalError("Could not find the current working directory.")
227228
}
228229

229230
guard let configPath = try swiftTool.getConfigPath() else {
@@ -267,7 +268,7 @@ extension SwiftPackageTool {
267268

268269
func run(_ swiftTool: SwiftTool) throws {
269270
guard let cwd = localFileSystem.currentWorkingDirectory else {
270-
throw InternalError("Could not find the current working directroy.")
271+
throw InternalError("Could not find the current working directory.")
271272
}
272273

273274
guard let configPath = try swiftTool.getConfigPath() else {
@@ -315,15 +316,17 @@ extension SwiftPackageTool {
315316
throw InternalError("Could not get the current working directory")
316317
}
317318

318-
if localFileSystem.exists(AbsolutePath(url)) {
319-
let currentTemplateLocation = AbsolutePath(url)
320-
let templateDirectory = configPath.appending(components: "templates", "new-package", url)
321-
322-
guard localFileSystem.exists(currentTemplateLocation) && localFileSystem.isDirectory(currentTemplateLocation) else {
323-
throw InternalError("Template folder \(url) could not be found in: \(cwd)")
319+
if let path = try? AbsolutePath(validating: url) {
320+
if localFileSystem.exists(path) {
321+
let currentTemplateLocation = path
322+
let templateDirectory = configPath.appending(components: "templates", "new-package", url)
323+
324+
guard localFileSystem.exists(currentTemplateLocation) && localFileSystem.isDirectory(currentTemplateLocation) else {
325+
throw InternalError("Template folder \(url) could not be found in: \(cwd)")
326+
}
327+
328+
try localFileSystem.copy(from: currentTemplateLocation, to: templateDirectory)
324329
}
325-
326-
try localFileSystem.copy(from: currentTemplateLocation, to: templateDirectory)
327330
} else {
328331
let templatePath: AbsolutePath
329332
let provider = GitRepositoryProvider()
@@ -341,6 +344,32 @@ extension SwiftPackageTool {
341344
}
342345
}
343346

347+
struct UpdateTemplate: SwiftCommand {
348+
static let configuration = CommandConfiguration(
349+
abstract: "Update template from remote")
350+
351+
@OptionGroup(_hiddenFromHelp: true)
352+
var swiftOptions: SwiftToolOptions
353+
354+
@Argument(help: "Template to update")
355+
var templateName: String
356+
357+
func run(_ swiftTool: SwiftTool) throws {
358+
guard let configPath = try swiftTool.getConfigPath() else {
359+
throw InternalError("Could not find config path")
360+
}
361+
362+
guard localFileSystem.exists(configPath.appending(components: "templates", "new-package", templateName)) else {
363+
throw InternalError("Could not find \(templateName)")
364+
}
365+
366+
let provider = GitRepositoryProvider()
367+
let templatePath = configPath.appending(components: "templates", "new-package", templateName)
368+
369+
try provider.pull(repository: RepositorySpecifier(url: templatePath.pathString), to: templatePath)
370+
}
371+
}
372+
344373
struct Format: SwiftCommand {
345374
static let configuration = CommandConfiguration(
346375
commandName: "_format")

Sources/SourceControl/GitRepository.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ public struct GitRepositoryProvider: RepositoryProvider {
107107
try self.fetch(repository: repository, to: path, mirror: true)
108108
}
109109

110+
public func pull(repository: RepositorySpecifier, to path: AbsolutePath) throws {
111+
try self.callGit("pull", "-C", path.pathString, repository: repository)
112+
}
113+
110114
public func isValidDirectory(_ directory: String) -> Bool {
111115
// Provides better feedback when mistakingly using url: for a dependency that
112116
// is a local package. Still allows for using url with a local package that has

Sources/Workspace/InitPackage.swift

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ public final class InitPackage {
148148
name: packageName)
149149
}
150150
} else {
151-
let packageTemplate = getSwiftPMDefaultTemplate(packageType: packageType)
151+
let packageTemplate = PackageTemplate(sourcesDirectory: RelativePath("./Sources"),
152+
testsDirectory: nil,
153+
createSubDirectoryForModule: false,
154+
packageType: packageType)
155+
152156
try writePackageStructure(template: packageTemplate)
153157
}
154158
}
@@ -161,26 +165,6 @@ public final class InitPackage {
161165

162166
try writePackageStructure(template: template)
163167
}
164-
165-
private func getSwiftPMDefaultTemplate(packageType: InitPackage.PackageType,
166-
sources: RelativePath = .init("./Sources"),
167-
tests: RelativePath? = nil,
168-
createSubDirectoryForModule: Bool = false) -> InitPackage.PackageTemplate {
169-
// Even if we are making a "classic" package that doesn't use a template we should till use templates
170-
// for consistency within the codebase
171-
switch InitPackage.createPackageMode {
172-
case .new:
173-
return InitPackage.PackageTemplate(sourcesDirectory: sources,
174-
testsDirectory: tests,
175-
createSubDirectoryForModule: createSubDirectoryForModule,
176-
packageType: packageType)
177-
case .legacy:
178-
return InitPackage.PackageTemplate(sourcesDirectory: RelativePath("./Sources"),
179-
testsDirectory: RelativePath("./Tests"),
180-
createSubDirectoryForModule: true,
181-
packageType: packageType)
182-
}
183-
}
184168

185169
private func copyTemplate(fileSystem: FileSystem, sourcePath: AbsolutePath, destinationPath: AbsolutePath, name: String) throws {
186170
// Recursively copy the template package

0 commit comments

Comments
 (0)