Skip to content

Commit 08678c6

Browse files
committed
更新脚本
1 parent 627866c commit 08678c6

2 files changed

Lines changed: 88 additions & 70 deletions

File tree

.github/workflows/ci.yml

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -290,41 +290,50 @@ jobs:
290290
endlocal
291291
EOF
292292
293-
cat > "artifacts/publish/win-x64/install-service.ps1" <<'EOF'
294-
param(
295-
[string]$ServiceName = "OneCode",
296-
[string]$DisplayName = "OneCode",
297-
[string]$Description = "OneCode service",
298-
[string]$Urls = "http://0.0.0.0:9110"
293+
cat > "artifacts/publish/win-x64/install-service.bat" <<'EOF'
294+
@echo off
295+
setlocal enableextensions
296+
set "SERVICE_NAME=%SERVICE_NAME%"
297+
if "%SERVICE_NAME%"=="" set "SERVICE_NAME=OneCode"
298+
set "DISPLAY_NAME=%DISPLAY_NAME%"
299+
if "%DISPLAY_NAME%"=="" set "DISPLAY_NAME=OneCode"
300+
set "DESCRIPTION=%DESCRIPTION%"
301+
if "%DESCRIPTION%"=="" set "DESCRIPTION=OneCode service"
302+
set "URLS=%URLS%"
303+
if "%URLS%"=="" set "URLS=http://0.0.0.0:9110"
304+
305+
net session >nul 2>&1
306+
if not "%errorlevel%"=="0" (
307+
echo Run this script as Administrator.
308+
exit /b 1
299309
)
300-
$installDir = Split-Path -Parent $MyInvocation.MyCommand.Path
301-
$exePath = Join-Path $installDir "OneCode.exe"
302-
if (-not (Test-Path $exePath)) {
303-
$exePath = Join-Path $installDir "OneCode.dll"
304-
if (-not (Test-Path $exePath)) {
305-
Write-Error "OneCode executable not found in $installDir"
306-
exit 1
307-
}
308-
$binaryPath = "dotnet `"$exePath`" --urls `"$Urls`""
309-
} else {
310-
$binaryPath = "`"$exePath`" --urls `"$Urls`""
311-
}
312-
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
313-
if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
314-
Write-Error "Run this script as Administrator."
315-
exit 1
316-
}
317-
$service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
318-
if ($null -eq $service) {
319-
New-Service -Name $ServiceName -DisplayName $DisplayName -BinaryPathName $binaryPath -Description $Description -StartupType Automatic | Out-Null
320-
} else {
321-
sc.exe config $ServiceName binPath= "$binaryPath" start= auto | Out-Null
322-
}
323-
Start-Service -Name $ServiceName
324-
$service = Get-Service -Name $ServiceName
325-
Write-Host "Service status: $service.Status"
326-
Write-Host "URL: $Urls"
327-
Write-Host "Open: http://localhost:9110"
310+
311+
set "INSTALL_DIR=%~dp0"
312+
set "EXE_PATH=%INSTALL_DIR%OneCode.exe"
313+
if exist "%EXE_PATH%" (
314+
set "BIN_PATH=\"%EXE_PATH%\" --urls \"%URLS%\""
315+
) else (
316+
set "DLL_PATH=%INSTALL_DIR%OneCode.dll"
317+
if not exist "%DLL_PATH%" (
318+
echo OneCode executable not found in %INSTALL_DIR%
319+
exit /b 1
320+
)
321+
set "BIN_PATH=\"dotnet\" \"%DLL_PATH%\" --urls \"%URLS%\""
322+
)
323+
324+
sc query "%SERVICE_NAME%" >nul 2>&1
325+
if "%errorlevel%"=="1060" (
326+
sc create "%SERVICE_NAME%" binPath= "%BIN_PATH%" start= auto DisplayName= "%DISPLAY_NAME%" >nul
327+
) else (
328+
sc config "%SERVICE_NAME%" binPath= "%BIN_PATH%" start= auto >nul
329+
)
330+
331+
sc description "%SERVICE_NAME%" "%DESCRIPTION%" >nul
332+
sc start "%SERVICE_NAME%" >nul
333+
sc query "%SERVICE_NAME%"
334+
echo URL: %URLS%
335+
echo Open: http://localhost:9110
336+
endlocal
328337
EOF
329338
330339
- name: Package release assets

publish.bat

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,49 +64,58 @@ set "RUN_BAT=%PUBLISH_DIR%\OneCode.bat"
6464
) > "%RUN_BAT%"
6565

