add_company_profile.ps1 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # 批量给 fs-admin-saas 的 com.fs.company.controller.X 控制器添加 @Profile("company")
  2. $saasRoot = "d:\ylrz\ylrz_saas_his_scrm\fs-admin-saas\src\main\java\com\fs\company\controller"
  3. $modifiedCount = 0
  4. $skippedCount = 0
  5. Get-ChildItem $saasRoot -Recurse -Filter "*Controller.java" -ErrorAction SilentlyContinue | ForEach-Object {
  6. $file = $_.FullName
  7. $lines = Get-Content $file -Encoding UTF8
  8. # 已经有 @Profile 的跳过
  9. $hasProfile = $false
  10. foreach ($line in $lines) {
  11. if ($line -match '@Profile\(') { $hasProfile = $true; break }
  12. }
  13. if ($hasProfile) { $skippedCount++; return }
  14. # 检查是否有 @RestController 或 @Controller
  15. $hasController = $false
  16. foreach ($line in $lines) {
  17. if ($line -match '@RestController|@Controller') { $hasController = $true; break }
  18. }
  19. if (-not $hasController) { return }
  20. $newLines = @()
  21. $importAdded = $false
  22. $profileAdded = $false
  23. foreach ($line in $lines) {
  24. # 添加 import Profile
  25. if (-not $importAdded -and $line -match '^import ' -and $line -notmatch '^import org\.springframework\.context\.annotation\.Profile;') {
  26. $nextIdx = [array]::IndexOf($lines, $line) + 1
  27. if ($nextIdx -lt $lines.Count -and $lines[$nextIdx] -notmatch '^import ') {
  28. $newLines += $line
  29. $newLines += "import org.springframework.context.annotation.Profile;"
  30. $importAdded = $true
  31. } else {
  32. $newLines += $line
  33. }
  34. }
  35. # 在 @RestController 或 @Controller 前添加 @Profile("company")
  36. elseif (-not $profileAdded -and ($line -match '^\s*@RestController' -or $line -match '^\s*@Controller\s')) {
  37. $newLines += '@Profile("company")'
  38. $newLines += $line
  39. $profileAdded = $true
  40. }
  41. else {
  42. $newLines += $line
  43. }
  44. }
  45. # 如果 import 没添加(所有 import 都连续),在第一个 import 块末尾添加
  46. if (-not $importAdded -and $profileAdded) {
  47. $idx = 0
  48. for ($i = $newLines.Count - 1; $i -ge 0; $i--) {
  49. if ($newLines[$i] -match '^import ') {
  50. $idx = $i
  51. break
  52. }
  53. }
  54. if ($idx -gt 0) {
  55. $before = $newLines[0..$idx]
  56. $after = $newLines[($idx+1)..($newLines.Count-1)]
  57. $newLines = $before + "import org.springframework.context.annotation.Profile;" + $after
  58. $importAdded = $true
  59. }
  60. }
  61. if ($profileAdded) {
  62. Set-Content $file $newLines -Encoding UTF8
  63. $modifiedCount++
  64. }
  65. }
  66. "Modified: $modifiedCount controllers, Skipped (already has @Profile): $skippedCount"