Skip to content

Commit a68f858

Browse files
authored
Add PowerShell support (#46)
This commit adds support for powershell scripts in Github Action steps. Example: - name "Powershell Step" shell: pwsh run: | Write-Output "Highlighted with Powershell syntax" Note: requires PowerShell v5.1.1+ (ST4107 - 4200) or v6.2.1+ (ST4205+).
1 parent b092752 commit a68f858

7 files changed

Lines changed: 204 additions & 1 deletion

.github/workflows/syntax.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
include:
2525
- build: 4200
2626
packages: binary
27-
powershell: st4107-5.0.3
27+
powershell: st4107-5.1.1
2828
- build: latest
2929
packages: master
3030
powershell: master
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%YAML 1.2
2+
---
3+
# See http://www.sublimetext.com/docs/syntax.html
4+
scope: source.powershell.folded
5+
version: 2
6+
hidden: true
7+
8+
extends: Packages/PowerShell/PowerShell.sublime-syntax
9+
10+
contexts:
11+
pop-eol: []
12+
13+
pop-before-eol: []
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
%YAML 1.2
2+
---
3+
name: PowerShell (Github Actions Folded)
4+
scope: source.powershell.folded-github-actions
5+
version: 2
6+
hidden: true
7+
8+
extends:
9+
- PowerShell (Folded).sublime-syntax
10+
- PowerShell (Github Actions).sublime-syntax
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
%YAML 1.2
2+
---
3+
name: PowerShell (Github Actions)
4+
scope: source.powershell.github-actions
5+
version: 2
6+
hidden: true
7+
8+
extends: Packages/PowerShell/PowerShell.sublime-syntax
9+
10+
variables:
11+
# lazy escaping from heredoc as shell maybe indented
12+
no_indent: ^\s*
13+
tab_indent: ^\s*

Github Actions.sublime-syntax

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ extends: Packages/YamlPipelines/YamlPipeline.sublime-syntax
99
contexts:
1010
node:
1111
- meta_prepend: true
12+
- match: \s+(shell)\s*(:)(?=\s|$)
13+
captures:
14+
1: string.unquoted.plain.out.yaml keyword.control.shell.pipeline
15+
2: punctuation.separator.key-value.yaml
16+
push: expect-shell-name
1217
- match: \s+(run|inlineScript)\s*(:)(?=\s|$)
1318
captures:
1419
1: string.unquoted.plain.out.yaml keyword.control.flow.script.pipeline
@@ -39,6 +44,37 @@ contexts:
3944
2: punctuation.separator.key-value.yaml
4045
push: expect-jobs
4146

47+
expect-shell-name:
48+
- match: \b(?i:pwsh|powershell)\b
49+
scope: constant.other.language-name.yaml
50+
set: pwsh-step
51+
- match: \b[\w-]+\b
52+
scope: constant.other.language-name.yaml
53+
pop: 1
54+
- match: ^
55+
pop: 1
56+
57+
pwsh-step:
58+
# bailout at end of step
59+
# - either next step
60+
# - or next job
61+
- match: ^(?=\s*-|\s{,2}\S)
62+
pop: 1
63+
# switch shell syntax (just in case of duplicate definitions)
64+
- match: \s+(shell)\s*(:)(?=\s|$)
65+
captures:
66+
1: string.unquoted.plain.out.yaml keyword.control.conditional.dependencies.pipeline
67+
2: punctuation.separator.key-value.yaml
68+
set: expect-shell-name
69+
# consume single script block with powershell syntax
70+
- match: \s+(run|inlineScript)\s*(:)(?=\s|$)
71+
captures:
72+
1: string.unquoted.plain.out.yaml keyword.control.flow.script.pipeline
73+
2: punctuation.separator.key-value.yaml
74+
set: pwsh-block-node
75+
# consume ordinary nodes
76+
- include: node
77+
4278
inside-if:
4379
- meta_include_prototype: false
4480
- include: flow-scalar-plain-out-body
@@ -73,11 +109,25 @@ contexts:
73109
- meta_include_prototype: false
74110
- meta_content_scope: source.shell.bash.embedded
75111
- include: scope:source.shell.bash.github-actions
112+
apply_prototype: true
76113

77114
embedded-bash-folded:
78115
- meta_include_prototype: false
79116
- meta_content_scope: source.shell.bash.embedded
80117
- include: scope:source.shell.bash.folded-github-actions
118+
apply_prototype: true
119+
120+
embedded-pwsh:
121+
- meta_include_prototype: false
122+
- meta_content_scope: source.powershell.embedded
123+
- include: scope:source.powershell.github-actions
124+
apply_prototype: true
125+
126+
embedded-pwsh-folded:
127+
- meta_include_prototype: false
128+
- meta_content_scope: source.powershell.embedded
129+
- include: scope:source.powershell.folded-github-actions
130+
apply_prototype: true
81131

82132
expect-jobs:
83133
- match: ^(\s*)([\w\-]+)\s*(:)(?=\s|$)

YamlPipeline.sublime-syntax

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,84 @@ contexts:
6363
scope: punctuation.separator.argument.pipeline
6464
- include: inside-expression
6565

66+
###[ POWERSHELL SCRIPT BLOCKS ]################################################
67+
68+
pwsh-block-node:
69+
- meta_scope: meta.block.pwsh.pipeline
70+
- include: pwsh-block-scalar
71+
- include: flow-scalar-pwsh-out
72+
73+
pwsh-block-scalar:
74+
# http://www.yaml.org/spec/1.2/spec.html#style/block/scalar
75+
# c-l+folded(n)
76+
- match: (>)([1-9])?([-+]?) # c-b-block-header(m,t)
77+
captures:
78+
1: keyword.control.flow.block-scalar.folded.yaml
79+
2: constant.numeric.indentation-indicator.yaml
80+
3: storage.modifier.chomping-indicator.yaml
81+
set: pwsh-block-folded-scalar-begin
82+
# c-l+literal(n)
83+
- match: (\|)([1-9])?([-+]?) # c-b-block-header(m,t)
84+
captures:
85+
1: keyword.control.flow.block-scalar.literal.yaml
86+
2: constant.numeric.indentation-indicator.yaml
87+
3: storage.modifier.chomping-indicator.yaml
88+
set: pwsh-block-scalar-begin
89+
90+
flow-scalar-pwsh-out:
91+
# double quoted scalar
92+
- match: \"
93+
scope: meta.string.yaml string.quoted.double.yaml punctuation.definition.string.begin.yaml
94+
embed: embedded-pwsh-folded
95+
embed_scope: meta.string.yaml
96+
escape: \"
97+
escape_captures:
98+
0: meta.string.yaml string.quoted.double.yaml punctuation.definition.string.end.yaml
99+
pop: 1
100+
# single quoted scalar
101+
- match: \'
102+
scope: meta.string.yaml string.quoted.single.yaml punctuation.definition.string.begin.yaml
103+
embed: embedded-pwsh-folded
104+
embed_scope: meta.string.yaml
105+
escape: \'
106+
escape_captures:
107+
0: meta.string.yaml string.quoted.single.yaml punctuation.definition.string.end.yaml
108+
pop: 1
109+
# unquoted scalar
110+
- match: (?={{ns_plain_first_plain_out}})
111+
embed: embedded-pwsh-folded
112+
embed_scope: meta.string.yaml
113+
escape: '{{_flow_scalar_end_plain_out}}'
114+
pop: 1
115+
116+
pwsh-block-scalar-begin:
117+
- meta_include_prototype: false
118+
- include: comment
119+
- match: ^([ ]+)(?! ) # match first non-empty line to determine indentation level
120+
scope: source.powershell.embedded
121+
# note that we do not check if indentation is enough
122+
embed: embedded-pwsh
123+
escape: ^(?!\1|\s*$)
124+
pop: 1
125+
- match: ^(?=\S) # the block is empty
126+
pop: 1
127+
- match: .+
128+
scope: invalid.illegal.expected-comment-or-newline.yaml
129+
130+
pwsh-block-folded-scalar-begin:
131+
- meta_include_prototype: false
132+
- include: comment
133+
- match: ^([ ]+)(?! ) # match first non-empty line to determine indentation level
134+
scope: source.powershell.embedded
135+
# note that we do not check if indentation is enough
136+
embed: embedded-pwsh-folded
137+
escape: ^(?!\1|\s*$)
138+
pop: 1
139+
- match: ^(?=\S) # the block is empty
140+
pop: 1
141+
- match: .+
142+
scope: invalid.illegal.expected-comment-or-newline.yaml
143+
66144
###[ SHELLSCRIPT SCRIPT BLOCKS ]###############################################
67145

68146
script-block-node:
@@ -190,3 +268,9 @@ contexts:
190268

191269
embedded-bash-folded:
192270
- meta_include_prototype: false
271+
272+
embedded-pwsh:
273+
- meta_include_prototype: false
274+
275+
embedded-pwsh-folded:
276+
- meta_include_prototype: false

tests/syntax_test_github_actions.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ jobs:
9090

9191
steps:
9292

93+
- name: Test leading source.powershell
94+
shell: pwsh
95+
run: |
96+
Write-Output Test
97+
# <- source.powershell.embedded
98+
#^^^^^^^^^^^^^^^^^^^^^^^^^^^ source.powershell.embedded - source.powershell source.powershell
99+
# ^^^^^^^^^^^^ support.function
100+
93101
- name: Test leading source.shell
94102
run: |
95103
echo Test
@@ -220,6 +228,31 @@ jobs:
220228
# ^ entity.name.tag.heredoc.shell
221229
)
222230
231+
- name: Run Powershell
232+
shell: pwsh
233+
# ^^^^^ string.unquoted.plain.out.yaml keyword.control.shell.pipeline
234+
# ^ punctuation.separator.key-value.yaml
235+
# ^^^^ constant.other.language-name.yaml
236+
if: true
237+
# ^^ string.unquoted.plain.out.yaml keyword.control.flow.condition.pipeline
238+
# ^ punctuation.separator.key-value.yaml
239+
# ^^^^ constant.language.boolean.pipeline
240+
run: |
241+
$heredoc = @'
242+
A HEREDOC string
243+
'@;
244+
# <- source.powershell.embedded meta.string.powershell string.quoted.single.heredoc.powershell punctuation.definition.string.end.powershell
245+
#^ source.powershell.embedded meta.string.powershell string.quoted.single.heredoc.powershell punctuation.definition.string.end.powershell
246+
#
247+
$job = Get-Process -Name pwsh
248+
# <- source.powershell.embedded variable.other.readwrite.powershell punctuation.definition.variable.powershell
249+
#^^^ source.powershell.embedded variable.other.readwrite.powershell
250+
251+
- name: Bash is the default
252+
run: |
253+
ls .
254+
#^^^^^^^^^^^^ source.shell.bash.embedded
255+
223256
job_foobar:
224257
runs-on: ubuntu-22.04
225258
needs: run_syntax_tests

0 commit comments

Comments
 (0)