gutter_runner/builder/helpers.odin

81 lines
1.7 KiB
Odin

package builder
import "base:intrinsics"
import "core:fmt"
import "core:log"
import os "core:os/os2"
import "core:path/filepath"
import "core:strings"
Process_Error :: enum {
OK,
Invalid_Exit_Code,
Crash,
}
Run_Error :: union #shared_nil {
Process_Error,
os.Error,
}
@(require_results)
run_cmd :: proc(cmd: []string, cwd: string, loc := #caller_location) -> Run_Error {
log.infof(
"running [%s]: %s",
cwd,
strings.join(cmd, " ", context.temp_allocator),
location = loc,
)
desc := os.Process_Desc {
command = cmd,
working_dir = cwd,
stderr = os.stderr,
stdout = os.stdout,
}
process := os.process_start(desc) or_return
state := os.process_wait(process) or_return
if !state.success {
return .Crash
}
if state.exit_code != 0 {
return .Invalid_Exit_Code
}
return nil
}
Copy_Error :: union #shared_nil {
os.Error,
filepath.Match_Error,
}
handle_error :: proc(
err: $E,
msg: string = "",
expr := #caller_expression,
loc := #caller_location,
) where intrinsics.type_is_enum(E) ||
intrinsics.type_is_union(E) {
if err != nil {
log.panicf("%v %s error: %v", expr, msg, err, location = loc)
}
}
remove_file :: proc(path: string, expr := #caller_expression, loc := #caller_location) {
log.infof("remove(%s)", path, location = loc)
err := os.remove(path)
if err != .Not_Exist {
handle_error(err, fmt.tprintf("failed to remove %s", path), expr = expr, loc = loc)
}
}
remove_all :: proc(path: string, expr := #caller_expression, loc := #caller_location) {
log.infof("remove_all(%s)", path, location = loc)
err := os.remove_all(path)
if err != .Not_Exist {
handle_error(err, fmt.tprintf("failed to remove %s", path), expr = expr, loc = loc)
}
}