使用Powershell脚本自动化格式化配置文件

时间:2015-07-31 21:20:11

标签: visual-studio powershell configuration nuget code-formatting

我在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

添加"外部工具"。转到工具>外部工具。使用以下设置添加新的

  • 标题:使用Powershell运行
  • 命令:powershell.exe
  • 参数:-ExecutionPolicy RemoteSigned -File" $(ItemPath)"
  • 初始目录:$(ItemDir)
  • 选中"使用输出窗口"
  • 如果您打算使用需要参数的脚本,请选中"提示参数"

记下工具在列表中的位置(1,2等等)。单击“确定”。

  • 现在转到工具>自定义,命令选项卡,选择上下文菜单,和 选择"项目和解决方案上下文菜单|项&#34 ;.
  • 点击"添加命令.."。
  • 选择工具类别,然后选择"外部命令X" 其中x是工具在列表中的位置。
  • 单击“确定”。
  • 将其移动到菜单中所需的位置,然后单击"修改选择" 给它一个友好的名字,
  • 添加键盘快捷键等。
  • 点击关闭。

右键单击解决方案资源管理器中的.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 }
}

0 个答案:

没有答案