diff options
author | Bryan Bishop <kanzure@gmail.com> | 2010-10-29 19:46:24 -0500 |
---|---|---|
committer | Bryan Bishop <kanzure@gmail.com> | 2010-10-29 19:46:24 -0500 |
commit | 7786ce2a332b0eba4b3ca7c57f906a32e8715da3 (patch) | |
tree | 5a9fe32b69a93f41ae2ac82a50788fe50c0d86fb /libpiny/lib/IkiWiki | |
parent | 413373be9ab30eb21b564cdc180cb2dcda77bfeb (diff) | |
download | piny-code-7786ce2a332b0eba4b3ca7c57f906a32e8715da3.tar.gz piny-code-7786ce2a332b0eba4b3ca7c57f906a32e8715da3.zip |
Starting repo cleanup to make this not so awful
Diffstat (limited to 'libpiny/lib/IkiWiki')
-rw-r--r-- | libpiny/lib/IkiWiki/FakeSetup.pm | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libpiny/lib/IkiWiki/FakeSetup.pm b/libpiny/lib/IkiWiki/FakeSetup.pm new file mode 100644 index 0000000..c9c823b --- /dev/null +++ b/libpiny/lib/IkiWiki/FakeSetup.pm @@ -0,0 +1,88 @@ +package IkiWiki::FakeSetup; + +use strict; +use warnings; + +use Data::Dumper qw( ); +use Exporter qw( import ); + +our @EXPORT_OK = qw( readSetup writeSetup ); + +sub readSetup { + my ( $setupfile ) = @_; + + # Sorry about the use of globals but it's hard to share lexicals with dynamically generated source. + $IkiWiki::FakeSetup::package = undef; + @IkiWiki::FakeSetup::args = (); + + sub inc { + my ( $self, $file ) = @_; + + my $package = $file; + $package =~ s/\//::/g; + $package =~ s/\.pm//; + + my @src = + ( "package $package;\n" + , "sub import {\n" + , " my ( \$class, \@args ) = \@_;\n" + , " \$IkiWiki::FakeSetup::package = \$class;\n" + , " \@IkiWiki::FakeSetup::args = \@args;\n" + , "}\n" + , "1;\n" + ); + + return sub { if ( scalar @src ) { $_ = shift @src; return 1; } else { return 0; } }; + + }; + + eval { + my @oldINC = @INC; + @INC = ( ".", \&inc ); + + my %oldINC = %INC; + %INC = ( ); + + do $setupfile; + + @INC = @oldINC; + %INC = %oldINC; + }; + + return ( $IkiWiki::FakeSetup::package, @IkiWiki::FakeSetup::args ); + +} + +sub writeSetup { + my ( $package, @args ) = @_; + + my $d = Data::Dumper->new( \@args ); + $d->Terse( 1 ); + + return "#!/usr/bin/perl\n\nuse $package " . $d->Dump( ) . ";\n"; +} + +1; + +__END__ + +Because it may not be completely obvious, here's how you would use this: + +#!/usr/bin/perl + +use strict; +use warnings; + +use IkiWiki::FakeSetup qw( readSetup writeSetup ); + +# This reads the setup file, returning the package it uses and any configuration it passed to that package. +my ( $package, $config ) = readSetup( "almighty_ikiwiki_template.setup" ); + +# Make changes to the configuration. +$config->{"srcdir"} = "/some/dir"; +$config->{"adminemail"} = "somejerk\@jerkville.com"; + +# Open a new file, and write the changed configuration to it. +open( SETUP, ">", "almighty_ikiwiki_template_changed.setup" ); +print SETUP writeSetup( $package, $config ); +close( SETUP ); |