forked from suyati/Mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmold.swift
57 lines (42 loc) · 1.79 KB
/
mold.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
// Created by Rijo George.
// Copyright © 2017 Rijo George. All rights reserved.
//
import Foundation
let destUrl = "/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application/"
func copyTemplate(){
let fileManager = FileManager.default
let destinationPath = bash(command: "xcode-select", arguments: ["--print-path"]).appending(destUrl)
do {
if !fileManager.fileExists(atPath:"\(destinationPath)/\("Mold")"){
try fileManager.copyItem(atPath: "Mold", toPath: "\(destinationPath)/\("Mold")")
print("Viper template is installed. You can use it from File -> New File")
}else{
try _ = fileManager.replaceItemAt(URL(fileURLWithPath:"\(destinationPath)/\("Mold")"), withItemAt: URL(fileURLWithPath:"Mold"))
print("Viper template updated.")
}
}
catch let error as NSError {
print("Error: \(error.localizedFailureReason!)")
}
}
func shell(launchPath: String, arguments: [String]) -> String
{
let process = Process()
process.launchPath = launchPath
process.arguments = arguments
let handle = Pipe()
process.standardOutput = handle
process.launch()
let data = handle.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)!
if output.characters.count > 0 {
let lastIndex = output.index(before: output.endIndex)
return output[output.startIndex ..< lastIndex]
}
return output
}
func bash(command: String, arguments: [String]) -> String {
let command = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ])
return shell(launchPath: command, arguments: arguments)
}
copyTemplate()