From f8414dd0c1f3a39545d1efebbc44677c3d160206 Mon Sep 17 00:00:00 2001 From: Josef Dolezal Date: Sun, 26 Aug 2018 23:00:21 +0200 Subject: [PATCH] Create remove command for templates --- Sources/xcman/XCManTemplatesTool.swift | 8 ++++++++ Sources/xcmanlib/TemplatesManager.swift | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/Sources/xcman/XCManTemplatesTool.swift b/Sources/xcman/XCManTemplatesTool.swift index b42ed2f..286fad9 100644 --- a/Sources/xcman/XCManTemplatesTool.swift +++ b/Sources/xcman/XCManTemplatesTool.swift @@ -36,4 +36,12 @@ let templatesCommand = Group { // Print each templates set one by one templates.forEach { print($0) } } + + $0.command("remove", + VariadicArgument("name", description: "Name of the templates set to be deleted"), + description: "Removes installed templates from Xcode" + ) { names in + // Remove all sets listed by user + try names.forEach { try templatesManager.delete(templatesSet: $0) } + } } diff --git a/Sources/xcmanlib/TemplatesManager.swift b/Sources/xcmanlib/TemplatesManager.swift index 5240bc5..4d77c67 100644 --- a/Sources/xcmanlib/TemplatesManager.swift +++ b/Sources/xcmanlib/TemplatesManager.swift @@ -52,4 +52,21 @@ public final class TemplatesManager { // Map to folders name .map { $0.lastPathComponent } } + + public func delete(templatesSet: String) throws { + // URL of templates set being deleted + let url = FileManager.default.homeDirectoryUrl.appendingPathComponent(UserDataType.templates.xcodePath(for: templatesSet), isDirectory: true) + + // Throw an error if directory does not exists + if !FileManager.default.fileExists(atPath: url.path) { + throw UserDataError.couldNotReadRepositoryContent(url) + } + + // Remove the directory + do { + try FileManager.default.removeItem(at: url) + } catch { + throw UserDataError.couldNotRemoveExistingUserData(url, error) + } + } }