iutils-raku/lib/IUtils/Compiler.rakumod

126 lines
3.2 KiB
Raku
Raw Permalink Normal View History

2024-12-31 16:30:53 +00:00
#| Utilities for interacting with the idris compiler and package manager
unit module IUtils::Compiler;
2024-12-31 23:12:35 +00:00
# Represents the output of an Expr that we need to run
enum ExprOutput is export <Unit Boolean Either>;
2024-12-31 16:30:53 +00:00
# Utility functions for pack
#| Invoke a pack command
sub pack-run(*@cmd) is export {
my $proc = run "pack", @cmd, :out, :err;
my $out = $proc.out.slurp(:close);
my $err = $proc.err.slurp(:close);
unless $proc {
die qq:to/END/;
Pack Failure!
Captured Output:
$out
Captured StdErr:
$err
END
}
}
#| Build a package with pack
sub pack-build($pkg) is export {
pack-run 'build', $pkg
}
#| Test a package with pack
sub pack-test($pkg) is export {
pack-run 'build', $pkg
}
#| Clean a package with pack
sub pack-clean($pkg) is export {
pack-run 'clean', $pkg
}
# Utility functions for idris
#| An error coming from the idris compiler
class IdrisError is Exception {
has Str $.out;
has Str $.err;
has Int $.exit-code;
has Str $.command;
method message {
qq:to/END/;
Error running idris command: $.command
Command exited with $.exit-code
StdOut:
{$.out.lines.map(*.indent: 2).join("\n")}
2024-12-31 16:30:53 +00:00
END
}
}
#| An error coming from a compiled expression
2024-12-31 23:12:35 +00:00
class ExpressionError is Exception is export {
has Str $.out;
has Str $.err;
has Int $.exit-code;
has Str $.expr;
method message {
qq:to/END/;
Error running expression: $.expr
Command exited with $.exit-code
END
}
}
2024-12-31 16:30:53 +00:00
#| Invoke an idris command
sub idris-run(*@cmd) is export {
my $proc = run "idris2", @cmd, :out, :err;
my $out = $proc.out.slurp(:close);
my $err = $proc.err.slurp(:close);
if !$proc || $out.contains("Error:") {
2024-12-31 16:30:53 +00:00
IdrisError.new(
out => $out, err => $err,
exit-code => $proc.exitcode, command => @cmd.Str)
.throw;
}
return $out;
}
2024-12-31 23:12:35 +00:00
my constant $bool-lambda =
'(\x => if x then exitSuccess else exitFailure)';
# TODO: Implemenent support for the Either case
# TODO: Use the ide protocol to drive this so we can avoid the user needing to import anything
#| Exec the expression with the given name in the given file
#|
#| Uses the provided $output-type to hook up an adaptor for tests returning a
#| non () value
2024-12-31 23:12:35 +00:00
sub idris-exec($expr, $file, $output-type? = Unit) is export {
# Have idris compile an executable for the expression,
2024-12-31 23:12:35 +00:00
given $output-type {
when Unit {
idris-run '--find-ipkg', '--client', ":c iutils_out $expr", $file;
}
when Boolean {
idris-run '--find-ipkg', '--client',
":c iutils_out ($expr >>= $bool-lambda)", $file;
}
default {
die "Unsupported output type encountered: $_";
}
}
# Run the expression
my $proc = run 'build/exec/iutils_out', :out, :err;
my $out = $proc.out.slurp(:close);
my $err = $proc.err.slurp(:close);
unless $proc {
ExpressionError.new(
out => $out, err => $err,
exit-code => $proc.exitcode, expr => $expr
).throw;
}
# Remove the output file
'build/exec/iutils_out'.IO.unlink;
return $out;
2024-12-31 16:30:53 +00:00
}