Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

🔁 Namespace & Path Refactor Script

A robust Bash utility to safely replace-allrings across an entire codebase, including:

  • Namespace updates (PHP, Symfony, etc.)
  • File path transformations
  • Escaped string replacements (e.g. composer.json, JSON configs)
  • File and directory renaming

Designed for large projects where consistency and precision are critical.


🚀 Features

  • ✅ Multi-format replacement:
    • Namespace (App\Services)
    • Path (App/Services)
    • Escaped namespace (App\\Services)
  • ✅ Recursive file content updates
  • ✅ File & directory renaming
  • ✅ Safe dry-run mode
  • ✅ Binary-safe processing
  • ✅ Ignore common build/system directories
  • ✅ No sed escaping issues (uses Python for exact byte replacement)

📦 Requirements

  • Bash (Linux, macOS, WSL)
  • python3
  • Standard Unix tools: grep, find, awk, mv

⚙️ Usage

./replace-all <OLD_STRING> <NEW_STRING> [dry-run: true|false]

Example

./replace-all "App\\Services" "App\\Modules\\Services"

⚠️ Important: Always use double backslashes (\\) in terminal input for namespaces.


🧪 Dry Run Mode

Preview changes without modifying files:

./replace-all "App\\Services" "App\\Modules\\Services" true

Output will show:

  • Files that would be modified
  • Rename operations that would occur

🧠 How It Works

1. Variant Generation

The script automatically generates 3 replacement patterns:

Type Example Input Generated
Namespace App\Services App\Services
Path App\Services App/Services
Escaped Namespace App\Services App\\Services

2. Content Replacement

  • Uses grep to locate files containing target strings
  • Uses Python byte-level replacement to avoid encoding/escaping issues
  • Processes only matching files for efficiency

3. File & Directory Renaming

  • Detects paths containing the target string
  • Renames them safely
  • Applies deep-first renaming to avoid conflicts

🚫 Ignored Directories

The script automatically skips:

  • .git
  • node_modules
  • vendor
  • dist
  • .idea
  • .vscode
  • storage

You can modify this list in:

IGNORE_DIRS=( ...)

⚠️ Safety Considerations

  • Always run dry-run first on large codebases

  • Commit your code before running:

    git commit -am "Before replace-all ```
  • This script performs global replacements — no semantic parsing


📌 Post-Processing

After running the script (especially for PHP projects):

composer dump-autoload

This ensures class autoloading is properly refreshed.


🧩 Example Use Cases

  • Refactoring Symfony namespaces
  • Migrating modules (App\CoreApp\Modules\Core)
  • Renaming domain layers
  • Updating legacy folder structures
  • Cleaning up inconsistent path formats

🛠️ Troubleshooting

Nothing happens

  • Ensure the string exists in the project
  • Try dry-run to confirm detection

Incorrect replacements

  • Verify escaping (\\ vs \)
  • Check generated variants

Permission issues

chmod +x replace-all

✨ Summary

This script is built for precision replace-all at scale:

  • No regex pitfalls
  • No partial replacements
  • No broken paths

Use it when simple tools like sed or IDE replace become unreliable.


---

If you want, I can extend this with:
- CLI flags instead of positional args
- Parallel processing (for large repos)
- Git-aware mode (only changed files)
- Rollback support

Just tell me.

📄 License

MIT License

Copyright (c) 2026 mduvernon

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.


⚠️ Disclaimer

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.

IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

CLI to replace-all <OLD_STRING> <NEW_STRING> (dry-run for preview)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages