File Coverage

blib/lib/BioX/Workflow/Command/Utils/Files.pm
Criterion Covered Total %
statement 21 38 55.2
branch 0 4 0.0
condition n/a
subroutine 7 10 70.0
pod 0 1 0.0
total 28 53 52.8


line stmt bran cond sub pod time code
1             package BioX::Workflow::Command::Utils::Files;
2              
3 1     1   626 use MooseX::App::Role;
  1         2  
  1         10  
4 1     1   8409 use MooseX::Types::Path::Tiny qw/AbsFile/;
  1         2  
  1         12  
5 1     1   2376 use File::Basename;
  1         3  
  1         68  
6 1     1   6 use DateTime;
  1         2  
  1         20  
7 1     1   5 use Try::Tiny;
  1         1  
  1         46  
8 1     1   6 use Config::Any;
  1         2  
  1         26  
9 1     1   5 use File::Spec;
  1         2  
  1         465  
10              
11             option 'workflow' => (
12             is => 'rw',
13             isa => AbsFile,
14             required => 1,
15             coerce => 1,
16             documentation => 'Supply a workflow',
17             cmd_aliases => ['w'],
18             );
19              
20             =head3 workflow_data
21              
22             initial config file read into a perl structure
23              
24             =cut
25              
26             has 'workflow_data' => (
27             is => 'rw',
28             isa => 'HashRef',
29             default => sub { return {} },
30             );
31              
32             option 'outfile' => (
33             is => 'rw',
34             isa => 'Str',
35             lazy => 1,
36             default => sub {
37             my $self = shift;
38             my $workflow = $self->workflow;
39             my @files = fileparse( $self->workflow, qr/\.[^.]*/ );
40             my $dt = DateTime->now( time_zone => 'local' );
41             my $file_name =
42             $files[0] . '_' . $dt->ymd . '_' . $dt->time('-') . '.sh';
43             return File::Spec->rel2abs( $file_name );
44             },
45             documentation => 'Write your workflow to a file',
46             cmd_aliases => ['o'],
47             );
48              
49             option 'stdout' => (
50             is => 'rw',
51             isa => 'Bool',
52             default => 0,
53             documentation => 'Write workflows to STDOUT',
54             predicate => 'has_stdout',
55             );
56              
57             has 'fh' => (
58             is => 'rw',
59             lazy => 1,
60             default => sub {
61             my $self = shift;
62             my $fh = new IO::File;
63             if ( $self->stdout ) {
64             $fh->fdopen( fileno(STDOUT), "w" );
65             }
66             else {
67             $fh->open( "> " . $self->outfile );
68             }
69             return $fh;
70             },
71             );
72              
73             sub load_yaml_workflow {
74 0     0 0   my $self = shift;
75              
76 0           my $cfg;
77 0           my @files = ( $self->workflow );
78 0           my $valid = 1;
79              
80 0           $self->app_log->info( 'Loading workflow ' . $self->workflow . ' ...' );
81              
82             try {
83 0     0     $cfg = Config::Any->load_files( { files => \@files, use_ext => 1 } );
84             }
85             catch {
86 0     0     $self->app_log->warn(
87             "Unable to load your workflow. The following error was received.\n"
88             );
89 0           $self->app_log->warn("$_\n");
90 0           $valid = 0;
91 0           };
92              
93 0 0         $self->app_log->info('Your workflow is valid'."\n") if $valid;
94              
95             #TODO Add Layering
96 0           for (@$cfg) {
97 0           my ( $filename, $config ) = %$_;
98 0           $self->workflow_data($config);
99             }
100              
101 0 0         if ( !exists $self->workflow_data->{global} ) {
102 0           $self->workflow_data->{global} = [];
103             }
104              
105 0           return $valid;
106             }
107              
108             1;