| 1 | package Zuzu::Doc::CLI; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | our $VERSION = '0.007002'; | ||
| 8 | |||
| 9 | use File::Spec; | ||
| 10 | use Getopt::Long qw( | ||
| 11 | Configure | ||
| 12 | GetOptionsFromArray | ||
| 13 | ); | ||
| 14 | use Pod::Text::Termcap; | ||
| 15 | use IO::Handle; | ||
| 16 | |||
| 17 | use Zuzu::Runtime; | ||
| 18 | |||
| 19 | sub run { | ||
| 20 | my ( @argv ) = @_; | ||
| 21 | @argv = @ARGV if not @argv; | ||
| 22 | |||
| 23 | my ( $options, $args, $error ) = _parse_options( \@argv ); | ||
| 24 | if ( defined $error ) { | ||
| 25 | _print_usage($error); | ||
| 26 | return 2; | ||
| 27 | } | ||
| 28 | |||
| 29 | my ( $target, $target_error ) = _extract_target($args); | ||
| 30 | if ( defined $target_error ) { | ||
| 31 | _print_usage($target_error); | ||
| 32 | return 2; | ||
| 33 | } | ||
| 34 | |||
| 35 | my $runtime = _build_runtime($options); | ||
| 36 | my $doc_file = _resolve_target_path( $runtime, $target, $options ); | ||
| 37 | if ( not defined $doc_file ) { | ||
| 38 | print STDERR "Could not locate documentation for '$target'\n"; | ||
| 39 | return 1; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $output = _render_pod($doc_file); | ||
| 43 | my $pager_ok = _page_output($output); | ||
| 44 | return $pager_ok ? 0 : 1; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub _parse_options { | ||
| 48 | my ( $argv ) = @_; | ||
| 49 | |||
| 50 | my $options = { | ||
| 51 | include_dirs => [], | ||
| 52 | }; | ||
| 53 | |||
| 54 | Configure( | ||
| 55 | 'no_ignore_case', | ||
| 56 | 'bundling', | ||
| 57 | ); | ||
| 58 | my $ok = GetOptionsFromArray( | ||
| 59 | $argv, | ||
| 60 | 'I=s@' => $options->{include_dirs}, | ||
| 61 | 'bin' => \$options->{bin}, | ||
| 62 | 'h|help' => \$options->{help}, | ||
| 63 | ); | ||
| 64 | return ( undef, undef, undef ) if not $ok; | ||
| 65 | |||
| 66 | if ( $options->{help} ) { | ||
| 67 | return ( undef, undef, '' ); | ||
| 68 | } | ||
| 69 | |||
| 70 | return ( $options, $argv, undef ); | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _extract_target { | ||
| 74 | my ( $args ) = @_; | ||
| 75 | |||
| 76 | my $target = shift @{ $args }; | ||
| 77 | return ( undef, '' ) if not defined $target; | ||
| 78 | return ( undef, 'Too many arguments' ) if @{ $args }; | ||
| 79 | |||
| 80 | return ( $target, undef ); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub _build_runtime { | ||
| 84 | my ( $options ) = @_; | ||
| 85 | |||
| 86 | return Zuzu::Runtime->new( | ||
| 87 | lib => [ @{ $options->{include_dirs} }, @Zuzu::Runtime::DEFAULT_LIB ], | ||
| 88 | ); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _resolve_target_path { | ||
| 92 | my ( $runtime, $target, $options ) = @_; | ||
| 93 | |||
| 94 | return $target if _is_existing_file($target); | ||
| 95 | |||
| 96 | if ( $options->{bin} ) { | ||
| 97 | my $bin_path = _resolve_path_target($target); | ||
| 98 | return $bin_path if defined $bin_path; | ||
| 99 | } | ||
| 100 | |||
| 101 | for my $candidate ( $runtime->_module_candidates( $target, undef ) ) { | ||
| 102 | return $candidate if -f $candidate; | ||
| 103 | } | ||
| 104 | |||
| 105 | my $bin_path = _resolve_path_target($target); | ||
| 106 | return $bin_path if defined $bin_path; | ||
| 107 | |||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub _resolve_path_target { | ||
| 112 | my ( $target ) = @_; | ||
| 113 | |||
| 114 | return undef if not defined $target; | ||
| 115 | return undef if $target eq ''; | ||
| 116 | |||
| 117 | for my $dir ( File::Spec->path ) { | ||
| 118 | next if not defined $dir or $dir eq ''; | ||
| 119 | my $candidate = File::Spec->catfile( $dir, $target ); | ||
| 120 | return $candidate if _is_existing_file($candidate); | ||
| 121 | } | ||
| 122 | |||
| 123 | return undef; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub _is_existing_file { | ||
| 127 | my ( $value ) = @_; | ||
| 128 | |||
| 129 | return 0 if not defined $value; | ||
| 130 | return 0 if $value eq ''; | ||
| 131 | return 1 if -f $value; | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _render_pod { | ||
| 136 | my ( $doc_file ) = @_; | ||
| 137 | |||
| 138 | my $formatter = Pod::Text::Termcap->new; | ||
| 139 | my $output = ''; | ||
| 140 | open my $out_fh, '>:encoding(UTF-8)', \$output | ||
| 141 | or die "Could not open in-memory buffer: $!\n"; | ||
| 142 | $formatter->output_fh($out_fh); | ||
| 143 | $formatter->parse_from_file($doc_file); | ||
| 144 | close $out_fh; | ||
| 145 | |||
| 146 | return $output; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _page_output { | ||
| 150 | my ( $output ) = @_; | ||
| 151 | |||
| 152 | my $pager = $ENV{PAGER} || 'less -FRX'; | ||
| 153 | open my $pager_fh, '|-', $pager | ||
| 154 | or die "Could not start pager '$pager': $!\n"; | ||
| 155 | $pager_fh->autoflush(1); | ||
| 156 | print {$pager_fh} $output; | ||
| 157 | |||
| 158 | my $closed = close $pager_fh; | ||
| 159 | return $closed ? 1 : 0; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub _print_usage { | ||
| 163 | my ( $message ) = @_; | ||
| 164 | |||
| 165 | if ( defined $message and $message ne '' ) { | ||
| 166 | print STDERR $message, "\n"; | ||
| 167 | } | ||
| 168 | print STDERR "Usage: zuzudoc.pl [--bin] [-I/path/to/lib] path/to/file.zzs|module/name\n"; | ||
| 169 | |||
| 170 | return; | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | =pod | ||
| 176 | |||
| 177 | =head1 COPYRIGHT AND LICENCE | ||
| 178 | |||
| 179 | B<< Zuzu::Doc::CLI >> is copyright Toby Inkster. | ||
| 180 | |||
| 181 | It is free software; you may redistribute it and/or modify it under | ||
| 182 | the terms of either the Artistic License 1.0 or the GNU General Public | ||
| 183 | License version 2. | ||
| 184 | |||
| 185 | =cut |