| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- # 批量给 fs-admin-saas 的 com.fs.company.controller.X 控制器添加 @Profile("company")
- $saasRoot = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs\company\controller"
- $modifiedCount = 0
- $skippedCount = 0
- Get-ChildItem $saasRoot -Recurse -Filter "*Controller.java" -ErrorAction SilentlyContinue | ForEach-Object {
- $file = $_.FullName
- $lines = Get-Content $file -Encoding UTF8
-
- # 已经有 @Profile 的跳过
- $hasProfile = $false
- foreach ($line in $lines) {
- if ($line -match '@Profile\(') { $hasProfile = $true; break }
- }
- if ($hasProfile) { $skippedCount++; return }
-
- # 检查是否有 @RestController 或 @Controller
- $hasController = $false
- foreach ($line in $lines) {
- if ($line -match '@RestController|@Controller') { $hasController = $true; break }
- }
- if (-not $hasController) { return }
-
- $newLines = @()
- $importAdded = $false
- $profileAdded = $false
-
- foreach ($line in $lines) {
- # 添加 import Profile
- if (-not $importAdded -and $line -match '^import ' -and $line -notmatch '^import org\.springframework\.context\.annotation\.Profile;') {
- $nextIdx = [array]::IndexOf($lines, $line) + 1
- if ($nextIdx -lt $lines.Count -and $lines[$nextIdx] -notmatch '^import ') {
- $newLines += $line
- $newLines += "import org.springframework.context.annotation.Profile;"
- $importAdded = $true
- } else {
- $newLines += $line
- }
- }
- # 在 @RestController 或 @Controller 前添加 @Profile("company")
- elseif (-not $profileAdded -and ($line -match '^\s*@RestController' -or $line -match '^\s*@Controller\s')) {
- $newLines += '@Profile("company")'
- $newLines += $line
- $profileAdded = $true
- }
- else {
- $newLines += $line
- }
- }
-
- # 如果 import 没添加(所有 import 都连续),在第一个 import 块末尾添加
- if (-not $importAdded -and $profileAdded) {
- $idx = 0
- for ($i = $newLines.Count - 1; $i -ge 0; $i--) {
- if ($newLines[$i] -match '^import ') {
- $idx = $i
- break
- }
- }
- if ($idx -gt 0) {
- $before = $newLines[0..$idx]
- $after = $newLines[($idx+1)..($newLines.Count-1)]
- $newLines = $before + "import org.springframework.context.annotation.Profile;" + $after
- $importAdded = $true
- }
- }
-
- if ($profileAdded) {
- Set-Content $file $newLines -Encoding UTF8
- $modifiedCount++
- }
- }
- "Modified: $modifiedCount controllers, Skipped (already has @Profile): $skippedCount"
|