SelectStringExceptComment.psm1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
enum MatchType { AllMatches = 0 Match = 1 NotMatch = 2 } function exceptMultiLineComment () { param( [ValidateNotNullOrEmpty()] $GrepData, [ValidateNotNullOrEmpty()] [string]$Extension ) [int]$commentNest = 0 $result = @() for ($i = 0; $i -lt $GrepData.Length; $i++) { if ($Extension -ne '.py' -or $commentNest -eq 0) { if ($GrepData[$i].Line -match $multiLineCommentDictionary.Item($Extension)[0]) { $commentNest += 1 if ($Extension -eq '.py') { continue } } } if ($commentNest -ne 0) { if ($GrepData[$i].Line -match $multiLineCommentDictionary.Item($Extension)[1]) { $commentNest -= 1 continue } } if ($commentNest -ne 0) { continue } $result += $GrepData[$i] } return $result } function Select-StringExceptComment () { <# .SYNOPSIS コメント以外の文字列を検索します。 .DESCRIPTION 指定パスの拡張子からコメント種類を判定し、 コメント行・ブランク行を除外した上で文字列検索を行います。 ただし、同一行内にてネスト構造になっているコメントがある場合は正常に動作しません。 また、コメント行とコーディングが同一ラインに存在する場合、 コメント行として除外される点にもご注意ください。 足りない拡張子は設定を追加してください。 .EXAMPLE PS C:\> Get-ChildItem ./ | Select-StringExceptComment -Pattern kore, sore .EXAMPLE PS C:\> Get-ChildItem ./ | Select-StringExceptComment -Pattern hoge -MatchType NotMatch .EXAMPLE PS C:\> Get-ChildItem ./ | Select-StringExceptComment -Pattern hoge -SimpleMatch -CaseSensitive -Encoding ([System.Text.Encoding]::Default) .PARAMETER LiteralPath 対象となるフォルダパス .PARAMETER Pattern 検索する単語。複数指定可 .PARAMETER MatchType AllMatches, Match, NotMatch。default = Match .PARAMETER SimpleMatch 正規表現を使用しない .PARAMETER CaseSensitive 大文字小文字を区別して判定する .PARAMETER Encoding System.Text.Encoding。default = UTF8 #> [CmdletBinding()] param( [parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)] [Alias("Path")] [ValidateNotNullOrEmpty()] [string[]]$LiteralPath, [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [ValidateNotNullOrEmpty()] [String[]]$Pattern, [Alias("Match")] [MatchType]$MatchType = [MatchType]::Match, [Alias("Simple")] [switch]$SimpleMatch, [Alias("Case")] [switch]$CaseSensitive, [Alias("Enc")] [System.Text.Encoding]$Encoding = [System.Text.Encoding]::UTF8 ) begin { $multiLineCommentDictionary = @{ '.applescript' = [string[]]@('(\*', '\*)') '.aspx' = [string[]]@('<%--', '--%>') '.c' = [string[]]@('/\*', '\*/') '.cpp' = [string[]]@('/\*', '\*/') '.cs' = [string[]]@('/\*', '\*/') '.css' = [string[]]@('/\*', '\*/') '.fs' = [string[]]@('(\*', '\*)') '.go' = [string[]]@('/\*', '\*/') '.groovy' = [string[]]@('/\*', '\*/') '.hs' = [string[]]@('{-', '-}') '.html' = [string[]]@('<!--', '-->') '.jar' = [string[]]@('/\*', '\*/') '.java' = [string[]]@('/\*', '\*/') '.js' = [string[]]@('/\*', '\*/') '.lhs' = [string[]]@('{-', '-}') '.m' = [string[]]@('/\*', '\*/') '.ps1' = [string[]]@('<#', '#>') '.psm1' = [string[]]@('<#', '#>') '.php' = [string[]]@('/\*', '\*/') '.pl' = [string[]]@('=comment', '=cut') '.py' = [string[]]@("(`'`'`'|`"`"`")", "(`'`'`'|`"`"`")") '.rb' = [string[]]@('=begin', '=end') '.scala' = [string[]]@('/\*', '\*/') '.scpt' = [string[]]@('(\*', '\*)') '.scptd' = [string[]]@('(\*', '\*)') '.swift' = [string[]]@('/\*', '\*/') '.ts' = [string[]]@('/\*', '\*/') } $singleLineCommentDictionary = @{ '.applescript' = [string[]]@("--") '.bat' = [string[]]@('\s*REM ', '^\s*::') '.c' = [string[]]@('//') '.cpp' = [string[]]@('//') '.cs' = [string[]]@('//') '.fs' = [string[]]@('//') '.go' = [string[]]@('//') '.groovy' = [string[]]@('//') '.hs' = [string[]]@('--') '.jar' = [string[]]@('//') '.java' = [string[]]@('//') '.js' = [string[]]@('//') '.lhs' = [string[]]@('--') '.m' = [string[]]@('//') '.php' = [string[]]@('//', '#') '.pl' = [string[]]@('#') '.pm' = [string[]]@('#') '.psm1' = [string[]]@('#') '.ps1' = [string[]]@('#') '.py' = [string[]]@('#') '.rb' = [string[]]@('#') '.scala' = [string[]]@('//') '.scpt' = [string[]]@("--") '.scptd' = [string[]]@("--") '.sh' = [string[]]@('#') '.sql' = [string[]]@('--') '.swift' = [string[]]@('//') '.ts' = [string[]]@('//') '.vb' = [string[]]@("`'") '.vbs' = [string[]]@("`'", "\s*Rem ") } $grepCommand = 'Select-String -InputObject $_ -Pattern $Pattern -Encoding $Encoding' switch ($MatchType) { 'AllMatches' {$grepCommand += " -AllMatches"; break} 'NotMatch' {$grepCommand += " -NotMatch"; break} } if ($SimpleMatch) {$grepCommand += " -SimpleMatch"} if ($CaseSensitive) {$grepCommand += " -CaseSensitive"} } process { foreach ($path in $LiteralPath) { $extension = [System.IO.Path]::GetExtension($path) $result = Get-Item -Path $path | Select-String -Pattern '^\s*$' -NotMatch -Encoding $Encoding if ($multiLineCommentDictionary.Keys -contains $extension) { $result = exceptMultiLineComment -GrepData $result -Extension $extension } if ($singleLineCommentDictionary.Keys -contains $extension) { $result = $result | Select-String -Pattern $singleLineCommentDictionary.Item($extension) -Encoding $Encoding -NotMatch } $result = $result | ForEach-Object {Invoke-Expression -Command $grepCommand} Write-Output $result } } } Export-ModuleMember -Function Select-StringExceptComment |