From df354d91e7e315b839d5c292e386e64ca339cb07 Mon Sep 17 00:00:00 2001 From: dakkar Date: Fri, 11 Sep 2015 15:32:51 +0100 Subject: filesets! don't work I think I'm trying to be too clever, merging "these files make up a document, a file per language" with "these files are needed to build a result file in a certain language". Those should probably be separate classes. --- lib/WebCoso.pm | 40 +++++++---------- lib/WebCoso/FileSet.pm | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/WebCoso/Maker.pm | 37 ++++++++-------- t/lib/Test/WebCoso.pm | 11 ++--- t/tests/deps.t | 48 +++++++++++++++++++++ t/tests/fileset.t | 99 ++++++++++++++++++++++++++++++++++++++++++ t/tests/page.t | 14 +++--- 7 files changed, 310 insertions(+), 54 deletions(-) create mode 100644 lib/WebCoso/FileSet.pm create mode 100644 t/tests/deps.t create mode 100644 t/tests/fileset.t diff --git a/lib/WebCoso.pm b/lib/WebCoso.pm index dbbfda4..d734bd8 100644 --- a/lib/WebCoso.pm +++ b/lib/WebCoso.pm @@ -2,6 +2,7 @@ use File::Temp; use JSON::Fast; use WebCoso::File; +use WebCoso::FileSet; use WebCoso::Doc::Page; use WebCoso::Doc::Feed; @@ -13,37 +14,28 @@ class WebCoso { 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; + my %ret = %deps{$dir}{$basename}{$ext} // {}; + return WebCoso::FileSet.new(files-hash=>%ret); } - method save-deps($dir,$basename,$ext,%new-deps is copy) { + method save-deps($dir,$basename,$ext,$new-deps) { my %deps = try { from-json($.srcdir.child('deps.json').IO.slurp) } // {}; - %new-deps.values».=path; - %deps{$basename}{$ext} = %new-deps; + %deps{$dir}{$basename}{$ext} = $new-deps.export-hash; $.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 { + my @filesets = (gather { for $.srcdir,$!tmpdir -> $base { - take $_ for try { - CATCH { when X::IO { } } - ( $dir ?? $base.child($dir) !! $base ).dir( - test => /$basename \. .+? \. $ext/, - ) - } // (); + take WebCoso::FileSet.new( + path => ( $dir ?? $base.child($dir) !! $base ), + :$basename, :$ext, + ); } - } - say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-src-files($dir,$basename,$ext) files = {@files.perl}"; - return ( - map { - my $lang = (.basename ~~ /$basename \. (.+?) \. $ext/)[0].Str; - $lang => WebCoso::File.new(:$lang,path=>$_) - }, @files - ).values; + }).values; + say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-src-files($dir,$basename,$ext) files = {@filesets».files}"; + return @filesets.reduce({$^a.merged-with($^b)}); } method get-file($dir,$path) { @@ -62,9 +54,9 @@ class WebCoso { method get-files($dir,$basename,$ext,:$make=True) { say "WebCoso($.srcdir,$.destdir,$!tmpdir)::get-files($dir,$basename,$ext,$make)"; - my %src = self.get-src-files($dir,$basename,$ext); - my %made = ( $make ?? self.get-made-files($dir,$basename,$ext) !! () ); - return flat %src,%made; + my $src = self.get-src-files($dir,$basename,$ext); + my $made = ( $make ?? self.get-made-files($dir,$basename,$ext) !! () ); + return $src.merged-with($made); } method put-file($dir,$basename,$lang,$ext,$contents) { diff --git a/lib/WebCoso/FileSet.pm b/lib/WebCoso/FileSet.pm new file mode 100644 index 0000000..958a649 --- /dev/null +++ b/lib/WebCoso/FileSet.pm @@ -0,0 +1,115 @@ +# -*- mode: perl6 -*- +use WebCoso::File; + +class WebCoso::FileSet { + has %.files; + + multi method new($self: :$path!, :$basename!, :$ext!) { + my @filenames = try { + CATCH { when X::IO { } } + $path.IO.dir( + test => /$basename \. .+? \. $ext/, + ) + } // (); + my %files = map { + my $lang = (.basename ~~ /$basename \. (.+?) \. $ext/)[0].Str; + $lang => WebCoso::File.new(:$lang,path=>$_) + }, @filenames; + $self.bless(:%files); + } + + multi method new($self: :@files!) { + $self.bless(files=>{}).merged-with(@files); + } + multi method new($self: :%files-hash!) { + $self.bless(files=>{}).merged-with(%files-hash); + } + multi method new($self: :%files!) { + $self.bless(files=>%files); + } + + submethod BUILD(:%!files) {} + + method langs() { + return %!files.keys; + } + + method files() { + return %!files.values; + } + method export-hash() { + my %ret; + for $.langs -> $lang { + my $f = $.for-lang($lang); + if ($f.does(Iterable)) { + %ret{$lang} = $f.list».path».Str; + } + else { + %ret{$lang} = $f.path.Str; + } + } + return %ret; + } + + method for-lang($lang) { + return %!files{$lang} // (); + } + + multi method set-for-lang($lang,WebCoso::File @files) { + return %!files{$lang} = @files; + } + multi method set-for-lang($lang,WebCoso::File $file) { + return %!files{$lang} = $file; + } + + multi method set-for-lang($lang,@files) { + return %!files{$lang} = map { + $_.isa(WebCoso::File) ?? $_ !! WebCoso::File.new($_) + },@files; + } + multi method set-for-lang($lang,$file) { + return %!files{$lang} = WebCoso::File.new($file); + } + + multi method merged-with(Any:U $) { return self } + multi method merged-with(% where !*) { return self } + multi method merged-with(@ where !*) { return self } + + multi method merged-with(WebCoso::File %b where *) { + my %new-files = %!files; + %new-files{$_} = %b{$_} for %b.keys; + return self.new( + files => %new-files, + ); + } + multi method merged-with(%b where *) { + my WebCoso::File %files = map { + my $lang = $_; + my $v = %b{$_}; + $lang => ( + $v.isa(WebCoso::File) + ?? $v + !! $v.does(Iterable) + ?? [ map { WebCoso::File.new(:$lang,path=>$_) },$v ] + !! WebCoso::File.new(:$lang,path=>$v) + ) + },%b.keys; + return self.merged-with(%files); + } + multi method merged-with(WebCoso::File @b where *) { + self.merged-with( + %(map { $_.lang => $_ },@b), + ); + } + multi method merged-with(@b where *) { + my WebCoso::File %files = map { + $_.isa(WebCoso::File) + ?? (($_.lang//'') => $_) + !! ('' => WebCoso::File.new(path=>$_)) + },@b; + self.merged-with(%files); + } + multi method merged-with(WebCoso::FileSet:D $b) { + self.merged-with($b.files); + } +} diff --git a/lib/WebCoso/Maker.pm b/lib/WebCoso/Maker.pm index 02e1a18..bcf759b 100644 --- a/lib/WebCoso/Maker.pm +++ b/lib/WebCoso/Maker.pm @@ -24,40 +24,43 @@ role WebCoso::Maker[$from,$to] { return $.wc.load-deps($.dir,$.basename,$to); } - method set-deps(%deps) { - $.wc.save-deps($.dir,$.basename,$to,%deps); + method set-deps($deps) { + $.wc.save-deps($.dir,$.basename,$to,$deps); } method make() { say "Maker[$from,$to]($.dir/$.basename)::make"; - my %srcs = $.src-files(); - my %dsts = $.dest-files(); - my %deps = $.dep-files(); + my $srcs = $.src-files(); + my $dsts = $.dest-files(); + my $deps = $.dep-files(); - say "Maker[$from,$to]($.dir/$.basename)::make from {%srcs.perl} and {%deps.perl} to {%dsts.perl}"; + say "Maker[$from,$to]($.dir/$.basename)::make from {$srcs.perl} and {$deps.perl} to {$dsts.perl}"; - for %srcs.keys -> $lang { - my $src = %srcs{$lang}; - my $dst = %dsts{$lang}; - my @deps = %deps{$lang} // (); + for $srcs.langs -> $lang { + my $src = $srcs.for-lang($lang)[0]; + my $dst = $dsts.for-lang($lang)[0]; + my @deps = $deps.for-lang($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} deps modified {@deps».modified}"; - next if $dst and not ($dst.modified <= - all($src.modified,@deps».modified.flat)); + next if $dst and $dst.modified >= + all($src.modified,@deps».modified.flat); say "Maker[$from,$to]($.dir/$.basename)::make processing $lang"; my ($processed-contents,@new_deps) = self.process-contents( $src, ); - %dsts{$lang} = $.wc.put-file($.dir,$.basename,$lang,$to, - $processed-contents); - %deps{$lang} = @new_deps + $dsts.set-for-lang( + $lang, + $.wc.put-file($.dir,$.basename,$lang,$to, + $processed-contents), + ); + $deps.set-for-lang($lang,@new_deps) if @new_deps; } - $.set-deps(%deps); + $.set-deps($deps); - return %dsts; + return $dsts; } } diff --git a/t/lib/Test/WebCoso.pm b/t/lib/Test/WebCoso.pm index 566e4bc..c179e47 100644 --- a/t/lib/Test/WebCoso.pm +++ b/t/lib/Test/WebCoso.pm @@ -1,19 +1,20 @@ # -*- mode: perl6 -*- unit module Test::WebCoso; use Test; +use WebCoso::FileSet; -sub cmp-files($a,$b,$msg) is export { +sub cmp-files(WebCoso::FileSet $a,$b,$msg) is export { ok( - ($a.defined and $b.defined and $a.keys eqv $b.keys and + ($a.defined and $b.defined and $a.langs eqv $b.keys and [and] map { my $cmp = $b{$_}; if $cmp ~~ Callable { - $cmp.($a{$_}) + $cmp.($a.for-lang($_)) } else { - $a{$_}.path.IO.abspath eq $cmp.IO.abspath + $a.for-lang($_).path.IO.abspath eq $cmp.IO.abspath } - }, $a.keys), + }, $a.langs), $msg, ); } diff --git a/t/tests/deps.t b/t/tests/deps.t new file mode 100644 index 0000000..2fde79b --- /dev/null +++ b/t/tests/deps.t @@ -0,0 +1,48 @@ +# -*- mode: perl6 -*- +use Test; +use lib 't/lib'; +use Test::WebCoso; +use File::Temp; +use WebCoso; +use WebCoso::FileSet; + +my $testdir = tempdir.IO; +my $srcdir = $testdir.child('src'); +my $destdir = $testdir.child('dst'); + +$srcdir.mkdir; +$destdir.mkdir; + +my $wc = WebCoso.new(:$srcdir,:$destdir); +$wc.save-deps( + 'foo','document','du.xml', + WebCoso::FileSet.new(files-hash=>{it=>['some.xsl']}), +); + +my $no-deps = $wc.load-deps('bar','random','txt'); +ok($no-deps.files.elems == 0, + 'no deps is empty'); + +my $deps = $wc.load-deps('foo','document','du.xml'); +cmp-files( + $deps, + { it => { $^x.path eq 'some.xsl' } }, + 'deps round-tripped', +); + +my @d; +$no-deps.set-for-lang('x',@d); +dd $no-deps; +dd $no-deps.export-hash; +$wc.save-deps('bar','random','txt',$no-deps); +say $srcdir.child('deps.json').IO.slurp; +my $empty-deps = $wc.load-deps('bar','random','txt'); +dd $empty-deps; +cmp-files( + $deps, + { x=> {$^x.elems == 0} }, + 'empty deps is empty', +); + + +done-testing; diff --git a/t/tests/fileset.t b/t/tests/fileset.t new file mode 100644 index 0000000..7723dc2 --- /dev/null +++ b/t/tests/fileset.t @@ -0,0 +1,99 @@ +# -*- mode: perl6 -*- +use Test; +use lib 't/lib'; +use Test::WebCoso; +use File::Temp; +use WebCoso::FileSet; + +my $testdir = tempdir.IO; + +$testdir.child('document.it.tt').spurt('it'); +$testdir.child('document.en.rest.txt').spurt('en'); +$testdir.child('du2xhtml.xsl').spurt('<>'); + +subtest { + my $fs1 = WebCoso::FileSet.new( + basename=>'document', + ext=>'tt', + path=>$testdir, + ); + cmp-files( + $fs1, + { it => { $^x.contents eq 'it' } }, + 'simple ext works', + ); + + my $fs2 = WebCoso::FileSet.new( + basename=>'document', + ext=>'rest.txt', + path=>$testdir, + ); + cmp-files( + $fs2, + { en => { $^x.contents eq 'en' } }, + 'harder ext works', + ); + + my $fs3 = WebCoso::FileSet.new( + files => [$testdir.child('du2xhtml.xsl')], + ); + cmp-files( + $fs3, + { '' => { $^x.contents eq '<>' } }, + 'direct files works', + ); + + my $fs4 = WebCoso::FileSet.new( + files-hash => { + xml => $testdir.child('du2xhtml.xsl'), + it => $testdir.child('document.it.tt'), + } + ); + cmp-files( + $fs4, + { + xml => { $^x.contents eq '<>' }, + it => { $^x.contents eq 'it' }, + }, + 'files hash works', + ); +},'construction'; + +subtest { + my $fs-merged = WebCoso::FileSet.new( + basename=>'document', + ext=>'tt', + path=>$testdir, + ).merged-with(WebCoso::FileSet.new( + basename=>'document', + ext=>'rest.txt', + path=>$testdir, + )); + cmp-files( + $fs-merged, + { + it => { $^x.contents eq 'it' }, + en => { $^x.contents eq 'en' }, + }, + 'merging works', + ); +},'merging'; + +subtest { + my $fs1 = WebCoso::FileSet.new( + basename=>'document', + ext=>'tt', + path=>$testdir, + ); + $fs1.set-for-lang('en',$testdir.child('document.en.rest.txt')); + cmp-files( + $fs1, + { + it => { $^x.contents eq 'it' }, + en => { $^x.contents eq 'en' }, + }, + 'set-for-lang works', + ); +},'set-for-lang'; + +done-testing; diff --git a/t/tests/page.t b/t/tests/page.t index 62faf98..9cff6d7 100644 --- a/t/tests/page.t +++ b/t/tests/page.t @@ -20,11 +20,11 @@ my $wc = WebCoso.new(:$srcdir,:$destdir); $wc.new-page(''); $wc.run(); -my %output = $wc.get-files('','document','html'); -dd %output; +my $output = $wc.get-files('','document','html'); +dd $output; cmp-files( - %output, + $output, { it => { $^x.contents eq 'it expanded parsed converted (<>) decorated' }, en => { $^x.contents eq 'en parsed converted (<>) decorated' }, @@ -34,13 +34,11 @@ cmp-files( $wc.get-file('','du2xhtml.xsl').contents(''); -%output = $wc.get-files('','document','html'); -dd %output; -dd %output.contents; -dd %output.contents; +$output = $wc.get-files('','document','html'); +dd $output; cmp-files( - %output, + $output, { it => { $^x.contents eq 'it expanded parsed converted () decorated' }, en => { $^x.contents eq 'en parsed converted () decorated' }, -- cgit v1.2.3