|
| 1 | +function Get-VariablesFromFile($filename = "BUILD") { |
| 2 | + $fullPath = Join-Path -Path $PWD -ChildPath $filename |
| 3 | + if (-not(Test-Path -Path $fullPath -PathType Leaf)) { |
| 4 | + Write-Output "BUILD file is missing" |
| 5 | + Write-Output "Script should execute from ""Scripts"" directory" |
| 6 | + exit |
| 7 | + } |
| 8 | + $content = Get-Content -Path $fullPath -Raw |
| 9 | + $pattern = @" |
| 10 | +(?<key>^[A-Z]\w+)="(?<value>.+)" |
| 11 | +"@ |
| 12 | + $dict = New-Object "System.Collections.Generic.Dictionary[String,String]" |
| 13 | + $_matches = [System.Text.RegularExpressions.Regex]::Matches($content, $pattern, |
| 14 | + [System.Text.RegularExpressions.RegexOptions]::Multiline) |
| 15 | + foreach ($match in $_matches) { |
| 16 | + $key = $match.Groups["key"].Value |
| 17 | + $value = $match.Groups["value"].Value |
| 18 | + $dict.Add($key, $value) |
| 19 | + } |
| 20 | + return $dict |
| 21 | +} |
| 22 | + |
| 23 | +function Remove-RootProjectFromString([string]$str) { |
| 24 | + $n = $str.IndexOf('/') |
| 25 | + return $str.Substring($n) |
| 26 | +} |
| 27 | + |
| 28 | +function Write-FormattedOutput { |
| 29 | + [CmdletBinding()] |
| 30 | + Param( |
| 31 | + [Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)][Object] $Object, |
| 32 | + [Parameter(Mandatory=$False)][ConsoleColor] $BackgroundColor, |
| 33 | + [Parameter(Mandatory=$False)][ConsoleColor] $ForegroundColor |
| 34 | + ) |
| 35 | + |
| 36 | + # save the current color |
| 37 | + $bc = $host.UI.RawUI.BackgroundColor |
| 38 | + $fc = $host.UI.RawUI.ForegroundColor |
| 39 | + |
| 40 | + # set the new color |
| 41 | + if($BackgroundColor -ne $null) { |
| 42 | + $host.UI.RawUI.BackgroundColor = $BackgroundColor |
| 43 | + } |
| 44 | + |
| 45 | + if($ForegroundColor -ne $null) { |
| 46 | + $host.UI.RawUI.ForegroundColor = $ForegroundColor |
| 47 | + } |
| 48 | + |
| 49 | + Write-Output $Object |
| 50 | + |
| 51 | + # restore the original color |
| 52 | + $host.UI.RawUI.BackgroundColor = $bc |
| 53 | + $host.UI.RawUI.ForegroundColor = $fc |
| 54 | +} |
| 55 | + |
| 56 | +$d = Get-VariablesFromFile -filename "BUILD" |
| 57 | + |
| 58 | +$project_root |
| 59 | +$server_path |
| 60 | +$client_path |
| 61 | +$config_path |
| 62 | +$build_path |
| 63 | +$npm_util = $d["NPM_UTIL"] |
| 64 | + |
| 65 | +if ($d["PROJECT_ROOT"] -eq "auto") { |
| 66 | + while (-not (Test-Path -Path (Join-Path -Path $PWD -ChildPath "SunEngine.md"))) { |
| 67 | + Set-Location ..\ | Out-Null |
| 68 | + } |
| 69 | + $project_root = $PWD |
| 70 | + |
| 71 | +} |
| 72 | +else { |
| 73 | + $project_root = $d["PROJECT_ROOT"] |
| 74 | +} |
| 75 | + |
| 76 | +$server_path = Join-Path -Path $PWD -ChildPath (Remove-RootProjectFromString($d["SERVER_PATH"])) |
| 77 | +$client_path = Join-Path -Path $PWD -ChildPath (Remove-RootProjectFromString($d["CLIENT_PATH"])) |
| 78 | +$config_path = Join-Path -Path $PWD -ChildPath (Remove-RootProjectFromString($d["CONFIG_PATH"])) |
| 79 | +$build_path = Join-Path -Path $PWD -ChildPath (Remove-RootProjectFromString($d["BUILD_PATH"])) |
| 80 | + |
| 81 | +Write-FormattedOutput -Object "Building $($d["PROJECT_NAME"]) project." -ForegroundColor Green |
| 82 | + |
| 83 | +Write-FormattedOutput -Object "PROJECT_ROOT = $($project_root)." -ForegroundColor Green |
| 84 | +Write-FormattedOutput -Object "SERVER_PATH = $($server_path)." -ForegroundColor Green |
| 85 | +Write-FormattedOutput -Object "CLIENT_PATH = $($client_path)." -ForegroundColor Green |
| 86 | +Write-FormattedOutput -Object "CONFIG_PATH = $($config_path)." -ForegroundColor Green |
| 87 | +Write-FormattedOutput -Object "BUILD_PATH = $($build_path)." -ForegroundColor Green |
| 88 | + |
| 89 | +# check Config directory exists |
| 90 | +if (Test-Path -Path (Join-Path -Path $project_root -ChildPath "Config.template")) { |
| 91 | + Write-FormattedOutput -Object "Creating Config directory from Config.template" -ForegroundColor Green |
| 92 | + $p = Join-Path -Path $project_root -ChildPath "Config.template" |
| 93 | + Copy-Item -Source $p -Destination $config_path -Force |
| 94 | +} |
| 95 | + |
| 96 | +Write-FormattedOutput -Object "Deleting old build" -ForegroundColor Green |
| 97 | +Remove-Item -Path $build_path -Recurse |
| 98 | +New-Item -Path $build_path -ItemType Directory | Out-Null |
| 99 | + |
| 100 | +# dotnet build |
| 101 | +try { |
| 102 | + $path1 = Join-Path -Path $server_path -ChildPath "SunEngine.Cli" |
| 103 | + $path2 = Join-Path -Path $build_path -ChildPath "Server" |
| 104 | + & dotnet publish --configuration Release $path1 --output $path2 | Out-Null |
| 105 | + if ($LASTEXITCODE -ne 0) { |
| 106 | + throw new [System.Exception] |
| 107 | + } |
| 108 | +} |
| 109 | +catch [System.Management.Automation.CommandNotFoundException] { |
| 110 | + Write-FormattedOutput -Object ".NET Core not install." -ForegroundColor Red |
| 111 | + exit |
| 112 | +} |
| 113 | +catch { |
| 114 | + Set-Location -Path (Join-Path -Path $project_root -ChildPath "Scripts") |
| 115 | + Write-FormattedOutput -Object "Crush build .net applicaton" -ForegroundColor Red |
| 116 | + exit |
| 117 | +} |
| 118 | + |
| 119 | +Write-FormattedOutput -Object "Building Client" -ForegroundColor Green |
| 120 | +Set-Location -Path $client_path | Out-Null |
| 121 | +# install node_modules |
| 122 | +try { |
| 123 | + & $npm_util install | Out-Null |
| 124 | + if ($LASTEXITCODE -ne 0) { |
| 125 | + throw new [System.Exception] |
| 126 | + } |
| 127 | +} |
| 128 | +catch [System.Management.Automation.CommandNotFoundException] { |
| 129 | + Write-FormattedOutput -Object "npm util not found" -ForegroundColor Red |
| 130 | + exit |
| 131 | +} |
| 132 | +catch { |
| 133 | + Set-Location -Path (Join-Path -Path $project_root -ChildPath "Scripts") |
| 134 | + Write-FormattedOutput -Object "Fail npm install" -ForegroundColor Red |
| 135 | + exit |
| 136 | +} |
| 137 | + |
| 138 | +# check Client/src/site exists |
| 139 | +if (-not(Test-Path -Path (Join-Path -Path $client_path -ChildPath "src/site"))) { |
| 140 | + Write-FormattedOutput -Object "Copying $($client_path)/src/site.template => $($client_path)/src/site" -ForegroundColor Green |
| 141 | + $path1 = Join-Path -Path $client_path -ChildPath "src\site.template\*" |
| 142 | + $path2 = Join-Path -Path $client_path -ChildPath "src\site" |
| 143 | + New-Item -Path $path2 -ItemType Directory | Out-Null |
| 144 | + Copy-Item -Path $path1 -Destination $path2 -Recurse |
| 145 | +} |
| 146 | +# quasar build |
| 147 | +Write-FormattedOutput -Object "Quasar building" -ForegroundColor Blue |
| 148 | +try { |
| 149 | + & quasar build | Out-Null |
| 150 | + if ($LASTEXITCODE -ne 0) { |
| 151 | + throw new [System.Exception] |
| 152 | + } |
| 153 | +} |
| 154 | +catch [System.Management.Automation.CommandNotFoundException] { |
| 155 | + Write-FormattedOutput -Object "Quasar not install please install by command ""npm install -g @quasar/cli""" -ForegroundColor Red |
| 156 | + exit |
| 157 | +} |
| 158 | +catch { |
| 159 | + Set-Location -Path (Join-Path -Path $project_root -ChildPath "Scripts") |
| 160 | + Write-FormattedOutput -Object "Quasar build was failed" -ForegroundColor Red |
| 161 | + exit |
| 162 | +} |
| 163 | + |
| 164 | +# copy dirs and files |
| 165 | +Write-FormattedOutput -Object "Copying Client to wwwroot directory" -ForegroundColor Green |
| 166 | +New-Item -Path (Join-Path -Path $build_path -ChildPath "wwwroot") -ItemType Directory | Out-Null |
| 167 | +Copy-Item -Path (Join-Path -Path $client_path -ChildPath "dist\spa\*") -Destination (Join-Path -Path $build_path -ChildPath "wwwroot") -Recurse |
| 168 | + |
| 169 | +Write-FormattedOutput -Object "Clearing dist directory" -ForegroundColor Green |
| 170 | +Remove-Item -Path (Join-Path -Path $client_path -ChildPath "dist") -Recurse | Out-Null |
| 171 | + |
| 172 | +Write-FormattedOutput -Object "Copying Config to build directory" -ForegroundColor Green |
| 173 | +New-Item -Path (Join-Path -Path $build_path -ChildPath "Сonfig") -ItemType Directory | Out-Null |
| 174 | +Copy-Item -Path $config_path -Destination (Join-Path -Path $build_path -ChildPath "Сonfig\") -Recurse |
| 175 | + |
| 176 | +Write-FormattedOutput -Object "Copying Data to build directory" -ForegroundColor Green |
| 177 | +New-Item -Path (Join-Path -Path $build_path -ChildPath "Resources") -ItemType Directory | Out-Null |
| 178 | +Copy-Item -Path (Join-Path -Path $project_root -ChildPath "Resources\*") -Destination (Join-Path -Path $build_path -ChildPath "Resources\") -Recurse |
| 179 | + |
| 180 | +Write-FormattedOutput -Object "Copying SunEngine.md file" -ForegroundColor Green |
| 181 | +Copy-Item -Path (Join-Path -Path $project_root -ChildPath "SunEngine.md") -Destination $build_path |
| 182 | + |
| 183 | +Set-Location -Path (Join-Path -Path $project_root -ChildPath "Scripts") |
| 184 | +Write-FormattedOutput -Object "All Done!" -ForegroundColor Blue |
| 185 | +[System.Media.SystemSounds]::Beep.Play() |
| 186 | +Add-Type -AssemblyName System.Windows.Forms |
| 187 | +[System.Windows.Forms.MessageBox]::Show("All Done!") |
0 commit comments