2014-09-08 3 views
0

Я написал пользовательскую задачу, которая создает файлы, создавая параллельный файл с другим расширением.Может ли MSBuild изменить собственный файл проекта?

Когда MSBuild идет и создает такой файл, я бы хотел добавить его к самому файлу проекта. Я также хотел бы вложить встроенный файл под номером источник (с DependentUpon).

Может ли MSBuild сделать это? Нужно ли перезагрузить проект? Могу ли я автоматизировать все это?

Ниже мой .targets файл, который получает установлен NuGet при добавлении моего пакета:

<UsingTask TaskName="PreCompiler" AssemblyFile="..\tools\Compiler.dll"> 
</UsingTask> 

<UsingTask TaskName="GenerateDependencies" AssemblyFile="..\tools\Compiler.dll"> 
</UsingTask> 

<PropertyGroup> 
    <BuildDependsOn> 
    $(BuildDependsOn); 
    BuildCoffeeFiles; 
    BuildLessFiles 
    </BuildDependsOn> 
    <ContentDir>Content\</ContentDir> 
</PropertyGroup> 

<ItemGroup Condition="'$(BuildingInsideVisualStudio)'=='true'"> 
    <AvailableItemName Include="CoffeeScript" /> 
    <AvailableItemName Include="Less" /> 
</ItemGroup> 

<Target Name="GenerateCoffeeDependencies"> 
    <GenerateDependencies Include="@(CoffeeScript->'%(FullPath)')"> 
    <Output TaskParameter="Dependencies" ItemName="InputCoffeeFiles"/> 
    </GenerateDependencies> 
    <ItemGroup> 
    <InputCoffeeFiles 
     Include="@(InputCoffeeFiles->Distinct())" 
     KeepDuplicates='false' /> 
    </ItemGroup> 
</Target> 

<Target Name="BuildCoffeeFiles" 
     DependsOnTargets="GenerateCoffeeDependencies" 
     Inputs="@(InputCoffeeFiles)" 
     Outputs="@(CoffeeScript->'%(RelativeDir)%(Filename).js')"> 
    <PreCompiler Include="@(CoffeeScript->'%(FullPath)')" /> 
</Target> 

<Target Name="GenerateLessDependencies"> 
    <GenerateDependencies Include="@(Less->'%(FullPath)')"> 
    <Output TaskParameter="Dependencies" ItemName="InputLessFiles"/> 
    </GenerateDependencies> 
    <ItemGroup> 
    <InputLessFiles 
     Include="@(InputLessFiles->Distinct())" 
     KeepDuplicates='false' /> 
    </ItemGroup> 
</Target> 

<Target Name="BuildLessFiles" 
     DependsOnTargets="GenerateLessDependencies" 
     Inputs="@(InputLessFiles)" 
     Outputs="@(Less->'%(RelativeDir)%(Filename).css')"> 
    <PreCompiler Include="@(Less->'%(FullPath)')" /> 
</Target> 
+0

Не могли бы вы предоставить некоторый код из msbuild? Порядок в этом случае важен. – Marlos

+0

Это почти полный контент. Compiler.dll обрабатывает живую перекомпиляцию, когда это необходимо в моем приложении Nancy, поскольку мне не особенно нравится Squishit или Cassette – Crisfole

+0

возможный дубликат [Modify .csproj in MSBuild] (http://stackoverflow.com/questions/25435857/modify- csproj-in-msbuild) –

ответ

1

Вы можете применить задачу MSBuild XslTransformation в проекте, прежде чем строить его, добавив нужный файл.

Это образец файла преобразования:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:ms="http://schemas.microsoft.com/developer/msbuild/2003"> 

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 

    <!-- Identity. --> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
     <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="//ms:Compile[@Include='Program.cs']"> 
     <xsl:copy> 
     <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
     <xsl:element name="DependentUpon" 
     namespace="http://schemas.microsoft.com/developer/msbuild/2003"> 
      <xsl:text>Output.cs</xsl:text> 
     </xsl:element> 
    </xsl:template> 

</xsl:stylesheet> 

А вот MSBuild файла целевой выборки:

<Project DefaultTargets="Build" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> 

    <Target Name="Build"> 
     <XslTransformation 
      XmlInputPaths="ConsoleApplication1.csproj" 
      OutputPaths="ConsoleApplication1.transformed.csproj" 
      XslInputPath="proj.xslt"> 
     </XslTransformation> 

     <MSBuild Projects="ConsoleApplication1.transformed.csproj" Targets="Build" /> 
    </Target> 
</Project> 

Вам нужно будет строить с .NET 4.0, по крайней мере. Для этого укажите ToolsVersion в файле msbuild.

Смежные вопросы