【问题标题】:How Best to Write To BundleConfig From Nuget如何最好地从 Nuget 写入 BundleConfig
【发布时间】:2013-05-16 21:20:55
【问题描述】:

我正在为团队中的其他开发人员创建一个 Nuget 包,并且我正在使用 SASS 压缩样式。

该包包含使用 SASS 的适当依赖项,并且可以很好地安装在新的 MVC4 项目上。

我的问题是,Nuget 包需要对 App_Start 中的 BundleConfig 进行某些更改,但我不确定自动化此过程的最佳方法。

我正在尝试使用 install.ps1 方法将更改插入到捆绑配置文件中,但我不确定这是否是最佳方法。

我希望最终结果是让我的团队成员零努力地将 Nuget 应用于解决方案,而不必对 BundleConfig 进行任何编辑。

请让我知道让这样的事情发生的最佳计划是什么。

它将需要为 BundleConfig 文件设置命名空间,获取默认配置并将它们替换为样式和脚本的新配置语句。

替换将是样板文件,因此我应该能够在我的 nuget 包中包含一个具有预期更改的文件,打开现有的包配置并将我的更改转储。

我对 powershell 知之甚少,但如果你能给我指出一篇很棒的文章!

param($installPath, $toolsPath, $package, $project)
$app_start = $project.ProjectItems | Where-Object { $_.Name -eq "App_Start" }
$app_start.GetType().FullName | out-file "C:\Code\Test2.txt" -append
$app_start | Get-Member | out-file "C:\Code\Test2.txt" -append
$files = $app_start.ProjectItems
foreach($file in $files)
{$file.Name | out-file "C:\Code\Test2.txt" -append }

这是我目前获取 BundleConfig.cs 文件的尝试。

我可以列出 ProjectItems,但我不确定如何打开 App_Start 文件夹以获取 BundleConfig.cs 文件。非常感谢任何帮助。

【问题讨论】:

    标签: asp.net-mvc-4 bundle nuget nuget-package


    【解决方案1】:

    我最终做了以下事情:

    param($installPath, $toolsPath, $package, $project)
    
    $prjFileNameAndPath = $project.FileName
    $arrFileNameAndPath = $prjFileNameAndPath.Split("\")
    $pathLength = $arrFileNameAndPath.count - 1
    $partToTrim = $arrFileNameAndPath[$arrFileNameAndPath.count - 1]
    
    $index = 0
    $destPath = ""
    foreach($node in $arrFileNameAndPath)
    {
       $index++
       if($index -le $pathLength) {
            $destPath += $node + "\"
       }
    }
    
    $destPath += "App_Start\BundleConfig.cs"
    $destPath | out-file "C:\Code\Test.txt" -append
    
    $content = Get-Content $destPath
    $content | out-file "C:\Code\Test2.txt" -append
    
    function Update-Text {
        [CmdletBinding()]
        Param (
        [Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
        [System.String]
        $FilePath,
    
        [Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$false)]
        [System.String]
        $MatchPattern,
    
        [Parameter(Position=2, Mandatory=$true, ValueFromPipeline=$false)]
        [System.String]
        $ReplacementPattern
    )
    
    BEGIN {
    }
    
    PROCESS {
        Write-Verbose "Replacing Content in $FilePath"
        Write-Verbose "Match pattern: $MatchPattern"
        Write-Verbose "Replace pattern: $ReplacementPattern"
    
        (Get-Content $FilePath) | % {$_ -replace $MatchPattern, $ReplacementPattern    } | Set-Content $FilePath
    }
    }
    

    这让我能够获取文件的内容。我计划使用正则表达式来替换文本。

    我必须基本上基于 Project FileName 属性构建文件的路径,然后裁剪 csproj 文件名并将 App_Start\BundleConfig.cs 附加到路径中。

    它并不漂亮,但它可以满足我们工作所需。希望这可以帮助某人。如果有人知道从 nuget install powershell 脚本更改文件内容的更简单方法,请加入。祝您编码愉快,愿原力与您同在!

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 2019-02-17
    • 1970-01-01
    • 2013-05-01
    • 2010-10-09
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多