From 99f8ccd6cddc6fa9a021d4118e6a706a19e474b1 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 10 Sep 2015 15:46:11 +0100 Subject: maybe I got deps working I'd still like to make the whole thing more readable, though --- lib/WebCoso.pm | 30 +++++++++++++++++++++++++----- lib/WebCoso/File.pm | 31 +++++++++++++++++++++++++++++++ lib/WebCoso/Maker.pm | 28 +++++++++++++++++++--------- lib/WebCoso/Maker/HTML.pm | 4 ++-- lib/WebCoso/Maker/RST.pm | 4 ++-- lib/WebCoso/Maker/TT.pm | 4 ++-- lib/WebCoso/Maker/XHTML.pm | 5 +++-- t/lib/Test/WebCoso.pm | 2 +- t/tests/maker.t | 4 ++-- t/tests/page.t | 5 +++-- t/tests/webcoso.t | 2 +- 11 files changed, 91 insertions(+), 28 deletions(-) create mode 100644 lib/WebCoso/File.pm diff --git a/lib/WebCoso.pm b/lib/WebCoso.pm index a990b67..dbbfda4 100644 --- a/lib/WebCoso.pm +++ b/lib/WebCoso.pm @@ -1,5 +1,7 @@ # -*- mode: perl6 -*- use File::Temp; +use JSON::Fast; +use WebCoso::File; use WebCoso::Doc::Page; use WebCoso::Doc::Feed; @@ -9,6 +11,20 @@ class WebCoso { has $!tmpdir = tempdir.IO; has %!docs; + method load-deps($dir,$basename,$ext) { + my %deps = try { from-json($.srcdir.child('deps.json').IO.slurp) } // {}; + my $ret = %deps{$basename}{$ext} // {}; + for $ret.values -> $f is rw { $f = WebCoso::File.new($f) }; + return $ret; + } + + method save-deps($dir,$basename,$ext,%new-deps is copy) { + my %deps = try { from-json($.srcdir.child('deps.json').IO.slurp) } // {}; + %new-deps.values».=path; + %deps{$basename}{$ext} = %new-deps; + $.srcdir.child('deps.json').IO.spurt(to-json(%deps)); + } + method get-src-files($dir,$basename,$ext) { say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-src-files($dir,$basename,$ext)"; my @files = gather { @@ -24,13 +40,17 @@ class WebCoso { say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-src-files($dir,$basename,$ext) files = {@files.perl}"; return ( map { - (.basename ~~ /$basename \. (.+?) \. $ext/)[0] - => - $_ + my $lang = (.basename ~~ /$basename \. (.+?) \. $ext/)[0].Str; + $lang => WebCoso::File.new(:$lang,path=>$_) }, @files ).values; } + method get-file($dir,$path) { + say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-file($dir,$path)"; + return WebCoso::File.new($.srcdir.child($dir).child($path)); + } + method get-made-files($dir,$basename,$ext) { say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-made-files($dir,$basename,$ext)"; if %!docs{$dir}{$basename} -> $doc { @@ -51,8 +71,8 @@ class WebCoso { say "WebCoso($.srcdir,$.destdir,$!tmpdir)::put-file($dir,$basename,$lang,$ext)"; my $t = ($dir ?? $!tmpdir.child($dir) !! $!tmpdir); $t.mkdir; - my $f = $t.child("{$basename}.{$lang}.{$ext}"); - $f.spurt($contents); + my $f = WebCoso::File.new(:$lang,path=>$t.child("{$basename}.{$lang}.{$ext}")); + $f.contents($contents); return $f; } diff --git a/lib/WebCoso/File.pm b/lib/WebCoso/File.pm new file mode 100644 index 0000000..764cf2f --- /dev/null +++ b/lib/WebCoso/File.pm @@ -0,0 +1,31 @@ +# -*- mode: perl6 -*- +class WebCoso::File { + has Str $.path; + has $.lang; + has $!contents; + + multi method new($self: $path) { + $self.bless(:$path); + } + + submethod BUILD(Str(Cool) :$!lang, Str(Cool) :$!path) {} + + multi method contents() { + return $!contents //= $.parse(); + } + multi method contents($new_contents) { + return $!contents = $.serialise($new_contents); + } + + method parse() { + return $.path.IO.slurp(); + } + method serialise($contents) { + $.path.IO.spurt($contents); + return $contents; + } + + method modified() { + return $.path.IO.modified; + } +} diff --git a/lib/WebCoso/Maker.pm b/lib/WebCoso/Maker.pm index 91ceb03..b64c9cf 100644 --- a/lib/WebCoso/Maker.pm +++ b/lib/WebCoso/Maker.pm @@ -4,7 +4,7 @@ role WebCoso::Maker[$from,$to] { has $.basename; has $.dir; - method process-contents(:$src,:@deps) { ... } + method process-contents($src) { ... } method dest-files() { say "Maker[$from,$to]($.dir/$.basename)::dest-files"; @@ -16,8 +16,16 @@ role WebCoso::Maker[$from,$to] { return $.wc.get-files($.dir,$.basename,$from); } + method get-file($path) { + return $.wc.get-file($.dir,$path); + } + method dep-files() { - return Hash; + return $.wc.load-deps($.dir,$.basename,$to); + } + + method set-deps(%deps) { + $.wc.save-deps($.dir,$.basename,$to,%deps); } method make() { @@ -34,20 +42,22 @@ role WebCoso::Maker[$from,$to] { my @deps = %deps{$lang} // (); say "Maker[$from,$to]($.dir/$.basename)::make lang $lang"; say "Maker[$from,$to]($.dir/$.basename)::make dst modified {$dst ?? $dst.modified !! 'not-there'} src modified {$src.modified}"; - next if $dst and $dst.modified after + next if $dst and not $dst.modified before all($src.modified,@deps».modified.flat); say "Maker[$from,$to]($.dir/$.basename)::make processing $lang"; - my $processed-contents = self.process-contents( - src => $src.slurp, - deps => @deps».slurp, + my ($processed-contents,@new_deps) = self.process-contents( + $src, ); - # that .Str.IO is a hack to work around - # https://rt.perl.org/Public/Bug/Display.html?id=126006 %dsts{$lang} = $.wc.put-file($.dir,$.basename,$lang,$to, - $processed-contents).Str.IO; + $processed-contents); + %deps{$lang} = @new_deps + if @new_deps; } + + $.set-deps(%deps); + return %dsts; } } diff --git a/lib/WebCoso/Maker/HTML.pm b/lib/WebCoso/Maker/HTML.pm index 414fe2b..d2803af 100644 --- a/lib/WebCoso/Maker/HTML.pm +++ b/lib/WebCoso/Maker/HTML.pm @@ -1,7 +1,7 @@ # -*- mode: perl6 -*- use WebCoso::Maker; class WebCoso::Maker::HTML does WebCoso::Maker['xhtml','html'] { - method process-contents(:src($xhtml),:@deps) { - return "{$xhtml} decorated"; + method process-contents($xhtml) { + return "{$xhtml.contents} decorated"; } } diff --git a/lib/WebCoso/Maker/RST.pm b/lib/WebCoso/Maker/RST.pm index 76bc127..8f11817 100644 --- a/lib/WebCoso/Maker/RST.pm +++ b/lib/WebCoso/Maker/RST.pm @@ -1,7 +1,7 @@ # -*- mode: perl6 -*- use WebCoso::Maker; class WebCoso::Maker::RST does WebCoso::Maker['rest.txt','du.xml'] { - method process-contents(:src($rst),:@deps) { - return "{$rst} parsed"; + method process-contents($rst) { + return "{$rst.contents} parsed"; } } diff --git a/lib/WebCoso/Maker/TT.pm b/lib/WebCoso/Maker/TT.pm index 256cf54..39ba283 100644 --- a/lib/WebCoso/Maker/TT.pm +++ b/lib/WebCoso/Maker/TT.pm @@ -1,7 +1,7 @@ # -*- mode: perl6 -*- use WebCoso::Maker; class WebCoso::Maker::TT does WebCoso::Maker['tt','rest.txt'] { - method process-contents(:src($tt),:@deps) { - return "{$tt} expanded"; + method process-contents($tt) { + return "{$tt.contents} expanded"; } } diff --git a/lib/WebCoso/Maker/XHTML.pm b/lib/WebCoso/Maker/XHTML.pm index c2b1d9e..ea50ec5 100644 --- a/lib/WebCoso/Maker/XHTML.pm +++ b/lib/WebCoso/Maker/XHTML.pm @@ -1,7 +1,8 @@ # -*- mode: perl6 -*- use WebCoso::Maker; class WebCoso::Maker::XHTML does WebCoso::Maker['du.xml','xhtml'] { - method process-contents(:src($du),:@deps) { - return "{$du} converted"; + method process-contents($du) { + my $stylesheet = $.get-file('du2xhtml.xsl'); + return ("{$du.contents} converted",$stylesheet); } } diff --git a/t/lib/Test/WebCoso.pm b/t/lib/Test/WebCoso.pm index 1f54c92..083ef09 100644 --- a/t/lib/Test/WebCoso.pm +++ b/t/lib/Test/WebCoso.pm @@ -11,7 +11,7 @@ sub cmp-files($a,$b,$msg) is export { $cmp.($a{$_}) } else { - $a{$_}.abspath eq $cmp.abspath + $a{$_}.path.IO.abspath eq $cmp.path.IO.abspath } }, $a.keys), $msg, diff --git a/t/tests/maker.t b/t/tests/maker.t index a09ec19..4d21a0f 100644 --- a/t/tests/maker.t +++ b/t/tests/maker.t @@ -25,8 +25,8 @@ my %output = $m.make(); cmp-files( %output, { - en => { $^x.slurp eq 'en expanded' }, - it => { $^x.slurp eq 'it expanded' }, + en => { $^x.contents eq 'en expanded' }, + it => { $^x.contents eq 'it expanded' }, }, 'made the files', ); diff --git a/t/tests/page.t b/t/tests/page.t index 939c404..57ac1cb 100644 --- a/t/tests/page.t +++ b/t/tests/page.t @@ -14,6 +14,7 @@ $destdir.mkdir; $srcdir.child('document.it.tt').spurt('it'); $srcdir.child('document.en.rest.txt').spurt('en'); +$srcdir.child('du2xhtml.xsl').spurt('<>'); my $wc = WebCoso.new(:$srcdir,:$destdir); $wc.new-page(''); @@ -25,8 +26,8 @@ dd %output; cmp-files( %output, { - it => { $^x.slurp eq 'it expanded parsed converted decorated' }, - en => { $^x.slurp eq 'en parsed converted decorated' }, + it => { $^x.contents eq 'it expanded parsed converted decorated' }, + en => { $^x.contents eq 'en parsed converted decorated' }, }, 'built everyting', ); diff --git a/t/tests/webcoso.t b/t/tests/webcoso.t index 49e28ee..84d24fb 100644 --- a/t/tests/webcoso.t +++ b/t/tests/webcoso.t @@ -36,7 +36,7 @@ subtest { my $put = $wc.put-file('two','document','en','txt', 'something'); - ok($put.abspath ~~ /two\/document\.en\.txt$/, + ok($put.path.IO.abspath ~~ /two\/document\.en\.txt$/, 'put-file works', ); -- cgit v1.2.3