NuGet: How can I change property of files with Install.ps1 file?

后端 未结 2 1302
轻奢々
轻奢々 2021-01-30 22:50

I am creating NuGet package and for that I have created Nuspec manifest file. In content folder I have two files, test.exe and test.config

相关标签:
2条回答
  • 2021-01-30 23:39

    Here is a bit more detail on how to solve this problem end to end:

    You need to do two things to ensure the status are set correctly on install...

    1. Write the install.ps1 script to mark the status of the files.
    2. Ensure the install.ps1 script is in the Tools directory in the nuget package.

    Install.ps1 Script

    The following example script will recursively mark every file in the "Content" and "View" directory as "Copy to newer". Note this example script is written to make it clear to read and understand. It will mark every file in the Content and Views folders in the root directory of the Visual Studios project.

    param($installPath, $toolsPath, $package, $project)
    
    function MarkDirectoryAsCopyToOutputRecursive($item)
    {
        $item.ProjectItems | ForEach-Object { MarkFileASCopyToOutputDirectory($_) }
    }
    
    function MarkFileASCopyToOutputDirectory($item)
    {
        Try
        {
            Write-Host Try set $item.Name
            $item.Properties.Item("CopyToOutputDirectory").Value = 2
        }
        Catch
        {
            Write-Host RecurseOn $item.Name
            MarkDirectoryAsCopyToOutputRecursive($item)
        }
    }
    
    #Now mark everything in the a directory as "Copy to newer"
    MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("Content"))
    MarkDirectoryAsCopyToOutputRecursive($project.ProjectItems.Item("Views"))
    

    Copy To Tools

    You must copy the install.ps1 file to the Tools directory for script to be executed by nuget. You can add the following to the nuspec template to do this.

    <files>
       <file src="install.ps1" target="Tools"/>
    </files>
    

    Note in this case I have the install.ps1 file in the root of my Visual Studios project and marked as "Copy if newer".

    0 讨论(0)
  • 2021-01-30 23:55

    Your install.ps1 file should look something like this.

    param($installPath, $toolsPath, $package, $project)
    
    $file1 = $project.ProjectItems.Item("test.exe")
    $file2 = $project.ProjectItems.Item("test.config")
    
    # set 'Copy To Output Directory' to 'Copy if newer'
    $copyToOutput1 = $file1.Properties.Item("CopyToOutputDirectory")
    $copyToOutput1.Value = 2
    
    $copyToOutput2 = $file2.Properties.Item("CopyToOutputDirectory")
    $copyToOutput2.Value = 2
    
    0 讨论(0)
提交回复
热议问题