Advertisement
Thunder-Menu

sln_Rebuild_as_in_VisualStudio.ps1

Apr 15th, 2023
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 8.07 KB | Source Code | 0 0
  1.  
  2. Add-Type -AssemblyName System.Windows.Forms
  3. Add-Type -AssemblyName System.Drawing
  4.  
  5. # Create OpenFileDialog object
  6. $dialog = New-Object System.Windows.Forms.OpenFileDialog
  7. $dialog.Filter = "Solution Files (*.sln)|*.sln"
  8. $dialog.InitialDirectory = [Environment]::GetFolderPath("Desktop")
  9.  
  10. # Show the dialog and check if the user selected a file
  11. if ($dialog.ShowDialog() -eq 'OK') {
  12. $global:buildConfig = ""
  13. $global:platformTarget = ""
  14. $global:buildName = ""
  15. $global:outputDir = ""
  16.     # Get the selected file path
  17.     $slnName = $dialog.FileName
  18.     #$slnPath = [System.IO.Path]::GetDirectoryName($dialog.FileName)
  19. #$slnfullPath = [System.IO.Path]::GetFullPath([System.IO.Path]::GetDirectoryName($dialog.FileName))
  20.  
  21.     # Create the output directory path
  22.     $global:outputDir = Join-Path (Split-Path $slnName) "thunder"
  23.  
  24.     # Create the output directory if it doesn't exist
  25.     if (!(Test-Path $global:outputDir)) {
  26.         New-Item -ItemType Directory -Path $global:outputDir | Out-Null
  27.     }
  28.  
  29.     # Choose the target platform
  30.     $platformDialog = New-Object System.Windows.Forms.Form
  31.     $platformDialog.AutoSize = $true
  32.     $platformDialog.Text = "Choose Target Platform"
  33.  
  34.         $releaseRadioButton = New-Object System.Windows.Forms.RadioButton
  35.         $releaseRadioButton.Location = New-Object System.Drawing.Point(20, 20)
  36.         $releaseRadioButton.Text = "Release"
  37.         $releaseRadioButton.Checked = $true
  38.  
  39.         $debugRadioButton = New-Object System.Windows.Forms.RadioButton
  40.         $debugRadioButton.Location = New-Object System.Drawing.Point(20, 40)
  41.         $debugRadioButton.Text = "Debug"
  42.  
  43. $x86Button = New-Object System.Windows.Forms.RadioButton
  44. $x86Button.Location = New-Object System.Drawing.Point(20, 20)
  45. $x86Button.Text = "x86"
  46. $x64Button = New-Object System.Windows.Forms.RadioButton
  47. $x64Button.Location = New-Object System.Drawing.Point(20, 40)
  48. $x64Button.Text = "x64"
  49. $anyCpuButton = New-Object System.Windows.Forms.RadioButton
  50. $anyCpuButton.Location = New-Object System.Drawing.Point(20, 60)
  51. $anyCpuButton.Text = "AnyCPU"
  52.  
  53. $okButton = New-Object System.Windows.Forms.Button
  54. $okButton.Location = New-Object System.Drawing.Point(20, 80)
  55. $okButton.Text = "OK"
  56. $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  57.  
  58. $platformDialog = New-Object System.Windows.Forms.Form
  59. $platformDialog.ClientSize = New-Object System.Drawing.Size(200, 150)
  60. $platformDialog.Text = "Select Platform"
  61. $platformDialog.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  62. $platformDialog.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  63.     $platformDialog.Controls.Add($x86Button)
  64.     $platformDialog.Controls.Add($x64Button)
  65.     $platformDialog.Controls.Add($anyCpuButton)
  66.     $platformDialog.Controls.Add($okButton)
  67. #$platformDialog.Controls.AddRange(@($x86Button, $x64Button, $anyCpuButton, $okButton))
  68.  
  69. $result = $platformDialog.ShowDialog()
  70.  
  71. # Prompt the user for a build name
  72. $buildNameDialog = New-Object System.Windows.Forms.Form
  73. $buildNameDialog.Text = "Build Name"
  74. $buildNameDialog.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  75. $buildNameDialog.MaximizeBox = $false
  76. $buildNameDialog.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  77.  
  78. $buildNameLabel = New-Object System.Windows.Forms.Label
  79. $buildNameLabel.Location = New-Object System.Drawing.Point(10, 20)
  80. $buildNameLabel.Size = New-Object System.Drawing.Size(200, 20)
  81. $buildNameLabel.Text = "Enter a name for this build:"
  82.  
  83. $buildNameTextBox = New-Object System.Windows.Forms.TextBox
  84. $buildNameTextBox.Location = New-Object System.Drawing.Point(10, 40)
  85. $buildNameTextBox.Size = New-Object System.Drawing.Size(200, 20)
  86.  
  87. $buildNameTextBox.Add_KeyPress({
  88.     if ($_.KeyChar -eq [char]::Enter) {
  89.         $global:buildName = $buildNameTextBox.Text
  90.         $buildNameDialog.DialogResult = [System.Windows.Forms.DialogResult]::OK
  91.         $buildNameDialog.Close()
  92.     }
  93. })
  94.  
  95. $okButton = New-Object System.Windows.Forms.Button
  96. $okButton.Location = New-Object System.Drawing.Point(70, 70)
  97. $okButton.Size = New-Object System.Drawing.Size(75, 23)
  98. $okButton.Text = "OK"
  99. $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
  100.  
  101. $buildNameDialog.Controls.Add($buildNameLabel)
  102. $buildNameDialog.Controls.Add($buildNameTextBox)
  103. $buildNameDialog.Controls.Add($okButton)
  104.  
  105. $buildNameDialogResult = $buildNameDialog.ShowDialog()
  106.  
  107. if ($buildNameDialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
  108.     $global:buildName = $buildNameTextBox.Text
  109. }
  110. # Prompt the user for the build configuration
  111. $buildConfigDialog = New-Object System.Windows.Forms.Form
  112. $buildConfigDialog.Text = "Build Configuration"
  113. $buildConfigDialog.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
  114. $buildConfigDialog.MaximizeBox = $false
  115. $buildConfigDialog.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
  116.  
  117. $releaseRadioButton = New-Object System.Windows.Forms.RadioButton
  118. $releaseRadioButton.Location = New-Object System.Drawing.Point(20, 20)
  119. $releaseRadioButton.Text = "Release"
  120. $releaseRadioButton.Checked = $true
  121.  
  122. $debugRadioButton = New-Object System.Windows.Forms.RadioButton
  123. $debugRadioButton.Location = New-Object System.Drawing.Point(20, 40)
  124. $debugRadioButton.Text = "Debug"
  125.  
  126. $buildConfigDialog.Controls.Add($releaseRadioButton)
  127. $buildConfigDialog.Controls.Add($debugRadioButton)
  128. $buildConfigDialog.Controls.Add($okButton)
  129.  
  130.  #Copy the configuration file to the output directory
  131.  $configFileName = [System.IO.Path]::GetFileNameWithoutExtension($slnName) + ".config"
  132.  $configFilePath = Join-Path $slnName $configFileName
  133.  if (Test-Path $configFilePath) {
  134.     Copy-Item -Path $configFilePath -Destination $global:outputDir -Force
  135.  }
  136.  
  137. $buildConfigDialogResult = $buildConfigDialog.ShowDialog()
  138. if ($buildConfigDialogResult -eq [System.Windows.Forms.DialogResult]::OK) {
  139.     $global:buildConfig = ""
  140.     if ($releaseRadioButton.Checked) {
  141.         $global:buildConfig = "Release"
  142.     } elseif ($debugRadioButton.Checked) {
  143.         $global:buildConfig = "Debug"
  144.     }
  145.  
  146.     # Modify the project file to include PostBuildEvent
  147.     $slnPath = Split-Path $slnName
  148.     $projectFiles = Get-ChildItem -Path $slnPath -Filter "*.csproj"
  149.     foreach ($projectFile in $projectFiles) {
  150.         $projectXml = [xml](Get-Content $projectFile.FullName)
  151.         switch ($result) {
  152.             "x86" {
  153.                 $projectXml.Project.PropertyGroup.PlatformTarget = "x86"
  154.                 break
  155.             }
  156.             "x64" {
  157.                 $projectXml.Project.PropertyGroup.PlatformTarget = "x64"
  158.                 break
  159.             }
  160.             "AnyCPU" {
  161.                 $projectXml.Project.PropertyGroup.PlatformTarget = "AnyCPU"
  162.                 break
  163.             }
  164.         }
  165.  
  166.         $postBuildNode = $projectXml.Project.PropertyGroup.PostBuildEvent
  167.         if (!$postBuildNode) {
  168.             $postBuildNode = $projectXml.CreateElement("PostBuildEvent")
  169.             $postBuildNode.InnerText = 'if "$(PlatformTarget)" == "x64" ( call "$(VCInstallDir)Auxiliary\Build\vcvars64.bat" ) else ( call "$(VCInstallDir)Auxiliary\Build\vcvars32.bat" ) editbin /largeaddressaware /TSAWARE "$(TargetPath)" sn -R "$(TargetPath)" "$(ProjectDir)\$(TargetFileName)"'
  170.             $projectXml.Project.PropertyGroup.AppendChild($postBuildNode) | Out-Null
  171.         }
  172.         $projectXml.Save($projectFile.FullName)
  173.     }
  174.  
  175.     # Compile the solution using MSBuild
  176.     #$msbuildPath = "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
  177.     $msbuildPath = "H:\Microsoft\Microsoft Visual Studio\Msbuild\Current\Bin\MSBuild.exe" #2022
  178.     $global:platformTarget = ""
  179.     if ($x86Button.Checked) {
  180.         $global:platformTarget = "x86"
  181.     } elseif ($x64Button.Checked) {
  182.         $global:platformTarget = "x64"
  183.     } elseif ($anyCpuButton.Checked) {
  184.         $global:platformTarget = "AnyCpu"
  185.     }
  186.         & $msbuildPath "$slnName" /t:Rebuild /p:Configuration=$global:buildConfig /p:Platform=$global:platformTarget /p:OutputPath="$global:outputDir" /p:BuildName="$global:buildName"
  187.     pause
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement