web project package content with CopyToOutputDirectory set

Aug 22, 2011 at 4:41 PM
Edited Aug 23, 2011 at 4:33 PM

i have a web project package with a content\startup.cmd batch script where the CopyToOutputDirectory element needs to be configured as follows when the file is added to the project during nuget package installation.   Its a matter specific to a web project that will ultimately be deployed as an azure role.  Is there a way to tell nuget to set <CopyToOutputDirectory>Always</CopyToOutputDirectory> when it adds this file to the project?

  <None Include="Startup.cmd">

Aug 22, 2011 at 7:40 PM

This came up before. e.g. in this thread and this bug.

I bet someone has written install.ps1 code to do that by automating the DTE, but I couldn't find it. I did do something similar in the PrecompiledMvcViewEngine, though in that case I was setting the Custom Tool on the item. Here is what I have there:

$rootProjectItems = $project.ProjectItems
$viewsProjectItems = $rootProjectItems.Item("Views").ProjectItems

# set the test view's custom tool to the razor generator
$testView = $viewsProjectItems.Item("Home").ProjectItems.Item("Test.cshtml")
$testView.Properties.Item("CustomTool").Value = "RazorGenerator"

The logic should be similar for CopyToOutputDirectory, with the last line doing something different. Maybe it's something like $testView.Properties.Item("CopyToOutputDirectory").Value = "Always". Though probably not exactly that. Yes, ugly object model :)


Aug 23, 2011 at 5:20 PM

Excellent thanks for the excerpt.  using it i was able to arrive at the following tools\install.ps1 solution where the comment out lines are what i used to debug/test the solution from the package manager console.

# $project = get-project MvcApplication1
$projectItems = $project.ProjectItems
# $projectItems.Count

# $contentProjectItems = $projectItems.Items("Content").ProjectItems
# $siteCss = $contentProjectItems.Item("Site.css")
# $siteCss.Properties.Item("BuildAction").Value = 2  # BuildAction = Content

$startupCmd = $projectItems.Item("Startup.cmd")
# $startupCmd.Properties > $env:temp\properties.before.txt
$startupCmd.Properties.Item("BuildAction").Value = 0  # BuildAction = None
$startupCmd.Properties.Item("CopyToOutputDirectory").Value = 1  # CopyToOutputDirectory = Copy always
# $startupCmd.Properties.Item("CopyToOutputDirectory").Value = 2  # CopyToOutputDirectory = Copy if newer
# $startupCmd.Properties > $env:temp\properties.after.txt

Aug 23, 2011 at 5:25 PM

Great, thanks for sharing!