added replace-all patch type
This commit is contained in:
parent
bb7da3e154
commit
3d08b492ac
5 changed files with 134 additions and 25 deletions
68
README.md
68
README.md
|
@ -63,6 +63,7 @@ Here are the details of each parameter used in the patching system:
|
|||
- Defines the type of patch being applied. This parameter can be skipped if not applicable.
|
||||
- Common types:
|
||||
1. **`standard`:** Standard patching type that follows the `+` (add) and `-` (remove) syntax.
|
||||
|
||||
- Used for general file modifications.
|
||||
- **Example:**
|
||||
```patch
|
||||
|
@ -72,9 +73,10 @@ Here are the details of each parameter used in the patching system:
|
|||
-MOZ_APP_DISPLAYNAME=Firefox
|
||||
+MOZ_APP_DISPLAYNAME=Spitfire
|
||||
```
|
||||
Note: *Type will fallback to t:standard when no type is specified.*
|
||||
|
||||
Note: *Type will fallback to t:standard when no type is specified.*
|
||||
2. **`pref`:** Indicates that the patch is modifying preference settings (e.g., Firefox `prefs.js`).
|
||||
|
||||
- When `t:pref` is specified, the script will search for existing preferences in the input file and replace them with the new ones provided in the patch. If a preference is not found, it will be added to the end of the file.
|
||||
- **Example:**
|
||||
```patch
|
||||
|
@ -85,6 +87,7 @@ Here are the details of each parameter used in the patching system:
|
|||
+pref("extensions.getAddons.showPane", false); // HIDDEN
|
||||
```
|
||||
3. **`new`:** Used for creating new files or overwriting existing files with specified content.
|
||||
|
||||
- Creates a new file at the `o:` location.
|
||||
- **Example:**
|
||||
```patch
|
||||
|
@ -94,6 +97,7 @@ Here are the details of each parameter used in the patching system:
|
|||
+This is a new file created by Spitfire Patcher.
|
||||
```
|
||||
4. **`copy`:**: Copies files or directories from the input path to the output path, overwriting if necessary.
|
||||
|
||||
- Copies the contents of a file or directory from i: to o:.
|
||||
- **Example:**
|
||||
```patch
|
||||
|
@ -102,21 +106,38 @@ Here are the details of each parameter used in the patching system:
|
|||
o:/browser/branding/official
|
||||
```
|
||||
5. **`marker`:** Modifies a file relative to a specific marker line.
|
||||
- A line starting with # defines the marker.
|
||||
- Lines starting with + are inserted immediately after the marker.
|
||||
- Lines starting with - are removed from the file (after the marker).
|
||||
- **Example:**
|
||||
```patch
|
||||
t:marker
|
||||
i:/browser/locales/en-US/firefox-l10n.js
|
||||
o:/browser/locales/en-US/firefox-l10n.js
|
||||
|
||||
#static const RedirEntry kRedirMap[] =
|
||||
+ {"store", "http://localhost:20351/",
|
||||
+ nsIAboutModule::ALLOW_SCRIPT |
|
||||
+ nsIAboutModule::URI_MUST_LOAD_IN_CHILD |
|
||||
+ nsIAboutModule::URI_CAN_LOAD_IN_PRIVILEGEDABOUT_PROCESS},
|
||||
```
|
||||
- A line starting with # defines the marker.
|
||||
- Lines starting with + are inserted immediately after the marker.
|
||||
- Lines starting with - are removed from the file (after the marker).
|
||||
- **Example:**
|
||||
|
||||
```patch
|
||||
t:marker
|
||||
i:/browser/locales/en-US/firefox-l10n.js
|
||||
o:/browser/locales/en-US/firefox-l10n.js
|
||||
|
||||
#static const RedirEntry kRedirMap[] =
|
||||
+ {"store", "http://localhost:20351/",
|
||||
+ nsIAboutModule::ALLOW_SCRIPT |
|
||||
+ nsIAboutModule::URI_MUST_LOAD_IN_CHILD |
|
||||
+ nsIAboutModule::URI_CAN_LOAD_IN_PRIVILEGEDABOUT_PROCESS},
|
||||
```
|
||||
6. **`replace-all`: Global String Replacer**
|
||||
|
||||
- This patch type performs literal string replacements across **all files** under the target path.
|
||||
- Unlike `standard` or `pref`, this uses `=>` to define replacements and does **not** use `+` or `-` prefixes.
|
||||
- The patcher will automatically **skip modifying itself** (its own executable and folder).
|
||||
|
||||
```patch
|
||||
t:replace-all
|
||||
i:.
|
||||
o:.
|
||||
|
||||
string_to_find => string_to_replace_with
|
||||
|
||||
|
||||
```
|
||||
|
||||
### Example Patch Files
|
||||
|
||||
|
@ -146,14 +167,15 @@ o:/browser/branding/official/new-file.txt
|
|||
|
||||
### Summary of Parameters
|
||||
|
||||
| Parameter | Purpose | Example |
|
||||
| ----------- | ------------------------------------- | ------------------------------------- |
|
||||
| `i:` | Specifies the input file path. | `i:/browser/app/profile/firefox.js` |
|
||||
| `o:` | Specifies the output file path. | `o:/browser/app/profile/firefox.js` |
|
||||
| `t:` | Defines the type of patch. | `t:pref`, `t:standard`, `t:new`, `t:marker` |
|
||||
| `+` | Adds a line to the output file. | `+MOZ_APP_DISPLAYNAME=Spitfire` |
|
||||
| `-` | Removes a line from the input file. | `-MOZ_APP_DISPLAYNAME=Firefox` |
|
||||
| `#` | Marks a reference line for insertion (used in `marker` type). | `# 'settings',` |
|
||||
|
||||
| Parameter | Purpose | Example |
|
||||
| ----------- | -------------------------------------------------------------- | --------------------------------------------- |
|
||||
| `i:` | Specifies the input file path. | `i:/browser/app/profile/firefox.js` |
|
||||
| `o:` | Specifies the output file path. | `o:/browser/app/profile/firefox.js` |
|
||||
| `t:` | Defines the type of patch. | `t:pref`, `t:standard`, `t:new`, `t:marker` |
|
||||
| `+` | Adds a line to the output file. | `+MOZ_APP_DISPLAYNAME=Spitfire` |
|
||||
| `-` | Removes a line from the input file. | `-MOZ_APP_DISPLAYNAME=Firefox` |
|
||||
| `#` | Marks a reference line for insertion (used in`marker` type). | `# 'settings',` |
|
||||
|
||||
### Workflow
|
||||
|
||||
|
|
4
main.go
4
main.go
|
@ -155,7 +155,7 @@ func applyPatch(patchPath, rootPath string) error {
|
|||
return fmt.Errorf("patch file must specify output (o:) file")
|
||||
}
|
||||
|
||||
if patchType != "new" && patchType != "copy" {
|
||||
if patchType != "new" && patchType != "copy" && inputFilePath != outputFilePath {
|
||||
err = os.Rename(inputFilePath, outputFilePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to replace output file: %v", err)
|
||||
|
@ -174,6 +174,8 @@ func applyPatch(patchPath, rootPath string) error {
|
|||
return applyCopyPatch(inputFilePath, outputFilePath)
|
||||
case "marker":
|
||||
return applyMarkerPatch(outputFilePath, modifications)
|
||||
case "replace-all":
|
||||
return applyReplaceAllPatch(rootPath, modifications)
|
||||
default:
|
||||
fmt.Printf("Type not specified defaulting to standard")
|
||||
return applyStandardModifications(outputFilePath, modifications)
|
||||
|
|
5
pre-compile-patches/delete-telemetry-adress.patch
Normal file
5
pre-compile-patches/delete-telemetry-adress.patch
Normal file
|
@ -0,0 +1,5 @@
|
|||
t:replace-all
|
||||
i:.
|
||||
o:.
|
||||
|
||||
https://incoming.telemetry.mozilla.org/submit => 127.0.0.1
|
80
replace-all.go
Normal file
80
replace-all.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// applyReplaceAllPatch performs global string replacements in all files
|
||||
func applyReplaceAllPatch(rootPath string, modifications []string) error {
|
||||
replacements := [][2]string{}
|
||||
for _, mod := range modifications {
|
||||
parts := strings.SplitN(mod, "=>", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
from := strings.TrimSpace(parts[0])
|
||||
to := strings.TrimSpace(parts[1])
|
||||
replacements = append(replacements, [2]string{from, to})
|
||||
}
|
||||
|
||||
// Get absolute path of the patcher's own directory
|
||||
selfPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot resolve own path: %v", err)
|
||||
}
|
||||
patcherDir := filepath.Dir(selfPath)
|
||||
|
||||
return filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
// Skip patcher's own directory and subdirectories
|
||||
if strings.HasPrefix(path, patcherDir) {
|
||||
return fs.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Skip patcher's own files
|
||||
if strings.HasPrefix(path, patcherDir) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Only patch known text/code files
|
||||
if strings.HasSuffix(path, ".cpp") || strings.HasSuffix(path, ".h") ||
|
||||
strings.HasSuffix(path, ".c") || strings.HasSuffix(path, ".go") ||
|
||||
strings.HasSuffix(path, ".js") || strings.HasSuffix(path, ".ts") ||
|
||||
strings.HasSuffix(path, ".html") || strings.HasSuffix(path, ".in") ||
|
||||
strings.HasSuffix(path, ".mjs") || strings.HasSuffix(path, ".txt") {
|
||||
return replaceInFile(path, replacements)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// replaceInFile performs replacements in one file
|
||||
func replaceInFile(filePath string, replacements [][2]string) error {
|
||||
content, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
text := string(content)
|
||||
original := text
|
||||
|
||||
for _, repl := range replacements {
|
||||
text = strings.ReplaceAll(text, repl[0], repl[1])
|
||||
}
|
||||
|
||||
if text != original {
|
||||
err = os.WriteFile(filePath, []byte(text), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write modified file: %v", err)
|
||||
}
|
||||
fmt.Printf("Replaced content in: %s\n", filePath)
|
||||
}
|
||||
return nil
|
||||
}
|
2
run.sh
2
run.sh
|
@ -41,7 +41,7 @@ if [[ ! -d "$PATCHES_SOURCE" ]]; then
|
|||
fi
|
||||
|
||||
# Run the Go application with the specified path and patches
|
||||
go run main.go pref.go standard.go new.go copy.go --path "$ROOT_PATH" --patches "$PATCHES_SOURCE"
|
||||
go run main.go pref.go standard.go new.go copy.go replace-all.go marker.go --path "$ROOT_PATH" --patches "$PATCHES_SOURCE"
|
||||
|
||||
# Exit with the status of the last command
|
||||
exit $?
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue