0

We have content (files/folders) created outside of VS2010. Using the project's "BeforeBuild", I'm able to import the files from our temp directory to our project's same directory structure. Once imported, how do we tell VS2010 IDE recognize 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? Here is what I have so far...

    <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>
osstech
  • 33
  • 1
  • 6

1 Answers1

0

I don't think you can change project during BeforeBuild steps of the same project. Even if it would be possible it does not sound like a good idea.

You can instead add these files in advance to the project file even if files are not there yet. If you don't know names - just use wildcard like *.cs instead. When build will start it will use files that you've just copied/created in pre-build step.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • If it is not possible to change "Build Action" on items in the current project's "BeforeBuild", are you suggesting I move the above target into a "Preprocess" msbuild project to copy and create the "Build Actions"? If so I still don't understand how you set the "Build Action" on items? – osstech Jun 12 '12 at 17:44
  • I'm suggesting to simply add items to a project (i.e. .csproj itself) and set build action you want. Items don't need yet to exist on disk to be included in the project, but should be picked up by build after being placed there by actions in BeforeBuild section. – Alexei Levenkov Jun 12 '12 at 18:00