Initial release
[manu/RT-Extension-ImportCustomFieldValues.git] / inc / Module / Install / RTx / Runtime.pm
1 #line 1
2 package Module::Install::RTx::Runtime;
3
4 use base 'Exporter';
5 our @EXPORT = qw/RTxDatabase RTxPlugin/;
6
7 use strict;
8 use File::Basename ();
9
10 sub _rt_runtime_load {
11     require RT;
12
13     eval { RT::LoadConfig(); };
14     if (my $err = $@) {
15         die $err unless $err =~ /^RT couldn't load RT config file/m;
16         my $warn = <<EOT;
17 This usually means that your current user cannot read the file.  You
18 will likely need to run this installation step as root, or some user
19 with more permissions.
20 EOT
21         $err =~ s/This usually means.*/$warn/s;
22         die $err;
23     }
24 }
25
26 sub RTxDatabase {
27     my ($action, $name, $version) = @_;
28
29     _rt_runtime_load();
30
31     require RT::System;
32     my $has_upgrade = RT::System->can('AddUpgradeHistory');
33
34     my $lib_path = File::Basename::dirname($INC{'RT.pm'});
35     my @args = (
36         "-Ilib",
37         "-I$RT::LocalLibPath",
38         "-I$lib_path",
39         "$RT::SbinPath/rt-setup-database",
40         "--action"      => $action,
41         ($action eq 'upgrade' ? () : ("--datadir"     => "etc")),
42         (($action eq 'insert') ? ("--datafile"    => "etc/initialdata") : ()),
43         "--dba"         => $RT::DatabaseAdmin || $RT::DatabaseUser,
44         "--prompt-for-dba-password" => '',
45         ($has_upgrade ? ("--package" => $name, "--ext-version" => $version) : ()),
46     );
47     # If we're upgrading against an RT which isn't at least 4.2 (has
48     # AddUpgradeHistory) then pass --package.  Upgrades against later RT
49     # releases will pick up --package from AddUpgradeHistory.
50     if ($action eq 'upgrade' and not $has_upgrade) {
51         push @args, "--package" => $name;
52     }
53
54     print "$^X @args\n";
55     (system($^X, @args) == 0) or die "...returned with error: $?\n";
56 }
57
58 sub RTxPlugin {
59     my ($name) = @_;
60
61     _rt_runtime_load();
62     require YAML::Tiny;
63     my $data = YAML::Tiny::LoadFile('META.yml');
64     my $name = $data->{name};
65
66     my @enabled = RT->Config->Get('Plugins');
67     for my $required (@{$data->{x_requires_rt_plugins} || []}) {
68         next if grep {$required eq $_} @enabled;
69
70         warn <<"EOT";
71
72 **** Warning: $name requires that the $required plugin be installed and
73               enabled; it is not currently in \@Plugins.
74
75 EOT
76     }
77 }
78
79 1;