| 1 | package CPAN::Maker::Bootstrapper::ConfigReader; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Config::Tiny; | ||
| 7 | use File::HomeDir; | ||
| 8 | |||
| 9 | use parent qw(Class::Accessor::Fast); | ||
| 10 | |||
| 11 | __PACKAGE__->follow_best_practice; | ||
| 12 | __PACKAGE__->mk_accessors(qw(config config_file)); | ||
| 13 | |||
| 14 | ######################################################################## | ||
| 15 | sub new { | ||
| 16 | ######################################################################## | ||
| 17 | my ( $class, $file ) = @_; | ||
| 18 | |||
| 19 | my $gitconfig = File::HomeDir->my_home . '/.gitconfig'; | ||
| 20 | my @candidates = ( $file, $ENV{CPAN_MAKER_CONFIG}, '.cpan-makerrc', '.git/config', $gitconfig, ); | ||
| 21 | |||
| 22 | my $explicit = defined $file ? $file : $ENV{CPAN_MAKER_CONFIG}; | ||
| 23 | |||
| 24 | if ( defined $explicit && !-f $explicit ) { | ||
| 25 | die sprintf "Configuration file does not exist: %s\n", $explicit; | ||
| 26 | } | ||
| 27 | |||
| 28 | ($file) = grep { defined $_ && -f $_ } @candidates; | ||
| 29 | |||
| 30 | die "ERROR: no configuration file\n" | ||
| 31 | if !$file || !-e $file; | ||
| 32 | |||
| 33 | my $config = Config::Tiny->read($file) | ||
| 34 | or die "ERROR: Could not read $file: " . Config::Tiny->errstr . "\n"; | ||
| 35 | |||
| 36 | if ( $file ne $gitconfig && -f $gitconfig ) { | ||
| 37 | my $git = Config::Tiny->read($gitconfig); | ||
| 38 | |||
| 39 | if ( $git && $git->{user} ) { | ||
| 40 | $config->{user} //= {}; | ||
| 41 | |||
| 42 | $config->{user}{name} //= $git->{user}{name}; | ||
| 43 | $config->{user}{email} //= $git->{user}{email}; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | my $self = $class->SUPER::new( { config => $config, config_file => $file } ); | ||
| 48 | |||
| 49 | return $self; | ||
| 50 | } | ||
| 51 | |||
| 52 | ######################################################################## | ||
| 53 | sub get_value { | ||
| 54 | ######################################################################## | ||
| 55 | my ( $self, $section, $parameter ) = @_; | ||
| 56 | |||
| 57 | return $self->get_config->{$section}->{$parameter}; | ||
| 58 | } | ||
| 59 | |||
| 60 | ######################################################################## | ||
| 61 | sub user_name { | ||
| 62 | ######################################################################## | ||
| 63 | my ($self) = @_; | ||
| 64 | |||
| 65 | return $self->get_value( 'user', 'name' ); | ||
| 66 | } | ||
| 67 | |||
| 68 | ######################################################################## | ||
| 69 | sub user_github { | ||
| 70 | ######################################################################## | ||
| 71 | my ($self) = @_; | ||
| 72 | return $self->get_value( 'user', 'github' ); | ||
| 73 | } | ||
| 74 | |||
| 75 | ######################################################################## | ||
| 76 | sub user_email { | ||
| 77 | ######################################################################## | ||
| 78 | my ($self) = @_; | ||
| 79 | return $self->get_value( 'user', 'email' ); | ||
| 80 | } | ||
| 81 | |||
| 82 | ######################################################################## | ||
| 83 | sub cpan_maker_basedir { | ||
| 84 | ######################################################################## | ||
| 85 | my ($self) = @_; | ||
| 86 | |||
| 87 | return $self->get_value( 'cpan-maker', 'basedir' ); | ||
| 88 | } | ||
| 89 | |||
| 90 | ######################################################################## | ||
| 91 | sub cpan_maker_resources { | ||
| 92 | ######################################################################## | ||
| 93 | my ($self) = @_; | ||
| 94 | |||
| 95 | return $self->get_value( 'cpan-maker', 'resources' ); | ||
| 96 | } | ||
| 97 | |||
| 98 | ######################################################################## | ||
| 99 | sub cpan_maker_perltidyrc { | ||
| 100 | ######################################################################## | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | return $self->get_value( 'cpan-maker', 'perltidyrc' ); | ||
| 104 | } | ||
| 105 | |||
| 106 | ######################################################################## | ||
| 107 | sub cpan_maker_perlcriticrc { | ||
| 108 | ######################################################################## | ||
| 109 | my ($self) = @_; | ||
| 110 | |||
| 111 | return $self->get_value( 'cpan-maker', 'perlcriticrc' ); | ||
| 112 | } | ||
| 113 | |||
| 114 | ######################################################################## | ||
| 115 | sub cpan_maker_syntax_checking { | ||
| 116 | ######################################################################## | ||
| 117 | my ($self) = @_; | ||
| 118 | |||
| 119 | return $self->get_value( 'cpan-maker', 'syntax-checking' ); | ||
| 120 | } | ||
| 121 | |||
| 122 | 1; | ||
| 123 | |||
| 124 | __END__ |