Initial commit
This commit is contained in:
99
replace.py
Normal file
99
replace.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
import shutil
|
||||
|
||||
def find_files_with_project_name(root_dir="."):
|
||||
"""Find all files that contain <ProjectName> but exclude specified files"""
|
||||
target_files = []
|
||||
ignored_dirs = {".git", ".svn", ".hg", ".vscode", "__pycache__"}
|
||||
ignored_files = {
|
||||
"./README.md",
|
||||
"./replace.py"
|
||||
}
|
||||
|
||||
# Normalize ignored files for comparison
|
||||
normalized_ignored = set()
|
||||
for f in ignored_files:
|
||||
normalized_ignored.add(os.path.normpath(f).replace("\\", "/"))
|
||||
|
||||
for dirpath, dirnames, filenames in os.walk(root_dir):
|
||||
# Filter out hidden directories
|
||||
dirnames[:] = [d for d in dirnames if d not in ignored_dirs and not d.startswith('.')]
|
||||
|
||||
for filename in filenames:
|
||||
if filename.startswith('.') or filename.endswith(('.pyc', '.pyo', '~')):
|
||||
continue
|
||||
|
||||
filepath = os.path.join(dirpath, filename)
|
||||
rel_path = os.path.relpath(filepath).replace("\\", "/")
|
||||
|
||||
# Skip ignored files
|
||||
if rel_path in normalized_ignored:
|
||||
continue
|
||||
|
||||
try:
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
if '<ProjectName>' in content:
|
||||
target_files.append(filepath)
|
||||
except (UnicodeDecodeError, PermissionError):
|
||||
# Skip unreadable or binary files
|
||||
pass
|
||||
|
||||
return sorted(target_files)
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Replace <ProjectName> with custom project name')
|
||||
parser.add_argument('project_name', help='The new project name to replace <ProjectName> with')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: python replace_project_name.py <NewProjectName>")
|
||||
sys.exit(1)
|
||||
|
||||
project_name = args.project_name
|
||||
|
||||
if not project_name:
|
||||
print("Error: Project name cannot be empty")
|
||||
sys.exit(1)
|
||||
|
||||
# Find all files containing <ProjectName>
|
||||
files_with_project_name = find_files_with_project_name()
|
||||
|
||||
if not files_with_project_name:
|
||||
print("No files found containing <ProjectName>")
|
||||
sys.exit(1)
|
||||
|
||||
print(f"Found {len(files_with_project_name)} file(s) containing <ProjectName>:")
|
||||
for f in files_with_project_name:
|
||||
print(f" • {f}")
|
||||
|
||||
# Perform replacements
|
||||
modified_count = 0
|
||||
for file_path in files_with_project_name:
|
||||
try:
|
||||
# Read the file
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Check if replacement is needed
|
||||
if '<ProjectName>' in content:
|
||||
# Replace all occurrences
|
||||
new_content = content.replace('<ProjectName>', project_name)
|
||||
|
||||
# Write back to file
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
print(f"Updated: {file_path}")
|
||||
modified_count += 1
|
||||
except Exception as e:
|
||||
print(f"Error processing {file_path}: {e}")
|
||||
|
||||
print(f"\nSuccessfully replaced <ProjectName> with '{project_name}' in {modified_count} file(s)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user