11// This file is part of vscode-vba.
2- // Copyright (c) 2023-present Lukas Neubert
2+ // Copyright (c) 2023-present Lukas Neubert.
33// This Source Code is subject to the terms of the Mozilla Public License 2.0.
44
55package main
@@ -9,7 +9,9 @@ import bait.util
99
1010const GIT_USER := 'tiabeast-bot'
1111const GIT_MAIL := '
[email protected] '
12+ const REPO_DIR := '/tmp/vscode-vba-json'
1213
14+ const DRY_RUN := os.user_args().contains('--dry')
1315const IS_CI := os.getenv('CI') == 'true'
1416
1517fun main() {
@@ -22,55 +24,63 @@ fun main() {
2224 errors += git_command('config --global user.email "${GIT_MAIL}"')
2325 }
2426
25- target_repo_dir := '/tmp/vscode-vba-json'
26- if os.exists(target_repo_dir) {
27- os.rmdir_all(target_repo_dir)
27+ if os.exists(REPO_DIR) {
28+ os.rmdir_all(REPO_DIR)
2829 }
2930
3031 // Clone repo as bot user
3132 access_token := os.getenv('BOT_TOKEN')
32- errors += git_command('clone --depth 1 https://${GIT_USER}:${access_token}@github.com/tiabeast/vscode-vba-json.git ${target_repo_dir }')
33+ errors += git_command('clone --depth 1 https://${GIT_USER}:${access_token}@github.com/tiabeast/vscode-vba-json.git ${REPO_DIR }')
3334
3435 // Run conversion
3536 errors += os.system('npm run convert-yaml')
3637
37- // Copy syntax files
38- syntax_dir := '${target_repo_dir}/syntaxes'
39- os.cp('./out/vba.tmGrammar.json', '${syntax_dir}/vba.tmGrammar.json')
40- os.cp('./out/wwb.tmGrammar.json', '${syntax_dir}/wwb.tmGrammar.json')
41-
42- // Sanity check for a reasonable file size
43- text := os.read_file(syntax_dir + '/vba.tmGrammar.json')
44- if text.length < 1000 {
45- eprintln('grammar is too small. Something went wrong.')
46- exit(1)
47- }
38+ // Copy grammar files
39+ println('Copying grammar files...')
40+ copy_grammar('vba', 1500)
41+ copy_grammar('wwb', 100)
4842
4943 // Stage changes
50- errors += git_command('-C ${target_repo_dir } add .')
44+ errors += git_command('-C ${REPO_DIR } add .')
5145
5246 // Exit if there is nothing to commit
53- has_changes := git_command('-C ${target_repo_dir } diff-index --quiet HEAD')
47+ has_changes := git_command('-C ${REPO_DIR } diff-index --quiet HEAD')
5448 if has_changes == 0 {
5549 println('No changes to commit.')
5650 exit(0)
5751 }
5852
5953 // Create commit
6054 println('Creating commit...')
61- os.chdir(target_repo_dir )
55+ os.chdir(REPO_DIR )
6256 commit_msg := 'update grammars'
6357 errors += git_command('commit -m "${commit_msg}"')
6458
6559 // Safety rebase and push changes
66- println('Pushing changes...')
67- errors += git_command('pull --rebase')
68- errors += git_command('push')
60+ if not DRY_RUN {
61+ println('Pushing changes...')
62+ errors += git_command('pull --rebase')
63+ errors += git_command('push')
64+ }
6965
7066 // Exit with the number of errors
7167 exit(errors)
7268}
7369
70+ fun copy_grammar(lang string, min_size i32) {
71+ src := './out/${lang}.tmGrammar.json'
72+
73+ // Sanity check for a reasonable file size
74+ text := os.read_file(src)
75+ if text.length < min_size {
76+ eprintln('error: Grammar for ${lang} is too small.')
77+ exit(1)
78+ }
79+
80+ // Copy
81+ os.cp(src, '${REPO_DIR}/syntaxes/${lang}.tmGrammar.json')
82+ }
83+
7484fun git_command(cmd string) i32 {
7585 esc_cmd := util.shell_escape(cmd)
7686 return os.system('git ${esc_cmd}')
0 commit comments