我在Powershell脚本中非常弱,并希望有人可以将以下各个部分放在一起,以帮助我解决以下问题。
背景
每当我更新我的NuGet包时,他们经常会触摸我项目中的app.config和web.config文件,从而更改格式。
这里已经报道了没有解决方案。
Nuget and web.config formatting
http://nuget.codeplex.com/workitem/1511
目标: 最终,我想要做的是在Visual Studio中有一个上下文菜单(例如在解决方案级别) 获取所有app.config和web.config的列表 并应用CTRL + K + D格式化配置文件 (我们将它设置为每行有一个属性)
基本上,我正在寻找一个修改过的脚本,我可以连接到Visual Studio上下文菜单(在解决方案级别),它只读取相关的配置文件。或者,或者,在文件保存时执行(对于相关的配置文件)。 谢谢!
我的出发点来自
a)Mark Melville展示了如何在Visual Studio的上下文菜单中执行powershell脚本
How do I run a PowerShell script from Visual Studio 2010
b)和Phil Haack和David Fowl的脚本
http://haacked.com/archive/2011/05/22/an-obsessive-compulsive-guide-to-source-code-formatting.aspx/
https://gist.github.com/davidfowl/984358
供参考,剪切并粘贴上面的链接, 将上下文菜单添加到Visual Studio
添加"外部工具"。转到工具>外部工具。使用以下设置添加新的
记下工具在列表中的位置(1,2等等)。单击“确定”。
右键单击解决方案资源管理器中的.ps1文件并欣赏。 (注意:我也为cmd.exe执行此操作以运行.bat文件。)
供参考,剪切并粘贴上面的链接, 用于格式化文档的powershell脚本
# Print all project items
Recurse-Project -Action {param($item) "`"$($item.ProjectItem.Name)`" is a $($item.Type)" }
# Function to format all documents based on https://gist.github.com/984353
function Format-Document {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName
)
Process {
$ProjectName | %{
Recurse-Project -ProjectName $_ -Action { param($item)
if($item.Type -eq 'Folder' -or !$item.Language) {
return
}
$window = $item.ProjectItem.Open('{7651A701-06E5-11D1-8EBD-00A0C90F26EA}')
if ($window) {
Write-Host "Processing `"$($item.ProjectItem.Name)`""
[System.Threading.Thread]::Sleep(100)
$window.Activate()
$Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.FormatDocument')
$Item.ProjectItem.Document.DTE.ExecuteCommand('Edit.RemoveAndSort')
$window.Close(1)
}
}
}
}
}
function Recurse-Project {
param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[string[]]$ProjectName,
[parameter(Mandatory = $true)]$Action
)
Process {
# Convert project item guid into friendly name
function Get-Type($kind) {
switch($kind) {
'{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}' { 'File' }
'{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}' { 'Folder' }
default { $kind }
}
}
# Convert language guid to friendly name
function Get-Language($item) {
if(!$item.FileCodeModel) {
return $null
}
$kind = $item.FileCodeModel.Language
switch($kind) {
'{B5E9BD34-6D3E-4B5D-925E-8A43B79820B4}' { 'C#' }
'{B5E9BD33-6D3E-4B5D-925E-8A43B79820B4}' { 'VB' }
default { $kind }
}
}
# Walk over all project items running the action on each
function Recurse-ProjectItems($projectItems, $action) {
$projectItems | %{
$obj = New-Object PSObject -Property @{
ProjectItem = $_
Type = Get-Type $_.Kind
Language = Get-Language $_
}
& $action $obj
if($_.ProjectItems) {
Recurse-ProjectItems $_.ProjectItems $action
}
}
}
if($ProjectName) {
$p = Get-Project $ProjectName
}
else {
$p = Get-Project
}
$p | %{ Recurse-ProjectItems $_.ProjectItems $Action }
}
}
# Statement completion for project names
Register-TabExpansion 'Recurse-Project' @{
ProjectName = { Get-Project -All | Select -ExpandProperty Name }
}