-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.swift
96 lines (79 loc) · 3.81 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
91
92
93
94
95
96
//
// install.swift
//
import Foundation
let templateNames = ["Module.xctemplate"]
let homeDirURL = FileManager.default.homeDirectoryForCurrentUser
let rootFileURL = homeDirURL.appendingPathComponent("Library").appendingPathComponent("Developer").appendingPathComponent("Xcode").appendingPathComponent("Templates").appendingPathComponent("File Templates")
enum InstallError: Error {
case sourceDirectoryDoesNotExist(URL)
case sourceTemplateDoesNotExist(URL)
}
func printErrorInConsole(_ message:Any){
print("====================================")
print("\(message)")
print("====================================")
}
func moveTemplates() {
let fileManager = FileManager.default
let currentURL = URL(fileURLWithPath: fileManager.currentDirectoryPath)
print("Attempting to install templates from \(currentURL.path)")
let viperDirectoryName = "VIPER"
// Install iOS templates
do {
let subdirectoryName = "iOS"
let sourceURL = currentURL.appendingPathComponent(subdirectoryName)
let destinationURL = rootFileURL.appendingPathComponent(subdirectoryName).appendingPathComponent(viperDirectoryName)
let templateNames = ["Module.xctemplate", "Unit Tests.xctemplate"]
print("-- iOS templates --")
try moveTemplates(for: templateNames, at: sourceURL, to: destinationURL)
}
catch let error {
printErrorInConsole("❌ Ooops! Something went wrong installing iOS templates 😡 : \(error)")
exit(1)
}
// Install MacOSX templates
do {
let subdirectoryName = "MacOSX"
let sourceURL = currentURL.appendingPathComponent(subdirectoryName)
let destinationURL = rootFileURL.appendingPathComponent(subdirectoryName).appendingPathComponent(viperDirectoryName)
let templateNames = ["Module.xctemplate", "Unit Tests.xctemplate"]
print("-- MacOSX templates --")
try moveTemplates(for: templateNames, at: sourceURL, to: destinationURL)
}
catch let error {
printErrorInConsole("❌ Ooops! Something went wrong installing MacOSX templates 😡 : \(error)")
exit(1)
}
}
func moveTemplates(for templateNames: [String], at sourceBaseURL: URL, to destinationBaseURL: URL) throws {
let fileManager = FileManager.default
// Verify that the source directory exists
if !fileManager.fileExists(atPath: sourceBaseURL.path) {
throw InstallError.sourceDirectoryDoesNotExist(sourceBaseURL)
}
// Create the destination directory (if needed)
if !fileManager.fileExists(atPath: destinationBaseURL.path) {
try fileManager.createDirectory(at: destinationBaseURL, withIntermediateDirectories: true)
print("✅ \(destinationBaseURL.lastPathComponent) Template directory created successfully")
}
// Copy/replace each template
for templateName in templateNames {
let sourceURL = sourceBaseURL.appendingPathComponent(templateName)
let destinationURL = destinationBaseURL.appendingPathComponent(templateName)
// Verify that the source URL exists
if !fileManager.fileExists(atPath: sourceURL.path) {
throw InstallError.sourceTemplateDoesNotExist(sourceURL)
}
// Copy the template (if it doesn't already exist)
if !fileManager.fileExists(atPath: destinationURL.path) {
try fileManager.copyItem(at: sourceURL, to: destinationURL)
print("✅ \(sourceURL.deletingPathExtension().lastPathComponent) Template installed succesfully 🎉.")
} else {
try fileManager.removeItem(at: destinationURL)
try fileManager.copyItem(at: sourceURL, to: destinationURL)
print("✅ \(sourceURL.deletingPathExtension().lastPathComponent) Template already exists. So has been replaced succesfully 🎉.")
}
}
}
moveTemplates()