#!perl

our $DATE = '2016-01-18'; # DATE
our $VERSION = '0.10'; # VERSION

use 5.010;
use strict;
use warnings;

use Perinci::CmdLine::Any;

die "App::lcpan required, please install it first\n"
    unless eval { require App::lcpan; 1 };

no warnings 'once';

our %SPEC;
$SPEC{list_dist_deps} = {
    v => 1.1,
    summary => 'List dependencies from dist.ini',
    description => <<'_',

Check the Prereqs/* sections in your `dist.ini` and list them. There is an
option to do recursive CPAN dependencies by using `lcpan`.

AutoPrereqs is not supported.

_
    args => {
        %App::lcpan::deps_args,
    },
};
sub list_dist_deps {
    require Config::IOD::Reader;

    my %args = @_;

    my $reader = Config::IOD::Reader->new(
        ignore_unknown_directive => 1,
    );
    my $dist_ini = $reader->read_file('dist.ini');

    my @res;
    for my $sectname (keys %$dist_ini) {
        next unless $sectname =~ m!^Prereqs(?:\s*/\s*
                                       (develop|configure|build|runtime|test)
                                       (requires|recommends|suggests|conflicts))?$!ix;
        my $phase = lc($1 // 'runtime');
        my $rel   = lc($2 // 'requires');
        next if $args{phase} ne 'ALL' && $phase ne $args{phase};
        next if $args{rel} ne 'ALL' && $rel ne $args{rel};
        my $sect = $dist_ini->{$sectname};
        for my $mod (keys %$sect) {
            next if $mod eq 'perl';
            push @res, {module=>$mod, phase=>$phase, rel=>$rel, version=>$sect->{$mod}};
        }
    }

    if ($args{level}) {
        require IPC::System::Options;
        require JSON;
        my $res = IPC::System::Options::backtick(
            {die=>1, log=>1},
            "lcpan", "deps",
            "--phase", $args{phase},
            "--rel", $args{rel},
            "--level", $args{level},
            ("--include-core") x !!($args{include_core}),
            ("--no-include-core") x !!(!$args{include_core}),
            "--perl-version", $args{perl_version},
            "--json", "--no-naked-res",
            map {$_->{module}} @res,
        );
        JSON->new->decode($res);
    } else {
        [200,"OK",\@res];
    }
}

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

# ABSTRACT: List dependencies from dist.ini
# PODNAME: list-dist-deps

__END__

=pod

=encoding UTF-8

=head1 NAME

list-dist-deps - List dependencies from dist.ini

=head1 VERSION

This document describes version 0.10 of list-dist-deps (from Perl distribution App-DzilUtils), released on 2016-01-18.

=head1 SYNOPSIS

 % list-dist-deps
 % list-dist-deps --phase ALL --rel ALL -R

=head1 DESCRIPTION

