Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

fix: swift 5.x support for script #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 62 additions & 25 deletions install.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,88 @@

import Foundation

let templateName = "Module VIPER.xctemplate"
let destinationRelativePath = "/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application"
struct Constants {

func printInConsole(_ message:Any){
print("====================================")
struct CommandLineValues {
static let yes = "YES"
static let no = "NO"
}
struct File {
static let templateName = "Module VIPER.xctemplate"
static let destinationRelativePath = "/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application"
}

struct Messages {
static let successMessage = "✅ Template was installed succesfully 🎉. Enjoy it 🙂"
static let successfullReplaceMessage = "✅ The Template has been replaced for you with the new version 🎉. Enjoy it 🙂"
static let errorMessage = "❌ Ooops! Something went wrong 😡"
static let exitMessage = "Bye Bye 👋"
static let promptReplace = "That Template already exists. Do you want to replace it? (YES or NO)"
}

struct Blocks {
static let printSeparator = { print("====================================") }
}
}


func printToConsole(_ message:Any){
Constants.Blocks.printSeparator()
print("\(message)")
print("====================================")
Constants.Blocks.printSeparator()
}

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 🙂")
let fileManager = FileManager.default
let destinationPath = bash(command: "xcode-select", arguments: ["--print-path"]).appending(Constants.File.destinationRelativePath)
printToConsole("Template will be copied to: \(destinationPath)")
if !fileManager.fileExists(atPath: "\(destinationPath)/\(Constants.File.templateName)"){
try fileManager.copyItem(atPath: Constants.File.templateName, toPath: "\(destinationPath)/\(Constants.File.templateName)")
printToConsole(Constants.Messages.successMessage)

} else {
print(Constants.Messages.promptReplace)
var input = ""
repeat {
guard let textFormCommandLine = readLine(strippingNewline: true) else {
continue
}
input = textFormCommandLine.uppercased()
} while(input != Constants.CommandLineValues.yes && input != Constants.CommandLineValues.no)

if input == Constants.CommandLineValues.yes {
try replaceItemAt(URL(fileURLWithPath: "\(destinationPath)/\(Constants.File.templateName)"), withItemAt: URL(fileURLWithPath: Constants.File.templateName))
printToConsole(Constants.Messages.successfullReplaceMessage)
} else {
print(Constants.Messages.exitMessage)
}
}
}

catch let error as NSError {
printInConsole("❌ Ooops! Something went wrong 😡 : \(error.localizedFailureReason!)")
printToConsole("\(Constants.Messages.errorMessage) : \(error.localizedFailureReason!)")
}
}

func shell(launchPath: String, arguments: [String]) -> String
{
func replaceItemAt(_ url: URL, withItemAt itemAtUrl: URL) throws {
let fileManager = FileManager.default
try fileManager.removeItem(at: url)
try fileManager.copyItem(atPath: itemAtUrl.path, toPath: url.path)
}

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.characters.count > 0 {
if output.count > 0 {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return String(output[output.startIndex ..< lastIndex])
Expand Down