-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.swift
60 lines (51 loc) · 2.18 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
//
// main.swift
// InstallTemplate
//
// Created by Dmytro Chumakov.
// Copyright © 2023 Dmytro Chumakov. All rights reserved.
//
import Foundation
let templateName = "Module TCA.xctemplate"
let destinationRelativePath = "/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application"
func printInConsole(_ message:Any) {
print("====================================")
print("\(message)")
print("====================================")
}
func moveTemplate() {
let fileManager = FileManager.default
let destinationPath = bash(command: "xcode-select", arguments: ["--print-path"]).appending(destinationRelativePath)
do {
if !fileManager.fileExists(atPath:"\(destinationPath)/\(templateName)") {
try fileManager.copyItem(atPath: templateName, toPath: "\(destinationPath)/\(templateName)")
printInConsole("✅ Template installed succesfully 🎉. Enjoy it 🙂")
} else {
try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\(templateName)"), withItemAt: URL(fileURLWithPath:templateName))
printInConsole("✅ Template already exists. So has been replaced succesfully 🎉. Enjoy it 🙂")
}
} catch let error as NSError {
printInConsole("❌ Ooops! Something went wrong 😡 : \(error.localizedFailureReason!)")
}
}
func shell(launchPath: String, arguments: [String]) -> String {
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)!
if !output.isEmpty {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return String(output[output.startIndex ..< lastIndex])
}
return output
}
func bash(command: String, arguments: [String]) -> String {
let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ])
return shell(launchPath: whichPathForCommand, arguments: arguments)
}
moveTemplate()