Skip to content

Commit

Permalink
made build command more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
emekoi committed Jul 24, 2021
1 parent 815cffa commit eefb972
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 27 deletions.
27 changes: 15 additions & 12 deletions Build Systems/Zig.sublime-build
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
{
"target": "zig_build",
// "cancel": { "kill": true },
"cancel": { "kill": true },
"selector": "source.zig",
"keyfiles": ["build.zig"],
"build": {},

"variants": [
{
"name": "Run File",
"run": true,
"cmd": ["run", "$file_name"],
},
{
"name": "Run Project",
"run": true,
"project": true,
"build": {
"step": "run"
}

},
{
"name": "Test File",
"test":true
"cmd": ["test", "$file_name"]
},
{
"name": "Test Project",
"test": true,
"project": true,
"build": {
"step": "test"
}
},
{
"name": "Format File",
"format": true,
"cmd": ["fmt", "$file_name"],
"quiet": true,
},
{
"name": "Format Project",
"format": true,
"project": true,
"quiet": true
"cmd": ["fmt", "$folder"],
"quiet": true,
},
{
"name": "Build Project",
"project": true,
"build": {}
},
]
}
38 changes: 23 additions & 15 deletions Zig.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class ZigBuildCommand(sublime_plugin.WindowCommand):
panel = None
panel_lock = threading.Lock()

def is_enabled(self, run=False, format=False, test=False, project=False, quiet=False, kill=False):
def is_enabled(self, cmd=None, build=None, quiet=False, kill=False):
# The Cancel build option should only be available
# when the process is still running
if kill:
return self.proc is not None and self.proc.poll() is None
return True

def run(self, run=False, format=False, test=False, project=False, quiet=False, kill=False):
def run(self, cmd=None, build=None, quiet=False, kill=False):
if kill:
if self.proc:
self.killed = True
Expand Down Expand Up @@ -89,19 +89,27 @@ def run(self, run=False, format=False, test=False, project=False, quiet=False, k

args = [get_setting(view, 'zig.executable', 'zig')]

if project:
if format:
args.append('fmt')
args.append(vars['folder'])
else:
args.append('build')
if run: args.append('run')
elif test: args.append('test')
else:
if format: args.append('fmt')
elif test: args.append('test')
else: args.append('run')
args.append(vars['file_name'])
if build is not None and cmd is None:
args.append('build')

if isinstance(build, dict):
step = build.get('step', '')
step_args = build.get('args', [])
if step != '': args.append(step)
if step_args != []:
args.append('--')
args.extend(step_args)
elif isinstance(build, list):
args.extend(build)
elif isinstance(build, str):
args.append(build)
elif cmd is not None:
if isinstance(cmd, list):
args.extend(cmd)
elif isinstance(cmd, str):
args.append(cmd)

args = sublime.expand_variables(args, vars)

self.proc = subprocess.Popen(
args,
Expand Down
44 changes: 44 additions & 0 deletions build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Build System
The build system provided for Sublime Text depends on `zig` being either in your path or the `zig.executable` setting. The build system comes with predefined build targets for running, testing, and formatting single files or whole projects that use a `build.zig`. If for whatever reason you would want to extend these build targets or add your own (e.g. your own `.sublime-project` file), the following shows all the options available:
```json
{
// .sublime-project

"folders": [
//...
],
"settings": {
// ...
},
"build_systems": [
// runs `zig fmt $file_name`
{
// the build systems functions are all under the `zig_build` command
// `target` must always be `"zig_build"`
"target": "zig_build",
"name": "Format File",
"cmd": ["fmt", "$file_name"]
},
// runs `zig build install`
{
// t
"target": "zig_build",
"name": "Install Project",
"step": {
"name": "install"
}
},
// runs `zig build run -- foo bar`
{
// t
"target": "zig_build",
"name": "Run Project",
"step": {
"name": "run",
"args": ["foo", "bar"]
}
}
]
}
```
See the Sublime Text [docs](https://www.sublimetext.com/docs/build_systems.html#variables) for all the possible expansion variables.

0 comments on commit eefb972

Please sign in to comment.