diff options
| author | Paul Buetow <paul@buetow.org> | 2022-04-15 21:42:04 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2022-04-15 21:42:04 +0100 |
| commit | 8511cab2ccd79b8d6bc237548a8e4e6050d0b8c3 (patch) | |
| tree | 74707819f5bb9041dc6ea847a7a29df9dee84e94 | |
| parent | 6f6df3de8f04c6aefe92c0c75492d1e1ff043bc6 (diff) | |
cleanup nonexisten images
| -rwxr-xr-x | katana.raku | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/katana.raku b/katana.raku index 6d5b104..2b9b4fd 100755 --- a/katana.raku +++ b/katana.raku @@ -14,7 +14,7 @@ class Template { } class Image { - has Str $!filename; + has Str $.filename; has Str $!source; has Str $!dist-dir; @@ -25,27 +25,39 @@ class Image { } method generate(Int :$thumb-geometry, Str :$bg-blur) { - my $thumb = "$!dist-dir/thumb/{$!filename}"; + my $thumb = "$!dist-dir/thumb/{$.filename}"; ⁉ [|<convert -auto-orient -geometry>, $thumb-geometry, $!source, $thumb]; - my $blur = "$!dist-dir/blur/{$!filename}"; + my $blur = "$!dist-dir/blur/{$.filename}"; ⁉ [|<convert -flip -geometry>, $thumb-geometry/4, '-blur', $bg-blur, $thumb, $blur]; - my $large = "$!dist-dir/large/{$!filename}"; + my $large = "$!dist-dir/large/{$.filename}"; ⁉ ['cp', $!source, $large]; } - method thumb_tag { "<img class='thumb' src='./thumb/{$!filename}' />" } - method large_tag { "<img class='large' src='./large/{$!filename}' />" } + method thumb_tag { "<img class='thumb' src='./thumb/{$.filename}' />" } + method large_tag { "<img class='large' src='./large/{$.filename}' />" } +} + +sub dist-dirs(Str $dist-dir --> List) { + $dist-dir <<~>> </large /blur /thumb> } sub ensure-directories(Str :$dist-dir) { mkdir $dist-dir unless $dist-dir.IO.d; - mkdir $_ unless .IO.d for $dist-dir <<~>> </large /blur /thumb>; + mkdir $_ unless .IO.d for dist-dirs $dist-dir; +} + +sub cleanup-nonexistent (Str :$dist-dir, :@images) { + my $images = set @images.map:{ $_.filename }; + for dist-dirs $dist-dir -> $dir { + unlink $_ if .IO.basename ∉ $images + for dir($dir, test => { "$dir/$_".IO.f }); + } } sub make-mr-proper(Str :$dist-dir) { - ❱ ['rm', '-rf', $dist-dir] if $dist-dir.IO.d; + ❱ ['rm', '-rf', $dist-dir] if $dist-dir.IO.d; } multi MAIN( @@ -63,6 +75,7 @@ multi MAIN( @images = @images.pick: * if $randomize; say "Found {@images.elems} images"; + cleanup-nonexistent :$dist-dir, :@images; make-mr-proper :$dist-dir if $mr-proper; ensure-directories :$dist-dir; |
