-
Notifications
You must be signed in to change notification settings - Fork 26
/
build-win-server.ps1
109 lines (95 loc) · 4.15 KB
/
build-win-server.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 脚本说明
# 本脚本用于自动编译 windows-x64 服务端程序
# 遇到错误即退出
$ErrorActionPreference = "Stop"
# 严格模式,
Set-StrictMode -Version Latest
# 检测环境
Write-Host "开始检测环境..." -ForegroundColor Yellow
# 检测脚本位置是否正确:当前目录下是否有 ui-src 目录和 backend-src 目录
$scriptRoot = $PSScriptRoot
$subDirs = $("ui-src", "backend-src")
foreach ($subDir in $subDirs) {
$dir = Join-Path -Path $scriptRoot -ChildPath $subDir
if (-not (Test-Path -Path $dir -PathType Container)) {
Write-Host "请在项目的根目录下执行当前脚本!"
return
}
}
Write-Host "脚本位置检测通过!" -ForegroundColor Green
# 检查是否有 yarn 环境
if (-not (Get-Command yarn -ErrorAction SilentlyContinue)) {
Write-Host "请先安装 yarn 环境!" -ForegroundColor Red
return
}
Write-Host "yarn 环境检测通过!" -ForegroundColor Green
# 检查是否有 node 环境
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
Write-Host "请先安装 node 环境!" -ForegroundColor Red
return
}
Write-Host "node 环境检测通过!" -ForegroundColor Green
# 检查是否有 dotnet 环境
if (-not (Get-Command dotnet -ErrorAction SilentlyContinue)) {
Write-Host "请先安装 dotnet 环境!" -ForegroundColor Red
return
}
Write-Host "dotnet 环境检测通过!" -ForegroundColor Green
# 检测 7z
if (-not (Get-Command 7z.exe -ErrorAction SilentlyContinue)) {
Write-Host "7z 未安装,请从 https://www.7-zip.org/download.html 下载并安装" -ForegroundColor Red
return
}
Write-Host "7z 环境检测通过!" -ForegroundColor Green
# 开始编译项目
Write-Host "开始编译项目..." -ForegroundColor Yellow
# 编译前端
Write-Host "前端编译中..." -ForegroundColor Yellow
$uiSrc = Join-Path -Path $scriptRoot -ChildPath "ui-src"
# 判断是否已经执行过 yarn install
$nodeModules = Join-Path -Path $uiSrc -ChildPath "node_modules"
if (-not (Test-Path -Path $nodeModules -PathType Container)) {
Write-Host "开始安装依赖..." -ForegroundColor Yellow
Set-Location -Path $uiSrc
yarn install
Write-Host "依赖安装完成!" -ForegroundColor Green
Write-Host "开始编译..." -ForegroundColor Yellow
}
Set-Location -Path $uiSrc
yarn build
Write-Host "前端编译完成!" -ForegroundColor Green
# 编译后端
Write-Host "后端服务编译中..." -ForegroundColor Yellow
$backendSrc = Join-Path -Path $scriptRoot -ChildPath "backend-src"
$serviceSr = Join-Path -Path $backendSrc -ChildPath "UZonMailService"
# 使用 dotnet 编译
Set-Location -Path $serviceSr
$serviceDist = "$scriptRoot/build/service-win-x64"
dotnet publish -c Release -o $serviceDist -r win-x64 --self-contained false
# 创建 public 目录
New-Item -Path "$serviceDist/public" -ItemType Directory -Force
# 创建 wwwwroot 目录
New-Item -Path "$serviceDist/wwwroot" -ItemType Directory -Force
# 复制 Quartz/quartz-sqlite.sqlite3 到 Quartz 目录中
New-Item -Path "$serviceDist/Quartz" -ItemType Directory -ErrorAction SilentlyContinue
Copy-Item -Path "$serviceSr/Quartz/quartz-sqlite.sqlite3" -Destination "$serviceDist/Quartz/quartz-sqlite.sqlite3" -Force
Write-Host "后端服务编译完成!" -ForegroundColor Green
# 整合环境
Write-Host "合并编译结果..." -ForegroundColor Yellow
# 复制前端编译结果到服务端指定位置
$serviceWwwroot = Join-Path -Path $serviceDist -ChildPath "wwwroot"
# 目录不存在时,创建
if (-not (Test-Path -Path $serviceWwwroot -PathType Container)) {
New-Item -Path $serviceWwwroot -ItemType Directory -Force
}
Copy-Item -Path $uiSrc/dist/spa/* -Destination $serviceWwwroot -Recurse -Force
# 读取服务端的版本号
$UZonMailServiceDll = Join-Path -Path $serviceDist -ChildPath "UZonMailService.dll"
$serviceVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($UZonMailServiceDll).FileVersion
# 生成文件路径
$zipDist = Join-Path -Path $scriptRoot -ChildPath "build\uzonmail-service-win-x64-$serviceVersion.zip"
# 打包文件
7z a -tzip $zipDist "$serviceDist\*"
# 回到根目录
Set-Location -Path $scriptRoot
Write-Host "编译完成:$zipDist" -ForegroundColor Green