-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.swift
90 lines (78 loc) · 2.89 KB
/
install.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// main.swift
// viper
//
// Created by Sergio García on 22/02/2020.
// Copyright © 2020 Sergio García. All rights reserved.
//
//#!/usr/bin/swift
import Foundation
extension URL {
private static let home = FileManager.default.homeDirectoryForCurrentUser
static let templates = home.appendingPathComponent("Library/Developer/Xcode/Templates")
static let output = templates.appendingPathComponent("GrendowDev")
}
class VIPERFileManager: FileManager {
enum FileManagerError: Error, LocalizedError {
case template
case remove
case install
var errorDescription: String? {
switch self {
case .template: return "Error preparing default template directory."
case .remove: return "Error removing current VIPER template directory"
case .install: return "Error installing VIPER templates."
}
}
}
private var templatesExists: Bool { return fileExists(atPath: URL.templates.path) }
private var exists: Bool { fileExists(atPath: URL.output.path) }
func createTemplatesDirectoryIfNeeded() throws {
guard !templatesExists else { return }
do {
debugPrint("INFO: Creating Xcode Templates directory..")
try createDirectory(at: URL.templates, withIntermediateDirectories: true, attributes: nil)
} catch {
throw FileManagerError.template
}
}
private func removeCurrents() throws {
do {
debugPrint("WARN: Removing current templates")
try removeItem(at: URL.output)
} catch {
throw FileManagerError.remove
}
}
func installTemplates() throws {
if exists {
try removeCurrents()
}
do {
debugPrint("INFO: Installing Template")
let inheritance = URL(fileURLWithPath: currentDirectoryPath, isDirectory: true)
.appendingPathComponent("Inheritance")
let protocols = URL(fileURLWithPath: currentDirectoryPath, isDirectory: true)
.appendingPathComponent("Protocols")
try createDirectory(at: URL.output, withIntermediateDirectories: true, attributes: nil)
try copyContent(at: inheritance, to: URL.output)
try copyContent(at: protocols, to: URL.output)
} catch {
debugPrint(error)
throw FileManagerError.install
}
}
private func copyContent(at: URL, to: URL) throws {
let content = try contentsOfDirectory(at: at, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
for item in content {
try copyItem(at: item, to: to.appendingPathComponent(item.lastPathComponent))
}
}
}
let fileManager = VIPERFileManager()
do {
try fileManager.createTemplatesDirectoryIfNeeded()
try fileManager.installTemplates()
} catch {
print(error.localizedDescription)
}