17

What is the simplest way to tell Nuget package to add all css files as an embedded resource (ie build action is embedded resource).

I am trying to do it through install.ps1 in the tools folder but still cant get anywhere

Note: I am creating the package from the directory structure(tools\content\lib)

This is my install.ps1 which does not work.

param($installPath, $toolsPath, $package, $project)
$MsbNS = @{msb = 'http://schemas.microsoft.com/developer/msbuild/2003'}    
function EmbeddContent($ProjectLink, [string]$XPath)
{
    $nodes = @(Select-Xml $XPath $ProjectLink -Namespace $MsbNS | Foreach {$_.Node})

    foreach ($node in $nodes)
    {   
    if($node.Include.StartsWith("Content\css"))
    {           
        $cet = $node.ownerdocument.CreateElement("EmbeddedResource")
        $cet.setAttribute("Include", $node.Include)
        $parent = $node.ParentNode          
        [void]$parent.RemoveChild($node)
        [void]$parent.Appendchild($cet)        
    }
    }
}
$project.Save()
$fileLocation = $project.FileName
$dte.ExecuteCommand("Project.UnloadProject");

$proj = [xml](gc $fileLocation)
Embeddcontent $fileLocation '//msb:Project/msb:ItemGroup/msb:Content'
$proj.Save($fileLocation)

Help Please ..

labroo
  • 2,901
  • 3
  • 27
  • 36

1 Answers1

37

You can use DTE instead of messing with xml to change the BuildAction. From http://nuget.codeplex.com/discussions/227696:

$item = $project.ProjectItems | where-object {$_.Name -eq "ReleaseNotes.txt"} 
$item.Properties.Item("BuildAction").Value = [int]3

This link shows the enumeration values: http://msdn.microsoft.com/en-us/library/aa983962(VS.71).aspx

davidfowl
  • 37,120
  • 7
  • 93
  • 103
  • 1
    Mr Fowler, You Sir are Wonderful. :) – labroo Sep 16 '11 at 17:47
  • 4
    There are only a couple of BuildActions listed in that enum. Is there a way to specify "TypeScriptCompile" build action? – BSick7 Jul 16 '13 at 19:06
  • 1
    @BSick7, heh I'm looking for the same thing, find anything? – Allen Rice Jun 06 '14 at 18:22
  • 7
    If you want to get to a file inside a folder, you have to get to it like so: $project.ProjectItems.Item("SomeFolder").ProjectItems.Item("ReleaseNotes.txt") – deadlydog Aug 20 '14 at 17:08
  • To set anything else do this: $item.Properties.Item("ItemType").Value = "MyOwnBuildActionChoice" – thekip Apr 21 '16 at 15:31
  • These values match up with the list that's under Properties -> Advanced -> Build Action for a given file in Visual Studio. The documentation for VS2003 has been obsoleted, and it seems there's not another good reference. To get a value, just select the file using the DTE PowerShell code above, manually change the "Build Action" by selecting from the drop-down, then get the number by querying the value (e.g. `$item.Properties.Item("BuildAction").Value`) – CJBS Jan 09 '18 at 23:36
  • Here is an updated link to the documentation for the available enumeration values: https://learn.microsoft.com/en-us/dotnet/api/vslangproj.prjbuildaction?view=visualstudiosdk-2017 – MartynJones87 Apr 28 '18 at 06:53