| 1 | package App::DeDuppy; | ||
| 2 | |||
| 3 | # ABSTRACT: generate json files from casperjs test arguments | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use 5.010; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | use Moo; | ||
| 10 | use MooX::Options; | ||
| 11 | use JSON; | ||
| 12 | |||
| 13 | option 'arglist' => (is => 'ro', | ||
| 14 | format => 's', | ||
| 15 | required => 1, | ||
| 16 | doc => 'the original casperjs arguments'); | ||
| 17 | |||
| 18 | option 'output' => (is => 'ro', | ||
| 19 | format => 's', | ||
| 20 | predicate => 'is_output_set'); | ||
| 21 | |||
| 22 | has 'options' => (is => 'ro', | ||
| 23 | default => sub { {} }); | ||
| 24 | |||
| 25 | has 'subcommand' => (is => 'ro', | ||
| 26 | writer => 'set_subcommand', | ||
| 27 | predicate => 'subcommand_is_set'); | ||
| 28 | |||
| 29 | has 'paths' => (is => 'ro', | ||
| 30 | default => sub { [] }); | ||
| 31 | |||
| 32 | sub truthify_maybe { | ||
| 33 | |||
| 34 | $_[0] eq 'false' ? JSON::false : | ||
| 35 | $_[0] eq 'true' ? JSON::true : $_[0]; | ||
| 36 | |||
| 37 | } | ||
| 38 | |||
| 39 | sub arrayrefify_maybe { | ||
| 40 | |||
| 41 | my @values = split(/,/, $_[0]); | ||
| 42 | if (@values > 1) { | ||
| 43 | return [ map { truthify_maybe($_) } @values ]; | ||
| 44 | } | ||
| 45 | return truthify_maybe($values[0]); | ||
| 46 | |||
| 47 | } | ||
| 48 | |||
| 49 | sub repsac_nur { | ||
| 50 | |||
| 51 | # if you play it backwards it sounds like an Elvis record | ||
| 52 | |||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | foreach my $argument (split(/\s+/, $self->arglist)) { | ||
| 56 | |||
| 57 | # four types of args: --option, --option=value (there's always | ||
| 58 | # a = in there fortunately), subcommand (the first arg that's | ||
| 59 | # not an option), and test files | ||
| 60 | |||
| 61 | if ($argument =~ m/^--(?<option>[^=]+)(?:=(?<value>.+))?/) { | ||
| 62 | |||
| 63 | my ($option, $value) = ($+{option}, $+{value}); | ||
| 64 | $self->options->{$option} = defined $value ? arrayrefify_maybe($value) : JSON::true; | ||
| 65 | |||
| 66 | } else { | ||
| 67 | |||
| 68 | if ($self->subcommand_is_set) { | ||
| 69 | |||
| 70 | push @{$self->paths}, $argument; | ||
| 71 | |||
| 72 | } else { | ||
| 73 | |||
| 74 | $self->set_subcommand($argument); | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | } | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | my $json = JSON->new->pretty->encode({ %{$self->options}, paths => $self->paths }); | ||
| 83 | if ($self->is_output_set) { | ||
| 84 | open my $fh, '>', $self->output | ||
| 85 | or croak sprintf(q{Cannot open '%s' for writing: %s}, | ||
| 86 | $self->output, $!); | ||
| 87 | $fh->print($json); | ||
| 88 | $fh->close; | ||
| 89 | } else { | ||
| 90 | print $json; | ||
| 91 | } | ||
| 92 | |||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | 1; | ||
| 97 | |||
| 98 | __END__ |