forked from euroboy/RMVIPERModuleGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.swift
executable file
·70 lines (62 loc) · 1.86 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
//
// main.swift
// InstallRMVIPERTemplate
//
import Foundation
import Darwin
let destinationPath = "/Library/Developer/Xcode/Templates"
let fm = FileManager.default
let currentDir = fm.currentDirectoryPath
func getTemplatesPaths() -> [String]? {
do {
let items = try fm.contentsOfDirectory(atPath: currentDir)
return items.filter { $0.contains(".xctemplate") }
} catch {
return nil
}
}
func copyTemplates() {
guard let templatePaths = getTemplatesPaths() else {
showMessage("Something went wrong :(")
return
}
for templatePath in templatePaths {
let source = currentDir + "/" + templatePath
let dest = destinationPath + "/" + templatePath
if !copyFiles(from: source, to: dest) {
showMessage("Something went wrong :(")
return
}
}
showMessage("Templates are installed successfully!")
}
func copyFiles(from source: String, to dest: String) -> Bool {
do {
let filelist = try fm.contentsOfDirectory(atPath: source)
try? fm.copyItem(atPath: source, toPath: dest)
for filename in filelist {
try? fm.copyItem(atPath: "\(source)/\(filename)", toPath: "\(dest)/\(filename)")
}
return true
} catch {
return false
}
}
func createTemplatesDirectoryIfNeeded() {
if !fm.fileExists(atPath: destinationPath) {
do {
try fm.createDirectory(atPath: destinationPath, withIntermediateDirectories: true, attributes: nil)
}
catch {
showMessage("Failed to create Templates directory: \(error)")
exit(0)
}
}
}
func showMessage(_ message: String) {
print("===============================================")
print("\(message)")
print("===============================================")
}
createTemplatesDirectoryIfNeeded()
copyTemplates()