« CPAN/Maker/Bootstrapper/Role/Provides.pm
↑ Summary
CPAN/Maker/Bootstrapper/Role/ScaffoldImporter.pm »
| 1 | package CPAN::Maker::Bootstrapper::Role::CreateBuildConfig; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use CLI::Simple::Constants qw(:booleans); | ||
| 7 | use CLI::Simple::Utils qw(choose); | ||
| 8 | use Cwd qw(abs_path); | ||
| 9 | use English qw(-no_match_vars); | ||
| 10 | use File::Basename qw(basename); | ||
| 11 | use File::ShareDir qw(dist_dir); | ||
| 12 | use File::Spec; | ||
| 13 | use File::Which; | ||
| 14 | |||
| 15 | use Role::Tiny; | ||
| 16 | |||
| 17 | ######################################################################## | ||
| 18 | sub cmd_create_build_config { | ||
| 19 | ######################################################################## | ||
| 20 | my ($self) = @_; | ||
| 21 | |||
| 22 | my $config = $self->_build_config; | ||
| 23 | |||
| 24 | foreach my $name ( sort keys %{$config} ) { | ||
| 25 | my $value = $config->{$name} // q{}; | ||
| 26 | print {*STDOUT} sprintf "%s := %s\n", $name, $value; | ||
| 27 | } | ||
| 28 | |||
| 29 | return $SUCCESS; | ||
| 30 | } | ||
| 31 | |||
| 32 | ######################################################################## | ||
| 33 | sub _build_config { | ||
| 34 | ######################################################################## | ||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | my $module_name = choose { | ||
| 38 | return $ENV{MODULE_NAME} | ||
| 39 | if $ENV{MODULE_NAME}; | ||
| 40 | |||
| 41 | my $module_name = basename abs_path q{.}; | ||
| 42 | $module_name =~ s{-}{::}gxsm; | ||
| 43 | |||
| 44 | return $module_name; | ||
| 45 | }; | ||
| 46 | |||
| 47 | my $module_path = File::Spec->catfile( 'lib', split( /::/xsm, $module_name ), ) . '.pm'; | ||
| 48 | |||
| 49 | my $project_name = $module_name; | ||
| 50 | $project_name =~ s{::}{-}gxsm; | ||
| 51 | |||
| 52 | my $unit_test_name = File::Spec->catfile( 't', sprintf q{00-%s.t}, lc $project_name, ); | ||
| 53 | |||
| 54 | my %defaults = map { $_->[0] => $_->[1] } @{ $self->_resolve_defaults }; | ||
| 55 | |||
| 56 | my %config = ( | ||
| 57 | MODULE_NAME => $module_name, | ||
| 58 | MODULE_PATH => $module_path, | ||
| 59 | PROJECT_NAME => $project_name, | ||
| 60 | UNIT_TEST_NAME => $unit_test_name, | ||
| 61 | BASEDIR => $defaults{basedir}, | ||
| 62 | PERLTIDYRC => $defaults{perltidyrc}, | ||
| 63 | PERLCRITICRC => $defaults{perlcriticrc}, | ||
| 64 | SYNTAX_CHECKING => $defaults{syntax_checking}, | ||
| 65 | BOOTSTRAPPER_DIST_DIR => dist_dir('CPAN-Maker-Bootstrapper'), | ||
| 66 | ); | ||
| 67 | |||
| 68 | my %helpers = ( | ||
| 69 | PERL => [ perl => 'perl' ], | ||
| 70 | PERLTIDY => [ perltidy => 'perltidy' ], | ||
| 71 | PERLCRITIC => [ perlcritic => 'perlcritic' ], | ||
| 72 | PODCHECKER => [ podchecker => 'podchecker' ], | ||
| 73 | CPM => [ cpm => 'cpm' ], | ||
| 74 | CARTON => [ carton => 'carton' ], | ||
| 75 | BOOTSTRAPPER => [ cmb => 'cmb' ], | ||
| 76 | DOCKER => [ docker => 'docker' ], | ||
| 77 | GIT => [ git => 'git' ], | ||
| 78 | CPAN_MAKER => [ cpan_maker => 'cpan-maker' ], | ||
| 79 | MD_UTILS => [ markdown_render => 'markdown-render' ], | ||
| 80 | POD2MARKDOWN => [ pod2markdown => 'pod2markdown' ], | ||
| 81 | PODEXTRACT => [ podextract => 'podextract' ], | ||
| 82 | SCANDEPS => [ scandeps_static => 'scandeps-static' ], | ||
| 83 | GITHUB_ACTIONS => [ gha_aws => 'gha-aws' ], | ||
| 84 | ); | ||
| 85 | |||
| 86 | my $reader = $self->get_reader; | ||
| 87 | |||
| 88 | for my $name ( sort keys %helpers ) { | ||
| 89 | my ( $key, $command ) = @{ $helpers{$name} }; | ||
| 90 | |||
| 91 | $config{$name} = $self->_resolve_helper( $reader, $key, $command, ); | ||
| 92 | } | ||
| 93 | |||
| 94 | if ( !$defaults{perltidyrc} ) { | ||
| 95 | $config{PERLTIDY} = q{}; | ||
| 96 | } | ||
| 97 | |||
| 98 | if ( !$defaults{perlcriticrc} ) { | ||
| 99 | $config{PERLCRITIC} = q{}; | ||
| 100 | } | ||
| 101 | |||
| 102 | return \%config; | ||
| 103 | } | ||
| 104 | |||
| 105 | ######################################################################## | ||
| 106 | sub _resolve_helper { | ||
| 107 | ######################################################################## | ||
| 108 | my ( $self, $reader, $key, $command ) = @_; | ||
| 109 | |||
| 110 | if ($reader) { | ||
| 111 | # present in config, including empty string | ||
| 112 | return $reader->get_config->{helpers}{$key} | ||
| 113 | if exists $reader->get_config->{helpers} && exists $reader->get_config->{helpers}{$key}; | ||
| 114 | } | ||
| 115 | |||
| 116 | # otherwise discover | ||
| 117 | return which($command); | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; |