Skip to content

Commit

Permalink
added: onyx pkg build can run shell commands now
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanfh committed Aug 19, 2024
1 parent 3dcf576 commit bc7d1c4
Showing 1 changed file with 136 additions and 53 deletions.
189 changes: 136 additions & 53 deletions scripts/onyx-pkg.onyx
Original file line number Diff line number Diff line change
Expand Up @@ -643,42 +643,83 @@ run_build_command :: (args: [] cstr) {
build_config = str.as_str(args[0]);
}

maybe_bc := config.build_configs[build_config];
run_build_configuration(build_config)
}

run_build_configuration :: (build_config: str) -> bool {
maybe_bc := config.build_configs[build_config]
if !maybe_bc {
error_print("Unrecognized build configuration '{}'.\n", build_config);
return;
error_print("Unrecognized build configuration '{}'\n", build_config)
return false
}

info_print("Building", "Compiling with build configuration '{}'.\n", build_config);
bc := maybe_bc->unwrap()
switch bc {
case .CompileOnyx as c {
info_print("Building", "Compiling '{}'\n", c.target)

bc := maybe_bc->unwrap();
command := os.command()
command->path("onyx")

args: [..] str;
args << "build";
command->args(.[ "build" ])
for c.include do command->args(.["-I", it])
for c.defines do command->args(.[tprintf("-D{}", it)])

for bc.include { args << "-I"; args << it; }
for bc.defines do args << tprintf("-D{}", it);
for bc.args do args << it;

args << "-r";
args << bc.runtime;
command->args(c.args)

command->args(.["-r", c.runtime])
command->args(.["-o", c.target])

args << "-o";
args << bc.target;
command->args(c.sources)

switch command->output() {
case .Ok as output {
info_print("Built", "Compiled '{}'\n", c.target);
return true
}

case .Err as e {
error_print("Failed to compile '{}'\n", c.target);
println(e.output);
return false
}
}
}

for bc.sources do args << it;
case .RunCommands as cmds {
for cmd in cmds {
cmd_str := str.join(cmd, " ")
info_print("Executing", "{}\n", cmd_str)

command := os.command()
command->path(cmd[0])
command->args(cmd[1 .. cmd.length])

switch command->output() {
case .Ok as output {
info_print("Executed", "{}\n", cmd_str)
}

case .Err as e {
error_print("Failed to run '{}'\n", str.join(cmd, " "))
println(e.output)
return false
}
}
}

p := os.process_spawn("onyx", args);
r := io.reader_make(&p);
output := io.read_all(&r);
switch os.process_wait(&p) {
case .Success {
info_print("Built", "Successfully compiled with build configuration '{}'.\n", build_config);
return true
}

case _ {
error_print("Failed to compile with build configuration '{}'.\n", build_config);
println(output);
case .Collection as steps {
info_print("Running", "Running collection '{}'\n", build_config)
for step in steps {
if !run_build_configuration(step) {
return false
}
}

return true
}
}
}
Expand Down Expand Up @@ -1182,13 +1223,17 @@ DependencySource :: union {
Git: str;
}

BuildConfig :: struct {
include: [..] str;
args: [..] str;
defines: [..] str;
sources: [..] str;
runtime: str;
target: str;
BuildConfig :: union {
CompileOnyx: struct {
include: [..] str;
args: [..] str;
defines: [..] str;
sources: [..] str;
runtime: str;
target: str;
}
RunCommands: [] [] str
Collection: [] str
}

load_config_file :: () -> bool {
Expand Down Expand Up @@ -1244,33 +1289,71 @@ load_config :: (path: str) -> ? Config {

for doc->query_all("top() > build > []") {
b: BuildConfig;
b.runtime = "onyx";
b.target = "out.wasm";

load_string(it, "runtime", &b.runtime);
load_string(it, "target", &b.target);
kind := it->value_or_null()->as_str() ?? "compile"
switch kind {
case "compile" {
runtime := "onyx";
target := "out.wasm";

for it->query_all("include") {
array.concat(&b.include,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}
include, defines, args, sources: [..] str

for it->query_all("define") {
array.concat(&b.defines,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}
load_string(it, "runtime", &runtime);
load_string(it, "target", &target);

for it->query_all("args") {
array.concat(&b.args,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}
for it->query_all("include") {
array.concat(&include,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}

for it->query_all("source") {
array.concat(&b.sources,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}
for it->query_all("define") {
array.concat(&defines,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}

c.build_configs[it.node] = b;
for it->query_all("args") {
array.concat(&args,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}

for it->query_all("source") {
array.concat(&sources,
iter.as_iter(it.values)->flatten(x => (*x)->as_str()));
}

c.build_configs[it.node] = .{
CompileOnyx = .{
include, args, defines, sources, runtime, target
}
};
}

case "shell" {
commands := it->query_all("run")
|> Iterator.map(x => {
return cast([] str,
Iterator.from(x.values)
|> Iterator.flatten(y => y.*->as_str())
|> Iterator.collect()
)
})
|> Iterator.collect()

c.build_configs[it.node] = .{
RunCommands = commands
}
}

case "collection" {
steps := it->query_all("build")
|> Iterator.flatten(x => x->value_or_null()->as_str())
|> Iterator.collect()

c.build_configs[it.node] = .{
Collection = steps
}
}
}
}

for doc->query_all("top() > dependencies > []") {
Expand Down

0 comments on commit bc7d1c4

Please sign in to comment.