asp.net-mvc-4 - 如何最好地从 Nuget 写入 BundleConfig

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

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

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

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

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

我希望最终结果是让我的团队成员零努力地将 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 文件。非常感谢任何帮助。

最佳答案

我最终做了以下事情:

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 脚本更改文件内容的更简单方法,请插话。祝你编码愉快,愿原力永远与你同在!

关于asp.net-mvc-4 - 如何最好地从 Nuget 写入 BundleConfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16567591/

相关文章:

javascript - 如何从 Controller 发送多个 JSON 结果以查看?

java - 当我尝试访问我放入 bundle 中的信息时,为什么我的 Android Activity 会强制关闭?

visual-studio - 您如何知道 Nuget 包是否已签名

javascript - Chrome 'please match the requested format' 验证消息

c# - 在 Acumatica 中调试自定义代码

php - Magento - 以编程方式添加的 bundle 产品不适用于购物篮/前端

c# - 尽管拥有正确的 NuGet 包,为什么我仍然需要在计算机上安装 MySQL 连接器?

.net - Visual Studio 2012/13 导出模板中缺少 MVC NuGet

asp.net-mvc - 已为布局页面定义部分但未呈现 "~/Views/Shared/_Layout.cshtml": "head"

ios - 在 swift 4 中使用来自 cocoapods 的 Nib