Skip to content

Instantly share code, notes, and snippets.

@htq287
Created August 29, 2019 09:59
Show Gist options
  • Save htq287/20cc5ed98d334647998879114392fa6f to your computer and use it in GitHub Desktop.
Save htq287/20cc5ed98d334647998879114392fa6f to your computer and use it in GitHub Desktop.
Script to rename files and replace text, contained in a folder
#!/usr/bin/env xcrun swift
import Foundation
let currentText = ""
var newText = ""
let fileManager = FileManager.default
let runScriptPathURL = NSURL(fileURLWithPath: fileManager.currentDirectoryPath, isDirectory: true)
let currentScriptPathURL = NSURL(fileURLWithPath: NSURL(fileURLWithPath: CommandLine.arguments[0], relativeTo: runScriptPathURL as URL).deletingLastPathComponent!.path, isDirectory: true)
let projectTemplateForlderURL = NSURL(fileURLWithPath: "Template", relativeTo: currentScriptPathURL as URL)
let ignoredFiles = [".DS_Store", "UserInterfaceState.xcuserstate"]
extension NSURL {
var fileName: String {
var fileName: AnyObject?
try! getResourceValue(&fileName, forKey: URLResourceKey.nameKey)
return fileName as! String
}
var isDirectory: Bool {
var isDirectory: AnyObject?
try! getResourceValue(&isDirectory, forKey: URLResourceKey.isDirectoryKey)
return isDirectory as! Bool
}
func renameFile(_ currentFileName: String, _ newFileName: String) {
if let _ = fileName.range(of: currentFileName) {
let renamedFileName = fileName.replacingOccurrences(of: currentFileName, with: newFileName)
try! FileManager.default.moveItem(at: self as URL, to: NSURL(fileURLWithPath: renamedFileName, relativeTo: deletingLastPathComponent) as URL)
}
}
func replaceText(_ currentText: String, _ newText: String) {
guard let path = path, let content = try? String(contentsOfFile: path, encoding: String.Encoding.utf8) else {
print("ERROR READING: \(self)")
return
}
var newContent = content.replacingOccurrences(of: currentText, with: newText)
try! newContent.write(to: self as URL, atomically: true, encoding: String.Encoding.utf8)
}
}
func printLogs<T>(_ message: T) {
print("Logs: \(message)")
}
func printErrorLogsAndExit<T>(logs message: T) {
print("ErrorLogs: \(message)")
exit(1)
}
func shell(args: String...) -> (output: String, exitCode: Int32) {
print("Get in Shell ...")
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.currentDirectoryPath = ""
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(decoding: data, as: UTF8.self)
//let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as? String ?? ""
return (output, task.terminationStatus)
}
// rename files and update content
let enumerator = fileManager.enumerator(at: projectTemplateForlderURL as URL, includingPropertiesForKeys: [.nameKey, .isDirectoryKey], options: [], errorHandler: nil)!
var directories = [NSURL]()
while let fileURL = enumerator.nextObject() as? NSURL {
guard !ignoredFiles.contains(fileURL.fileName) else { continue }
if fileURL.isDirectory {
directories.append(fileURL)
}
else {
fileURL.replaceText(currentText, newText)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment