Skip to content

Instantly share code, notes, and snippets.

@lucs
Last active September 10, 2023 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucs/ce48d73cad420fc6c56007e5fe8fdbb6 to your computer and use it in GitHub Desktop.
Save lucs/ce48d73cad420fc6c56007e5fe8fdbb6 to your computer and use it in GitHub Desktop.
Use Code::Snippets to manage Raku snippets
#!/usr/bin/env raku
=begin pod
=head1 NAME
snipraku - Extract and run Raku snippets.
=head1 SYNOPSIS
Run the app without arguments for help.
=end pod
# --------------------------------------------------------------------
use Code::Snippets;
my $CS;
# --------------------------------------------------------------------
sub help-exit ($msg = '') {
note "$msg\n" if $msg;
note qq:to/EoT/;
Usage: ▸ ⋯ ⟨snips file⟩ ⟨snips dir⟩ ⟨option⟩ ⟨snal⟩ ⟨args⋯⟩
Possible options:
List all known snippet aliases.
l
List the files that would be extracted by using the shown
snippet alias, 「main」 tagged with '*', others with '-'.
l ⟨snal⟩
Extract the files for this snippet alias.
x ⟨snal⟩
Run the main file for this snippet alias.
r ⟨snal⟩ ⟨args⋯⟩
Both extract the files for this snippet alias and run its main
file.
b ⟨snal⟩ ⟨args⋯⟩
Invocation examples:
▸ ⋯ ~/memo/eg.memo /tmp/snips-raku b some-alias
EoT
exit 1;
}
# --------------------------------------------------------------------
sub extract ($snal) {
$CS.extract: $snal;
}
# --------------------------------------------------------------------
sub run ($snal, *@args) {
help-exit "No 「$snal」 snippet alias." unless $CS.snips{$snal};
my $main-path;
for $CS.snips{$snal}.kv -> $path, $vals {
if $vals<main> {
help-exit "More than one main path for snippet alias 「$snal」."
if $main-path.defined;
$main-path = $path;
}
}
help-exit "No main path for snippet alias 「$snal」."
unless $main-path.defined;
my $main-file = $CS.snips-dir ~ "/" ~ $main-path;
chdir $main-file.IO.dirname;
my @cmd = $*EXECUTABLE-NAME, $main-file, |@args;
CORE::<&run>(@cmd);
}
# --------------------------------------------------------------------
sub ensure-envvar ($envvar) {
help-exit "Envvar '$envvar' is not set."
unless %*ENV{$envvar}.defined;
}
# --------------------------------------------------------------------
proto sub MAIN (|) {
my ($fSnips, $dSnips) = @*ARGS[0, 1];
help-exit "Can't read snips file <$fSnips>"
unless .f && .r given $fSnips.IO;
(my $ok, $CS) = Code::Snippets.build(
snim => / ^^ "# " "-"+ "\n# ID:" /,
file-ext => ".raku",
snips-file => $fSnips,
snips-dir => $dSnips,
);
$ok or say($CS), exit 1;
if $CS.errors {
say " ", $CS.snips-file;
say $CS.errors.join("\n");
}
{*}
}
# --------------------------------------------------------------------
multi MAIN ($, $, 'l', $snal?) {
if $snal.defined {
say $CS.list-paths("$snal").map({
($CS.snips{"$snal"}{$_}<main> ?? "*" !! "-") ~ " $_";
}).join("\n");
}
else {
say $CS.list-snals.join(" ");
}
}
multi MAIN ($, $, 'x', $snal) {
extract("$snal");
}
multi MAIN ($, $, 'r', $snal, *@args) {
run("$snal", @args);
}
multi MAIN ($, $, 'b', $snal, *@args) {
extract($_), run($_, @args) for "$snal";
}
multi MAIN (|) { help-exit "Invalid invocation: 「{@*ARGS.join: ' '}」." }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment