|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <#
- .SYNOPSIS
- Batch replace PageData references in all TypeScript files
-
- .DESCRIPTION
- 1. Automatically backup original files
- 2. Remove all PageData import statements
- 3. Replace all PageData references with this.PageData
- 4. Generate detailed execution log
- #>
-
- # Configuration parameters
- $baseDir = "D:\front_Vue\ant-design-pro-vue3"
- $entityDir = "src/views/front/develop/Contract/Function/Entity"
- $backupRoot = "$baseDir\backups"
- $logFile = "$baseDir\batch_replace_log_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
-
- # Create backup directory and log file
- $backupDir = "$backupRoot\$(Get-Date -Format 'yyyyMMdd_HHmmss')"
- New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
- "Batch replace started at $(Get-Date)" | Out-File $logFile -Encoding UTF8
-
- # Get all .ts files (excluding demo directory)
- $files = Get-ChildItem -Path "$baseDir\$entityDir" -Filter "*.ts" -Recurse |
- Where-Object { $_.FullName -notmatch "\\demo\\" }
-
- # Process each file
- foreach ($file in $files) {
- try {
- $relativePath = $file.FullName.Substring($baseDir.Length + 1)
- "Processing: $relativePath" | Out-File $logFile -Encoding UTF8 -Append
-
- # Backup file (maintaining original directory structure)
- $backupPath = $file.FullName.Replace($baseDir, $backupDir)
- $backupDirPath = Split-Path $backupPath -Parent
- if (-not (Test-Path $backupDirPath)) {
- New-Item -ItemType Directory -Path $backupDirPath -Force | Out-Null
- }
- Copy-Item $file.FullName $backupPath
-
- # Read and modify content
- $content = Get-Content $file.FullName -Raw -Encoding UTF8
-
- # 1. Remove PageData import
- $content = $content -replace '(?m)^import\s*{\s*PageData\s*}\s*from\s*[''"].*page-data[''"];?\s*$', ''
-
- # 2. Replace all PageData references
- $content = $content -replace '(?<!this\.)PageData\.', 'this.PageData.'
-
- # Write to file
- $content | Set-Content $file.FullName -Encoding UTF8
-
- "Successfully processed: $relativePath" | Out-File $logFile -Encoding UTF8 -Append
- }
- catch {
- "ERROR processing $($file.FullName): $_" | Out-File $logFile -Encoding UTF8 -Append
- }
- }
-
- # Complete processing
- "`nBatch replace completed at $(Get-Date)" | Out-File $logFile -Encoding UTF8 -Append
- "Total files processed: $($files.Count)" | Out-File $logFile -Encoding UTF8 -Append
- "Backup location: $backupDir" | Out-File $logFile -Encoding UTF8 -Append
-
- Write-Host "Batch replacement completed!"
- Write-Host "Files processed: $($files.Count)"
- Write-Host "Backup location: $backupDir"
- Write-Host "Detailed log: $logFile"
|