File Coverage

blib/lib/Project/Easy/Helper/Status.pm
Criterion Covered Total %
statement 3 77 3.9
branch 0 34 0.0
condition 0 6 0.0
subroutine 1 7 14.2
pod 0 4 0.0
total 4 128 3.1


line stmt bran cond sub pod time code
1             package Project::Easy::Helper;
2              
3 2     2   10 use Class::Easy;
  2         4  
  2         18  
4              
5             sub run_script {
6 0     0 0   my $script = shift;
7 0           my $path = shift;
8            
9 0           my $out = `$script 2>&1`;
10            
11 0           my $res = $? >> 8;
12 0 0         if ($res == 0) {
    0          
13 0           debug $path, " … OK\n";
14             } elsif ($res == 255) {
15 0           warn $out, $path, " … DIED\n";
16 0           exit;
17             } else {
18 0           warn $out, $path, " … FAILED $res TESTS\n";
19 0           exit;
20             }
21             }
22              
23             # TODO: when status run, new available scripts must be created
24             sub status {
25 0     0 0   my ($project_class, $libs);
26            
27 0           eval {
28 0           ($project_class, $libs) = &_script_wrapper;
29             };
30            
31 0 0         if ($@) {
32 0           &status_fail ($project_class, $libs);
33 0           die $@;
34             }
35            
36 0           return &status_ok ($project_class, $libs);
37            
38             }
39              
40             sub status_fail {
41 0     0 0   my ($project_class, $libs, $params) = @_;
42            
43             # my $root = $project_class->root;
44            
45             # here we must recreate var directories
46             # TODO: make it by Project::Easy::Helper service 'install' routine
47            
48             # my $global_config = $project_class->conf_path->deserialize;
49             }
50              
51             sub status_ok {
52 0     0 0   my ($pack, $libs, $params) = @_;
53            
54 0           my $root = $pack->root;
55            
56 0           my $data_files = file->__data__files;
57            
58 0           create_scripts ($root, $data_files);
59            
60 0           my $lib_dir = $root->append ('lib')->as_dir;
61            
62 0           my $includes = join ' ', map {"-I$_"} (@$libs, @INC);
  0            
63            
64 0           my $files = [];
65 0           my $all_uses = {};
66 0           my $all_packs = {};
67            
68             $lib_dir->scan_tree (sub {
69 0     0     my $file = shift;
70            
71 0 0         return 1 if $file->type eq 'dir';
72            
73 0 0         if ($file =~ /\.pm$/) {
74 0           push @$files, $file;
75 0           my $content = $file->contents;
76              
77 0           while ($content =~ m/^(use|package) ([^\$\;\s]+)/igms){
78 0 0         if ($1 eq 'use') {
79 0           $all_uses->{$2} = $file;
80             } else {
81 0           $all_packs->{$2} = $file;
82             }
83             }
84             }
85 0           });
86            
87 0           foreach (keys %$all_uses) {
88 0 0         delete $all_uses->{$_}
89             if /^[a-z][a-z0-9]+$/;
90             }
91            
92 0           my $failed = {};
93 0           my $external = {};
94            
95             # here we try to find dependencies
96 0           foreach (keys %$all_uses) {
97 0 0         $external->{$_} = $all_uses->{$_}
98             unless exists $all_packs->{$_};
99            
100 0 0 0       $failed->{$_} = $all_uses->{$_}
101             if ! try_to_use ($_) and ! exists $all_packs->{$_};
102             }
103            
104 0           debug "external modules: ", join (' ', sort keys %$external), "\n";
105            
106 0 0         warn "requirements not satisfied. you must install these modules:\ncpan -i ",
107             join (' ', sort keys %$failed), "\n"
108             if scalar keys %$failed;
109            
110 0           foreach my $file (@$files) {
111 0           my $abs_path = $file->abs_path;
112 0           my $rel_path = $file->rel_path ((ref $root)->current->abs_path);
113 0           my $project_path = $file->rel_path ($root->abs_path);
114              
115             # warn "$^X -c $rel_path";
116            
117 0           run_script ("$^X $includes -c $abs_path", $rel_path);
118            
119             }
120              
121             # TODO: move db check code to unit
122            
123 0           my $db_outdated = 0;
124            
125 0           foreach my $datasource (keys %{$pack->config->{db}}) {
  0            
126 0           my $ver;
127 0           eval {$ver = update_schema (datasource => $datasource, dry_run => 1);};
  0            
128            
129             next
130 0 0         unless defined $ver; # if update file not defined
131            
132 0 0         die "datasource '$datasource' error: $@"
133             if $@;
134            
135 0 0 0       if (!$@ and ($ver->{db} ne $ver->{schema})) {
136 0           warn "datasource '$datasource' out-of-date: db ($ver->{db}) vs. schema file ($ver->{schema})\n";
137 0           $db_outdated = 1;
138             }
139             }
140              
141 0 0         warn "please update db using: 'bin/updatedb --datasource='\n"
142             if $db_outdated;
143              
144 0           my $test_dir = $root->append ('t')->as_dir;
145            
146 0 0         if (-d $test_dir) {
147             $test_dir->scan_tree (sub {
148 0     0     my $file = shift;
149            
150 0 0         return 1
151             if $file->type eq 'dir';
152            
153 0 0         if ($file =~ /\.(?:t|pl)$/) {
154 0           my $path = $file->abs_path;
155            
156 0           run_script ("$^X $includes $path", $path);
157             }
158 0           });
159             }
160            
161 0           print "OK\n";
162            
163 0           return $pack;
164             }
165              
166             1;