personal-tooling/src/upload.raku

179 lines
5.5 KiB
Raku
Executable file

#!/usr/bin/env raku
use paths;
use File::Temp;
use HTML::Lazy (:ALL);
my $build-path;
my $dir;
my constant $remote = "ubuntu@static.stranger.systems";
my constant $web-base = "/var/www/static.stranger.systems";
# Color values for foreground and background
my constant $background-css = '
body {
color: #b9b9b9;
background-color: #181818;
}
';
# Stylize katla output
sub stylize($katla-output) {
# Apply regex edits to colorize existing elements
$katla-output.match(/'color: darkgray'/).replace-with('color: #dedede')
.match(/'color: lightgrey'/).replace-with('color: #777777')
.match(/'color: yellow'/).replace-with('color: #3b3b3b')
.match(/'color: gray'/).replace-with('color: #777777')
.match(/'<style>'/).replace-with("<style>\n$background-css")
}
# Renders the file at the given location to the temporary directory
sub render-idris-file($idr, $katla-config, $base-dir = "src/".IO) {
say "Converting $idr";
# Figure out our file paths
my $source-file = IO::Path.new($idr, :CWD($base-dir)).resolve;
my $ttm-file =
IO::Path.new($idr, :CWD($build-path)).extension("ttm").resolve;
my $out-file = IO::Path.new($idr, :CWD($dir)).extension("html").resolve;
# Bail out now if the ttm doesn't exist
return unless $ttm-file ~~ :e;
# Generate the html
# TODO: Stop relying on the config for now and just colorize it in regexs
my $katla = run <katla html --config>, $katla-config,
$source-file, $ttm-file, :out;
my $contents = $katla.out.slurp :close;
if $katla.exitcode != 0 {
say "Katla Error:";
say $contents;
exit $katla.exitcode;
}
$contents = stylize $contents;
# Make sure the directory exists and write the contents to it
$out-file.dirname.IO.mkdir;
$out-file.spurt: $contents;
# Return the out-file
$out-file
}
# Add a path to our index hash
my method add-path(%index: $path, $value) {
# Split the path up
my @parts = $path.split("/");
my $head = @parts[0];
@parts = @parts[1..*-1];
if @parts.elems == 0 {
%index{$head} = $value;
} else {
if !%index{$head} {
%index{$head} = %();
}
%index{$head}.&add-path(join('/', @parts), $value);
}
}
# Render the index to html
my method render-index(%index:) {
# TODO sort these properly
my @elements = [];
for %index.keys -> $key {
given %index{$key} {
when Str {
my $x = $_;
@elements.push(
li(Map,
a({ :href($x) }, text($key))));
}
default {
my $rest = $_.&render-index();
@elements.push(li(Map, text($key), $rest));
}
}
}
ul(Map, @elements)
}
sub MAIN(
$project_dir?, #= Base directory of project to upload, defaults to $*CWD
$dest_dir?, #= Destination directory on the remote
$katla-config = #= Configuration file to pass to katla
"/home/nathan/Projects/Idris/.scripts/src/katla.dhall",
) {
# cd to the projet directory if needed
if $project_dir ~~ Str {
$*CWD = $project_dir.IO.resolve;
};
# Find and collect all of our idris files
my @idris-files =
paths("src", :file(-> $a {$a.ends-with(".idr") || $a.ends-with(".md")}))
.map: *.IO.relative("src/".IO);
# Find and collect all of our test files
my @test-files =
paths("test/src", :file(-> $a {$a.ends-with(".idr") || $a.ends-with(".md")}))
.map: *.IO.relative("test/src/".IO);
# Clean if there is more than one ttc folder, easiest way to locate the
# correct one
my $ttc-path = "build/ttc".IO;
if !($ttc-path ~~ :e) || $ttc-path.dir.elems != 1 {
# Do a pack clean build so we can find the ttms
exit -1 unless run <pack clean>;
exit -1 unless run <pack build>;
}
# Now do the same for the test directory
$ttc-path = "test/build/ttc".IO;
if !($ttc-path ~~ :e) || $ttc-path.dir.elems != 1 {
exit -1 unless run <pack clean test/test.ipkg>;
exit -1 unless run <pack build test/test.ipkg>;
}
# find our ttc folder
$build-path = "build/ttc/".IO.dir[0];
# Get a temporary directory to work in
$dir = tempdir().IO;
# For each of our idris files, find the ttm, and generate the output in the
# temporary directory
my %index = %();
for @idris-files.hyper :batch(1) -> $idr {
my $out = render-idris-file $idr, $katla-config.IO;
%index.&add-path($idr, $out.relative($dir)) when $out ~~ IO;
}
# Now repeat for the test directory
my %test-index = %();
$build-path = "test/build/ttc/".IO.dir[0];
for @test-files.hyper :batch(1) -> $idr {
my $out = render-idris-file $idr, $katla-config.IO, "test/src".IO;
%test-index.&add-path($idr, $out.relative($dir)) when $out ~~ IO;
}
my %final-index = ("src" => %index, "test" => ("src" => %test-index));
my $index = %final-index.&render-index();
my $title = $*CWD.basename;
$index = html-en
head(Map,
title(Map, text($title))
),
body(Map,
$index
);
"$dir/index.html".IO.spurt(render($index));
# Figure out our destination
my $dest = do if $dest_dir ~~ Str {
$dest_dir
} else {
my $fst = $*CWD.basename;
my $snd = $*CWD.parent.basename;
"Projects/$snd/$fst"
};
# Upload files
run <rsync -rvh --mkpath --delete>, "$dir/", "$remote:$web-base/$dest";
}