| 1 | package Zuzu::Tidy::CLI; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.007000'; | ||
| 8 | |||
| 9 | use Getopt::Long qw( | ||
| 10 | Configure | ||
| 11 | GetOptionsFromArray | ||
| 12 | ); | ||
| 13 | |||
| 14 | use Zuzu::Tidy; | ||
| 15 | |||
| 16 | sub run { | ||
| 17 | my ( @argv ) = @_; | ||
| 18 | @argv = @ARGV if not @argv; | ||
| 19 | |||
| 20 | binmode *STDOUT, ':utf8'; | ||
| 21 | binmode *STDERR, ':utf8'; | ||
| 22 | |||
| 23 | my ( $options, $args, $error ) = _parse_options( \@argv ); | ||
| 24 | if ( defined $error ) { | ||
| 25 | _print_usage($error); | ||
| 26 | return 2; | ||
| 27 | } | ||
| 28 | |||
| 29 | my $file = shift @{ $args }; | ||
| 30 | if ( $options->{stdin} ) { | ||
| 31 | if ( defined $file ) { | ||
| 32 | _print_usage('--stdin does not accept a script path'); | ||
| 33 | return 2; | ||
| 34 | } | ||
| 35 | if ( $options->{in_place} ) { | ||
| 36 | _print_usage('--stdin cannot be combined with --in-place'); | ||
| 37 | return 2; | ||
| 38 | } | ||
| 39 | binmode *STDIN, ':encoding(UTF-8)'; | ||
| 40 | } | ||
| 41 | elsif ( not defined $file or $file eq '' ) { | ||
| 42 | _print_usage('Missing script path'); | ||
| 43 | return 2; | ||
| 44 | } | ||
| 45 | if ( @{ $args } ) { | ||
| 46 | _print_usage('Too many arguments'); | ||
| 47 | return 2; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $source; | ||
| 51 | my $filename = $file; | ||
| 52 | if ( $options->{stdin} ) { | ||
| 53 | $source = do { local $/; <STDIN> }; | ||
| 54 | $filename = '<stdin>'; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | open my $in_fh, '<:encoding(UTF-8)', $file | ||
| 58 | or die "Could not open '$file': $!\n"; | ||
| 59 | $source = do { local $/; <$in_fh> }; | ||
| 60 | close $in_fh; | ||
| 61 | } | ||
| 62 | |||
| 63 | my $tidied = Zuzu::Tidy->tidy( | ||
| 64 | $source, | ||
| 65 | filename => $filename, | ||
| 66 | canonical_operators => $options->{canonical_operators}, | ||
| 67 | ); | ||
| 68 | |||
| 69 | if ( $options->{in_place} ) { | ||
| 70 | open my $out_fh, '>:encoding(UTF-8)', $file | ||
| 71 | or die "Could not write '$file': $!\n"; | ||
| 72 | print {$out_fh} $tidied; | ||
| 73 | close $out_fh; | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | print $tidied; | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _parse_options { | ||
| 82 | my ( $argv ) = @_; | ||
| 83 | |||
| 84 | my $options = { | ||
| 85 | in_place => 0, | ||
| 86 | stdin => 0, | ||
| 87 | canonical_operators => 0, | ||
| 88 | }; | ||
| 89 | |||
| 90 | Configure( | ||
| 91 | 'no_ignore_case', | ||
| 92 | 'bundling', | ||
| 93 | ); | ||
| 94 | my $ok = GetOptionsFromArray( | ||
| 95 | $argv, | ||
| 96 | 'i|in-place' => \$options->{in_place}, | ||
| 97 | 'stdin' => \$options->{stdin}, | ||
| 98 | 'canonical-operators!' => \$options->{canonical_operators}, | ||
| 99 | 'h|help' => \$options->{help}, | ||
| 100 | ); | ||
| 101 | return ( undef, undef, undef ) if not $ok; | ||
| 102 | |||
| 103 | if ( $options->{help} ) { | ||
| 104 | return ( undef, undef, '' ); | ||
| 105 | } | ||
| 106 | |||
| 107 | return ( $options, $argv, undef ); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub _print_usage { | ||
| 111 | my ( $message ) = @_; | ||
| 112 | |||
| 113 | if ( defined $message and $message ne '' ) { | ||
| 114 | print STDERR $message, "\n"; | ||
| 115 | } | ||
| 116 | print STDERR "Usage: zuzu-tidy.pl [--stdin] [--in-place] [--canonical-operators] path/to/script.zzs\n"; | ||
| 117 | |||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =head1 NAME | ||
| 124 | |||
| 125 | Zuzu::Tidy::CLI - command-line wrapper for Zuzu::Tidy | ||
| 126 | |||
| 127 | =head1 SYNOPSIS | ||
| 128 | |||
| 129 | zuzu-tidy.pl path/to/script.zzs | ||
| 130 | zuzu-tidy.pl --stdin < path/to/script.zzs | ||
| 131 | zuzu-tidy.pl --canonical-operators path/to/script.zzs | ||
| 132 | zuzu-tidy.pl --in-place path/to/script.zzs | ||
| 133 | |||
| 134 | =head1 DESCRIPTION | ||
| 135 | |||
| 136 | Provides the executable interface for C<bin/zuzu-tidy.pl>. The command reads a | ||
| 137 | ZuzuScript file, formats it via C<Zuzu::Tidy>, and writes to STDOUT by | ||
| 138 | default or updates the file in-place with C<--in-place>. | ||
| 139 | |||
| 140 | C<--stdin> reads source from STDIN and writes tidied output to STDOUT. It | ||
| 141 | cannot be combined with C<--in-place>. | ||
| 142 | |||
| 143 | C<--canonical-operators> rewrites non-canonical operator spellings such as | ||
| 144 | C<*> and C<< |> >> to canonical forms such as C<×> and C<▷>. This is | ||
| 145 | opt-in; aliases are preserved by default. | ||
| 146 | |||
| 147 | =head1 METHODS | ||
| 148 | |||
| 149 | =head2 run | ||
| 150 | |||
| 151 | Parses CLI options, formats the target file, and returns a process exit code. | ||
| 152 | |||
| 153 | =head1 COPYRIGHT AND LICENCE | ||
| 154 | |||
| 155 | B<< Zuzu::Tidy::CLI >> is copyright Toby Inkster. | ||
| 156 | |||
| 157 | It is free software; you may redistribute it and/or modify it under | ||
| 158 | the terms of either the Artistic License 1.0 or the GNU General Public | ||
| 159 | License version 2. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | 1; |