How to modify an existing file by installing the NuGet package?

Can I modify the contents of a file that exists through the NuGet package? Sense, let's say you create a new MVC3 website. You want to install the NuGet package, which will update the home page. Perhaps change the welcome message, and then change the layout to include the new view you created, and update the Home Controller to include a method to return the new view. How to do it?

+3
source share
2 answers

After installing the NuGet package, you can manually modify any files you want. Just edit them in VS (or with another editor). The only difference in NuGet packages is that your modified files will not be deleted if you uninstall / update the package when changes are detected between the package source files and your files. There will be a mention that these are magazines.

Update:

NuGet has support for modifying project files during package installation via . transform and .pp files . But this has limitations, .transform files add content to configuration files (they cannot edit), while .pp files are for new source code files.

, EnvDte CodeModel PowerShell (.. install.ps1). , .

+4

JQuery NuGet NuGet Package Explorer ( ) install.ps1 common.ps1, , _references.js JQuery, API- JQuery IntelliSense.

, ( , ). JQuery , / ( , , ).

, OP.

common.ps1:

function AddOrUpdate-Reference($scriptsFolderProjectItem, $fileNamePattern, $newFileName) {
    try {
        $referencesFileProjectItem = $scriptsFolderProjectItem.ProjectItems.Item("_references.js")
    }
    catch {
        # _references.js file not found
        return
    }

    if ($referencesFileProjectItem -eq $null) {
        # _references.js file not found
        return
    }

    $referencesFilePath = $referencesFileProjectItem.FileNames(1)
    $referencesTempFilePath = Join-Path $env:TEMP "_references.tmp.js"

    if ((Select-String $referencesFilePath -pattern $fileNamePattern).Length -eq 0) {
        # File has no existing matching reference line
        # Add the full reference line to the beginning of the file
        "/// <reference path=""$newFileName"" />" | Add-Content $referencesTempFilePath -Encoding UTF8
         Get-Content $referencesFilePath | Add-Content $referencesTempFilePath
    }
    else {
        # Loop through file and replace old file name with new file name
        Get-Content $referencesFilePath | ForEach-Object { $_ -replace $fileNamePattern, $newFileName } > $referencesTempFilePath
    }

    # Copy over the new _references.js file
    Copy-Item $referencesTempFilePath $referencesFilePath -Force
    Remove-Item $referencesTempFilePath -Force
}
+2

All Articles