# 托普瑞控制库发布脚本 请在 chcp 65001下运行 # 配置参数 - 基本设置 $scriptDir = Get-Location $output = Join-Path $scriptDir "Output" $server = "http://47.111.181.23:8081/repository/nuget-releases/" $key = "admin:admin" $actualDllName = "JoyD.Windows.CS.CubeLib.dll" $targetDllName = "JoyD.Windows.CS.CubeLib.dll" # 保持与AssemblyName一致,避免引用问题 # NuGet包元数据配置 - 在此处修改所有元数据 $packageId = "com.joyd.cubelib" # 从AssemblyInfo.cs的AssemblyFileVersion获取版本号 $assemblyInfoPath = Join-Path $scriptDir "CubeLib\Properties\AssemblyInfo.cs" if (Test-Path $assemblyInfoPath) { $assemblyInfoContent = Get-Content -Path $assemblyInfoPath -Encoding UTF8 -Raw # 只从AssemblyFileVersion获取版本号 $fileVersionMatch = [regex]::Match($assemblyInfoContent, '(? "@ # 设置配置文件 Set-Content -Path $localNugetConfigPath -Value $nugetConfigContent Write-Host "已在Output目录设置NuGet.Config: $localNugetConfigPath" # 创建一个简单的.csproj文件用于打包,无需还原 $tempProjContent = @" net40 $packageId $version $title $authors $owners $description $copyright $tagsString $projectUrl $licenseUrl $iconFileName $releaseNotes . true true true true "@ $tempProjPath = "$output\Temp.csproj" Set-Content -Path $tempProjPath -Value $tempProjContent # 在输出目录中创建包结构 Write-Host "3. 创建NuGet包.." # 在输出目录中创建完整的nuspec文件 $nuspecPath = Join-Path $output "$packageId.nuspec" # 将tags数组转换为空格分隔的字符串 $tagsString = $tags -join " " $nuspecContent = @" $packageId $version $title $authors $owners false $description $releaseNotes $copyright $tagsString $projectUrl $licenseUrl $iconFileName $iconUrl "@ Set-Content -Path $nuspecPath -Value $nuspecContent -Encoding UTF8 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" # 复制pdb文件到包结构中 $targetPdbName = [System.IO.Path]::ChangeExtension($targetDllName, ".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文档文件 $targetXmlName = [System.IO.Path]::ChangeExtension($targetDllName, ".xml") $sourceXmlPath = Join-Path $output $targetXmlName # 初始化binDir变量 $binDir = Join-Path $scriptDir "CubeLib\bin\$configuration" # 尝试从bin目录复制XML文件 $binXmlPath = Join-Path $binDir $targetXmlName if (Test-Path $binXmlPath) { Copy-Item -Path $binXmlPath -Destination $sourceXmlPath -Force Write-Host "Copied $binXmlPath to $sourceXmlPath" } else { # 如果bin目录中没有xml文件,尝试生成一个包含文档注释的xml文件 $xmlContent = @' JoyD.Windows.CS.CubeLib WebSocket客户端接口 WebSocket客户端实现 WebSocket服务器接口 WebSocket服务器实现 '@ Set-Content -Path $sourceXmlPath -Value $xmlContent -Encoding UTF8 Write-Host "Created XML documentation file with basic comments at $sourceXmlPath" } # 移除重复的复制逻辑 $destXmlPath = Join-Path $libDir $targetXmlName if (Test-Path $sourceXmlPath) { Copy-Item -Path $sourceXmlPath -Destination $destXmlPath -Force Write-Host "已复制 $sourceXmlPath 到 $destXmlPath" } # 复制websocket-sharp.dll到包结构中 $webSocketSharpDll = "websocket-sharp.dll" $sourceWebSocketSharpPath = Join-Path $output $webSocketSharpDll $destWebSocketSharpPath = Join-Path $libDir $webSocketSharpDll if (Test-Path $sourceWebSocketSharpPath) { Copy-Item -Path $sourceWebSocketSharpPath -Destination $destWebSocketSharpPath -Force Write-Host "已复制$sourceWebSocketSharpPath 到$destWebSocketSharpPath" } # 复制websocket-sharp.pdb到包结构中 $webSocketSharpPdb = "websocket-sharp.pdb" $sourceWebSocketSharpPdbPath = Join-Path $output $webSocketSharpPdb $destWebSocketSharpPdbPath = Join-Path $libDir $webSocketSharpPdb if (Test-Path $sourceWebSocketSharpPdbPath) { Copy-Item -Path $sourceWebSocketSharpPdbPath -Destination $destWebSocketSharpPdbPath -Force Write-Host "已复制$sourceWebSocketSharpPdbPath 到$destWebSocketSharpPdbPath" } # 复制websocket-sharp.xml到包结构中 $webSocketSharpXml = "websocket-sharp.xml" $sourceWebSocketSharpXmlPath = Join-Path $output $webSocketSharpXml $destWebSocketSharpXmlPath = Join-Path $libDir $webSocketSharpXml if (Test-Path $sourceWebSocketSharpXmlPath) { Copy-Item -Path $sourceWebSocketSharpXmlPath -Destination $destWebSocketSharpXmlPath -Force Write-Host "已复制$sourceWebSocketSharpXmlPath 到$destWebSocketSharpXmlPath" } # 复制图标文件到包结构根目录 $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 } # 创建[Content_Types].xml文件(有效nupkg必需) $contentTypesPath = Join-Path $tempDir "[Content_Types].xml" # 使用-LiteralPath参数避免方括号被解释为通配符 Set-Content -LiteralPath $contentTypesPath -Value "" Write-Host "已在 $contentTypesPath 创建[Content_Types].xml" # 复制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 } # 创建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" Write-Host "标题: $title" Write-Host "作者: $authors" Write-Host "所有者: $owners" Write-Host "描述: $description" Write-Host "标签: $tagsString" 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 "发布过程完成"