add_admin_profile.ps1 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # 批量给 fs-admin-saas 的独立模块控制器添加 @Profile("admin")
  2. $saasRoot = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs"
  3. # 需要添加 @Profile("admin") 的模块(com.fs.X.controller 或 com.fs.X 直接包含控制器)
  4. $modules = @("his","course","crm","qw","live","chat","hisStore","fastGpt","newAdv","ad","billing","user","stats","tenant")
  5. $modifiedCount = 0
  6. $skippedCount = 0
  7. foreach ($m in $modules) {
  8. $ctrlPath = "$saasRoot\$m\controller"
  9. if (-not (Test-Path $ctrlPath)) {
  10. # 有些模块没有 controller 子目录,控制器直接在模块包下
  11. $ctrlPath = "$saasRoot\$m"
  12. }
  13. if (-not (Test-Path $ctrlPath)) { continue }
  14. Get-ChildItem $ctrlPath -Recurse -Filter "*Controller.java" -ErrorAction SilentlyContinue | ForEach-Object {
  15. $file = $_.FullName
  16. $content = Get-Content $file -Raw -Encoding UTF8
  17. # 已经有 @Profile 的跳过
  18. if ($content -match '@Profile\(') {
  19. $skippedCount++
  20. return
  21. }
  22. # 检查是否有 @RestController 或 @Controller
  23. if ($content -notmatch '@RestController' -and $content -notmatch '@Controller') {
  24. return
  25. }
  26. # 在 @RestController 或 @Controller 前面添加 @Profile("admin")
  27. # 同时确保 import org.springframework.context.annotation.Profile; 存在
  28. # 添加 import
  29. if ($content -notmatch 'import org\.springframework\.context\.annotation\.Profile;') {
  30. # 找到最后一个 import 行后添加
  31. $content = $content -replace "(import\s+[^;]+;\r?\n)(?!import)", "`$1import org.springframework.context.annotation.Profile;`n"
  32. }
  33. # 在 @RestController 或 @Controller 前添加 @Profile("admin")
  34. $content = $content -replace '(@RestController)', '@Profile("admin")`n`$1'
  35. $content = $content -replace '(@Controller)', '@Profile("admin")`n`$1'
  36. # 修正反引号 - PowerShell 的 -replace 使用 $1 而不是 \1
  37. # 重新处理
  38. }
  39. }
  40. # 上面的替换在 PowerShell 中有问题,改用更可靠的方式
  41. $modifiedCount = 0
  42. $skippedCount = 0
  43. foreach ($m in $modules) {
  44. $ctrlPath = "$saasRoot\$m\controller"
  45. if (-not (Test-Path $ctrlPath)) {
  46. $ctrlPath = "$saasRoot\$m"
  47. }
  48. if (-not (Test-Path $ctrlPath)) { continue }
  49. Get-ChildItem $ctrlPath -Recurse -Filter "*Controller.java" -ErrorAction SilentlyContinue | ForEach-Object {
  50. $file = $_.FullName
  51. $lines = Get-Content $file -Encoding UTF8
  52. # 已经有 @Profile 的跳过
  53. $hasProfile = $false
  54. foreach ($line in $lines) {
  55. if ($line -match '@Profile\(') { $hasProfile = $true; break }
  56. }
  57. if ($hasProfile) { $skippedCount++; return }
  58. # 检查是否有 @RestController 或 @Controller
  59. $hasController = $false
  60. foreach ($line in $lines) {
  61. if ($line -match '@RestController|@Controller') { $hasController = $true; break }
  62. }
  63. if (-not $hasController) { return }
  64. $newLines = @()
  65. $importAdded = $false
  66. $profileAdded = $false
  67. foreach ($line in $lines) {
  68. # 添加 import Profile
  69. if (-not $importAdded -and $line -match '^import ' -and $line -notmatch '^import org\.springframework\.context\.annotation\.Profile;') {
  70. # 检查是否是最后一个 import
  71. $nextIdx = [array]::IndexOf($lines, $line) + 1
  72. if ($nextIdx -lt $lines.Count -and $lines[$nextIdx] -notmatch '^import ') {
  73. $newLines += $line
  74. $newLines += "import org.springframework.context.annotation.Profile;"
  75. $importAdded = $true
  76. } else {
  77. $newLines += $line
  78. }
  79. }
  80. # 在 @RestController 或 @Controller 前添加 @Profile("admin")
  81. elseif (-not $profileAdded -and ($line -match '^\s*@RestController' -or $line -match '^\s*@Controller\s')) {
  82. $newLines += '@Profile("admin")'
  83. $newLines += $line
  84. $profileAdded = $true
  85. }
  86. else {
  87. $newLines += $line
  88. }
  89. }
  90. # 如果 import 没添加(所有 import 都连续),在第一个 import 块末尾添加
  91. if (-not $importAdded -and $profileAdded) {
  92. $idx = 0
  93. for ($i = $newLines.Count - 1; $i -ge 0; $i--) {
  94. if ($newLines[$i] -match '^import ') {
  95. $idx = $i
  96. break
  97. }
  98. }
  99. if ($idx -gt 0) {
  100. $before = $newLines[0..$idx]
  101. $after = $newLines[($idx+1)..($newLines.Count-1)]
  102. $newLines = $before + "import org.springframework.context.annotation.Profile;" + $after
  103. $importAdded = $true
  104. }
  105. }
  106. if ($profileAdded) {
  107. Set-Content $file $newLines -Encoding UTF8
  108. $modifiedCount++
  109. }
  110. }
  111. }
  112. "Modified: $modifiedCount controllers, Skipped (already has @Profile): $skippedCount"