RTx.pm 6.66 KB
Newer Older
Kevin Falcone's avatar
Kevin Falcone committed
1
2
3
#line 1
package Module::Install::RTx;

Kevin Falcone's avatar
Kevin Falcone committed
4
use 5.008;
Kevin Falcone's avatar
Kevin Falcone committed
5
use strict;
Kevin Falcone's avatar
Kevin Falcone committed
6
7
8
9
10
use warnings;
no warnings 'once';

use Module::Install::Base;
use base 'Module::Install::Base';
Thomas Sibley's avatar
Thomas Sibley committed
11
our $VERSION = '0.29_02';
Kevin Falcone's avatar
Kevin Falcone committed
12

Kevin Falcone's avatar
Kevin Falcone committed
13
use FindBin;
Kevin Falcone's avatar
Kevin Falcone committed
14
use File::Glob     ();
Kevin Falcone's avatar
Kevin Falcone committed
15
16
use File::Basename ();

Kevin Falcone's avatar
Kevin Falcone committed
17
18
19
my @DIRS = qw(etc lib html bin sbin po var);
my @INDEX_DIRS = qw(lib bin sbin);

Kevin Falcone's avatar
Kevin Falcone committed
20
sub RTx {
Kevin Falcone's avatar
Kevin Falcone committed
21
22
23
    my ( $self, $name ) = @_;

    my $original_name = $name;
Kevin Falcone's avatar
Kevin Falcone committed
24
25
26
27
28
29
30
    my $RTx = 'RTx';
    $RTx = $1 if $name =~ s/^(\w+)-//;
    my $fname = $name;
    $fname =~ s!-!/!g;

    $self->name("$RTx-$name")
        unless $self->name;
Kevin Falcone's avatar
Kevin Falcone committed
31
32
    $self->all_from( -e "$name.pm" ? "$name.pm" : "lib/$RTx/$fname.pm" )
        unless $self->version;
Kevin Falcone's avatar
Kevin Falcone committed
33
34
35
36
    $self->abstract("RT $name Extension")
        unless $self->abstract;

    my @prefixes = (qw(/opt /usr/local /home /usr /sw ));
Kevin Falcone's avatar
Kevin Falcone committed
37
38
    my $prefix   = $ENV{PREFIX};
    @ARGV = grep { /PREFIX=(.*)/ ? ( ( $prefix = $1 ), 0 ) : 1 } @ARGV;
Kevin Falcone's avatar
Kevin Falcone committed
39
40
41
42

    if ($prefix) {
        $RT::LocalPath = $prefix;
        $INC{'RT.pm'} = "$RT::LocalPath/lib/RT.pm";
Kevin Falcone's avatar
Kevin Falcone committed
43
    } else {
Kevin Falcone's avatar
Kevin Falcone committed
44
        local @INC = (
Kevin Falcone's avatar
Kevin Falcone committed
45
            $ENV{RTHOME} ? ( $ENV{RTHOME}, "$ENV{RTHOME}/lib" ) : (),
Kevin Falcone's avatar
Kevin Falcone committed
46
            @INC,
Kevin Falcone's avatar
Kevin Falcone committed
47
48
            map { ( "$_/rt4/lib", "$_/lib/rt4", "$_/rt3/lib", "$_/lib/rt3", "$_/lib" )
                } grep $_, @prefixes
Kevin Falcone's avatar
Kevin Falcone committed
49
50
        );
        until ( eval { require RT; $RT::LocalPath } ) {
Kevin Falcone's avatar
Kevin Falcone committed
51
52
            warn
                "Cannot find the location of RT.pm that defines \$RT::LocalPath in: @INC\n";
Kevin Falcone's avatar
Kevin Falcone committed
53
54
            $_ = $self->prompt("Path to directory containing your RT.pm:") or exit;
            $_ =~ s/\/RT\.pm$//;
Kevin Falcone's avatar
Kevin Falcone committed
55
            push @INC, $_, "$_/rt3/lib", "$_/lib/rt3", "$_/lib";
Kevin Falcone's avatar
Kevin Falcone committed
56
57
58
        }
    }

Kevin Falcone's avatar
Kevin Falcone committed
59
    my $lib_path = File::Basename::dirname( $INC{'RT.pm'} );
Kevin Falcone's avatar
Kevin Falcone committed
60
    my $local_lib_path = "$RT::LocalPath/lib";
Kevin Falcone's avatar
Kevin Falcone committed
61
    print "Using RT configuration from $INC{'RT.pm'}:\n";
Kevin Falcone's avatar
Kevin Falcone committed
62
    unshift @INC, "$RT::LocalPath/lib" if $RT::LocalPath;
Kevin Falcone's avatar
Kevin Falcone committed
63
    unshift @INC, $lib_path;
Kevin Falcone's avatar
Kevin Falcone committed
64

Kevin Falcone's avatar
Kevin Falcone committed
65
66
67
    $RT::LocalVarPath  ||= $RT::VarPath;
    $RT::LocalPoPath   ||= $RT::LocalLexiconPath;
    $RT::LocalHtmlPath ||= $RT::MasonComponentRoot;
Kevin Falcone's avatar
Kevin Falcone committed
68
    $RT::LocalLibPath  ||= "$RT::LocalPath/lib";
Kevin Falcone's avatar
Kevin Falcone committed
69
70

    my $with_subdirs = $ENV{WITH_SUBDIRS};
Kevin Falcone's avatar
Kevin Falcone committed
71
72
    @ARGV = grep { /WITH_SUBDIRS=(.*)/ ? ( ( $with_subdirs = $1 ), 0 ) : 1 }
        @ARGV;
Kevin Falcone's avatar
Kevin Falcone committed
73

Kevin Falcone's avatar
Kevin Falcone committed
74
75
76
77
78
    my %subdirs;
    %subdirs = map { $_ => 1 } split( /\s*,\s*/, $with_subdirs )
        if defined $with_subdirs;
    unless ( keys %subdirs ) {
        $subdirs{$_} = 1 foreach grep -d "$FindBin::Bin/$_", @DIRS;
Kevin Falcone's avatar
Kevin Falcone committed
79
80
    }

Kevin Falcone's avatar
Kevin Falcone committed
81
82
83
    # If we're running on RT 3.8 with plugin support, we really wany
    # to install libs, mason templates and po files into plugin specific
    # directories
Kevin Falcone's avatar
Kevin Falcone committed
84
85
86
87
88
89
90
91
92
93
94
    my %path;
    if ( $RT::LocalPluginPath ) {
        die "Because of bugs in RT 3.8.0 this extension can not be installed.\n"
            ."Upgrade to RT 3.8.1 or newer.\n" if $RT::VERSION =~ /^3\.8\.0/;
        $path{$_} = $RT::LocalPluginPath . "/$original_name/$_"
            foreach @DIRS;
    } else {
        foreach ( @DIRS ) {
            no strict 'refs';
            my $varname = "RT::Local" . ucfirst($_) . "Path";
            $path{$_} = ${$varname} || "$RT::LocalPath/$_";
Kevin Falcone's avatar
Kevin Falcone committed
95
        }
Kevin Falcone's avatar
Kevin Falcone committed
96
97

        $path{$_} .= "/$name" for grep $path{$_}, qw(etc po var);
Kevin Falcone's avatar
Kevin Falcone committed
98
99
    }

Kevin Falcone's avatar
Kevin Falcone committed
100
101
    my %index = map { $_ => 1 } @INDEX_DIRS;
    $self->no_index( directory => $_ ) foreach grep !$index{$_}, @DIRS;
Kevin Falcone's avatar
Kevin Falcone committed
102

Kevin Falcone's avatar
Kevin Falcone committed
103
104
105
106
107
108
109
110
    my $args = join ', ', map "q($_)", map { ($_, $path{$_}) }
        grep $subdirs{$_}, keys %path;

    print "./$_\t=> $path{$_}\n" for sort keys %subdirs;

    if ( my @dirs = map { ( -D => $_ ) } grep $subdirs{$_}, qw(bin html sbin) ) {
        my @po = map { ( -o => $_ ) }
            grep -f,
Kevin Falcone's avatar
Kevin Falcone committed
111
            File::Glob::bsd_glob("po/*.po");
Kevin Falcone's avatar
Kevin Falcone committed
112
113
114
115
116
117
118
119
120
121
122
        $self->postamble(<< ".") if @po;
lexicons ::
\t\$(NOECHO) \$(PERL) -MLocale::Maketext::Extract::Run=xgettext -e \"xgettext(qw(@dirs @po))\"
.
    }

    my $postamble = << ".";
install ::
\t\$(NOECHO) \$(PERL) -MExtUtils::Install -e \"install({$args})\"
.

Kevin Falcone's avatar
Kevin Falcone committed
123
    if ( $subdirs{var} and -d $RT::MasonDataDir ) {
Kevin Falcone's avatar
Kevin Falcone committed
124
        my ( $uid, $gid ) = ( stat($RT::MasonDataDir) )[ 4, 5 ];
Kevin Falcone's avatar
Kevin Falcone committed
125
126
127
128
129
130
        $postamble .= << ".";
\t\$(NOECHO) chown -R $uid:$gid $path{var}
.
    }

    my %has_etc;
Kevin Falcone's avatar
Kevin Falcone committed
131
    if ( File::Glob::bsd_glob("$FindBin::Bin/etc/schema.*") ) {
Kevin Falcone's avatar
Kevin Falcone committed
132
133
        $has_etc{schema}++;
    }
Kevin Falcone's avatar
Kevin Falcone committed
134
    if ( File::Glob::bsd_glob("$FindBin::Bin/etc/acl.*") ) {
Kevin Falcone's avatar
Kevin Falcone committed
135
136
        $has_etc{acl}++;
    }
Kevin Falcone's avatar
Kevin Falcone committed
137
    if ( -e 'etc/initialdata' ) { $has_etc{initialdata}++; }
Kevin Falcone's avatar
Kevin Falcone committed
138
139

    $self->postamble("$postamble\n");
Kevin Falcone's avatar
Kevin Falcone committed
140
    unless ( $subdirs{'lib'} ) {
Kevin Falcone's avatar
Kevin Falcone committed
141
142
        $self->makemaker_args( PM => { "" => "" }, );
    } else {
Kevin Falcone's avatar
Kevin Falcone committed
143
144
        $self->makemaker_args( INSTALLSITELIB => $path{'lib'} );
        $self->makemaker_args( INSTALLARCHLIB => $path{'lib'} );
Kevin Falcone's avatar
Kevin Falcone committed
145
146
    }

Kevin Falcone's avatar
Kevin Falcone committed
147
148
149
150
    $self->makemaker_args( INSTALLSITEMAN1DIR => "$RT::LocalPath/man/man1" );
    $self->makemaker_args( INSTALLSITEMAN3DIR => "$RT::LocalPath/man/man3" );
    $self->makemaker_args( INSTALLSITEARCH => "$RT::LocalPath/man" );

Kevin Falcone's avatar
Kevin Falcone committed
151
152
153
154
155
    if (%has_etc) {
        $self->load('RTxInitDB');
        print "For first-time installation, type 'make initdb'.\n";
        my $initdb = '';
        $initdb .= <<"." if $has_etc{schema};
Thomas Sibley's avatar
Thomas Sibley committed
156
\t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(schema \$(NAME) \$(VERSION)))"
Kevin Falcone's avatar
Kevin Falcone committed
157
158
.
        $initdb .= <<"." if $has_etc{acl};
Thomas Sibley's avatar
Thomas Sibley committed
159
\t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(acl \$(NAME) \$(VERSION)))"
Kevin Falcone's avatar
Kevin Falcone committed
160
161
.
        $initdb .= <<"." if $has_etc{initialdata};
