|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +This script builds specified projects or the whole solution using MsBuild. |
| 4 | +
|
| 5 | +.DESCRIPTION |
| 6 | +This PowerShell script is used to build specific projects or the entire solution |
| 7 | +using the MsBuild tool. It takes in parameters for projects, action, and configuration |
| 8 | +and constructs and executes the appropriate MsBuild commands. |
| 9 | +
|
| 10 | +.PARAMETER projects |
| 11 | +A string containing the project name or solution to build. Must be one of this possibilities (store in $possibleProjectValues) : |
| 12 | +("PluginInteropUnityCUDA", "SampleBasic" |
| 13 | + , "Utilities") |
| 14 | +or "solution" or "sln" or "all" if you want to build the whole solution |
| 15 | +
|
| 16 | +.PARAMETER action |
| 17 | +A string indicating the action to perform (e.g., build, rebuild, clean). |
| 18 | +
|
| 19 | +.PARAMETER configuration |
| 20 | +A string indicating the build configuration (e.g., release, debug). |
| 21 | +
|
| 22 | +.EXAMPLE |
| 23 | +.\BuildProjects.ps1 -projects @("PluginInteropUnityCUDA", "SampleBasic") -action "build" -configuration "release" |
| 24 | +This example command builds the specified projects in release configuration. |
| 25 | +
|
| 26 | +.NOTES |
| 27 | +File Name : BuildProjects.ps1 |
| 28 | +Author : David Algis |
| 29 | +Prerequisite : PowerShell v3 and MsBuild |
| 30 | +Copyright 2023 - Studio Nyx |
| 31 | +#> |
| 32 | +param( |
| 33 | + [string]$project, |
| 34 | + [string]$action, |
| 35 | + [string]$configuration |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +. $PSScriptRoot\dependencies.ps1 |
| 40 | + |
| 41 | +$version = $Env:HYBRIDIZATION_VERSION |
| 42 | +# For non dll project, write NO in description |
| 43 | +[string[][]]$possibleProjectValues = @( |
| 44 | + @("PluginInteropUnityCUDA", "Contains a library with the core of interoperability between Unity and CUDA."), |
| 45 | + @("SampleBasic", "NO"), |
| 46 | + @("Utilities", "Contains the source of a library that contains utilities and common tools for all the other library and executable.") |
| 47 | +) |
| 48 | + |
| 49 | + |
| 50 | +Write-Host "Launch $action of $project..."-ForegroundColor DarkMagenta |
| 51 | +# TODO |
| 52 | + |
| 53 | + |
| 54 | +$currentDir = Get-Location |
| 55 | + |
| 56 | +$buildSolution = $false |
| 57 | + |
| 58 | +# Check if the project name is in the list of possible project values |
| 59 | +$isValidProject = $false |
| 60 | +$projectDescription = "" |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +if ($action -eq "build" -or $action -eq "rebuild") { |
| 65 | + if ($configuration -ne "debug" -and $configuration -ne "release" -and $configuration -ne "") { |
| 66 | + Write-Warning "The configuration argument '$configuration' is not a valid configuration for the action : $action. Exiting the program." |
| 67 | + Exit |
| 68 | + } |
| 69 | +} |
| 70 | +else { |
| 71 | + if ($action -ne "clean") { |
| 72 | + Write-Warning "The action argument '$action' is not a valid action. Exiting the program." |
| 73 | + Exit |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +if ($configuration -ne "") { |
| 78 | + $configuration = "/p:Configuration=$configuration" |
| 79 | +} |
| 80 | + |
| 81 | +if ($project -eq "sln" -or $project -eq "solution" -or $project -eq "all") { |
| 82 | + $buildSolution = $true |
| 83 | +} |
| 84 | +else { |
| 85 | + foreach ($possibleProject in $possibleProjectValues) { |
| 86 | + if ($possibleProject[0].ToLower() -eq $project.ToLower()) { |
| 87 | + $isValidProject = $true |
| 88 | + # to be sure it has the correct cases |
| 89 | + $project = $possibleProject[0] |
| 90 | + $projectDescription = "$($possibleProject[1]) $configuration)" |
| 91 | + break |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (-not $isValidProject) { |
| 96 | + Write-Warning "The project name '$project' is not a valid project. Exiting the program." |
| 97 | + Exit |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +Set-Location -Path "$PSScriptRoot\..\" |
| 105 | + |
| 106 | + |
| 107 | +function TryGetCommitHash() { |
| 108 | + $result = "" |
| 109 | + # Test if git enabled ... |
| 110 | + if (Get-Command git -erroraction silentlycontinue) { |
| 111 | + # Test if we are in a git repository ... |
| 112 | + $process = start-process git -ArgumentList "status" -PassThru -Wait -WindowStyle Hidden |
| 113 | + if ($process.ExitCode -eq 0) { |
| 114 | + $result = & git rev-parse --verify HEAD |
| 115 | + } |
| 116 | + } |
| 117 | + return $result |
| 118 | +} |
| 119 | + |
| 120 | +function TryGetCommitTime() { |
| 121 | + $result = "" |
| 122 | + # Test if git enabled ... |
| 123 | + if (Get-Command git -erroraction silentlycontinue) { |
| 124 | + # Test if we are in a git repository ... |
| 125 | + $process = start-process git -ArgumentList "status" -PassThru -Wait -WindowStyle Hidden |
| 126 | + if ($process.ExitCode -eq 0) { |
| 127 | + $result = & git log -n 1 --pretty='format:%cd' --date=format:'%Y-%m-%d %H:%M:%S %z' |
| 128 | + } |
| 129 | + } |
| 130 | + return $result |
| 131 | +} |
| 132 | + |
| 133 | +function setupResources([System.IO.FileInfo]$outputPath, [String]$version, [String]$name, [String]$description) { |
| 134 | + # Get hash of current HEAD |
| 135 | + $commitHash = TryGetCommitHash |
| 136 | + |
| 137 | + $metadata = @" |
| 138 | +#pragma once |
| 139 | +
|
| 140 | +#define NAME "$name" |
| 141 | +#define INTERNAL_NAME "$name.dll" |
| 142 | +#define COMPANY "Studio Nyx" |
| 143 | +#define COPYRIGHT "Copyright (C) 2023" |
| 144 | +#define DESCRIPTION "$description" |
| 145 | +#define VERSION "$version" |
| 146 | +#define COMMIT "$commitHash" |
| 147 | +"@ |
| 148 | + Out-File -InputObject $metadata -FilePath $outputPath -Encoding Default |
| 149 | + |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | +# if the user want to build the whole solutions |
| 155 | +if ($buildSolution) { |
| 156 | + |
| 157 | + $solutionPath = ".\PluginInteropUnity.sln" |
| 158 | + Write-Host "Updating Metadata of whole solutions..." |
| 159 | + foreach ($possibleProject in $possibleProjectValues) { |
| 160 | + $projectName = $possibleProject[0] |
| 161 | + $projectDescription = "$($possibleProject[1]) $configuration" |
| 162 | + $projectPath = ".\$projectName" |
| 163 | + # Form the full project path using the solution's directory |
| 164 | + $fullProjectPath = Join-Path (Split-Path $solutionPath) $projectPath |
| 165 | + |
| 166 | + $projectPath = "$fullProjectPath\$projectName.vcxproj" |
| 167 | + $metadataFile = [System.IO.FileInfo]"$fullProjectPath\include\versionRC.h" |
| 168 | + |
| 169 | + # We setup ressources metadata only for dll projects |
| 170 | + if ($possibleProject[1] -ne "NO") { |
| 171 | + # Check if the file exists |
| 172 | + if (-not (Test-Path -Path $metadataFile)) { |
| 173 | + # Create the file |
| 174 | + Write-Host "Create $metadataFile" |
| 175 | + New-Item -Path $metadataFile -ItemType File |
| 176 | + } |
| 177 | + setupResources $metadataFile $version $projectName $projectDescription |
| 178 | + } |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | + $msbuildArguments = @( |
| 185 | + "PluginInteropUnity.sln", |
| 186 | + "/t:$action", |
| 187 | + "/m", |
| 188 | + "$configuration" |
| 189 | + ) |
| 190 | + Write-Host "Execute command : MsBuild $msbuildArguments" |
| 191 | + & MsBuild $msbuildArguments |
| 192 | + |
| 193 | + Set-Location -Path $currentDir |
| 194 | + exit |
| 195 | +} |
| 196 | + |
| 197 | +$metadataFile = [System.IO.FileInfo]".\$project\include\versionRC.h" |
| 198 | + |
| 199 | +Write-Host "Updating Metadata of $project..." |
| 200 | +# We setup ressources metadata only for dll projects |
| 201 | +if ($possibleProject[1] -ne "NO") { |
| 202 | + # Check if the file exists |
| 203 | + if (-not (Test-Path -Path $metadataFile)) { |
| 204 | + # Create the file |
| 205 | + Write-Host "Create $metadataFile" |
| 206 | + New-Item -Path $metadataFile -ItemType File |
| 207 | + } |
| 208 | + setupResources $metadataFile $version $project $projectDescription |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | + |
| 213 | +$msbuildArguments = @( |
| 214 | + "-target:$project", |
| 215 | + "/t:$action", |
| 216 | + "/m", |
| 217 | + "$configuration" |
| 218 | +) |
| 219 | +Write-Host "Execute command : MsBuild $msbuildArguments" |
| 220 | +& MsBuild $msbuildArguments |
| 221 | + |
| 222 | +Set-Location -Path $currentDir |
0 commit comments