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

后端 未结 2 1303
轻奢々
轻奢々 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.

    
       
    
    

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

提交回复
热议问题