Check the Prereqs/* sections in your C<dist.ini> and list them. There is an
option to do recursive CPAN dependencies by using C<lcpan>.

AutoPrereqs is not supported.

=head1 OPTIONS

C<*> marks required options.

=head2 Configuration options

=over

=item B<--config-path>=I<filename>

Set path to configuration file.

Can be specified multiple times.

=item B<--config-profile>=I<s>

Set configuration profile to use.

=item B<--no-config>

Do not use any configuration file.

=back

=head2 Environment options

=over

=item B<--no-env>

Do not read environment for default options.

=back

=head2 Filter options

=over

=item B<--all>

Equivalent to --phase ALL --rel ALL.

See C<--phase>.

=item B<--include-core>

Include Perl core modules.

=item B<--phase>=I<s>

Default value:

 "runtime"

Valid values:

 ["develop","configure","build","runtime","test","ALL"]

=item B<--rel>=I<s>

Default value:

 "requires"

Valid values:

 ["requires","recommends","suggests","conflicts","ALL"]

=item B<--with-xs-or-pp>

Check each dependency as XS/PP.

=back

=head2 Output options

=over

=item B<--format>=I<s>

Choose output format, e.g. json, text.

Default value:

 undef

=item B<--json>

Set output format to json.

=item B<--naked-res>

When outputing as JSON, strip result envelope.

Default value:

 0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd
element) as well as result metadata/extra result (4th element) instead of just
the result (3rd element). However, sometimes you want just the result, e.g. when
you want to pipe the result for more post-processing. In this case you can use
`--naked-res` so you just get:

    [1,2,3]


=back

=head2 Other options

=over

=item B<--flatten>

Instead of showing tree-like information, flatten it.

When recursing, the default is to show the final result in a tree-like table,
i.e. indented according to levels, e.g.:

    % lcpan deps -R MyModule
    | module            | author  | version |
    |-------------------|---------|---------|
    | Foo               | AUTHOR1 | 0.01    |
    |   Bar             | AUTHOR2 | 0.23    |
    |   Baz             | AUTHOR3 | 1.15    |
    | Qux               | AUTHOR2 | 0       |

To be brief, if `Qux` happens to also depends on `Bar`, it will not be shown in
the result. Thus we don't know the actual `Bar` version that is needed by the
dependency tree of `MyModule`. For example, if `Qux` happens to depends on `Bar`
version 0.45 then `MyModule` indirectly requires `Bar` 0.45.

To list all the direct and indirect dependencies on a single flat list, with
versions already resolved to the largest version required, use the `flatten`
option:

    % lcpan deps -R --flatten MyModule
    | module            | author  | version |
    |-------------------|---------|---------|
    | Foo               | AUTHOR1 | 0.01    |
    | Bar               | AUTHOR2 | 0.45    |
    | Baz               | AUTHOR3 | 1.15    |
    | Qux               | AUTHOR2 | 0       |

Note that `Bar`'s required version is already 0.45 in the above example.


=item B<--help>, B<-h>, B<-?>

Display help message and exit.

=item B<--level>=I<i>, B<-l>

Recurse for a number of levels (-1 means unlimited).

Default value:

 1

=item B<--perl-version>=I<s>, B<-V>

Set base Perl version for determining core modules.

Default value:

 "v5.22.0"

=item B<--version>, B<-v>

Display program's version and exit.

=item B<-R>

Recurse (alias for `--level -1`).

See C<--level>.

=back

=head1 COMPLETION

This script has shell tab completion capability with support for several
shells.

=head2 bash

To activate bash completion for this script, put:

 complete -C list-dist-deps list-dist-deps

in your bash startup (e.g. C<~/.bashrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is recommended, however, that you install L<shcompgen> which allows you to
activate completion scripts for several kinds of scripts on multiple shells.
Some CPAN distributions (those that are built with
L<Dist::Zilla::Plugin::GenShellCompletion>) will even automatically enable shell
completion for their included scripts (using C<shcompgen>) at installation time,
so you can immadiately have tab completion.

=head2 tcsh

To activate tcsh completion for this script, put:

 complete list-dist-deps 'p/*/`list-dist-deps`/'

in your tcsh startup (e.g. C<~/.tcshrc>). Your next shell session will then
recognize tab completion for the command. Or, you can also directly execute the
line above in your shell to activate immediately.

It is also recommended to install C<shcompgen> (see above).

=head2 other shells

For fish and zsh, install C<shcompgen> as described above.

=head1 CONFIGURATION FILE

This script can read configuration file, which by default is searched at C<~/.config/list-dist-deps.conf>, C<~/list-dist-deps.conf> or C</etc/list-dist-deps.conf> (can be changed by specifying C<--config-path>). All found files will be read and merged.

To disable searching for configuration files, pass C<--no-config>.

Configuration file is in the format of L<IOD>, which is basically INI with some extra features. 

You can put multiple profiles in a single file by using section names like C<[profile=SOMENAME]>. Those sections will only be read if you specify the matching C<--config-profile SOMENAME>.

List of available configuration parameters:

 flatten (see --flatten)
 format (see --format)
 include_core (see --include-core)
 level (see --level)
 naked_res (see --naked-res)
 perl_version (see --perl-version)
 phase (see --phase)
 rel (see --rel)
 with_xs_or_pp (see --with-xs-or-pp)

=head1 ENVIRONMENT

=head2 LIST_DIST_DEPS_OPT => str

Specify additional command-line options

=head1 FILES

~/.config/list-dist-deps.conf

~/list-dist-deps.conf

/etc/list-dist-deps.conf

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by perlancar@cpan.org.

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
