generated from SublimeLinter/SublimeLinter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinter.py
48 lines (46 loc) · 1.63 KB
/
linter.py
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
from SublimeLinter.lint import Linter
import os.path
import re
import logging
from base64 import b64encode
logger = logging.getLogger('SublimeLinter.plugins.PSScriptAnalyzer')
class PSScriptAnalyzer(Linter):
analyzer_cmd_template = (
'Invoke-ScriptAnalyzer -Settings:{0} -ScriptDefinition ($INPUT | Out-String) | '
'Select-Object -Property Line,Message,Severity,Column,RuleName | '
'ConvertTo-Csv -Delimiter "\t" -QuoteFields False | Select-Object -Skip 1;'
)
regex = (
r'(?P<line>\d+)\t(?P<message>[^\t]+)\t'
r'((?P<error>ParseError|Error)|(?P<warning>Warning|Information))\t'
r'(?P<col>\d*)\t(?P<code>[^\t]*)'
)
multiline = False
defaults = {
'selector': 'source.powershell',
'settings': None
}
def cmd(self):
settings = self.analyzer_settings() or '$false'
logger.info("PSScriptAnalyzer settings: " + settings)
analyzer_cmd = self.analyzer_cmd_template.format(settings)
return [
'pwsh',
'-NoProfile',
'-OutputFormat',
'Text',
'-EncodedCommand',
b64encode(analyzer_cmd.encode('utf_16_le')).decode('ascii')
]
def analyzer_settings(self):
settings = self.settings['settings']
if settings:
if re.match(r'^[\w-]+$', settings):
return settings
settings = os.path.expandvars(settings)
else:
settings = os.path.join(
os.path.expanduser("~"),
'PSScriptAnalyzerSettings.psd1'
)
return settings if os.path.isfile(settings) else None