如何从Nuget写入BundleConfig最好

时间:2013-05-15 14:19:47

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

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

包中包含了使用SASS的相应依赖项,它可以在新的MVC4项目上安装。

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

我正在尝试使用install.ps1方法将更改添加到bundle配置文件,但我不确定这是否是最好的方法。

我希望最终结果是我的团队成员无需将Nuget应用于解决方案而无需对BundleConfig进行任何编辑。

请让我知道制作此类内容的最佳方案是什么。

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

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

我知道很少甚至没有强大的力量,但如果你能指出我一篇很棒的文章!

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文件。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我最终做了以下事情:

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
}
}

这使我无法获得文件的Get-Content。我打算使用正则表达式来替换文本。

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

它并不漂亮,但它可以满足我的工作需要。希望这有助于某人。如果有人知道从nuget install powershell脚本更改文件内容的更简单方法,请加入。快乐的编码和力量与你同在,永远!