Refactor prompts into seperate subroutines.
[sepia.git] / Makefile.PL
blobacbe0d369d05d4c10c333178fb982152e07987e3
1 use warnings;
2 use strict;
4 use ExtUtils::MakeMaker qw(WriteMakefile prompt);
5 use 5.006;                      # for "no warnings" -- sorry!
6 # See lib/ExtUtils/MakeMaker.pm for details of how to influence
7 # the contents of the Makefile that is written.
9 my %prereq = (
10     'Data::Dumper'    => 0,
11     'Scalar::Util'    => 0,
14 ## Poor man's optional deps.
15 sub test_for
17     my $mod = shift;
18     eval "require $mod";
19     if ($@) {
20         my $resp = prompt("@_.  Install $mod (y/n)? ", 'n');
21         $prereq{$mod} = 0 if $resp =~ /^y/i;
22     }
25 test_for 'Devel::Peek', 'Printing internals requires Devel::Peek';
26 test_for 'Devel::Size', 'Printing variable sizes requires Devel::Size';
27 test_for 'IO::Scalar', 'Printing internals requires IO::Scalar because Devel::Peek sucks.';
28 test_for 'Lexical::Persistence', 'Strict mode requires Lexical::Persistence';
29 test_for 'LWP::Simple', 'CPAN documentation browsing requires LWP::Simple';
30 test_for 'Module::CoreList', 'sepia-core-version requires Module::CoreList';
31 test_for 'Module::Info', 'Required for some Emacs functions';
32 test_for 'PadWalker', 'Stack/lexical inspection requires PadWalker >= 1.0';
33 test_for 'BSD::Resource', 'Detailed command timing.';
34 test_for 'Time::HiRes', 'Basic command timing.';
35 # test_for 'Pod::Webserver', 'Pod::Webserver creates nice documentation.';
36 # test_for 'Scope::Upper', 'Required for return-from-context';
38 my $ELISP_FIND_SITELISP = <<'END_ELISP';
39 (dolist (x load-path) (princ x) (terpri))
40 END_ELISP
42 my $ELISP_FIND_INFODIR = <<'END_ELISP';
43 (progn
44  (require (quote info))
45  (princ (car Info-default-directory-list)))
46 END_ELISP
48 my ($EMACS, $SITE_LISP, $INSTALL_INFO, $INFO_DIR);
50 sub escape_path
52     my ($path) = @_;
53     $path =~ s/([$?* ])/\\$1/g; # escape special characters
54     $path =~ s{/+$}{};          # remove trailing forward-slashes
55     return $path;
58 sub prompt_for_emacs
60     # try to compile and install Elisp files, but don't die if we can't.
61     my ($sysemacs) = grep { defined && -x }
62         $ENV{EMACS}, glob '/usr/bin{/local,}/emacs';
63     $sysemacs ||= q{emacs};
65     my $emacs = prompt("Where is your emacs? ", $sysemacs);
67     # Make sure emacs is a valid string/program path...
68     return undef unless $emacs;
70     # Make sure -x is passed an absolute path...
71     unless ( $emacs =~ m{^/} && -x $emacs ) { 
72         warn "$emacs is not executable! Undefined.\n";
73         return undef;
74     }
76     return escape_path( $emacs );
79 # Runs some elisp code with emacs via the command line.
80 # We must set $EMACS before running this... if it is an untrue value
81 # then we return an empty list or string, depending on what caller wants.
82 sub run_elisp
84     my ($elisp) = @_;
86     return wantarray ? qw// : q{} unless $EMACS;
87     return `$EMACS --batch --eval '$elisp' 2>/dev/null`;
90 sub prompt_for_sitelisp
92     my @lp = run_elisp( $ELISP_FIND_SITELISP );
93     chomp @lp;
95     my $site_lisp;
96     if ( scalar @lp ) {
97         ($site_lisp) = grep { /site-lisp$/ && -d $_ } @lp;
98     }
100     $site_lisp = prompt("Elisp install directory?", $site_lisp);
102     # We don't check if the directory exists, because we can create it...
103     return escape_path( $site_lisp );
106 sub prompt_for_infodir
108     my $info_dir = run_elisp( $ELISP_FIND_INFODIR );
109     chomp $info_dir;
111     # pass prompt undef so it will not display brackets if $info_dir is ""
112     $info_dir = prompt("Info directory?", $info_dir || undef);
113     return escape_path( $info_dir );
116 # Append info page and elisp installation to the end of the install
117 # section in the Makefile...
119 sub MY::postamble
121     my $maketxt = <<"END_MAKETXT";
122 install ::
123 END_MAKETXT
125     if ( $INFO_DIR ) { $maketxt .= <<"END_MAKETXT" }
126         install -d -m755 \$(DESTDIR)$INFO_DIR
127         install -m644 sepia.info \$(DESTDIR)$INFO_DIR
128         $INSTALL_INFO \$(DESTDIR)$INFO_DIR/sepia.info \$(DESTDIR)$INFO_DIR/dir
129         
130 END_MAKETXT
132     if ( $SITE_LISP ) {
133         $maketxt .= <<"END_MAKETXT"
134         install -d -m755 \$(DESTDIR)$SITE_LISP
135         install -m644 *.el *.elc \$(DESTDIR)$SITE_LISP
136 END_MAKETXT
137     } else {
138         print <<'END_MSG'
140 ======================================================================
141 To actually use this package, you need to move the Emacs Lisp files
142 somewhere Emacs will find them.
144 You may also want to install the HTML documentation somewhere
145 appropriate to your system.
146 ======================================================================
148 END_MSG
149     }
151     # Create a new target for compiled elisp (.elc) files...
152     # Allow the compilation to fail (it is prefixed with "-")...
153     if ( $EMACS ) {
154         $maketxt .= <<"EOS";
156 \%.elc : \%.el
157         - $EMACS -L '$ENV{PWD}' --batch -f batch-byte-compile \$? 2>/dev/null
159 all :: sepia-snippet.elc sepia.elc sepia-cpan.elc sepia-tree.elc \\
160            sepia-w3m.elc sepia-ido.elc
162     }
164     return $maketxt;
167 $EMACS        ||= prompt_for_emacs()
168     or warn "Disabling elisp compilation and dir detection.\n";
169 $SITE_LISP    ||= prompt_for_sitelisp();
170 $INFO_DIR     ||= prompt_for_infodir();
171 $INSTALL_INFO ||= prompt("install-info program?", 'install-info')
172     if $INFO_DIR;
174 WriteMakefile(
175     NAME                => 'Sepia',
176     VERSION_FROM        => 'lib/Sepia.pm', # finds $VERSION
177     PREREQ_PM           => \%prereq,
178     EXE_FILES           => ['sepl'],
179     AUTHOR              => "Sean O'Rourke <seano\@cpan.org>",
180     ABSTRACT            => 'Simple Emacs-Perl InterAction',
181     clean           => { FILES => '*.elc' },