-
Notifications
You must be signed in to change notification settings - Fork 10
/
install.swift
executable file
·93 lines (65 loc) · 3.59 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
// Created by Fauzi Sholichin on 23/02/2019.
// Copyright © 2019 Fauzi Sholichin. All rights reserved.
//
import Foundation
let templateMVP = "MVP Kit.xctemplate"
let templateMVVM = "MVVM Kit.xctemplate"
let templateVIPER = "VIPER Kit.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)/\(templateMVP)"){
try fileManager.copyItem(atPath: templateMVP, toPath: "\(destinationPath)/\(templateMVP)")
printInConsole("✅ Architecture Kit MVP installed succesfully 🎉. Enjoy it 🙂")
}else{
try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\(templateMVP)"), withItemAt: URL(fileURLWithPath:templateMVP))
printInConsole("✅ Architecture Kit MVP already exists. So has been replaced succesfully 🎉. Enjoy it 🙂")
}
if !fileManager.fileExists(atPath:"\(destinationPath)/\(templateMVVM)"){
try fileManager.copyItem(atPath: templateMVVM, toPath: "\(destinationPath)/\(templateMVVM)")
printInConsole("✅ Architecture Kit MVVM installed succesfully 🎉. Enjoy it 🙂")
}else{
try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\(templateMVVM)"), withItemAt: URL(fileURLWithPath:templateMVVM))
printInConsole("✅ Architecture Kit MVVM already exists. So has been replaced succesfully 🎉. Enjoy it 🙂")
}
if !fileManager.fileExists(atPath:"\(destinationPath)/\(templateVIPER)"){
try fileManager.copyItem(atPath: templateVIPER, toPath: "\(destinationPath)/\(templateVIPER)")
printInConsole("✅ Architecture Kit VIPER installed succesfully 🎉. Enjoy it 🙂")
}else{
try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\(templateVIPER)"), withItemAt: URL(fileURLWithPath:templateVIPER))
printInConsole("✅ Architecture Kit VIPER 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.count > 0 {
//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()