iOS项目 project.pbxproj文件UUID批量修改
2023-03-29 本文已影响0人
Lcc不想混_b503
package main
import (
"fmt"
"io/ioutil"
"math/rand"
"regexp"
"strings"
"time"
)
func scanStrings(filePath string) ([]string, error) {
// 读取文件内容
content, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
// 使用正则表达式匹配字符串
pattern := regexp.MustCompile(`[0-9A-Z]{24}`)
matches := pattern.FindAllString(string(content), -1)
return matches, nil
}
func removeDuplicates(slice []string) []string {
// 使用 map 来记录已经出现过的字符串
seen := make(map[string]bool)
result := []string{}
for _, s := range slice {
if _, ok := seen[s]; !ok {
seen[s] = true
result = append(result, s)
}
}
return result
}
func replaceUUIDs(filePath string, uuids []string) error {
// 读取文件内容
content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
// 为了防止重复,使用 map 来记录替换过的字符串
replaced := make(map[string]bool)
// 循环替换所有的 UUID
for _, uuid := range uuids {
if _, ok := replaced[uuid]; ok {
continue // 已经替换过了
}
// 随机生成一个 24 位字符串来替换 UUID
randStr := randString(24)
// 替换 UUID
content = []byte(strings.ReplaceAll(string(content), uuid, randStr))
// 记录替换过的 UUID
replaced[uuid] = true
}
// 将修改后的内容写回文件
err = ioutil.WriteFile(filePath, content, 0644)
if err != nil {
return err
}
return nil
}
func randString(n int) string {
rand.Seed(time.Now().UnixNano())
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return strings.ToUpper(string(b))
}
func main() {
filePath := "Runner.xcodeproj/project.pbxproj"
uuids, err := scanStrings(filePath)
if err != nil {
fmt.Println(err)
return
}
uuids = removeDuplicates(uuids) // 去重
err = replaceUUIDs(filePath, uuids)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("UUIDs replaced successfully.")
}