| 1 | |||
| 2 | package App::gherkin; | ||
| 3 | $App::gherkin::VERSION = '42.0.1'; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use open ':std', ':encoding(UTF-8)'; | ||
| 7 | |||
| 8 | |||
| 9 | use Class::XSAccessor accessors => | ||
| 10 | [ qw/out_handle include_source include_ast | ||
| 11 | include_pickles predictable_ids /, ]; | ||
| 12 | |||
| 13 | use Cpanel::JSON::XS; | ||
| 14 | use Data::UUID; | ||
| 15 | use Getopt::Long qw(GetOptionsFromArray :config bundling); | ||
| 16 | use Pod::Usage; | ||
| 17 | |||
| 18 | use Gherkin; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ($class) = @_; | ||
| 22 | return bless { | ||
| 23 | out_handle => \*STDOUT, | ||
| 24 | include_source => 1, | ||
| 25 | include_ast => 1, | ||
| 26 | include_pickles => 1, | ||
| 27 | predictable_ids => 0, | ||
| 28 | }, $class; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub parse_options { | ||
| 32 | my ($self, @options) = @_; | ||
| 33 | |||
| 34 | my $help = 0; | ||
| 35 | GetOptionsFromArray( | ||
| 36 | \@options, | ||
| 37 | 'help' => \$help, | ||
| 38 | 'source!' => \$self->{include_source}, | ||
| 39 | 'ast!' => \$self->{include_ast}, | ||
| 40 | 'pickles!' => \$self->{include_pickles}, | ||
| 41 | 'predictable-ids!' => \$self->{predictable_ids}, | ||
| 42 | ) | ||
| 43 | or pod2usage(2); | ||
| 44 | pod2usage(1) if $help; | ||
| 45 | |||
| 46 | return @options; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub id_generator { | ||
| 50 | my ($self) = @_; | ||
| 51 | |||
| 52 | if ($self->predictable_ids) { | ||
| 53 | my $next_id = 0; | ||
| 54 | return sub { | ||
| 55 | my $id = $next_id++; | ||
| 56 | return "$id"; | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | my $gen = Data::UUID->new; | ||
| 61 | return sub { | ||
| 62 | return lc($gen->create_str); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | # unreachable | ||
| 67 | } | ||
| 68 | |||
| 69 | sub formatter { | ||
| 70 | my ($self) = @_; | ||
| 71 | |||
| 72 | my $fh = $self->out_handle; | ||
| 73 | return sub { | ||
| 74 | my $msg = shift; | ||
| 75 | print $fh $msg->to_json . "\n"; | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub run { | ||
| 80 | my ( $self, @file_list ) = @_; | ||
| 81 | |||
| 82 | Gherkin->from_paths(\@file_list, | ||
| 83 | $self->id_generator, | ||
| 84 | $self->formatter, | ||
| 85 | include_source => $self->include_source, | ||
| 86 | include_ast => $self->include_ast, | ||
| 87 | include_pickles => $self->include_pickles, | ||
| 88 | ); | ||
| 89 | return 0; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | 1; |