2016-10-10 2 views
1

私は既存のスクリプト(Visual Studioを使用して生成)からインポートしてMSBuildを介して実行して、構成情報を抽出しようとしています(CLCompileLinkLib 、など)'mining'設定情報のためのmsbuildプロジェクトファイル

SEN_EXTRACT_CONFIG_GetProjectConfigurationターゲットは、構成/プラットフォームの組み合わせごとに1回実行され、それぞれについて、プロジェクトファイルに対して複数回MSBuildが実行され、各項目リストからそれぞれ1つのアイテムが取得されます時間。このアイテムには、そのコンフィグレーションに設定されている「デフォルト」のメタデータが必要です(プロジェクトのItemDefinitionGroupのおかげで)

私はMSBuildにはかなり新しく、これまでのところはありますが、私の期待どおりの仕事。インラインタスクの中では、私はちょうど1つではなく、4つのアイテムを取得します(私はターゲット/タスクのバッチ処理が正しく分かっていないと推測しています)... CLCompileLinkなど持っていただろう。同様に素晴らしいことだVisual StudioのGUIを、必要としない、このためのより良い方法があれば自分のアイデンティティ「XXX_SHOULD_BE_UNIQUE」いえ...

です。

<?xml version="1.0" encoding="utf-8"?> 
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" > 
    <UsingTask TaskName="LogItem" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"> 
    <ParameterGroup> 
     <Items ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" /> 
     <ItemName Required="true" /> 
     <ItemKey Required="true" /> 
     <ItemLog Output="true" /> 
    </ParameterGroup> 
    <Task> 
     <Code Type="Fragment" Language="cs"> 
<![CDATA[ 
     System.Diagnostics.Debugger.Break(); // So I can debug, comment out to prevent the build from failing. 
     bool found = false; 
     foreach (var item in Items) 
     { 
      if (item.GetMetadata("Identity") == ItemKey) 
      { 
      if (found) return false; 
      found = true; 

      ItemLog = ItemName + "\n"; 
      foreach (var key in item.MetadataNames) 
      { 
       if ("Identity" == key) continue; 
       ItemLog = ItemLog + (String)key + ": " + item.GetMetadata((String)key) + "\n"; 
      } 
      } 
     } 

     return found; 
]]> 
     </Code> 
    </Task> 
    </UsingTask> 

    <PropertyGroup> 
    <UniqueKey>XXX_SHOULD_BE_UNIQUE</UniqueKey> 
    </PropertyGroup> 

    <Target Name="SEN_EXTRACT_CONFIG_GetProjectConfiguration" Outputs="@(ProjectConfiguration->'%(Configuration)|%(Platform)')" > 
    <Message Text="%(ProjectConfiguration.Configuration)|%(ProjectConfiguration.Platform)" /> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetCLCompile" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="CLCompileTemplate" /> 
    </MSBuild> 
    <LogItem Items="@(CLCompileTemplate)" ItemKey="$(UniqueKey)" ItemName="CLCompile" > 
     <Output TaskParameter="ItemLog" PropertyName="CLCompileTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetLink" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="LinkTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(LinkTemplate)" ItemKey="$(UniqueKey)" ItemName="Link" > 
     <Output TaskParameter="ItemLog" PropertyName="LinkTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetLib" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="LibTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(LibTemplate)" ItemKey="$(UniqueKey)" ItemName="Lib" > 
     <Output TaskParameter="ItemLog" PropertyName="LibTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetResourceCompile" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="ResourceCompileTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(ResourceCompileTemplate)" ItemKey="$(UniqueKey)" ItemName="ResourceCompile" > 
     <Output TaskParameter="ItemLog" PropertyName="ResourceCompileTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetProjectReference" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="ProjectReferenceTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(ProjectReferenceTemplate)" ItemKey="$(UniqueKey)" ItemName="ProjectReference" > 
     <Output TaskParameter="ItemLog" PropertyName="ProjectReferenceTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetPostBuildEvent" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="PostBuildEventTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(PostBuildEventTemplate)" ItemKey="$(UniqueKey)" ItemName="PostBuildEvent" > 
     <Output TaskParameter="ItemLog" PropertyName="PostBuildEventTemplateLog" /> 
    </LogItem> 
    <MSBuild 
     Projects="$(MSBuildProjectFullPath)" 
     Targets="SEN_EXTRACT_CONFIG_GetMidl" 
     Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)" > 
     <Output TaskParameter="TargetOutputs" ItemName="MidlTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(MidlTemplate)" ItemKey="$(UniqueKey)" ItemName="Midl" > 
     <Output TaskParameter="ItemLog" PropertyName="MidlTemplateLog" /> 
    </LogItem> 

    <Message Text="%(ProjectConfiguration.Configuration)|%(ProjectConfiguration.Platform)" /> 
    <Message Text="$(CLCompileTemplateLog)" /> 
    <Message Text="$(LinkTemplateLog)" /> 
    <Message Text="$(LibTemplateLog)" /> 
    <Message Text="$(ResourceCompileTemplateLog)" /> 
    <Message Text="$(ProjectReferenceTemplateLog)" /> 
    <Message Text="$(PostBuildEventTemplateLog)" /> 
    <Message Text="$(MidlTemplateLog)" /> 
    </Target> 

    <Target Name="SEN_EXTRACT_CONFIG_GetCLCompile" Returns="@(CLCompileTemplate)" > 
    <ItemGroup> 
     <CLCompile Include="$(UniqueKey)" /> 
     <CLCompileTemplate Include="%(CLCompile.Identity)" Condition="'%(CLCompile.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetLink" Returns="@(LinkTemplate)" > 
    <ItemGroup> 
     <Link Include="$(UniqueKey)" /> 
     <LinkTemplate Include="%(Link.Identity)" Condition="'%(Link.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetLib" Returns="@(LibTemplate)" > 
    <ItemGroup> 
     <Lib Include="$(UniqueKey)" /> 
     <LibTemplate Include="%(Lib.Identity)" Condition="'%(Lib.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetResourceCompile" Returns="@(ResourceCompileTemplate)" > 
    <ItemGroup> 
     <ResourceCompile Include="$(UniqueKey)" /> 
     <ResourceCompileTemplate Include="%(ResourceCompile.Identity)" Condition="'%(ResourceCompile.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetProjectReference" Returns="@(ProjectReferenceTemplate)" > 
    <ItemGroup> 
     <ProjectReference Include="$(UniqueKey)" /> 
     <ProjectReferenceTemplate Include="%(ProjectReference.Identity)" Condition="'%(ProjectReference.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetPostBuildEvent" Returns="@(PostBuildEventTemplate)" > 
    <ItemGroup> 
     <PostBuildEvent Include="$(UniqueKey)" /> 
     <PostBuildEventTemplate Include="%(PostBuildEvent.Identity)" Condition="'%(PostBuildEvent.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
    <Target Name="SEN_EXTRACT_CONFIG_GetMidl" Returns="@(MidlTemplate)" > 
    <ItemGroup> 
     <Midl Include="$(UniqueKey)" /> 
     <MidlTemplate Include="%(Midl.Identity)" Condition="'%(Midl.Identity)'=='$(UniqueKey)'" /> 
    </ItemGroup> 
    </Target> 
