()
| 10 | ) |
| 11 | |
| 12 | func Patch() { |
| 13 | utils.PrintBold("Applying custom patches:") |
| 14 | keys := patchSection.Keys() |
| 15 | |
| 16 | re := regexp.MustCompile(`^([\w\d\-~\.]+)_find_(\d+)$`) |
| 17 | for _, key := range keys { |
| 18 | keyName := key.Name() |
| 19 | matches := re.FindStringSubmatch(keyName) |
| 20 | if len(matches) == 0 { |
| 21 | continue |
| 22 | } |
| 23 | |
| 24 | name := matches[1] |
| 25 | assetPath := filepath.Join(appPath, "xpui", name) |
| 26 | index := matches[2] |
| 27 | |
| 28 | if _, err := os.Stat(assetPath); err != nil { |
| 29 | utils.PrintError("File name \"" + name + "\" is not found.") |
| 30 | continue |
| 31 | } |
| 32 | |
| 33 | replName := name + "_repl_all_" + index |
| 34 | replOnceName := name + "_repl_" + index |
| 35 | replKey, errAll := patchSection.GetKey(replName) |
| 36 | replOnceKey, errOnce := patchSection.GetKey(replOnceName) |
| 37 | |
| 38 | if errAll != nil && errOnce != nil { |
| 39 | utils.PrintError("Cannot find replace string for patch \"" + keyName + "\"") |
| 40 | utils.PrintInfo("Correct key name for replace string are:") |
| 41 | utils.PrintInfo(" \"" + replOnceName + "\"") |
| 42 | utils.PrintInfo(" \"" + replName + "\"") |
| 43 | continue |
| 44 | } |
| 45 | |
| 46 | patchRegexp, errReg := regexp.Compile(key.String()) |
| 47 | if errReg != nil { |
| 48 | utils.PrintError("Cannot compile find RegExp for patch \"" + keyName + "\"") |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | utils.ModifyFile(assetPath, func(content string) string { |
| 53 | // Prioritize replace all |
| 54 | if errAll == nil { |
| 55 | return patchRegexp.ReplaceAllString(content, replKey.MustString("")) |
| 56 | } else { |
| 57 | match := patchRegexp.FindString(content) |
| 58 | if len(match) > 0 { |
| 59 | toReplace := patchRegexp.ReplaceAllString(match, replOnceKey.MustString("")) |
| 60 | content = strings.Replace(content, match, toReplace, 1) |
| 61 | } |
| 62 | return content |
| 63 | } |
| 64 | }) |
| 65 | |
| 66 | utils.PrintSuccess("\"" + keyName + "\" is patched") |
| 67 | } |
| 68 | |
| 69 | utils.PrintSuccess("Applied custom patches") |
no test coverage detected