diff options
Diffstat (limited to 'IkiWiki/Setup')
-rw-r--r-- | IkiWiki/Setup/Automator.pm | 111 | ||||
-rw-r--r-- | IkiWiki/Setup/Standard.pm | 117 |
2 files changed, 226 insertions, 2 deletions
diff --git a/IkiWiki/Setup/Automator.pm b/IkiWiki/Setup/Automator.pm new file mode 100644 index 000000000..060a8be56 --- /dev/null +++ b/IkiWiki/Setup/Automator.pm @@ -0,0 +1,111 @@ +#!/usr/bin/perl +# Ikiwiki setup automator. + +package IkiWiki::Setup::Automator; + +use warnings; +use strict; +use IkiWiki; +use Term::ReadLine; +use File::Path; + +sub ask ($$) { #{{{ + my ($question, $default)=@_; + + my $r=Term::ReadLine->new("ikiwiki"); + $r->readline($question." ", $default); +} #}}} + +sub import (@) { #{{{ + my %setup=@_; + + # Sanitize this to avoid problimatic directory names. + $setup{wikiname}=~s/[^-A-Za-z0-9_] //g; + if (! length $setup{wikiname}) { + die "you must enter a wikiname\n"; + } + + # Avoid overwriting any existing files. + foreach my $key (qw{srcdir destdir repository setupfile}) { + next unless exists $setup{$key}; + my $add=""; + while (-e $setup{$key}.$add) { + $add=1 if ! $add; + $add++; + } + $setup{$key}.=$add; + } + + print "\n\nSetting up $setup{wikiname} ...\n"; + + # Set up the repository. + mkpath($setup{srcdir}) || die "mkdir $setup{srcdir}: $!"; + delete $setup{repository} if ! $setup{rcs} || $setup{rcs}=~/bzr|mercurial/; + if ($setup{rcs}) { + my @params=($setup{rcs}, $setup{srcdir}); + push @params, $setup{repository} if exists $setup{repository}; + if (system("ikiwiki-makerepo", @params) != 0) { + die "failed: ikiwiki-makerepo @params"; + } + } + + # Generate setup file. + my @params=( + "--dumpsetup", $setup{setupfile}, + "--wikiname", $setup{wikiname}, + "--url", $setup{url}, + "--cgiurl", $setup{cgiurl} + ); + if ($setup{rcs}) { + push @params, "--rcs", $setup{rcs}; + if ($setup{rcs} eq 'git') { + push @params, "--set", "git_wrapper=". + $setup{repository}."/hooks/post-update"; + } + elsif ($setup{rcs} eq 'svn') { + push @params, "--set", "svn_wrapper=". + $setup{repository}."/hooks/post-commit"; + } + elsif ($setup{rcs} eq 'bzr') { + # TODO + } + elsif ($setup{rcs} eq 'mercurial') { + # TODO + } + } + if (exists $setup{add_plugins}) { + foreach my $plugin (@{$setup{add_plugins}}) { + push @params, "--plugin", $plugin; + } + } + if (exists $setup{disable_plugins}) { + foreach my $plugin (@{$setup{disable_plugins}}) { + push @params, "--disable-plugin", $plugin; + } + } + foreach my $key (keys %setup) { + next if $key =~ /^(disable_plugins|add_plugins|setupfile|wikiname|url|cgiurl||srcdir|destdir|repository)$/; + push @params, "--set", "$key=$setup{$key}"; + } + if (system("ikiwiki", @params, $setup{srcdir}, $setup{destdir}) != 0) { + die "failed: ikiwiki @params"; + } + + # Build the wiki. + mkpath($setup{destdir}) || die "mkdir $setup{destdir}: $!"; + if (system("ikiwiki", "--setup", $setup{setupfile}) != 0) { + die "ikiwiki --setup $setup{setupfile} failed"; + } + + # Done! + print "\n\nSuccessfully set up $setup{wikiname}:\n"; + foreach my $key (qw{url srcdir destdir repository setupfile}) { + next unless exists $setup{$key}; + my $value=$setup{$key}; + $value=~s/^\Q$ENV{HOME}\E\//~\//; + print "\t$key: ".(" " x (10 - length($key)))." $value\n"; + } + exit 0; +} #}}} + +1 diff --git a/IkiWiki/Setup/Standard.pm b/IkiWiki/Setup/Standard.pm index f67c3829b..99da7ed86 100644 --- a/IkiWiki/Setup/Standard.pm +++ b/IkiWiki/Setup/Standard.pm @@ -7,9 +7,122 @@ package IkiWiki::Setup::Standard; use warnings; use strict; +use IkiWiki; -sub import { +sub import { #{{{ $IkiWiki::Setup::raw_setup=$_[1]; -} +} #}}} + +sub dumpline ($$$$) { #{{{ + my $key=shift; + my $value=shift; + my $type=shift; + my $prefix=shift; + + eval q{use Data::Dumper}; + error($@) if $@; + local $Data::Dumper::Terse=1; + local $Data::Dumper::Indent=1; + local $Data::Dumper::Pad="\t"; + local $Data::Dumper::Sortkeys=1; + local $Data::Dumper::Quotekeys=0; + + my $dumpedvalue; + if ($type eq 'boolean' || $type eq 'integer') { + # avoid quotes + $dumpedvalue=$value; + } + elsif ($type eq 'string' && ref $value eq 'ARRAY' && @$value && + ! grep { /[^-A-Za-z0-9_]/ } @$value) { + # dump simple array as qw{} + $dumpedvalue="[qw{ ".join(" ", @$value)." }]"; + } + else { + $dumpedvalue=Dumper($value); + chomp $dumpedvalue; + if (length $prefix) { + # add to second and subsequent lines + my @lines=split(/\n/, $dumpedvalue); + $dumpedvalue=""; + for (my $x=0; $x <= $#lines; $x++) { + $lines[$x] =~ s/^\t//; + $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n"; + } + } + $dumpedvalue=~s/^\t//; + chomp $dumpedvalue; + } + + return "\t$prefix$key => $dumpedvalue,"; +} #}}} + +sub dumpvalues ($@) { #{{{ + my $setup=shift; + my @ret; + while (@_) { + my $key=shift; + my %info=%{shift()}; + + next if $info{type} eq "internal"; + + push @ret, "\t# ".$info{description} if exists $info{description}; + + if (exists $setup->{$key} && defined $setup->{$key}) { + push @ret, dumpline($key, $setup->{$key}, $info{type}, ""); + delete $setup->{$key}; + } + elsif (exists $info{example}) { + push @ret, dumpline($key, $info{example}, $info{type}, "#"); + } + } + return @ret; +} #}}} + +sub gendump ($) { #{{{ + my $description=shift; + my %setup=(%config); + my @ret; + + # disable logging to syslog while dumping + $config{syslog}=0; + + push @ret, "\t# basic setup"; + push @ret, dumpvalues(\%setup, IkiWiki::getsetup()); + + # Load all plugins, so that all setup options are available. + # (But skip a few problematic external demo plugins.) + my @plugins=grep { ! /^(externaldemo|pythondemo|\Q$config{rcs}\E)$/ } + sort(IkiWiki::listplugins()); + unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st + foreach my $plugin (@plugins) { + eval { IkiWiki::loadplugin($plugin) }; + if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) { + my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() }; + } + } + + foreach my $id (@plugins) { + if (exists $IkiWiki::hooks{getsetup}{$id}{call}) { + # use an array rather than a hash, to preserve order + my @s=eval { $IkiWiki::hooks{getsetup}{$id}{call}->() }; + next unless @s; + push @ret, "", "\t# $id plugin"; + push @ret, dumpvalues(\%setup, @s); + } + } + + unshift @ret, + "#!/usr/bin/perl", + "# $description", + "#", + "# Passing this to ikiwiki --setup will make ikiwiki generate", + "# wrappers and build the wiki.", + "#", + "# Remember to re-run ikiwiki --setup any time you edit this file.", + "use IkiWiki::Setup::Standard {"; + push @ret, "}"; + + return @ret; +} #}}} 1 |