App-FatPackUtils

 view release on metacpan or  search on metacpan

script/update-fatpack-snippets  view on Meta::CPAN

#!perl

our $DATE = '2020-04-30'; # DATE
our $VERSION = '0.121'; # VERSION

use 5.010;
use strict;
use warnings;
use Log::ger;

use Perinci::Object;
use Perinci::CmdLine::Any;

our %SPEC;
$SPEC{update_fatpack_snippets} = {
    v => 1.1,
    summary => 'Update fatpack snippets in a module source code',
    description => <<'_',

This routine will try to find blocks of text enclosed by:

    # BEGIN FATPACK CODE: Foo/Bar.pm Baz.pm
    ...
    # END OF FATPACK CODE

(which is the snippet generated by <pm:Module::FatPack>) and try to run
`Module::FatPack::fatpack_modules()` again for those specified modules, then
replace the snippets with the newly generated ones.

_
    args => {
        filenames => {
            'x.name.is_plural' => 1,
            'x.name.singular' => 'filename',
            summary => 'Path to module source code (.pm files)',
            schema => ['array*', of=>'filename*', min_len=>1],
            req => 1,
            pos => 0,
            greedy => 1,
        },
    },
};
sub update_fatpack_snippets {
    require File::Slurper;
    require Module::FatPack;

    my %args = @_;

    my $filenames = $args{filenames};

    my $envres = envresmulti();

    for my $filename (@$filenames) {
        unless (-f $filename) {
            log_warn("File %s does not exist, skipped", $filename);
            $envres->add_result(404, "File does not exist", {item_id=>$filename});
            next;
        }
        my $content = File::Slurper::read_text($filename);
        my $orig_content = $content;

        my $code_gen_snippet = sub {
            my @mod_pms = @_;

            my $res = Module::FatPack::fatpack_modules(
                module_names => \@mod_pms,
                pm => 1,
                stripper => 1,
            );

            die "Can't generate fatpack code for [" . join(" ", @mod_pms) .
                "]: $res->[0] - $res->[1]"
                    unless $res->[0] == 200;

            # remove extraneous newlines
            $res->[2] =~ s/^(# END OF FATPACK CODE\n)\n+/$1/m;

            $res->[2];
        };

        $content =~ s!^# BEGIN FATPACK CODE: ([^\n]+).+?^# END OF FATPACK CODE\n!$code_gen_snippet->(split /\s+/, $1)!egms;

        if ($content eq $orig_content) {
            $envres->add_result(304, "Not modified", {item_id=>$filename});
            next;
        }

        File::Slurper::write_text($filename, $content);
        $envres->add_result(200, "Updated", {item_id=>$filename});
    }

    $envres->as_struct;
}

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

# ABSTRACT: Update fatpack snippets in a module source code
# PODNAME: update-fatpack-snippets

__END__

=pod

=encoding UTF-8

=head1 NAME

update-fatpack-snippets - Update fatpack snippets in a module source code

=head1 VERSION

This document describes version 0.121 of update-fatpack-snippets (from Perl distribution App-FatPackUtils), released on 2020-04-30.

=head1 SYNOPSIS



( run in 1.221 second using v1.01-cache-2.11-cpan-39bf76dae61 )