1、环境切换,配置文件修改
#!/usr/bin/env swift
import Foundation
/*
使用
./changeEnv.swift dev
./changeEnv.swift pro
*/
/// 检查参数
func checkParameter() -> Bool {
guard CommandLine.arguments.count > 1 else {
print("要传入参数 dev 或 pro")
return false
}
return true
}
/// 读取文件
/// - Parameters:
/// - filePath: 要读取的文件的位置
/// - saveToPath: 保存在哪个位置
/// - edit: 处理文件字符串内容
func readFileAtPath(_ filePath: String, saveToPath: String, edit:(_ str: String)->String ) {
do {
// 读取文件为字符串
let str = try String(contentsOfFile: filePath, encoding: .utf8)
// 把读取的字符串传出去,让外界查找替换,再返回处理好的字符串
let lastStr = edit(str)
// 准备写入到文件
do {
// 写入到某个位置
try Data(lastStr.utf8).write(to: URL(fileURLWithPath: saveToPath), options: .atomic)
} catch {
print(error)
}
} catch {
print(error)
}
}
func changeEnvToDev(){
guard checkParameter() else {
return
}
let isDev = (CommandLine.arguments[1] == "dev")
// 修改base url
let baseUrlFilePath = "./SyServerManager.h"
readFileAtPath(baseUrlFilePath, saveToPath: baseUrlFilePath) { str in
var newStr = ""
if isDev {
newStr = str.replacingOccurrences(of: "生产环境字符串", with: "测试环境字符串")
} else {
newStr = str.replacingOccurrences(of: "测试环境字符串", with: "生产环境字符串")
}
return newStr
}
// 修改plist文件配置
let plistFilePath = "./customization.plist"
readFileAtPath(plistFilePath, saveToPath: plistFilePath) { str in
var newStr = ""
if isDev {
newStr = str.replacingOccurrences(of: "<key>AppVersion</key>\n\t\t<string>166</string>", with: "<key>AppVersion</key>\n\t\t<string>999</string>")
} else {
newStr = str.replacingOccurrences(of: "<key>AppVersion</key>\n\t\t<string>999</string>", with: "<key>AppVersion</key>\n\t\t<string>166</string>")
}
return newStr
}
}
changeEnvToDev()
2、生成模板页面
//调用 ./generateListPage.swift Certifica
// 以下是文件内容
#!/usr/bin/env swift
import Foundation
/// 检查参数
func checkParameter() -> Bool {
guard CommandLine.arguments.count > 1 else {
print("要传入类名参数")
return false
}
return true
}
/// 读取文件
/// - Parameters:
/// - filePath: 要读取的文件的位置
/// - saveToPath: 保存在哪个位置
/// - edit: 处理文件字符串内容
func readFileAtPath(_ filePath: String, saveToPath: String, edit:(_ str: String)->String ) {
do {
// 读取文件为字符串
let str = try String(contentsOfFile: filePath, encoding: .utf8)
// 把读取的字符串传出去,让外界查找替换,再返回处理好的字符串
let lastStr = edit(str)
// 准备写入到文件
do {
// 写入到某个位置
try Data(lastStr.utf8).write(to: URL(fileURLWithPath: saveToPath), options: .atomic)
} catch {
print(error)
}
} catch {
print(error)
}
}
func generate(){
guard checkParameter() else {
return
}
// 去除尾部ListVC
let newClassName = CommandLine.arguments[1].replacingOccurrences(of: "ListVC", with: "")
// vc
let hFilePath = "./VC/TemplateListVC.h"
let mFilePath = "./VC/TemplateListVC.m"
readFileAtPath(hFilePath, saveToPath: "~/Desktop/\(newClassName)ListVC.h") { str in
let newStr = str.replacingOccurrences(of: "TemplateListVC", with: "\(newClassName)ListVC")
return newStr.replacingOccurrences(of: "TemplateListCell", with: "\(newClassName)ListCell")
}
readFileAtPath(mFilePath, saveToPath: "~/Desktop/\(newClassName)ListVC.m") { str in
let newStr = str.replacingOccurrences(of: "TemplateListVC", with: "\(newClassName)ListVC")
return newStr.replacingOccurrences(of: "TemplateListCell", with: "\(newClassName)ListCell")
}
// cell
let cellHFilePath = "./TemplateListCell.h"
let cellMFilePath = "./TemplateListCell.m"
readFileAtPath(cellHFilePath, saveToPath: "~/Desktop/\(newClassName)ListCell.h") { str in
return str.replacingOccurrences(of: "TemplateListCell", with: "\(newClassName)ListCell")
}
readFileAtPath(cellMFilePath, saveToPath: "~/Desktop/\(newClassName)ListCell.m") { str in
return str.replacingOccurrences(of: "TemplateListCell", with: "\(newClassName)ListCell")
}
}
// 调用
generate()