2012-06-11 56 views
0

我們擁有在VS2010之外創建的內容(文件/文件夾)。使用該項目的「BeforeBuild」,我可以將文件從我們的臨時目錄導入到我們項目的相同目錄結構中。一旦導入,我們如何告訴VS2010 IDE將Build Action識別爲「內容」?在VS2010 IDE中,宏「包含在項目中」爲您做到了這一點。它與指定文件的MetaData有什麼關係?這是我到目前爲止......如何使用構建操作「內容」將文件複製到項目中

<Target Name="BeforeBuild" Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' " DependsOnTargets="ImportProperties;CustomImportsTarget" /> 
    <Target Name="ImportProperties"> 
    <PropertyGroup> 
     <ImportSrcPath>$(temp)\$(MSBuildProjectName)\Imports</ImportSrcPath> 
     <!-- File to save project references to --> 
     <ImportLogFile>$(ImportSrcPath)\ImportResults.txt</ImportLogFile> 
    </PropertyGroup> 
    </Target> 
    <Target Name="ImportLogger"> 
    <Delete Files="$(ImportLogFile)" /> 
    <WriteLinesToFile File="$(ImportLogFile)" Lines="Import Results%09============" /> 
    </Target> 
    <Target Name="CustomImportsTarget" Condition="Exists('$(ImportSrcPath)')" DependsOnTargets="ImportLogger"> 
    <!-- 
     ============================================================== 
     Allows for dynamically created content (files/folders) to be imported from our temp directory to our project's same 
     directory structure as "Compile Include content" 
     i.e. move %temp%\scripts\1.1.0\sample.js to <project>\scripts\1.1.0\sample.js 
     ============================================================== 
     --> 
    <Message Text="==============================================================%0aCustomImportsTarget Begin%0a==============================================================" /> 
    <!-- Create an Item list that contains the files to import. $(ImportSrcPath) is the property that contains the location where 
      import source files are placed. --> 
    <CreateItem Include="$(ImportSrcPath)\**\*" Exclude="$(ImportLogFile)"> 
     <Output TaskParameter="Include" ItemName="Files2Import" /> 
    </CreateItem> 
    <!-- For testing purposes, Copy the files into our project. TODO:Should be a Move operation so project file modified dates are not constantly changing. --> 
    <Copy SkipUnchangedFiles="false" SourceFiles="@(Files2Import)" DestinationFolder="$(MSBuildProjectDirectory)\%(Files2Import.RecursiveDir)"> 
     <Output TaskParameter="CopiedFiles" ItemName="Changed" /> 
    </Copy> 
    <!-- Imported files are now located in our project folders, 
     How do we assign properties to the files in @(Changed) so VS2010 IDE recognizes Build Action as "Content"??? 
     In VS2010 IDE, the macro "Include in Project" does this for you. Does it have anything to do with specifying a file's MetaData??? 
     --> 
    <!-- Now send these values to the logger --> 
    <Message Text="Changed:@(Changed->'%(Filename)%(Extension)')" Importance="high" /> 
    <Message Text="Script:@(Files2Import->'%(Filename)%(Extension)')" Importance="high" /> 
    <!--<Message Text="MSBuildProjectDirectory:$(MSBuildProjectDirectory)" Importance="high" />--> 
    <Message Text="==============================================================%0aCustomImportsTarget End%0a==============================================================" /> 
    </Target> 
+0

我猜測可以通過使用T4模板來讀取/寫入動態生成的文件。 – Smartkid

回答

0

我不認爲你可以在同一個項目的BeforeBuild步驟中更改項目。即使有可能,這聽起來不是一個好主意。

您可以改爲將這些文件預先添加到項目文件中,即使文件尚未存在。如果您不知道名稱 - 只需使用通配符(如* .cs)即可。當構建將開始時,它將使用您在預構建步驟中複製/創建的文件。

+0

如果無法在當前項目的「BeforeBuild」中的項目上更改「Build Action」,您是否建議將上述目標移動到「Preprocess」msbuild項目中以複製並創建「Build Actions」?如果是這樣,我仍然不明白你如何設置項目上的「構建行動」? – osstech

+0

我建議只需將項目添加到項目(即.csproj本身)並設置所需的構建操作。項目不需要存在於磁盤上以包含在項目中,但是應該在BeforeBuild部分中的操作放置在那裏之後由build生成。 –

相關問題