Merge commit 'c0baaa7'
[urisagit/Stem.git] / BuildStem.pm
1 package BuildStem;
2
3 use strict;
4 use warnings;
5
6 use Config;
7 use File::Basename;
8 use File::Spec;
9 use IO::File;
10
11 use Module::Build;
12
13 use vars qw(@ISA);
14 @ISA = qw(Module::Build);
15
16
17 sub process_script_files {
18         my ( $self ) = @_ ;
19         my $files = $self->find_script_files();
20         return unless keys %$files;
21
22         my $script_dir = File::Spec->catdir($self->blib, 'script');
23         my $demo_dir   = File::Spec->catdir($self->blib, 'demo');
24         File::Path::mkpath( $script_dir );
25         File::Path::mkpath( $demo_dir );
26     $self->add_to_cleanup($demo_dir);
27
28         foreach my $file (keys %$files) {
29                 my $dest_dir = $file =~ /_demo$/ ? $demo_dir : $script_dir ;
30                 my $result = $self->copy_if_modified($file, $dest_dir, 'flatten') or next;
31                 $self->fix_shebang_line($result) if $self->is_unixish();
32                 $self->make_executable($result);
33         my $demo_run_dir = File::Spec->catdir($self->base_dir(), 'demo');
34                 if ( $result =~ /(?:run_stem$)|(?:_demo$)/ ) {
35                         my $result2 = $self->copy_if_modified($result, $demo_run_dir, 'flatten') or next;
36                         $self->add_to_cleanup($result2);
37                 }
38         }
39         return 1;
40 }
41
42 sub process_conf_files {
43         my ( $self ) = @_ ;
44         my $files = $self->_find_file_by_type('stem','conf');
45         return unless keys %$files;
46
47         my $conf_dir = File::Spec->catdir($self->blib, 'conf');
48         File::Path::mkpath( $conf_dir );
49
50         foreach my $file (keys %$files) {
51                 my $result = $self->copy_if_modified($file, $conf_dir, 'flatten') or next;
52                 $self->fix_shebang_line($result) if $self->is_unixish();
53         }
54         return 1;
55 }
56
57 sub find_binary {
58         my ( $self, $prog ) = @_ ;
59         if ( $self->do_system( "which $prog >/dev/null" ) ) {
60                 return `which $prog` ;
61         }
62         return;
63 }
64
65
66 ###########################################################
67 # Various convenience routines.
68 #
69 # To use ACTION_foo, call ./Build foo
70
71
72
73 # ACTION: grep through MANIFEST
74 # command line args:
75 #   files=<regex>
76 #
77 # do we need this action?
78
79
80 sub ACTION_grep_manifest {
81
82     my( $self ) = @_ ;
83
84     my @manifest_sublist = $self->grep_manifest() ;
85
86     print join( "\n", @manifest_sublist ), "\n" ;
87     return;
88 }
89
90
91
92 # grep through all matched files
93 # command line args:
94 #   files=<regex> (default is all .pm files)
95 #   re=<regex>
96
97 sub ACTION_grep {
98
99     my( $self ) = @_ ;
100
101     my $args = $self->{'args'} ;
102
103     my $file_regex = $args->{ files } || qr/\.pm$/ ;
104     my $grep_regex = $args->{ re } or die "need grep regex" ;
105
106     my @manifest_sublist = $self->grep_manifest( $file_regex ) ;
107
108     local( @ARGV ) = @manifest_sublist ;
109
110     while( <> ) {
111
112         next unless /$grep_regex/ ;
113
114         print "$ARGV:$. $_"
115     }
116     continue {
117
118         close ARGV if eof ;
119     }
120
121     return;
122 }
123
124 my ( @manifest_lines ) ;
125
126 # MANIFEST helper subs
127
128 sub grep_manifest {
129
130     my( $self, $file_regex ) = @_ ;
131
132     $file_regex ||= $self->{ args }{ files } || qr/.*/ ;
133
134     manifest_load() ;
135
136     return grep( /$file_regex/, @manifest_lines ) ;
137 }
138
139 sub manifest_load {
140
141     return if @manifest_lines ;
142
143     @manifest_lines = grep ! /^\s*$|^\s*#/, read_file( 'MANIFEST' ) ;
144
145     chomp @manifest_lines ;
146
147     return ;
148 }
149
150 sub read_file {
151
152     my ( $file_name ) = @_ ;
153
154     local( *FH );
155
156     open( FH, $file_name ) || die "Can't open $file_name $!";
157
158     return <FH> if wantarray;
159
160     read FH, my $buf, -s FH;
161     return $buf;
162 }
163
164
165 1;