6666
echo [5/5] Writing Windows service installer...
67-
set "SERVICE_PS1=%PUBLISH_DIR%\install-service.ps1"
67+
set "SERVICE_BAT=%PUBLISH_DIR%\install-service.bat"
6868
(
69-
echo param^(
70-
echo [string]$ServiceName = "OneCode",
71-
echo [string]$DisplayName = "OneCode",
72-
echo [string]$Description = "OneCode service",
73-
echo [string]$Urls = "http://0.0.0.0:9110"
69+
echo @echo off
70+
echo setlocal enableextensions
71+
echo set "SERVICE_NAME=%%SERVICE_NAME%%"
72+
echo if "%%SERVICE_NAME%%"=="" set "SERVICE_NAME=OneCode"
73+
echo set "DISPLAY_NAME=%%DISPLAY_NAME%%"
74+
echo if "%%DISPLAY_NAME%%"=="" set "DISPLAY_NAME=OneCode"
75+
echo set "DESCRIPTION=%%DESCRIPTION%%"
76+
echo if "%%DESCRIPTION%%"=="" set "DESCRIPTION=OneCode service"
77+
echo set "URLS=%%URLS%%"
78+
echo if "%%URLS%%"=="" set "URLS=http://0.0.0.0:9110"
79+
echo.
80+
echo net session ^>nul 2^>^&1
81+
echo if not "%%errorlevel%%"=="0" ^(
82+
echo echo Run this script as Administrator.
83+
echo exit /b 1
84+
echo ^)
85+
echo.
86+
echo set "INSTALL_DIR=%%~dp0"
87+
echo set "EXE_PATH=%%INSTALL_DIR%%OneCode.exe"
88+
echo if exist "%%EXE_PATH%%" ^(
89+
echo set "BIN_PATH=\"%%EXE_PATH%%\" --urls \"%%URLS%%\""
90+
echo ^) else ^(
91+
echo set "DLL_PATH=%%INSTALL_DIR%%OneCode.dll"
92+
echo if not exist "%%DLL_PATH%%" ^(
93+
echo echo OneCode executable not found in %%INSTALL_DIR%%
94+
echo exit /b 1
95+
echo ^)
96+
echo set "BIN_PATH=\"dotnet\" \"%%DLL_PATH%%\" --urls \"%%URLS%%\""
7497
echo ^)
75-
echo $installDir = Split-Path -Parent $MyInvocation.MyCommand.Path
76-
echo $exePath = Join-Path $installDir "OneCode.exe"
77-
echo if ^(-not ^(Test-Path $exePath^)^) ^{
78-
echo $exePath = Join-Path $installDir "OneCode.dll"
79-
echo if ^(-not ^(Test-Path $exePath^)^) ^{
80-
echo Write-Error "OneCode executable not found in $installDir"
81-
echo exit 1
82-
echo ^}
83-
echo $binaryPath = "dotnet `"$exePath`" --urls `"$Urls`""
84-
echo ^} else ^{
85-
echo $binaryPath = "`"$exePath`" --urls `"$Urls`""
86-
echo ^}
87-
echo $principal = New-Object Security.Principal.WindowsPrincipal^([Security.Principal.WindowsIdentity]::GetCurrent^()^)
88-
echo if ^(-not $principal.IsInRole^([Security.Principal.WindowsBuiltInRole]::Administrator^)^) ^{
89-
echo Write-Error "Run this script as Administrator."
90-
echo exit 1
91-
echo ^}
92-
echo $service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
93-
echo if ^($null -eq $service^) ^{
94-
echo New-Service -Name $ServiceName -DisplayName $DisplayName -BinaryPathName $binaryPath -Description $Description -StartupType Automatic ^| Out-Null
95-
echo ^} else ^{
96-
echo sc.exe config $ServiceName binPath= "$binaryPath" start= auto ^| Out-Null
97-
echo ^}
98-
echo Start-Service -Name $ServiceName
99-
echo $service = Get-Service -Name $ServiceName
100-
echo Write-Host "Service status: $service.Status"
101-
echo Write-Host "URL: $Urls"
102-
echo Write-Host "Open: http://localhost:9110"
103-
) > "%SERVICE_PS1%"
98+
echo.
99+
echo sc query "%%SERVICE_NAME%%" ^>nul 2^>^&1
100+
echo if "%%errorlevel%%"=="1060" ^(
101+
echo sc create "%%SERVICE_NAME%%" binPath^= "%%BIN_PATH%%" start^= auto DisplayName^= "%%DISPLAY_NAME%%" ^>nul
102+
echo ^) else ^(
103+
echo sc config "%%SERVICE_NAME%%" binPath^= "%%BIN_PATH%%" start^= auto ^>nul
104+
echo ^)
105+
echo.
106+
echo sc description "%%SERVICE_NAME%%" "%%DESCRIPTION%%" ^>nul
107+
echo sc start "%%SERVICE_NAME%%" ^>nul
108+
echo sc query "%%SERVICE_NAME%%"
109+
echo echo URL: %%URLS%%
110+
echo echo Open: http://localhost:9110
111+
echo endlocal
112+
) > "%SERVICE_BAT%"
104113

105114
echo.
106115
echo Done.
107116
echo Output: "%PUBLISH_DIR%"
108117
echo Start: "%RUN_BAT%"
109-
echo Service install: "%SERVICE_PS1%"
118+
echo Service install: "%SERVICE_BAT%"
110119

111120
popd >nul
112121
endlocal

0 commit comments

Comments
 (0)