1. Fabrice Gabolde
  2. duppy

Commits

Fabrice Gabolde  committed e083ca5

Build JSON files from casperjs arguments.

  • Participants
  • Parent commits 810ad7b
  • Branches feature/deduppy

Comments (0)

Files changed (2)

File bin/deduppy

View file
+#!/usr/bin/env perl 
+use strict;
+use warnings;
+use FindBin qw/$Bin/;
+use lib "$Bin/../lib";
+use App::DeDuppy;
+
+my $deduppy = App::DeDuppy->new_with_options();
+$deduppy->repsac_nur();

File lib/App/DeDuppy.pm

View file
+package App::DeDuppy;
+
+# ABSTRACT: generate json files from casperjs test arguments
+use strict;
+use warnings;
+use 5.010;
+use Carp;
+
+use Moo;
+use MooX::Options;
+use JSON;
+
+option 'arglist' => (is => 'ro',
+                     format => 's',
+                     required => 1,
+                     doc => 'the original casperjs arguments');
+
+option 'output' => (is => 'ro',
+                    format => 's',
+                    predicate => 'is_output_set');
+
+has 'options' => (is => 'ro',
+                  default => sub { {} });
+
+has 'subcommand' => (is => 'ro',
+                     writer => 'set_subcommand',
+                     predicate => 'subcommand_is_set');
+
+has 'paths' => (is => 'ro',
+                default => sub { [] });
+
+sub truthify_maybe {
+
+    $_[0] eq 'false' ? JSON::false :
+    $_[0] eq 'true'  ? JSON::true  : $_[0];
+
+}
+
+sub arrayrefify_maybe {
+
+    my @values = split(/,/, $_[0]);
+    if (@values > 1) {
+        return [ map { truthify_maybe($_) } @values ];
+    }
+    return truthify_maybe($values[0]);
+
+}
+
+sub repsac_nur {
+
+    # if you play it backwards it sounds like an Elvis record
+
+    my $self = shift;
+
+    foreach my $argument (split(/\s+/, $self->arglist)) {
+
+        # four types of args: --option, --option=value (there's always
+        # a = in there fortunately), subcommand (the first arg that's
+        # not an option), and test files
+
+        if ($argument =~ m/^--(?<option>[^=]+)(?:=(?<value>.+))?/) {
+
+            my ($option, $value) = ($+{option}, $+{value});
+            $self->options->{$option} = defined $value ? arrayrefify_maybe($value) : JSON::true;
+
+        } else {
+
+            if ($self->subcommand_is_set) {
+
+                push @{$self->paths}, $argument;
+
+            } else {
+
+                $self->set_subcommand($argument);
+
+            }
+
+        }
+
+    }
+
+    my $json = JSON->new->pretty->encode({ %{$self->options}, paths => $self->paths });
+    if ($self->is_output_set) {
+        open my $fh, '>', $self->output
+            or croak sprintf(q{Cannot open '%s' for writing: %s},
+                             $self->output, $!);
+        $fh->print($json);
+        $fh->close;
+    } else {
+        print $json;
+    }
+
+}
+
+1;