/
Install.PL
executable file
·178 lines (142 loc) · 4.19 KB
/
Install.PL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use FindBin qw($Bin);
use lib "$Bin/lib", "$Bin/local/lib";
my $neaf_home = 'https://github.com/dallaylaen/perl-mvc-neaf.git';
my $check;
my $write;
GetOptions(
"install" => \$write,
"check" => \$check,
"help" => \&usage,
"neaf=s" => \$neaf_home,
) or die "Bad usage, see $0 --help";
sub usage {
print <<"USAGE"; exit 0;
Usage:
$0 [options] --check # check dependencies & prerequisites
$0 [options] --install # also create files & directories
The following checks are performed:
* check CPAN dependencies
* check/create ./local
* (w) install MVC::Neaf into local/, if absent
* run tests from t/
* (w) create local/potracheno.cfg and local/potracheno.sqlite, if absent
* check that DB in potracheno.cfg is suitable for running
Only local directory is affected.
USAGE
};
if ($check || $write) {
setup($Bin, $write);
} else {
usage();
};
sub setup {
my ($root, $write) = @_;
check_deps();
mkdir "$root/local" if $write;
die "Cannot access local directory at $root/local"
unless -d "$root/local";
if ($write) {
install_neaf( $root, "$root/local/perl-mvc-neaf", $neaf_home );
} else {
# Don't install, just check
require MVC::Neaf;
};
run_tests( $root );
my $conf = "$root/local/potracheno.cfg";
if( !-f $conf ) {
die "Config not found at $conf"
if !$write;
create_config( $conf );
my $sqlite = "$root/local/potracheno.sqlite";
create_sqlite( $sqlite )
unless -f $sqlite;
};
check_db( $root, $conf );
print "Ready to go, now run:\n";
print " plackup bin/potracheno.psgi\n";
};
sub check_deps {
my @modlist = qw(
Carp CGI
Data::Dumper DBI DBD::SQLite Digest::MD5
Encode Errno
File::Basename File::Find File::Temp FindBin
Getopt::Long
HTTP::Headers
JSON::XS
LWP::UserAgent
LWP::Protocol::https
Plack::Request POSIX
Scalar::Util Sys::Hostname
Template Test::More Text::Markdown Time::HiRes
URI::Escape
overload parent
);
my @missing = grep { !eval "require $_;" } @modlist; ## no critic
if (@missing) {
die "Required modules missing, please install them: @missing";
};
};
sub create_config {
my $conf = shift;
open my $fd, ">", $conf
or die "Failed to create conf $conf: $!";
print $fd <<'CONF' or die "Failed to write config $conf: $!";
# default config
[db]
handle = "dbi:SQLite:$(ROOT)/potracheno.sqlite"
# handle = "dbi:mysql:database=potracheno;host=localhost"
# user = 'my_user'
# pass = 'my_pass'
[status]
1 = Open
70 = "Solution underway"
100 = Closed
[security]
member_only = 0
[update]
interval = 86400
CONF
close $fd or die "Failed to sync config $conf: $!";
};
sub check_db {
my ($root, $conf) = @_;
system perl => "$root/bin/potracheno-dbtest.pl" => $conf;
$? and die "DB check failed, adjust config or set up db";
};
sub install_neaf {
my ($root, $neaf_local, $neaf_home) = @_;
# already there - nothing to do
return if eval { require MVC::Neaf; };
# MVC::Neaf not installed, try to get from github
system git => clone => $neaf_home => $neaf_local
unless -d $neaf_local;
$? and die "Failed to clone MVC::Neaf to $neaf_local";
system prove => "-I$neaf_local/lib" => -r => "$neaf_local/t";
$? and die "Failed to build & test MVC::Neaf in $neaf_local";
mkdir "$root/local/lib";
system cp => -r => "$neaf_local/lib/MVC" => "$root/local/lib/";
$? and die "Failed to make local copy of Neaf libs";
require MVC::Neaf; # or die
print "check_heaf() done\n";
};
sub run_tests {
my $root = shift;
system prove => "-I$root/lib" => "-I$root/local/lib" => -r => "$root/t";
$? and die "Tests fail";
};
sub create_sqlite {
my ( $sqlite ) = @_;
require DBI;
my $dbh = DBI->connect("dbi:SQLite:dbname=$sqlite"
, '', '', { RaiseError => 1} );
require App::Its::Potracheno::Model;
my $sql = App::Its::Potracheno::Model->get_schema_sqlite;
foreach (split /;/, $sql) {
$dbh->do($_);
};
};