不编辑csproj文件,获取MSBuild将标记为内容的所有文件复制到文件夹,保留文件夹结构? [英] Without editing the csproj file, get MSBuild to copy all files marked as Content to a folder, preserving folder structure?

查看:346
本文介绍了不编辑csproj文件,获取MSBuild将标记为内容的所有文件复制到文件夹,保留文件夹结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题



最后,我们希望使用命令行复制 csproj 标记为 Content ,同时保留目录结构,而无需编辑原始的 csprog 文件。
$ b

我们已经看到我如何获得MSBuild将所有标记为内容的文件复制到文件夹,保留文件夹结构?这对我们不起作用,因为它涉及编辑 csproj 文件。我们想避免这样做。



MSBuild?



我们已经考虑过使用 msbuild 通过命令行,但我们不知道如何要求它做相当于

 < Target Name =CopyContentFiles> 
< Copy SourceFiles =@(Content)
DestinationFiles =@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')/> ;
< / Target>

换句话说,我们想使用命令行 define 一个目标(不只是指定一个)从命令行。例如。在伪代码中我们想要这样:

  msbuild MyApp.csproj / t:Copy SourceFiles =@ = ...



XCopy?



我们还想过使用 xcopy ,虽然我们还没有确定如何请求它只复制 csproj 标记为内容

解决方案

p>

如果您有一个脚本Get-VSProjectItems.ps1,像这样:

  param(
[Parameter(Mandatory = $ true)] $ project


$ ErrorActionPreference ='stop';
$ project =(resolve-path $ project).Path;
$ projectDir = Split-Path $ project -Parent;
$ ns = @ {
msb ='http://schemas.microsoft.com/developer/msbuild/2003';
}

Select-Xml -Path:$ project -XPath:'// msb:Content'-Namespace:$ ns |
Select-Object -ExpandProperty:Node |
%{
New-Object psobject -Property:@ {
RelPath = $ _。Include;
Directory =(Split-Path -Parent $ _。Include);
FullName = Join-Path $ projectDir $ _。Include;
}
}

然后复制内容文件就像管道输出到创建目标文件夹结构并复制文件的脚本块(nb:下面的片段假定您要在当前工作目录中重新创建目录树):

  .\Get-VSProjectItems.ps1 ..\SomeProject\SomeProject.csproj | %{
[void](mkdir $ _。Directory -ErrorAction:SilentlyContinue);
copy-item $ _。fullname $ _。relpath;
}


Question

Ultimately, we would like to use the command line to copy all files that a csproj has marked as Content, while preserving the directory structure, and without editing the original csprog file.

We have seen How can I get MSBuild to copy all files marked as Content to a folder, preserving folder structure? This doesn't work for us, because it involves editing the csproj file. We want to avoid doing that.

MSBuild?

We've thought of using msbuild via the command line but we don't know how to ask it to do just the equivalent of this:

<Target Name="CopyContentFiles">
  <Copy SourceFiles="@(Content)"
        DestinationFiles="@(Content->'$(DestFolder)%(RelativeDir)%(Filename)%(Extension)')"/>
</Target>

In other words, we would like to use the command line to define a target (not just specify one) from at the command line. E.g. in pseudo-code we want this:

msbuild MyApp.csproj /t:"Copy SourceFiles="@(Content)" DestinationFiles=..."

XCopy?

We've also thought of using xcopy though we haven't determined how to ask it to only copy files that the csproj marks as Content.

解决方案

I use PowerShell.

If you have a script Get-VSProjectItems.ps1 like this:

param(
    [Parameter(Mandatory=$true)] $project
)

$ErrorActionPreference = 'stop';
$project = (resolve-path $project).Path;
$projectDir = Split-Path $project -Parent;
$ns = @{
    msb = 'http://schemas.microsoft.com/developer/msbuild/2003';
}

Select-Xml -Path:$project -XPath:'//msb:Content' -Namespace:$ns |
    Select-Object -ExpandProperty:Node | 
    % {
        New-Object psobject -Property:@{
            RelPath = $_.Include;
            Directory = (Split-Path -Parent $_.Include);
            FullName = Join-Path $projectDir $_.Include;
        }
    }

Then copying the content files becomes as simple as piping the output to a script block that creates the target folder structure and copies the file (nb: fragment below assumes you want to recreate the directory tree in current working directory):

.\Get-VSProjectItems.ps1 ..\SomeProject\SomeProject.csproj | % { 
    [void](mkdir $_.Directory -ErrorAction:SilentlyContinue); 
    copy-item $_.fullname $_.relpath;
}

这篇关于不编辑csproj文件,获取MSBuild将标记为内容的所有文件复制到文件夹,保留文件夹结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