使用Powershell在文本文件中注释掉“代码块”吗?

时间:2018-07-20 08:36:05

标签: regex powershell

我正在尝试注释掉大量文件中的一些代码

文件全部包含以下内容:

    stage('inrichting'){
        steps{
              build job: 'SOMENAME', parameters: param
              build job: 'SOMEOTHERNAME', parameters: param
              echo 'TEXT'
        }
    }

steps{ }中的内容是可变的,但始终存在于0..N'echo'和0..N'build job'

我需要这样的输出:

    //stage('inrichting'){
    //   steps{
    //        build job: 'SOMENAME', parameters: param
    //        build job: 'SOMEOTHERNAME', parameters: param
    //        echo 'TEXT'
    //  }
    //}

PowerShell有什么好的方法吗?我用pattern.replace尝试了一些东西,但是距离还很远。

$list = Get-ChildItem -Path 'C:\Program Files (x86)\Jenkins\jobs' -Filter config.xml -Recurse -ErrorAction SilentlyContinue -Force | % { $_.fullname };
foreach ($item in $list) {
...
}

1 个答案:

答案 0 :(得分:1)

这有点棘手,因为您要查找整个部分,然后在其中的所有行中添加注释标记。如果您的结构允许,我可能会用switch -regex编写一个临时解析器(计算大括号可能会使事情变得更健壮,但在所有情况下都很难正确使用)。如果代码足够常规,则可以将其简化为以下内容:

stage('inrichting'){
    steps{
        ... some amount of lines that don't contain braces
    }
}

然后我们可以检查在开始处是否出现了两条固定线,并最终检查了两条带有大括号的线:

 foreach ($file in $list) {
    # lines of the file
    $lines = Get-Content $file
    # line numbers to comment out
    $linesToComment = @()
    # line number of the current block to comment
    $currentStart = -1
    # the number of closing braces on single lines we've encountered for the current block
    $closingBraces = 0

    for ($l = 0; $l -le $lines.Count; $l++) {
        switch -regex ($lines[$l]) {
            '^\s*stage\('inrichting'\)\{' {
                # found the first line we're looking for
                $currentStart = $l
            }
            '^\s*steps\{' {
                # found the second line, it may not belong to the same block, so reset if needed
                if ($l -ne $currentStart + 1) { $currentStart = -1 }
            }
            '^\s*}' {
                # only count braces if we're at the correct point
                if ($currentStart -ne -1) { $closingBraces++ }
                if ($closingBraces -eq 2) {
                # we've reached the end, add the range to the lines to comment out
                $linesToComment += $currentStart..$l
                $currentStart = -1
                $closingBraces = 0
                }
            }
        }
    }

    $commentedLines = 0..($lines.Count-1) | % {
      if ($linesToComment -contains $_) {
        '//' + $lines[$_]
      } else {
        $lines[$_]
      }
    } | Set-Content $file

 }

未经测试,但总体思路可能可行。

更新:已修复并经过测试