From 23d7c8bdfdd320f28f651d9d9f9029cbe2f6efa7 Mon Sep 17 00:00:00 2001 From: Vadim Belman Date: Fri, 9 Dec 2016 21:41:59 -0500 Subject: [PATCH] Item14237: Added classes for manipulating spec files. Namely Foswiki::Config::Spec::Files and Foswiki::Config::Spec::File. Both are in deep draft state. --- core/lib/Foswiki/Config/Spec/File.pm | 85 +++++++++++++++++++++++ core/lib/Foswiki/Config/Spec/Files.pm | 98 +++++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 core/lib/Foswiki/Config/Spec/File.pm create mode 100644 core/lib/Foswiki/Config/Spec/Files.pm diff --git a/core/lib/Foswiki/Config/Spec/File.pm b/core/lib/Foswiki/Config/Spec/File.pm new file mode 100644 index 0000000000..55ea66c786 --- /dev/null +++ b/core/lib/Foswiki/Config/Spec/File.pm @@ -0,0 +1,85 @@ +# See bottom of file for license and copyright information + +# A single spec file object. +package Foswiki::Config::Spec::File; + +use File::stat; +use File::Spec; + +use Foswiki::Class qw(app extensible); +extends qw(Foswiki::File); + +# This is the file where defaults would be cached for fast access. +has cachePath => ( + is => 'rw', + lazy => 1, + clearer => 1, + builder => 'prepareCachePath', +); + +has fmt => ( + is => 'rw', + lazy => 1, + builder => 'prepareFmt', +); + +# Make trigger auto-save defaults cache onto disk. +has cacheContent => ( + is => 'ro', + lazy => 1, + clearer => 1, + predicate => 1, + builder => 'prepareCacheContent', +); + +pluggable guessFormat => sub { + my $this = shift; + + return 'legacy'; +}; + +sub prepareFmt { + my $this = shift; + + return $this->guessFormat; +} + +sub prepareCachePath { + my $this = shift; + + my ( $vol, $dir, $fname ) = File::Spec->splitpath( $this->path ); + + $fname =~ s/^(.+)\.[^\.]+$/\.$1.cache/; + + return File::Spec->catpath( $vol, $dir, $fname ); +} + +sub prepareCacheContent { + my $this = shift; + + my $cachePath = $this->cachePath; + + return '' unless -e $cachePath; + + return $this->app->readFile( $cachePath, raiseError => 1, ); +} + +1; +__END__ +Foswiki - The Free and Open Source Wiki, http://foswiki.org/ + +Copyright (C) 2016 Foswiki Contributors. Foswiki Contributors +are listed in the AUTHORS file in the root of this distribution. +NOTE: Please extend that file, not this notice. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. For +more details read LICENSE in the root of this distribution. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +As per the GPL, removal of this notice is prohibited. diff --git a/core/lib/Foswiki/Config/Spec/Files.pm b/core/lib/Foswiki/Config/Spec/Files.pm new file mode 100644 index 0000000000..e7a6c79610 --- /dev/null +++ b/core/lib/Foswiki/Config/Spec/Files.pm @@ -0,0 +1,98 @@ +# See bottom of file for license and copyright information + +package Foswiki::Config::Spec::Files; + +use Foswiki (); +use File::Spec (); + +use Foswiki::Class qw(app callbacks extensible); +extends qw(Foswiki::Object); + +has list => ( + is => 'rw', + lazy => 1, + clearer => 1, + isa => Foswiki::Object::isaHASH( 'list', noUndef => 1, ), + builder => 'prepareList', +); + +sub _scanDir { + my $this = shift; + my $dir = shift; + + return () unless -d $dir; + + my $dh; + + return () + unless opendir $dh, $dir; + + my @specFiles; + + state $curdir = File::Spec->curdir; + state $updir = File::Spec->updir; + + DIR_ENTRY: + while ( my $entry = readdir $dh ) { + next DIR_ENTRY if $entry eq $curdir || $entry eq $updir; + + my $fullpath = File::Spec->catdir( $dir, $entry ); + + if ( -d $fullpath ) { + push @specFiles, $this->_scanDir($fullpath); + next DIR_ENTRY; + } + + next DIR_ENTRY unless $entry =~ /(?:^Spec\.[^.]+|\.spec)$/; + + push @specFiles, + $this->create( 'Foswiki::Config::File', path => $fullpath ); + } +} + +sub collectSpecs { + my $this = shift; + + my @specFileList; + + my @subDirs = qw(Plugins Contrib Extension); + + my ( $vol, $dir ) = File::Spec->splitpath( $INC{'Foswiki.pm'} ); + + my $baseDir = File::Spec->catpath( $vol, $dir ); + + push @specFileList, File::Spec->catfile( $baseDir, 'Foswiki.spec' ); + + foreach my $subDir (@subDirs) { + my $specDir = File::Spec->catdir( $baseDir, 'Foswiki', $subDir ); + push @specFileList, $this->_scanDir($specDir); + } + + return @specFileList; +} + +sub prepareList { + my $this = shift; + + return [ $this->collectSpecs ]; +} + +1; +__END__ +Foswiki - The Free and Open Source Wiki, http://foswiki.org/ + +Copyright (C) 2016 Foswiki Contributors. Foswiki Contributors +are listed in the AUTHORS file in the root of this distribution. +NOTE: Please extend that file, not this notice. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. For +more details read LICENSE in the root of this distribution. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +As per the GPL, removal of this notice is prohibited.