Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. <#
  2. PowerShell script to rename C# Project step-by-step:
  3. * Copying project folder to folder with new project name
  4. * Renaming .csproj file and other files with project name
  5. * Changing project name reference in .sln solution file
  6. * Changing RootNamespace and AssemblyName in .csproj file
  7. * Renaming project inside AssemblyInfo.cs
  8. #>
  9. param(
  10. [parameter(
  11. HelpMessage="Existing C# Project path to rename.",
  12. Mandatory=$true, ValueFromPipeline=$true)]
  13. [ValidateScript({
  14. if (Test-Path $_.Trim().Trim('"')) { $true }
  15. else { throw "Path does not exist: $_" }
  16. })]
  17. [string]$ProjectFilePath,
  18.  
  19. [parameter(
  20. HelpMessage="New project file name, without extension.",
  21. Mandatory=$true)]
  22. [string]$NewProjectName,
  23.  
  24. [parameter(
  25. HelpMessage="Path to solution file.",
  26. Mandatory=$true)]
  27. [ValidateScript({
  28. if (Test-Path $_.Trim().Trim('"')) { $true }
  29. else { throw "Path does not exist: $_" }
  30. })]
  31. [string]$SolutionFilePath,
  32.  
  33. [parameter(
  34. HelpMessage="Relative path to AssemblyInfo.cs file, e.g. "".\Properties\AssemblyInfo.cs"".`nSpecify empty string to skip AssemblyInfo processing.",
  35. Mandatory=$true)]
  36. [AllowEmptyString()]
  37. [string]$RelativeAssemblyInfoPath
  38. )
  39.  
  40. $ProjectFilePath = $ProjectFilePath.Trim().Trim('"')
  41. $SolutionFilePath = $SolutionFilePath.Trim().Trim('"')
  42.  
  43. function ProceedOrExit {
  44. if ($?) { echo "Proceed.." } else { echo "Script FAILED! Exiting.."; exit 1 }
  45. }
  46.  
  47. echo "Rename project from '$OldProjectName' to '$NewProjectName'"
  48. echo "=========="
  49.  
  50. $OldProjectFolder = Split-Path $ProjectFilePath
  51. echo "1. Set current location to project folder '$OldProjectFolder'"
  52. cd $OldProjectFolder
  53. ProceedOrExit
  54.  
  55. $NewProjectFolder="..\$NewProjectName"
  56. echo "----------"
  57. echo "2. Copy project folder to '$NewProjectFolder'"
  58. copy . $NewProjectFolder -Recurse #-WhatIf
  59. ProceedOrExit
  60.  
  61. $NewProjectFolder=Resolve-Path $NewProjectFolder
  62. echo "----------"
  63. echo "3. Set current location to New project folder '$NewProjectFolder'"
  64. cd $NewProjectFolder
  65. ProceedOrExit
  66.  
  67. echo "----------"
  68. echo "4. Rename .proj and other files inside '$NewProjectFolder'"
  69.  
  70. $OldProjectName=[IO.Path]::GetFileNameWithoutExtension($ProjectFilePath)
  71. dir -Include "$OldProjectName.*" -Recurse |
  72. ren -NewName {$_.Name -replace [regex]("^"+$OldProjectName+"\b"), $NewProjectName} #-WhatIf
  73. ProceedOrExit
  74.  
  75. echo "----------"
  76. echo "5. Copy solution file to '$SolutionFilePath.backup'"
  77. copy "$SolutionFilePath" "$SolutionFilePath.backup" #-WhatIf
  78. ProceedOrExit
  79.  
  80. echo "----------"
  81. echo "6. Rename project reference inside solution file."
  82.  
  83. # Put get-content in () brackets to read whole file before modifying it.
  84. (Get-Content $SolutionFilePath) |
  85. % { if ($_ -match ("\b(" + $OldProjectName + ")\b\.csproj")) { $_ -replace $($matches[1]), $NewProjectName } else { $_ }} |
  86. Set-Content $SolutionFilePath
  87. ProceedOrExit
  88.  
  89. $NewProjectFile="$NewProjectName.csproj"
  90. echo "----------"
  91. echo "7. Rename project in tags AssemblyName and RootNamespace inside '$NewProjectFile'"
  92.  
  93. (Get-Content $NewProjectFile) |
  94. % { if ($_ -match ("<(?:AssemblyName|RootNamespace)>(" + $OldProjectName +")</")) { $_ -replace $($matches[1]), $NewProjectName } else { $_ }} |
  95. Set-Content $NewProjectFile
  96. ProceedOrExit
  97.  
  98. $RelativeAssemblyInfoPath = $RelativeAssemblyInfoPath.Trim().Trim('"')
  99. if ($RelativeAssemblyInfoPath) {
  100. echo "----------"
  101. echo "8. Rename project inside '$RelativeAssemblyInfoPath'"
  102.  
  103. if (!(Test-Path $RelativeAssemblyInfoPath)) { echo "ERROR: Path does not exist." }
  104. else {
  105. (Get-Content $RelativeAssemblyInfoPath) |
  106. % { if ($_ -match ("""\b("+$OldProjectName+")\b""")) { $_ -replace $($matches[1]), $NewProjectName } else { $_ }} |
  107. Set-Content $RelativeAssemblyInfoPath #-WhatIf
  108. ProceedOrExit
  109. }
  110. }
  111.  
  112. echo "=========="
  113. echo "DONE."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement