rcs hook setup for the two I know
[ikiwiki.git] / IkiWiki / Setup / Automator.pm
1 #!/usr/bin/perl
2 # Ikiwiki setup automator.
3
4 package IkiWiki::Setup::Automator;
5
6 use warnings;
7 use strict;
8 use IkiWiki;
9 use Term::ReadLine;
10 use File::Path;
11
12 sub ask ($$) { #{{{
13         my ($question, $default)=@_;
14
15         my $r=Term::ReadLine->new("ikiwiki");
16         $r->readline($question." ", $default);
17 } #}}}
18
19 sub import (@) { #{{{
20         my %setup=@_;
21
22         # Sanitize this to avoid problimatic directory names.
23         $setup{wikiname}=~s/[^-A-Za-z0-9_] //g;
24         if (! length $setup{wikiname}) {
25                 die "you must enter a wikiname\n";
26         }
27
28         # Avoid overwriting any existing files.
29         foreach my $key (qw{srcdir destdir repository setupfile}) {
30                 next unless exists $setup{$key};
31                 my $add="";
32                 while (-e $setup{$key}.$add) {
33                         $add=1 if ! $add;
34                         $add++;
35                 }
36                 $setup{$key}.=$add;
37         }
38
39         print "\n\nSetting up $setup{wikiname} ...\n";
40
41         # Set up the repository.
42         mkpath($setup{srcdir}) || die "mkdir $setup{srcdir}: $!";
43         delete $setup{repository} if ! $setup{rcs} || $setup{rcs}=~/bzr|mercurial/;
44         if ($setup{rcs}) {
45                 my @params=($setup{rcs}, $setup{srcdir});
46                 push @params, $setup{repository} if exists $setup{repository};
47                 if (system("ikiwiki-makerepo", @params) != 0) {
48                         die "failed: ikiwiki-makerepo @params";
49                 }
50         }
51
52         # Generate setup file.
53         my @params=(
54                 "--dumpsetup", $setup{setupfile},
55                 "--wikiname", $setup{wikiname},
56                 "--url", $setup{url},
57                 "--cgiurl", $setup{cgiurl}
58         );
59         if ($setup{rcs}) {
60                 push @params, "--rcs", $setup{rcs};
61                 if ($setup{rcs} eq 'git') {
62                         push @params, "--set", "git_wrapper=".
63                                 $setup{repository}."/hooks/post-update";
64                 }
65                 elsif ($setup{rcs} eq 'svn') {
66                         push @params, "--set", "svn_wrapper=".
67                                 $setup{repository}."/hooks/post-commit";
68                 }
69                 elsif ($setup{rcs} eq 'bzr') {
70                         # TODO
71                 }
72                 elsif ($setup{rcs} eq 'mercurial') {
73                         # TODO
74                 }
75         }
76         if (exists $setup{add_plugins}) {
77                 foreach my $plugin (@{$setup{add_plugins}}) {
78                         push @params, "--plugin", $plugin;
79                 }
80         }
81         if (exists $setup{disable_plugins}) {
82                 foreach my $plugin (@{$setup{disable_plugins}}) {
83                         push @params, "--disable-plugin", $plugin;
84                 }
85         }
86         foreach my $key (keys %setup) {
87                 next if $key =~ /^(disable_plugins|add_plugins|setupfile|wikiname|url|cgiurl||srcdir|destdir|repository)$/;
88                 push @params, "--set", "$key=$setup{$key}";
89         }
90         if (system("ikiwiki", @params, $setup{srcdir}, $setup{destdir}) != 0) {
91                 die "failed: ikiwiki @params";
92         }
93
94         # Build the wiki.
95         mkpath($setup{destdir}) || die "mkdir $setup{destdir}: $!";
96         if (system("ikiwiki", "--setup", $setup{setupfile}) != 0) {
97                 die "ikiwiki --setup $setup{setupfile} failed";
98         }
99
100         # Done!
101         print "\n\nSuccessfully set up $setup{wikiname}:\n";
102         foreach my $key (qw{url srcdir destdir repository setupfile}) {
103                 next unless exists $setup{$key};
104                 my $value=$setup{$key};
105                 $value=~s/^\Q$ENV{HOME}\E\//~\//;
106                 print "\t$key: ".(" " x (10 - length($key)))." $value\n";
107         }
108         exit 0;
109 } #}}}
110
111 1