.net 编译后压缩源代码
每次构建解决方案时,我都必须制作源代码的压缩zip文件。 我已经尝试过了,所以在项目属性页面的“后期生成事件”文本框中有以下代码: 但它不起作用 上面说.net 编译后压缩源代码,.net,visual-studio-2010,build,msbuild,xcopy,.net,Visual Studio 2010,Build,Msbuild,Xcopy,每次构建解决方案时,我都必须制作源代码的压缩zip文件。 我已经尝试过了,所以在项目属性页面的“后期生成事件”文本框中有以下代码: 但它不起作用 上面说 Error 1 The command "if ==Release xcopy /df *.csSource.zip if ==Release xcopy /df Runtime.zip " exited with code 255. c:\Windows\Microsoft.NET\F
Error 1 The command "if ==Release xcopy /df *.csSource.zip
if ==Release xcopy /df Runtime.zip
" exited with code 255. c:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 3717 9 BuildEventHooks
我怎样才能让它工作?
我使用的是Visual Studio 2010和.NET 4(MSBuild 4)在本例中,请使用合适的源代码管理,如SVN或GIT。两者都提供了创建“外部”的能力,这将解决您的问题 您的想法似乎很奇怪,因为构建时间将大大增加 这就是说,如果您想允许创建插件,请在自定义DLL中隔离您的插件契约(可能是一个接口),并发送此DLL。为什么要发布源代码来创建可组合的应用程序 最后,仅供参考,xcopy不会压缩文件。您可以使用7zip.exe或类似的压缩工具,或创建在生成后事件中启动的参数化powershell脚本。脚本可以包含预期的压缩 下面是一个可以压缩内容的示例函数:
function Compress-Directory
{
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$SourceDirectory,
[Parameter(Mandatory=$true)]
[string]
$Target,
[ValidateSet("Auto", "Cab", "Zip")] # Supported formats : Cab or Auto for infering the format from the file extension
[string]
$Format = "Auto",
[Switch]$Force
)
process{
if($Format -Match "auto") {
switch([System.IO.Path]::GetExtension($Target).ToLower())
{
".cab" { $Format = "Cab" }
".zip" { $Format = "Zip" }
default { throw "Could not infer the kind of archive from the target file name. Please specify a file name with a known extention, or use the 'Format' parameter to specify it" }
}
}
if(Test-Path($Target))
{
if($Force) {
Write-Verbose "Deleting existing archive $Target"
Remove-Item $Target -Force
Write-Verbose "Deleted existing archive $Target"
}else{
throw "The target file '$Target' already exists. Either delete it or use the -Force switch"
}
}
switch($Format) {
"Cab" {
[void][reflection.assembly]::LoadFile((Join-Path $PSScriptRoot "CabLib.dll"))
## the cablib dll can be downloaded from http://wspbuilder.codeplex.com
$c = new-object CabLib.Compress
$c.CompressFolder($SourceDirectory, $Target, $null, $null, $null, 0)
## thanks to http://www.pseale.com for this function
}
"Zip" {
Get-ChildItem $SourceDirectory | Compress-ToZip $Target
}
default { throw "No compress method known for $Format files."}
}
}
}
function Compress-ToZip {
param([string]$zipfilename)
Write-Host "Creating the archive $zipfilename"
if(-not (test-path($zipfilename))) {
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(Get-ChildItem $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input) {
$zipPackage.CopyHere($file.FullName)
$size = $zipPackage.Items().Item($file.Name).Size
Write-Host "Adding $file"
while($zipPackage.Items().Item($file.Name) -Eq $null)
{
start-sleep -seconds 1
write-host "." -nonewline
}
write-host " Done" -ForegroundColor Green
}
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zipPackage) | out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shellApplication)| out-Null
}
我看它不认识它。但是为什么呢?在“宏”窗口中,我有ConfigurationName宏。另外,如果xcopy不这样做,我如何压缩源代码?有没有内置的命令?你真的想让每个构建花费更多的时间吗?您想要实现什么?我必须在构建服务器上的每个构建上从我们的部分源代码生成一个zip文件。这是必需的,因为源代码包含我们希望其他开发人员可以使用的插件,以开发他们自己的插件。我也尝试过这个简化版本(我以前创建了xcopy可能需要的zip文件):xcopy/df$(ProjectDir)$(TargetName)*.cs$(ProjectDir)$(TargetName)Source.zip但这也不起作用:“命令”xcopy/df*.cs Source.zip“已退出,代码为2”。看起来在生成过程中很多宏都无法识别。我们使用TFS。我认为可以很容易地定义一个构建后脚本,它将在构建服务器上运行,而不是在本地机器上运行。源不是太大,所以生成归档应该很快。我们必须提供样本来源以支持我们的合作伙伴。我看到您已经编辑了您的答案。谢谢这个功能!我会看一下,但目前我在想,也许有一个TFS构建目标,可以把我的文件压缩到Team Foundation Server上。我想我会以更具体的格式重新发布这个问题。
function Compress-Directory
{
[CmdLetBinding()]
param(
[Parameter(Mandatory=$true)]
[string]
$SourceDirectory,
[Parameter(Mandatory=$true)]
[string]
$Target,
[ValidateSet("Auto", "Cab", "Zip")] # Supported formats : Cab or Auto for infering the format from the file extension
[string]
$Format = "Auto",
[Switch]$Force
)
process{
if($Format -Match "auto") {
switch([System.IO.Path]::GetExtension($Target).ToLower())
{
".cab" { $Format = "Cab" }
".zip" { $Format = "Zip" }
default { throw "Could not infer the kind of archive from the target file name. Please specify a file name with a known extention, or use the 'Format' parameter to specify it" }
}
}
if(Test-Path($Target))
{
if($Force) {
Write-Verbose "Deleting existing archive $Target"
Remove-Item $Target -Force
Write-Verbose "Deleted existing archive $Target"
}else{
throw "The target file '$Target' already exists. Either delete it or use the -Force switch"
}
}
switch($Format) {
"Cab" {
[void][reflection.assembly]::LoadFile((Join-Path $PSScriptRoot "CabLib.dll"))
## the cablib dll can be downloaded from http://wspbuilder.codeplex.com
$c = new-object CabLib.Compress
$c.CompressFolder($SourceDirectory, $Target, $null, $null, $null, 0)
## thanks to http://www.pseale.com for this function
}
"Zip" {
Get-ChildItem $SourceDirectory | Compress-ToZip $Target
}
default { throw "No compress method known for $Format files."}
}
}
}
function Compress-ToZip {
param([string]$zipfilename)
Write-Host "Creating the archive $zipfilename"
if(-not (test-path($zipfilename))) {
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(Get-ChildItem $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input) {
$zipPackage.CopyHere($file.FullName)
$size = $zipPackage.Items().Item($file.Name).Size
Write-Host "Adding $file"
while($zipPackage.Items().Item($file.Name) -Eq $null)
{
start-sleep -seconds 1
write-host "." -nonewline
}
write-host " Done" -ForegroundColor Green
}
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($zipPackage) | out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($shellApplication)| out-Null
}