</Project> 

あなたが使用したいプロジェクトファイルの下部に、このファイルをINGのImportことにより、それをテストすることができます。

+0

あなたが持って、あなたが期待する詳細結果は何に?私は、コードを変更することができます:https://1drv.ms/t/s!AresBGZVYryjhR3aJ9-CHelZMBst –

+0

まあ、私は、CLCompileTemplateアイテムが 'AdditionalIncludeDirectories'メタデータを持つことを期待しています't。 – Bwmat

+1

このコードを使用してAdditionalIncludeDirectoriesメタデータを取得する方法はありますか? varコレクション=新しいProjectCollection(); var project = collection.LoadProject(Item.GetMetadata( "Identity"));最初の(Value.Metadata.FirstOrDefault(idm => idm.Name == "AdditionalIncludeDirectories");)。 文字列s = d == null? "空":d.EvaluatedValue; ItemLog = s; がtrueを返します。 –

答えて

0

AdditionalIncludeDirectoriesのメタデータを取得するための簡単なサンプル:

<UsingTask TaskName="LogItem" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll"> 
     <ParameterGroup> 
      <Item ParameterType="Microsoft.Build.Framework.ITaskItem" Required="true" /> 
      <ItemName ParameterType="System.String" Required="true" /> 
      <ItemKey ParameterType="System.String" Required="true" /> 
      <ItemLog ParameterType="System.String" Output="true" /> 
     </ParameterGroup> 
     <Task> 
      <Reference Include="System.Xml" /> 
      <Reference Include="Microsoft.Build" /> 
      <Using Namespace="Microsoft.Build" /> 
      <Using Namespace="Microsoft.Build.Evaluation" /> 
      <Using Namespace="Microsoft.Build.Utilities" /> 
      <Code Type="Fragment" Language="cs"><![CDATA[ 

      var collection = new ProjectCollection(); 
       var project = collection.LoadProject(Item.GetMetadata("Identity")); 
       var d= project.ItemDefinitions.Where(id => id.Key == "ClCompile").First().Value.Metadata.FirstOrDefault(idm=>idm.Name== "AdditionalIncludeDirectories"); 
       string s = d == null ? "empty" : d.EvaluatedValue; 
       ItemLog=s; 
       return true; 

      ]]></Code> 
     </Task> 
     </UsingTask> 
<Target Name="CheckTarget"> 
    <MSBuild Projects="$(MSBuildProjectFullPath)" Targets="SEN_EXTRACT_CONFIG_GetCLCompile" Properties="Configuration=%(ProjectConfiguration.Configuration);Platform=%(ProjectConfiguration.Platform)"> 
     <Output TaskParameter="TargetOutputs" ItemName="CLCompileTemplate" /> 
    </MSBuild> 
    <LogItem Item="@(CLCompileTemplate)" ItemKey="$(UniqueKey)" ItemName="CLCompile"> 
     <Output TaskParameter="ItemLog" PropertyName="CLCompileTemplateLog" /> 
    </LogItem> 
    <Message Text="output value: $(CLCompileTemplateLog)" Importance="high" /> 
    </Target> 
関連する問題