1

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

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)

4

1 回答 1

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
}
于 2012-10-05T12:33:27.683 回答