Skip to content

Commit a2d8680

Browse files
authored
attempt at automated JSON conversion (#77)
1 parent daab654 commit a2d8680

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Convert grammars to JSON for linguist
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
paths:
9+
- 'syntaxes/*.yml'
10+
11+
jobs:
12+
convert-grammars:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: actions/setup-node@v3
17+
with:
18+
node-version: 16
19+
- uses: tiabeast/[email protected]
20+
- run: npm install
21+
- name: Run grammar converter and push changes
22+
run: bait run scripts/convert_grammars.bt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
out/
33
dist/
44
*.vsix
5+
*.js
56

67
# editor files
78
*.code-workspace

scripts/convert_grammars.bt

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file is part of vscode-vba.
2+
// Copyright (c) 2023-present Lukas Neubert
3+
// This Source Code is subject to the terms of the Mozilla Public License 2.0.
4+
5+
package main
6+
7+
import os
8+
import bait.util
9+
10+
const GIT_USER := 'tiabeast-bot'
11+
const GIT_MAIL := '[email protected]'
12+
13+
const IS_CI := os.getenv('CI') == 'true'
14+
15+
fun main() {
16+
// Count the number of errors by adding the os.system return values
17+
mut errors := 0
18+
19+
if IS_CI {
20+
println('Configuring git user...')
21+
errors += git_command('config --global user.name "${GIT_USER}"')
22+
errors += git_command('config --global user.email "${GIT_MAIL}"')
23+
}
24+
25+
target_repo_dir := '/tmp/vscode-vba-json'
26+
if os.exists(target_repo_dir) {
27+
os.rmdir_all(target_repo_dir)
28+
}
29+
30+
errors += git_command('clone --depth 1 https://github.com/serkonda7/vscode-vba-json ${target_repo_dir}')
31+
32+
errors += os.system('npm run convert-yaml')
33+
os.cp('./out', '${target_repo_dir}/syntaxes')
34+
35+
// Sanity check for a reasonable file size
36+
text := os.read_file(target_repo_dir + '/syntaxes/vba.tmGrammar.json')
37+
if text.length < 1000 {
38+
eprintln('grammar is too small. Something went wrong.')
39+
exit(1)
40+
}
41+
42+
// Stage changes
43+
errors += git_command('-C ${target_repo_dir} add .')
44+
45+
// Exit if there is nothing to commit
46+
has_changes := git_command('-C ${target_repo_dir} diff-index --quiet HEAD')
47+
if has_changes == 0 {
48+
println('No changes to commit.')
49+
exit(0)
50+
}
51+
52+
// Create commit
53+
println('Creating commit...')
54+
os.chdir(target_repo_dir)
55+
commit_msg := 'update grammars'
56+
errors += git_command('commit -m "${commit_msg}"')
57+
58+
// Safety rebase and push changes
59+
println('Pushing changes...')
60+
errors += git_command('pull --rebase')
61+
errors += git_command('push')
62+
63+
// Exit with the number of errors
64+
exit(errors)
65+
}
66+
67+
fun git_command(cmd string) i32 {
68+
esc_cmd := util.shell_escape(cmd)
69+
return os.system('git ${esc_cmd}')
70+
}

0 commit comments

Comments
 (0)