From 0e044bae1faf9175edffcc80c4b6c504ab3832bd Mon Sep 17 00:00:00 2001 From: Pavlo Kapinos Date: Fri, 3 Jul 2020 11:57:33 +0300 Subject: [PATCH] fix: swift 5.x support for script --- install.swift | 87 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 62 insertions(+), 25 deletions(-) diff --git a/install.swift b/install.swift index d095874..d5e2d37 100644 --- a/install.swift +++ b/install.swift @@ -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])