#!perl

use 5.010;
use strict;
use warnings;
use Log::Any '$log';

use App::PMUtils;
use Perinci::CmdLine::Lite;

our $DATE = '2014-07-29'; # DATE
our $VERSION = '0.19'; # VERSION

our %SPEC;
$SPEC{pmversion} = {
    v => 1.1,
    summary => 'Get Perl module version',
    description => <<'_',
_
    args => {
        module => {
            schema => ['array*' => of => 'str*', min_len=>1],
            req => 1,
            pos => 0,
            greedy => 1,
            element_completion => $App::PMUtils::_complete_module,
        },
    },
    "x.perinci.cmdline.default_format" => 'text-simple',
};
sub pmversion {
    require Module::Load;
    require SHARYANTO::Module::Path;

    my %args = @_;
    my $mods = $args{module};

    my $found;
    my $res = [];
    for my $mod (@$mods) {
        $mod =~ s!/!::!g;
        my $mpath = SHARYANTO::Module::Path::module_path(module=>$mod);
        unless ($mpath) {
            push @$res, "Module $mod is not installed";
            next;
        }
        $found++;
        # load to get $VERSION & $DATE
        {
            no strict 'refs';
            Module::Load::load($mod);
            my $v = ${"$mod\::VERSION"};
            if (defined $v) {
                push @$res, (@$mods > 1 ? "$mod $v" : $v);
            } else {
                push @$res, "Module $mod does not define \$VERSION";
            }
        }
    }

    if (!$found) {
        [404, "No such module(s): " . join(", ", @$mods)];
    } else {
        [200, "OK", $res];
    }
}

Perinci::CmdLine::Lite->new(
    url => '/main/pmversion',
)->run;

# ABSTRACT: Get Perl module version
# PODNAME: pmversion

__END__

=pod

=encoding UTF-8

=head1 NAME

pmversion - Get Perl module version

=head1 VERSION

This document describes version 0.19 of pmversion (from Perl distribution App-PMUtils), released on 2014-07-29.

=head1 SYNOPSIS

 % pmversion Some::Module Another::Module

To active bash completion:

 % complete -C pmversion pmversion ; # can be put in bash startup file e.g. .bashrc
 % pmversion Some<tab>
 % pmversion test/builder/mo<tab> ; # resolve to Test/Builder/Module, can resolve case
 % pmversion "Text::Abb<tab>      ; # use quote (' or ") if you want to use :: as separator

=head1 SEE ALSO

L<pminfo>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-PMUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/sharyanto/perl-App-PMUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-PMUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
