#| Utilities for interacting with idris and associated tooling
unit module IUtils;

need IUtils::IDEMode;

use paths;

#| Structure representing the root of what idris considers a package directory,
#| with the associated ipkg and source files. These can and will overlap within
#| the same directory.
class PackageInfo {
    has IO::Path:D $.ipkg is required;
    has IO::Path:D $.root is required;
    has IO::Path:D @.sources is required;
}

#| Scan a particular ipkg for its associated sources
sub scan-ipkg(IO::Path:D $ipkg --> PackageInfo:D) {
    my $contents = $ipkg.slurp;
    my $src-dir =
        ($contents ~~
         / 'sourcedir' \h* '=' \h*
           '"' $<value>=[<-["]>*] '"' /)<value>
            // "src";

    my IO::Path:D @sources =
        paths($ipkg.parent.add($src-dir), :file(*.ends-with(".idr"))).map(*.IO);
    PackageInfo.new(ipkg => $ipkg, root => $ipkg.parent, sources => @sources)
}

# TODO: Add some parsing of pack.toml to locate test packages and associate them
# with their source ipkg

#| Scan $*CWD to locate ipkgs and their associated sources
sub scan-packages(--> Array[PackageInfo:D]) is export {
    my PackageInfo:D @ipkgs =
        paths(:file(*.ends-with(".ipkg"))).map(*.IO.&scan-ipkg);
    return @ipkgs;
}


# 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
        END
    }
}

#| 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);
    unless $proc {
        IdrisError.new(
            out => $out, err => $err,
            exit-code => $proc.exitcode, command => @cmd.Str)
        .throw;
    }
    return $out;
}

#| Exec the expression with the given name in the given file
sub idris-exec($expr, $file) is export {
    idris-run '--find-ipkg', '--exec', $expr, $file
}