Thomas Sibley's avatar
Thomas Sibley committed
162
\t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Minc::Module::Install -e"RTxInitDB(qw(insert \$(NAME) \$(VERSION)))"
Kevin Falcone's avatar
Kevin Falcone committed
163
164
165
166
167
168
.
        $self->postamble("initdb ::\n$initdb\n");
        $self->postamble("initialize-database ::\n$initdb\n");
    }
}

Kevin Falcone's avatar
Kevin Falcone committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# stolen from RT::Handle so we work on 3.6 (cmp_versions came in with 3.8)
{ my %word = (
    a     => -4,
    alpha => -4,
    b     => -3,
    beta  => -3,
    pre   => -2,
    rc    => -1,
    head  => 9999,
);
sub cmp_version($$) {
    my ($a, $b) = (@_);
    my @a = grep defined, map { /^[0-9]+$/? $_ : /^[a-zA-Z]+$/? $word{$_}|| -10 : undef }
        split /([^0-9]+)/, $a;
    my @b = grep defined, map { /^[0-9]+$/? $_ : /^[a-zA-Z]+$/? $word{$_}|| -10 : undef }
        split /([^0-9]+)/, $b;
    @a > @b
        ? push @b, (0) x (@a-@b)
        : push @a, (0) x (@b-@a);
    for ( my $i = 0; $i < @a; $i++ ) {
        return $a[$i] <=> $b[$i] if $a[$i] <=> $b[$i];
    }
    return 0;
}}
sub requires_rt {
    my ($self,$version) = @_;

    # if we're exactly the same version as what we want, silently return
    return if ($version eq $RT::VERSION);

    my @sorted = sort cmp_version $version,$RT::VERSION;

    if ($sorted[-1] eq $version) {
        # should we die?
        warn "\nWarning: prerequisite RT $version not found. Your installed version of RT ($RT::VERSION) is too old.\n\n";
    }
}

Kevin Falcone's avatar
Kevin Falcone committed
207
208
209
210
1;

__END__

Thomas Sibley's avatar
Thomas Sibley committed
211
#line 328