Files
JoyD/Windows/CS/Framework4.0/Toprie/publish-nuget.ps1

383 lines
14 KiB
PowerShell
Raw Normal View History

2025-10-23 11:16:35 +08:00
# 托普瑞控制库发布脚本 请在 chcp 65001下运行
2025-10-23 16:32:27 +08:00
# 配置参数 - 基本设置
2025-12-23 16:50:44 +08:00
# 使用PSScriptRoot获取脚本所在目录
$scriptDir = $PSScriptRoot
2025-10-23 11:16:35 +08:00
$output = Join-Path $scriptDir "Output"
$server = "http://47.111.181.23:8081/repository/nuget-releases/"
$key = "admin:admin"
$actualDllName = "JoyD.Windows.CS.Toprie.dll"
$targetDllName = "Toprie.dll"
2025-10-23 16:32:27 +08:00
2025-12-23 16:50:44 +08:00
# 从AssemblyInfo.cs获取版本号
$assemblyInfoPath = "$scriptDir\Toprie\Properties\AssemblyInfo.cs"
Write-Host "正在从 $assemblyInfoPath 获取版本号..."
if (-Not (Test-Path $assemblyInfoPath)) {
Write-Host "错误: 找不到AssemblyInfo.cs文件!" -ForegroundColor Red
exit 1
}
# 读取文件内容并提取AssemblyFileVersion
$assemblyInfoContent = Get-Content $assemblyInfoPath -Raw
$versionMatch = [regex]::Match($assemblyInfoContent, '\[assembly: AssemblyFileVersion\("([\d\.]+)"\)\]')
if (-Not $versionMatch.Success) {
Write-Host "错误: 无法从AssemblyInfo.cs中提取版本号!" -ForegroundColor Red
exit 1
}
$version = $versionMatch.Groups[1].Value
Write-Host "成功获取版本号: $version" -ForegroundColor Green
2025-10-23 16:32:27 +08:00
# NuGet包元数据配置 - 在此处修改所有元数据
$packageId = "com.joyd.toprie"
$title = "托普瑞热像仪控制库"
$authors = "曾庆明"
$owners = "JoyD Technology"
$description = "托普瑞热像仪控制库,提供热像仪设备的连接、配置和数据获取功能"
$copyright = "Copyright 2025 JoyD Technology"
$tags = @("thermal", "camera", "toprie", "红外", "热像仪", "控制")
$projectUrl = "https://github.com/joyd/toprie-library"
$licenseUrl = "https://opensource.org/licenses/MIT"
$iconFileName = "Toprie.png"
$iconSourcePath = Join-Path $scriptDir $iconFileName
2025-12-12 09:47:56 +08:00
$iconUrl = "http://47.111.181.23:8081/repository/gradle/main/Toprie.png"
2025-10-23 16:32:27 +08:00
$releaseNotes = "初始版本发布"
# 其他设置
2025-10-23 11:16:35 +08:00
$nupkgFileName = "$packageId.$version.nupkg"
$nupkgFilePath = Join-Path $output $nupkgFileName
Write-Host "========== 托普瑞库发布开始 =========="
# 创建输出目录
if (!(Test-Path $output)) {
New-Item -ItemType Directory -Path $output -Force | Out-Null
Write-Host "已创建输出目录: $output"
} else {
Write-Host "使用现有输出目录: $output"
}
2025-12-23 16:50:44 +08:00
# 定义pdb和xml文件名称
$actualPdbName = $actualDllName -replace "\.dll$", ".pdb"
$actualXmlName = $actualDllName -replace "\.dll$", ".xml"
$targetPdbName = $targetDllName -replace "\.dll$", ".pdb"
$targetXmlName = $targetDllName -replace "\.dll$", ".xml"
2025-10-23 11:16:35 +08:00
# 清理之前的构建文件
Write-Host "清理之前的构建文件.."
if (Test-Path "$output\$actualDllName") { Remove-Item -Path "$output\$actualDllName" -Force }
2025-12-23 16:50:44 +08:00
if (Test-Path "$output\$actualPdbName") { Remove-Item -Path "$output\$actualPdbName" -Force }
if (Test-Path "$output\$actualXmlName") { Remove-Item -Path "$output\$actualXmlName" -Force }
2025-10-23 11:16:35 +08:00
if (Test-Path "$output\$targetDllName") { Remove-Item -Path "$output\$targetDllName" -Force }
2025-12-23 16:50:44 +08:00
if (Test-Path "$output\$targetPdbName") { Remove-Item -Path "$output\$targetPdbName" -Force }
if (Test-Path "$output\$targetXmlName") { Remove-Item -Path "$output\$targetXmlName" -Force }
2025-10-23 11:16:35 +08:00
if (Test-Path "$output\$nupkgFileName") { Remove-Item -Path "$output\$nupkgFileName" -Force }
if (Test-Path "$output\$packageId.nuspec") { Remove-Item -Path "$output\$packageId.nuspec" -Force }
2025-12-23 16:50:44 +08:00
# 构建项目确保生成pdb和xml文件
2025-10-23 11:16:35 +08:00
Write-Host "1. 正在构建项目..."
Write-Host "当前目录: $scriptDir"
2025-12-23 16:50:44 +08:00
dotnet build "$scriptDir\Toprie\Toprie.csproj" -c Release -o "$output" /p:DebugSymbols=true /p:DebugType=full /p:DocumentationFile=true
2025-10-23 11:16:35 +08:00
if ($LASTEXITCODE -ne 0) {
Write-Host "错误: 构建失败!" -ForegroundColor Red
exit 1
}
# 验证DLL是否存在
if (!(Test-Path "$output\$actualDllName")) {
Write-Host "错误: 在输出目录中找不到$actualDllName!" -ForegroundColor Red
exit 1
}
# 复制DLL并重命名用于打包
Copy-Item -Path "$output\$actualDllName" -Destination "$output\$targetDllName" -Force
Write-Host "已将 $actualDllName 复制并重命名为$targetDllName 用于打包"
2025-12-23 16:50:44 +08:00
# 复制PDB和XML文件并重命名用于打包
if (Test-Path "$output\$actualPdbName") {
Copy-Item -Path "$output\$actualPdbName" -Destination "$output\$targetPdbName" -Force
Write-Host "已将 $actualPdbName 复制并重命名为$targetPdbName 用于打包"
}
if (Test-Path "$output\$actualXmlName") {
Copy-Item -Path "$output\$actualXmlName" -Destination "$output\$targetXmlName" -Force
Write-Host "已将 $actualXmlName 复制并重命名为$targetXmlName 用于打包"
}
# 复制图标文件到输出目录
if (Test-Path $iconSourcePath) {
Copy-Item -Path $iconSourcePath -Destination (Join-Path $output $iconFileName) -Force
Write-Host "已复制图标文件到输出目录: $iconFileName"
} else {
Write-Host "警告: 图标文件 $iconSourcePath 不存在,将使用默认图标" -ForegroundColor Yellow
}
2025-10-23 11:16:35 +08:00
Write-Host "2. 准备打包文件..."
# 在Output目录创建NuGet.Config文件这样dotnet nuget push命令就能正确找到它
$localNugetConfigPath = Join-Path -Path $output -ChildPath "NuGet.Config"
# 创建包含全面配置的NuGet.Config文件
$nugetConfigContent = @"
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="allowInsecureConnections" value="true" />
<add key="DefaultPushSource" value="$server" />
</config>
<packageSources>
<add key="JoyD-Private" value="$server" />
</packageSources>
<packageSourceMapping>
<packageSource key="JoyD-Private">
<package pattern="*" />
</packageSource>
</packageSourceMapping>
<packageSourceCredentials>
<JoyD-Private>
<add key="Username" value="admin" />
<add key="Password" value="YWRtaW46YWRtaW4=" />
</JoyD-Private>
</packageSourceCredentials>
</configuration>
"@
# 设置配置文件
Set-Content -Path $localNugetConfigPath -Value $nugetConfigContent
Write-Host "已在Output目录设置NuGet.Config: $localNugetConfigPath"
# 创建一个简单的.csproj文件用于打包无需还原
$tempProjContent = @"
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net40</TargetFramework>
<PackageId>$packageId</PackageId>
<Version>$version</Version>
2025-10-23 16:32:27 +08:00
<Title>$title</Title>
<Authors>$authors</Authors>
<Owners>$owners</Owners>
<Description>$description</Description>
<Copyright>$copyright</Copyright>
<PackageTags>$tagsString</PackageTags>
<PackageProjectUrl>$projectUrl</PackageProjectUrl>
<PackageLicenseUrl>$licenseUrl</PackageLicenseUrl>
2025-12-12 09:47:56 +08:00
<PackageIcon>$iconFileName</PackageIcon>
<PackageIconUrl></PackageIconUrl>
2025-10-23 16:32:27 +08:00
<PackageReleaseNotes>$releaseNotes</PackageReleaseNotes>
2025-10-23 11:16:35 +08:00
<PackageOutputPath>.</PackageOutputPath>
<RestorePackagesConfig>true</RestorePackagesConfig>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<IsPackable>true</IsPackable>
<NoBuild>true</NoBuild>
</PropertyGroup>
<ItemGroup>
<Content Include="$targetDllName" Pack="true" PackagePath="lib\net40\$targetDllName" />
2025-12-23 16:50:44 +08:00
<Content Include="$targetPdbName" Pack="true" PackagePath="lib\net40\$targetPdbName" />
<Content Include="$targetXmlName" Pack="true" PackagePath="lib\net40\$targetXmlName" />
2025-12-12 09:47:56 +08:00
<Content Include="$iconFileName" Pack="true" PackagePath="$iconFileName" />
2025-10-23 11:16:35 +08:00
</ItemGroup>
</Project>
"@
$tempProjPath = "$output\Temp.csproj"
Set-Content -Path $tempProjPath -Value $tempProjContent
# 在输出目录中创建包结构
Write-Host "3. 创建NuGet包.."
2025-10-23 16:32:27 +08:00
# 在输出目录中创建完整的nuspec文件
2025-10-23 11:16:35 +08:00
$nuspecPath = Join-Path $output "$packageId.nuspec"
2025-10-23 16:32:27 +08:00
# 将tags数组转换为空格分隔的字符串
$tagsString = $tags -join " "
2025-10-23 11:16:35 +08:00
$nuspecContent = @"
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>$packageId</id>
<version>$version</version>
2025-10-23 16:32:27 +08:00
<title>$title</title>
<authors>$authors</authors>
<owners>$owners</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description</description>
<releaseNotes>$releaseNotes</releaseNotes>
<copyright>$copyright</copyright>
<tags>$tagsString</tags>
<projectUrl>$projectUrl</projectUrl>
<licenseUrl>$licenseUrl</licenseUrl>
2025-12-12 09:47:56 +08:00
<icon>$iconFileName</icon>
<iconUrl>$iconUrl</iconUrl>
2025-10-23 16:32:27 +08:00
<!-- 可以根据需要添加更多元数据如依赖项框架引用等 -->
<!-- <dependencies> -->
<!-- <group targetFramework=".NETFramework4.0"> -->
<!-- <dependency id="Newtonsoft.Json" version="13.0.1" /> -->
<!-- </group> -->
<!-- </dependencies> -->
2025-10-23 11:16:35 +08:00
</metadata>
<files>
<file src="$targetDllName" target="lib\net40\" />
2025-12-23 16:50:44 +08:00
<file src="$targetPdbName" target="lib\net40\" />
<file src="$targetXmlName" target="lib\net40\" />
2025-12-12 09:47:56 +08:00
<file src="$iconFileName" target="\" />
2025-10-23 11:16:35 +08:00
</files>
</package>
"@
2025-10-23 16:32:27 +08:00
Set-Content -Path $nuspecPath -Value $nuspecContent -Encoding UTF8
2025-10-23 11:16:35 +08:00
Write-Host "已创建nuspec文件: $nuspecPath"
# 手动创建nupkg包结构
Write-Host "创建包结构.."
$tempDir = Join-Path $output "temp_pkg"
# 确保临时目录存在
if (!(Test-Path $tempDir)) {
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
}
# 创建lib/net40子目录
$libDir = Join-Path $tempDir "lib\net40"
if (!(Test-Path $libDir)) {
New-Item -ItemType Directory -Path $libDir -Force | Out-Null
}
# 复制DLL到包结构中
$sourceDllPath = Join-Path $output $targetDllName
$destDllPath = Join-Path $libDir $targetDllName
Copy-Item -Path $sourceDllPath -Destination $destDllPath -Force
Write-Host "已复制$sourceDllPath$destDllPath"
2025-12-23 16:50:44 +08:00
# 复制PDB文件到包结构中
$sourcePdbPath = Join-Path $output $targetPdbName
$destPdbPath = Join-Path $libDir $targetPdbName
if (Test-Path $sourcePdbPath) {
Copy-Item -Path $sourcePdbPath -Destination $destPdbPath -Force
Write-Host "已复制$sourcePdbPath$destPdbPath"
}
# 复制XML文件到包结构中
$sourceXmlPath = Join-Path $output $targetXmlName
$destXmlPath = Join-Path $libDir $targetXmlName
if (Test-Path $sourceXmlPath) {
Copy-Item -Path $sourceXmlPath -Destination $destXmlPath -Force
Write-Host "已复制$sourceXmlPath$destXmlPath"
}
# 复制图标文件到包结构根目录
$sourceIconPath = Join-Path $output $iconFileName
$destIconPath = Join-Path $tempDir $iconFileName
if (Test-Path $sourceIconPath) {
Copy-Item -Path $sourceIconPath -Destination $destIconPath -Force
Write-Host "已复制图标文件到包结构: $destIconPath"
} else {
Write-Host "警告: 在输出目录中找不到图标文件 $sourceIconPath" -ForegroundColor Yellow
}
2025-10-23 11:16:35 +08:00
# 创建[Content_Types].xml文件有效nupkg必需
$contentTypesPath = Join-Path $tempDir "[Content_Types].xml"
# 使用-LiteralPath参数避免方括号被解释为通配符
2025-12-12 09:47:56 +08:00
Set-Content -LiteralPath $contentTypesPath -Value "<?xml version='1.0' encoding='utf-8'?><Types xmlns='http://schemas.openxmlformats.org/package/2006/content-types'><Default Extension='dll' ContentType='application/octet-stream'/><Default Extension='xml' ContentType='application/xml'/><Default Extension='png' ContentType='image/png'/></Types>"
2025-10-23 11:16:35 +08:00
Write-Host "已在 $contentTypesPath 创建[Content_Types].xml"
2025-10-23 15:41:28 +08:00
# 复制nuspec文件到包结构根目录
$sourceNuspecPath = Join-Path $output "$packageId.nuspec"
$destNuspecPath = Join-Path $tempDir "$packageId.nuspec"
if (Test-Path $sourceNuspecPath) {
Copy-Item -Path $sourceNuspecPath -Destination $destNuspecPath -Force
Write-Host "已复制nuspec文件到包结构: $destNuspecPath"
} else {
Write-Host "警告: 找不到nuspec文件 $sourceNuspecPath" -ForegroundColor Yellow
}
2025-10-23 11:16:35 +08:00
# 创建zip文件并将其重命名为nupkg
$zipPath = Join-Path $output "temp_manual.zip"
# 清理任何现有文件
if (Test-Path $zipPath) {
Remove-Item -Path $zipPath -Force
Write-Host "已移除现有的zip文件"
}
if (Test-Path $nupkgFilePath) {
Remove-Item -Path $nupkgFilePath -Force
Write-Host "已移除现有的nupkg文件"
}
Write-Host "$tempDir\* 创建zip归档到$zipPath..."
Compress-Archive -Path "$tempDir\*" -DestinationPath $zipPath -Force
if (Test-Path $zipPath) {
Write-Host "正在将$zipPath 重命名为 $nupkgFilePath..."
Rename-Item -Path $zipPath -NewName $nupkgFileName -Force
if (Test-Path $nupkgFilePath) {
$nupkgFile = Get-Item $nupkgFilePath
Write-Host "✅ 成功创建包: $($nupkgFile.FullName)"
Write-Host "包大小: $([math]::Round($nupkgFile.Length / 1KB, 2)) KB"
}
}
# 清理临时文件夹
Remove-Item -Path $tempDir -Recurse -Force
Write-Host "已清理临时文件夹"
# 检查包文件是否存在
if (Test-Path $nupkgFilePath) {
$nupkgFile = Get-Item $nupkgFilePath
Write-Host "找到包文件: $($nupkgFile.FullName)"
Write-Host "4. 正在发布包到仓库..."
# 使用curl直接发布到HTTP仓库
Write-Host "使用curl直接发布到HTTP仓库..."
# 构建curl命令
$curlCommand = "curl.exe -X PUT -u $key -F package=@$nupkgFilePath $server"
Write-Host "正在执行: $curlCommand"
# 执行curl命令使用curl.exe避免PowerShell别名冲突
& curl.exe -X PUT -u $key -F package=@$nupkgFilePath $server
if ($LASTEXITCODE -eq 0) {
Write-Host "✅ 成功: 包发布成功" -ForegroundColor Green
Write-Host "包ID: $packageId"
Write-Host "版本: $version"
2025-10-23 16:32:27 +08:00
Write-Host "标题: $title"
Write-Host "作者: $authors"
Write-Host "所有者: $owners"
Write-Host "描述: $description"
Write-Host "标签: $tagsString"
2025-10-23 11:16:35 +08:00
Write-Host "仓库: $server"
} else {
Write-Host "❌ 错误: 包创建成功但发布失败" -ForegroundColor Red
Write-Host "包文件位置: $($nupkgFile.FullName)"
Write-Host "
您可以尝试手动发布使用curl命令"
Write-Host "curl -X PUT -u admin:admin -F \"package=@$($nupkgFile.FullName)\" $server"
Write-Host "
或者直接在项目中使用创建的包文件"
}
} else {
Write-Host "❌ 错误: 未创建NuGet包" -ForegroundColor Red
Write-Host "请检查dotnet CLI是否正确配置"
Write-Host "您可以尝试使用Output目录中的文件手动创建包"
}
# 最终清理
Write-Host "正在执行最终清理.."
if (Test-Path "$output\Temp.csproj") {
Remove-Item -Path "$output\Temp.csproj" -Force
Write-Host "已移除Temp.csproj"
}
# 删除我们在Output目录创建的NuGet.Config文件
if (Test-Path $localNugetConfigPath) {
Remove-Item -Path $localNugetConfigPath -Force
Write-Host "已删除临时创建的NuGet.Config"
}
Write-Host "
发布过程完成"