PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/Module/Install/RTx.pm

https://gitlab.com/rajul/RT-Extension-OurVoteLive
Perl | 261 lines | 197 code | 48 blank | 16 comment | 33 complexity | 19cb7536c79f3d15efdff11d3b98ac4b MD5 | raw file
  1. #line 1
  2. package Module::Install::RTx;
  3. use 5.008;
  4. use strict;
  5. use warnings;
  6. no warnings 'once';
  7. use Module::Install::Base;
  8. use base 'Module::Install::Base';
  9. our $VERSION = '0.37';
  10. use FindBin;
  11. use File::Glob ();
  12. use File::Basename ();
  13. my @DIRS = qw(etc lib html static bin sbin po var);
  14. my @INDEX_DIRS = qw(lib bin sbin);
  15. sub RTx {
  16. my ( $self, $name, $extra_args ) = @_;
  17. $extra_args ||= {};
  18. # Set up names
  19. my $fname = $name;
  20. $fname =~ s!-!/!g;
  21. $self->name( $name )
  22. unless $self->name;
  23. $self->all_from( "lib/$fname.pm" )
  24. unless $self->version;
  25. $self->abstract("$name Extension")
  26. unless $self->abstract;
  27. unless ( $extra_args->{no_readme_generation} ) {
  28. $self->readme_from( "lib/$fname.pm",
  29. { options => [ quotes => "none" ] } );
  30. }
  31. $self->add_metadata("x_module_install_rtx_version", $VERSION );
  32. # Try to find RT.pm
  33. my @prefixes = qw( /opt /usr/local /home /usr /sw /usr/share/request-tracker4);
  34. $ENV{RTHOME} =~ s{/RT\.pm$}{} if defined $ENV{RTHOME};
  35. $ENV{RTHOME} =~ s{/lib/?$}{} if defined $ENV{RTHOME};
  36. my @try = $ENV{RTHOME} ? ($ENV{RTHOME}, "$ENV{RTHOME}/lib") : ();
  37. while (1) {
  38. my @look = @INC;
  39. unshift @look, grep {defined and -d $_} @try;
  40. push @look, grep {defined and -d $_}
  41. map { ( "$_/rt4/lib", "$_/lib/rt4", "$_/lib" ) } @prefixes;
  42. last if eval {local @INC = @look; require RT; $RT::LocalLibPath};
  43. warn
  44. "Cannot find the location of RT.pm that defines \$RT::LocalPath in: @look\n";
  45. my $given = $self->prompt("Path to directory containing your RT.pm:") or exit;
  46. $given =~ s{/RT\.pm$}{};
  47. $given =~ s{/lib/?$}{};
  48. @try = ($given, "$given/lib");
  49. }
  50. print "Using RT configuration from $INC{'RT.pm'}:\n";
  51. my $local_lib_path = $RT::LocalLibPath;
  52. unshift @INC, $local_lib_path;
  53. my $lib_path = File::Basename::dirname( $INC{'RT.pm'} );
  54. unshift @INC, $lib_path;
  55. # Set a baseline minimum version
  56. unless ( $extra_args->{deprecated_rt} ) {
  57. $self->requires_rt('4.0.0');
  58. }
  59. # Installation locations
  60. my %path;
  61. $path{$_} = $RT::LocalPluginPath . "/$name/$_"
  62. foreach @DIRS;
  63. # Copy RT 4.2.0 static files into NoAuth; insufficient for
  64. # images, but good enough for css and js.
  65. $path{static} = "$path{html}/NoAuth/"
  66. unless $RT::StaticPath;
  67. # Delete the ones we don't need
  68. delete $path{$_} for grep {not -d "$FindBin::Bin/$_"} keys %path;
  69. my %index = map { $_ => 1 } @INDEX_DIRS;
  70. $self->no_index( directory => $_ ) foreach grep !$index{$_}, @DIRS;
  71. my $args = join ', ', map "q($_)", map { ($_, $path{$_}) }
  72. sort keys %path;
  73. printf "%-10s => %s\n", $_, $path{$_} for sort keys %path;
  74. if ( my @dirs = map { ( -D => $_ ) } grep $path{$_}, qw(bin html sbin etc) ) {
  75. my @po = map { ( -o => $_ ) }
  76. grep -f,
  77. File::Glob::bsd_glob("po/*.po");
  78. $self->postamble(<< ".") if @po;
  79. lexicons ::
  80. \t\$(NOECHO) \$(PERL) -MLocale::Maketext::Extract::Run=xgettext -e \"xgettext(qw(@dirs @po))\"
  81. .
  82. }
  83. $self->include('Module::Install::RTx::Runtime') if $self->admin;
  84. $self->include_deps( 'YAML::Tiny', 0 ) if $self->admin;
  85. my $postamble = << ".";
  86. install ::
  87. \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Iinc -MModule::Install::RTx::Runtime -e"RTxPlugin()"
  88. \t\$(NOECHO) \$(PERL) -MExtUtils::Install -e \"install({$args})\"
  89. .
  90. if ( $path{var} and -d $RT::MasonDataDir ) {
  91. my ( $uid, $gid ) = ( stat($RT::MasonDataDir) )[ 4, 5 ];
  92. $postamble .= << ".";
  93. \t\$(NOECHO) chown -R $uid:$gid $path{var}
  94. .
  95. }
  96. my %has_etc;
  97. if ( File::Glob::bsd_glob("$FindBin::Bin/etc/schema.*") ) {
  98. $has_etc{schema}++;
  99. }
  100. if ( File::Glob::bsd_glob("$FindBin::Bin/etc/acl.*") ) {
  101. $has_etc{acl}++;
  102. }
  103. if ( -e 'etc/initialdata' ) { $has_etc{initialdata}++; }
  104. if ( grep { /\d+\.\d+\.\d+.*$/ } glob('etc/upgrade/*.*.*') ) {
  105. $has_etc{upgrade}++;
  106. }
  107. $self->postamble("$postamble\n");
  108. if ( $path{lib} ) {
  109. $self->makemaker_args( INSTALLSITELIB => $path{'lib'} );
  110. $self->makemaker_args( INSTALLARCHLIB => $path{'lib'} );
  111. } else {
  112. $self->makemaker_args( PM => { "" => "" }, );
  113. }
  114. $self->makemaker_args( INSTALLSITEMAN1DIR => "$RT::LocalPath/man/man1" );
  115. $self->makemaker_args( INSTALLSITEMAN3DIR => "$RT::LocalPath/man/man3" );
  116. $self->makemaker_args( INSTALLSITEARCH => "$RT::LocalPath/man" );
  117. if (%has_etc) {
  118. print "For first-time installation, type 'make initdb'.\n";
  119. my $initdb = '';
  120. $initdb .= <<"." if $has_etc{schema};
  121. \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Iinc -MModule::Install::RTx::Runtime -e"RTxDatabase(qw(schema \$(NAME) \$(VERSION)))"
  122. .
  123. $initdb .= <<"." if $has_etc{acl};
  124. \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Iinc -MModule::Install::RTx::Runtime -e"RTxDatabase(qw(acl \$(NAME) \$(VERSION)))"
  125. .
  126. $initdb .= <<"." if $has_etc{initialdata};
  127. \t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Iinc -MModule::Install::RTx::Runtime -e"RTxDatabase(qw(insert \$(NAME) \$(VERSION)))"
  128. .
  129. $self->postamble("initdb ::\n$initdb\n");
  130. $self->postamble("initialize-database ::\n$initdb\n");
  131. if ($has_etc{upgrade}) {
  132. print "To upgrade from a previous version of this extension, use 'make upgrade-database'\n";
  133. my $upgradedb = qq|\t\$(NOECHO) \$(PERL) -Ilib -I"$local_lib_path" -I"$lib_path" -Iinc -MModule::Install::RTx::Runtime -e"RTxDatabase(qw(upgrade \$(NAME) \$(VERSION)))"\n|;
  134. $self->postamble("upgrade-database ::\n$upgradedb\n");
  135. $self->postamble("upgradedb ::\n$upgradedb\n");
  136. }
  137. }
  138. }
  139. sub requires_rt {
  140. my ($self,$version) = @_;
  141. _load_rt_handle();
  142. if ($self->is_admin) {
  143. $self->add_metadata("x_requires_rt", $version);
  144. my @sorted = sort RT::Handle::cmp_version $version,'4.0.0';
  145. $self->perl_version('5.008003') if $sorted[0] eq '4.0.0'
  146. and (not $self->perl_version or '5.008003' > $self->perl_version);
  147. @sorted = sort RT::Handle::cmp_version $version,'4.2.0';
  148. $self->perl_version('5.010001') if $sorted[0] eq '4.2.0'
  149. and (not $self->perl_version or '5.010001' > $self->perl_version);
  150. }
  151. # if we're exactly the same version as what we want, silently return
  152. return if ($version eq $RT::VERSION);
  153. my @sorted = sort RT::Handle::cmp_version $version,$RT::VERSION;
  154. if ($sorted[-1] eq $version) {
  155. die <<"EOT";
  156. **** Error: This extension requires RT $version. Your installed version
  157. of RT ($RT::VERSION) is too old.
  158. EOT
  159. }
  160. }
  161. sub requires_rt_plugin {
  162. my $self = shift;
  163. my ( $plugin ) = @_;
  164. if ($self->is_admin) {
  165. my $plugins = $self->Meta->{values}{"x_requires_rt_plugins"} || [];
  166. push @{$plugins}, $plugin;
  167. $self->add_metadata("x_requires_rt_plugins", $plugins);
  168. }
  169. my $path = $plugin;
  170. $path =~ s{\:\:}{-}g;
  171. $path = "$RT::LocalPluginPath/$path/lib";
  172. if ( -e $path ) {
  173. unshift @INC, $path;
  174. } else {
  175. my $name = $self->name;
  176. warn <<"EOT";
  177. **** Warning: $name requires that the $plugin plugin be installed and
  178. enabled; it does not appear to be installed.
  179. EOT
  180. }
  181. $self->requires(@_);
  182. }
  183. sub rt_too_new {
  184. my ($self,$version,$msg) = @_;
  185. my $name = $self->name;
  186. $msg ||= <<EOT;
  187. **** Error: Your installed version of RT (%s) is too new; this extension
  188. only works with versions older than %s.
  189. EOT
  190. $self->add_metadata("x_rt_too_new", $version) if $self->is_admin;
  191. _load_rt_handle();
  192. my @sorted = sort RT::Handle::cmp_version $version,$RT::VERSION;
  193. if ($sorted[0] eq $version) {
  194. die sprintf($msg,$RT::VERSION,$version);
  195. }
  196. }
  197. # RT::Handle runs FinalizeDatabaseType which calls RT->Config->Get
  198. # On 3.8, this dies. On 4.0/4.2 ->Config transparently runs LoadConfig.
  199. # LoadConfig requires being able to read RT_SiteConfig.pm (root) so we'd
  200. # like to avoid pushing that on users.
  201. # Fake up just enough Config to let FinalizeDatabaseType finish, and
  202. # anyone later calling LoadConfig will overwrite our shenanigans.
  203. sub _load_rt_handle {
  204. unless ($RT::Config) {
  205. require RT::Config;
  206. $RT::Config = RT::Config->new;
  207. RT->Config->Set('DatabaseType','mysql');
  208. }
  209. require RT::Handle;
  210. }
  211. 1;
  212. __END__
  213. #line 390