构建后压缩源代码

时间:2012-10-05 12:11:26

标签: .net visual-studio-2010 build msbuild xcopy

每次构建解决方案时,我都必须制作源代码的压缩zip文件。 我试过this,所以在项目属性页面上,我在“构建后事件”文本框中有这个代码:

if $(ConfigurationName)==Release 
   xcopy /df $(ProjectDir)$(TargetName)*.cs $(ProjectDir)$(TargetName)Source.zip
if $(ConfigurationName)==Release 
   xcopy /df $(TargetDir)$(TargetName)$(TargetExt) $(ProjectDir)$(TargetName)Runtime.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\Framework\v4.0.30319\Microsoft.Common.targets  3717    9   BuildEventHooks

我怎样才能让它发挥作用? 我正在使用Visual Studio 2010和.NET 4(MSBuild 4)

1 个答案:

答案 0 :(得分:0)

在这种情况下,请使用合适的源控件,如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
}