利用PowerShell截取Checkstyle关于bSpell拼写检查的当前错误行的源文件内容

 

== ps1 ==

function go{
    $gbk = [System.Text.Encoding]::GetEncoding("GBK")
    $utf8 = [System.Text.Encoding]::UTF8

    $result = 'spell-filter.txt'
    $encoding = [System.Text.Encoding]::GetEncoding("GBK")
    rm $result -ea SilentlyContinue
    $xml = cat checkstyle.xml
    $xml = [xml]$xml  # 类型转换
    $xml.checkstyle.file|%{$_.error}|?{$_.source -match 'SpellCheck'}|
    %{
      $fileContent = cat $_.parentNode.name -total $_.line -ea SilentlyContinue # 文件有可能不存在,所以要【静音】
      $word = (($_.message -replace 'The Spelling of word ', '') -replace 'may be illegal. .*', '') # 错误的拼写内容

      if ($fileContent -ne $Null){
        $code = $gbk.GetBytes($fileContent[-1]) # 错误的代码行
        $code = $utf8.GetString($code)
        '文件: ' + $_.parentNode.name + ' ,行号:' + $_.line + ' ,拼写错误:' +  $word + ' ,代码:' + $code>> $result
      }else{
        '不存在的文件: ' + $_.parentNode.name + ' , ' + $_.line + ' , ' +  $word 
      }
    }
}

go

== checkstyle.xml ==

<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.0">
<file name="package_zh_CN.properties">
<error line="114" column="81" severity="error" message="The Spelling of word &quot;pageing&quot; may be illegal. The suggestion(s) include(s): [paging, ageing, panging, pugging, pegging]" source="SpellCheck"/>
<error line="117" column="84" severity="error" message="The Spelling of word &quot;pageing&quot; may be illegal. The suggestion(s) include(s): [paging, ageing, panging, pugging, pegging]" source="SpellCheck"/>
</file>
<file name="addOrDel.js">
<error line="1" severity="info" message="File length is 2,373 lines (max allowed is 2,000)." source="com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck"/>
</file>
<file name="showDataDictDetail.jsp">
<error line="10" column="19" severity="error" message="The Spelling of word &quot;stylesheet&quot; may be illegal. The suggestion(s) include(s): [StyleSheet]" source="SpellCheck"/>
</file>
</checkstyle>










你可能感兴趣的:(powershell)