Skip to content

Commit

Permalink
housekeeping: Update build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Dec 26, 2021
1 parent 543f3ca commit 111e4f4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
9 changes: 0 additions & 9 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,3 @@ updates:
labels:
- "Automatic"
open-pull-requests-limit: 99
- package-ecosystem: "gitsubmodule"
directory: "/"
schedule:
interval: "daily"
time: "07:30"
timezone: "Asia/Shanghai"
labels:
- "Automatic"
open-pull-requests-limit: 99
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

1 change: 0 additions & 1 deletion Build/DotNetDllPathPatcher
Submodule DotNetDllPathPatcher deleted from 3d390e
74 changes: 74 additions & 0 deletions Build/DotNetDllPathPatcher.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using namespace System.IO
using namespace System.Text

param([string]$exe_path, [string]$target_path = 'bin')
$ErrorActionPreference = 'Stop'
#$DebugPreference = 'Continue'

$exe_path = (Resolve-Path -Path $exe_path).Path

Write-Host "Origin path: `"$exe_path`""
Write-Host "Target dll path: $target_path"

$separator = '\'
$max_path_length = 1024

$exe_name = [Path]::GetFileName($exe_path)
$dll_name = [Path]::ChangeExtension($exe_name, '.dll')
Write-Debug "exe: $exe_name"
Write-Debug "dll: $dll_name"

function Update-Exe {
$old_bytes = [Encoding]::UTF8.GetBytes("$dll_name`0")
if ($old_bytes.Count -gt $max_path_length) {
throw [PathTooLongException] 'old dll path is too long'
}

$new_dll_path = "$target_path$separator$dll_name"
$new_bytes = [Encoding]::UTF8.GetBytes("$new_dll_path`0")
Write-Host "Dll path Change to `"$new_dll_path`""
if ($new_bytes.Count -gt $max_path_length) {
throw [PathTooLongException] 'new dll path is too long'
}

$bytes = [File]::ReadAllBytes($exe_path)
$index = (Get-Content $exe_path -Raw -Encoding 28591).IndexOf("$dll_name`0")
if ($index -lt 0) {
throw [InvalidDataException] 'Could not find old dll path'
}
Write-Debug "Position: $index"
$end_postion = $index + $($new_bytes.Count)
$end_length = $bytes.Count - $end_postion
if ($end_postion -gt $bytes.Count) {
throw [PathTooLongException] 'new dll path is too long'
}
Write-Debug "End Position: $end_postion"
Write-Debug "End Length: $end_length"

$fs = [File]::OpenWrite($exe_path)
try {
$fs.Write($bytes, 0, $index)
$fs.Write($new_bytes)
$fs.Write($bytes, $end_postion, $end_length)
}
finally {
$fs.Dispose();
}
}

function Move-Dll {
$tmpbin = 'tmpbin'
$dir = [Path]::GetDirectoryName($exe_path);
$root = [Path]::GetDirectoryName($dir);
Write-Debug "root path: $root"
Write-Debug "dir path: $dir"

Rename-Item $dir $tmpbin
New-Item -ItemType Directory $dir > $null
Move-Item $root\$tmpbin $dir
Rename-Item $dir\$tmpbin $target_path
Move-Item $dir\$target_path\$exe_name $dir
}

Update-Exe
Move-Dll
11 changes: 2 additions & 9 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ $configuration = 'Release'
$output_dir = "$PSScriptRoot\$proj\bin\$configuration"
$proj_path = "$PSScriptRoot\$proj\$proj.csproj"

$dllpatcher_tfm = 'net6.0'
$dllpatcher_dir = "$PSScriptRoot\Build\DotNetDllPathPatcher"
$dllpatcher_exe = "$dllpatcher_dir\bin\$configuration\$dllpatcher_tfm\DotNetDllPathPatcher.exe"

function Build-Generic
{
Write-Host 'Building generic'
Expand All @@ -27,7 +23,7 @@ function Build-Generic
dotnet publish -c $configuration -f $net_tfm $proj_path -o $publishDir
if ($LASTEXITCODE) { exit $LASTEXITCODE }

& "$dllpatcher_exe" "$publishDir\$exe" bin
& "$PSScriptRoot\Build\DotNetDllPathPatcher.ps1" "$publishDir\$exe" bin
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

Expand All @@ -45,13 +41,10 @@ function Build-SelfContained
dotnet publish -c $configuration -f $net_tfm -r $rid --self-contained true $proj_path
if ($LASTEXITCODE) { exit $LASTEXITCODE }

& "$dllpatcher_exe" "$publishDir\$exe" bin
& "$PSScriptRoot\Build\DotNetDllPathPatcher.ps1" "$publishDir\$exe" bin
if ($LASTEXITCODE) { exit $LASTEXITCODE }
}

dotnet build -c $configuration -f $dllpatcher_tfm $dllpatcher_dir\DotNetDllPathPatcher.csproj
if ($LASTEXITCODE) { exit $LASTEXITCODE }

if($rid -eq 'all' -or $rid -eq 'generic')
{
Build-Generic
Expand Down

0 comments on commit 111e4f4

Please sign in to comment.