| 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 | ($file) = grep {defined} ( $file, $ENV{CPAN_MAKER_CONFIG}, File::HomeDir->my_home . '/.gitconfig' ); | ||
| 20 | |||
| 21 | die "ERROR: no configuration file\n" | ||
| 22 | if !$file || !-e $file; | ||
| 23 | |||
| 24 | my $config = Config::Tiny->read($file) | ||
| 25 | or die "ERROR: Could not read $file: " . Config::Tiny->errstr . "\n"; | ||
| 26 | |||
| 27 | my $self = $class->SUPER::new( { config => $config, config_file => $file } ); | ||
| 28 | |||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | ######################################################################## | ||
| 33 | sub get_value { | ||
| 34 | ######################################################################## | ||
| 35 | my ( $self, $section, $parameter ) = @_; | ||
| 36 | |||
| 37 | return $self->get_config->{$section}->{$parameter}; | ||
| 38 | } | ||
| 39 | |||
| 40 | ######################################################################## | ||
| 41 | sub user_name { | ||
| 42 | ######################################################################## | ||
| 43 | my ($self) = @_; | ||
| 44 | return $self->get_value( 'user', 'name' ); | ||
| 45 | } | ||
| 46 | |||
| 47 | ######################################################################## | ||
| 48 | sub user_github { | ||
| 49 | ######################################################################## | ||
| 50 | my ($self) = @_; | ||
| 51 | return $self->get_value( 'user', 'github' ); | ||
| 52 | } | ||
| 53 | |||
| 54 | ######################################################################## | ||
| 55 | sub user_email { | ||
| 56 | ######################################################################## | ||
| 57 | my ($self) = @_; | ||
| 58 | return $self->get_value( 'user', 'email' ); | ||
| 59 | } | ||
| 60 | |||
| 61 | ######################################################################## | ||
| 62 | sub cpan_maker_basedir { | ||
| 63 | ######################################################################## | ||
| 64 | my ($self) = @_; | ||
| 65 | |||
| 66 | return $self->get_value( 'cpan-maker', 'basedir' ); | ||
| 67 | } | ||
| 68 | |||
| 69 | ######################################################################## | ||
| 70 | sub cpan_maker_max_tokens { | ||
| 71 | ######################################################################## | ||
| 72 | my ($self) = @_; | ||
| 73 | |||
| 74 | return $self->get_value( 'cpan-maker', 'max-tokens' ); | ||
| 75 | } | ||
| 76 | |||
| 77 | ######################################################################## | ||
| 78 | sub cpan_maker_resources { | ||
| 79 | ######################################################################## | ||
| 80 | my ($self) = @_; | ||
| 81 | |||
| 82 | return $self->get_value( 'cpan-maker', 'resources' ); | ||
| 83 | } | ||
| 84 | |||
| 85 | ######################################################################## | ||
| 86 | sub cpan_maker_perltidyrc { | ||
| 87 | ######################################################################## | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | return $self->get_value( 'cpan-maker', 'perltidyrc' ); | ||
| 91 | } | ||
| 92 | |||
| 93 | ######################################################################## | ||
| 94 | sub cpan_maker_perlcriticrc { | ||
| 95 | ######################################################################## | ||
| 96 | my ($self) = @_; | ||
| 97 | |||
| 98 | return $self->get_value( 'cpan-maker', 'perlcriticrc' ); | ||
| 99 | } | ||
| 100 | |||
| 101 | ######################################################################## | ||
| 102 | sub cpan_maker_syntax_checking { | ||
| 103 | ######################################################################## | ||
| 104 | my ($self) = @_; | ||
| 105 | |||
| 106 | return $self->get_value( 'cpan-maker', 'syntax-checking' ); | ||
| 107 | } | ||
| 108 | |||
| 109 | ######################################################################## | ||
| 110 | sub cpan_maker_llm_api_key_helper { | ||
| 111 | ######################################################################## | ||
| 112 | my ($self) = @_; | ||
| 113 | |||
| 114 | return $self->get_value( 'cpan-maker', 'llm-api-key-helper' ); | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | |||
| 119 | __END__ | ||
| 120 | |||
| 121 | =pod | ||
| 122 | |||
| 123 | =encoding utf8 | ||
| 124 | |||
| 125 | =head1 NAME | ||
| 126 | |||
| 127 | CPAN::Maker::ConfigReader - Read CPAN::Maker configuration from an INI file | ||
| 128 | |||
| 129 | =head1 SYNOPSIS | ||
| 130 | |||
| 131 | use CPAN::Maker::ConfigReader; | ||
| 132 | |||
| 133 | # uses ~/.gitconfig by default | ||
| 134 | my $reader = CPAN::Maker::ConfigReader->new; | ||
| 135 | |||
| 136 | # or specify a file explicitly | ||
| 137 | my $reader = CPAN::Maker::ConfigReader->new('/path/to/.cpan-makerrc'); | ||
| 138 | |||
| 139 | # or set CPAN_MAKER_CONFIG in the environment | ||
| 140 | # CPAN_MAKER_CONFIG=~/.cpan-makerrc | ||
| 141 | my $reader = CPAN::Maker::ConfigReader->new; | ||
| 142 | |||
| 143 | print $reader->user_name; | ||
| 144 | print $reader->cpan_maker_basedir; | ||
| 145 | |||
| 146 | =head1 DESCRIPTION | ||
| 147 | |||
| 148 | C<CPAN::Maker::ConfigReader> reads configuration for the | ||
| 149 | C<CPAN::Maker> build system from an INI-format file. By default it | ||
| 150 | reads from the user's global F<~/.gitconfig> file, which means no | ||
| 151 | additional configuration is required for most developers who already | ||
| 152 | have git set up. | ||
| 153 | |||
| 154 | For users who prefer a dedicated configuration file or who do not use | ||
| 155 | git, any properly formatted INI file can be used instead. See | ||
| 156 | L</CONFIGURATION FILE> for the expected format. | ||
| 157 | |||
| 158 | =head1 CONFIGURATION FILE | ||
| 159 | |||
| 160 | The configuration file uses standard INI format as read by | ||
| 161 | L<Config::Tiny>. The following sections and keys are recognized: | ||
| 162 | |||
| 163 | =head2 [user] | ||
| 164 | |||
| 165 | [user] | ||
| 166 | name = First Last | ||
| 167 | email = you@example.com | ||
| 168 | github = your-github-username | ||
| 169 | |||
| 170 | =over 4 | ||
| 171 | |||
| 172 | =item C<name> | ||
| 173 | |||
| 174 | Your full name. Used to populate the author field in generated module | ||
| 175 | stubs and F<buildspec.yml>. | ||
| 176 | |||
| 177 | =item C<email> | ||
| 178 | |||
| 179 | Your email address. Used in generated module stubs, F<buildspec.yml>, | ||
| 180 | and the bugtracker C<mailto> field when C<--resources github> is | ||
| 181 | specified. | ||
| 182 | |||
| 183 | =item C<github> | ||
| 184 | |||
| 185 | Your GitHub username. Used to construct repository, homepage, and | ||
| 186 | bugtracker URLs when C<--resources github> is specified. | ||
| 187 | |||
| 188 | =back | ||
| 189 | |||
| 190 | =head2 [cpan-maker] | ||
| 191 | |||
| 192 | [cpan-maker] | ||
| 193 | basedir = /home/you/git | ||
| 194 | resources = github | ||
| 195 | syntax-checking = on | ||
| 196 | perltidyrc = /home/you/.perltidyrc | ||
| 197 | perlcriticrc = /home/you/.perlcriticrc | ||
| 198 | llm-api-key-helper = cat ~/.ssh/anthropic-api-key | ||
| 199 | max-tokens = 4096 | ||
| 200 | |||
| 201 | =over 4 | ||
| 202 | |||
| 203 | =item C<basedir> | ||
| 204 | |||
| 205 | The directory in which new projects are created. Equivalent to passing | ||
| 206 | C<--basedir> on the command line. | ||
| 207 | |||
| 208 | =item C<resources> | ||
| 209 | |||
| 210 | Controls generation of the F<resources> file. Currently only C<github> | ||
| 211 | is supported. Equivalent to passing C<--resources github> on the | ||
| 212 | command line. | ||
| 213 | |||
| 214 | =item C<syntax-checking> | ||
| 215 | |||
| 216 | Set to C<on> to enable Perl syntax checking during the build via | ||
| 217 | C<perl -wc> in the C<%.pm> and C<%.pl> pattern rules. See | ||
| 218 | L<CPAN::Maker::Bootstrapper/EXTENDING THE BUILD SYSTEM> for details. | ||
| 219 | |||
| 220 | =item C<perltidyrc> | ||
| 221 | |||
| 222 | Path to a F<.perltidyrc> configuration file. When set, enables | ||
| 223 | C<perltidy> checking in the build system pattern rules. | ||
| 224 | |||
| 225 | =item C<perlcriticrc> | ||
| 226 | |||
| 227 | Path to a F<.perlcriticrc> configuration file. When set, enables | ||
| 228 | C<perlcritic> checking in the build system pattern rules. | ||
| 229 | |||
| 230 | =item C<llm-api-key-helper> | ||
| 231 | |||
| 232 | A shell command whose output is used as the LLM API key. Executed when | ||
| 233 | no key is passed directly and C<LLM_API_KEY> is not set in the environment. | ||
| 234 | The command should print the key and nothing else. The file it reads should | ||
| 235 | be chmod 600. | ||
| 236 | |||
| 237 | Example: cat ~/.ssh/anthropic-api-key | ||
| 238 | |||
| 239 | =back | ||
| 240 | |||
| 241 | =head1 METHODS | ||
| 242 | |||
| 243 | =head2 new | ||
| 244 | |||
| 245 | my $reader = CPAN::Maker::ConfigReader->new; | ||
| 246 | my $reader = CPAN::Maker::ConfigReader->new($file); | ||
| 247 | |||
| 248 | Creates a new C<ConfigReader> instance. The configuration file is | ||
| 249 | resolved in the following order: | ||
| 250 | |||
| 251 | =over 4 | ||
| 252 | |||
| 253 | =item 1. The C<$file> argument if provided | ||
| 254 | |||
| 255 | =item 2. The C<CPAN_MAKER_CONFIG> environment variable | ||
| 256 | |||
| 257 | =item 3. F<~/.gitconfig> | ||
| 258 | |||
| 259 | =back | ||
| 260 | |||
| 261 | Dies if the resolved file does not exist or cannot be read. | ||
| 262 | |||
| 263 | =head2 get_value | ||
| 264 | |||
| 265 | my $val = $reader->get_value($section, $key); | ||
| 266 | |||
| 267 | Low-level accessor for any section and key in the config file. Useful | ||
| 268 | for reading project-specific keys beyond those C<ConfigReader> knows | ||
| 269 | about. | ||
| 270 | |||
| 271 | =head2 user_name | ||
| 272 | |||
| 273 | Returns C<name> from the C<[user]> section. | ||
| 274 | |||
| 275 | =head2 user_email | ||
| 276 | |||
| 277 | Returns C<email> from the C<[user]> section. | ||
| 278 | |||
| 279 | =head2 user_github | ||
| 280 | |||
| 281 | Returns C<github> from the C<[user]> section. | ||
| 282 | |||
| 283 | =head2 cpan_maker_basedir | ||
| 284 | |||
| 285 | Returns C<basedir> from the C<[cpan-maker]> section. | ||
| 286 | |||
| 287 | =head2 cpan_llm_api_key_helper | ||
| 288 | |||
| 289 | Bash snippet or the name of an executable script that provides the LLM | ||
| 290 | API key. | ||
| 291 | |||
| 292 | =head2 max-tokens | ||
| 293 | |||
| 294 | The maxium number of tokens output tokens. | ||
| 295 | |||
| 296 | =head2 cpan_maker_perltidyrc | ||
| 297 | |||
| 298 | Returns C<perltidyrc> from the C<[cpan-maker]> section. | ||
| 299 | |||
| 300 | =head2 cpan_maker_perlcriticrc | ||
| 301 | |||
| 302 | Returns C<perlcriticrc> from the C<[cpan-maker]> section. | ||
| 303 | |||
| 304 | =head2 cpan_maker_resources | ||
| 305 | |||
| 306 | Returns C<resources> from the C<[cpan-maker]> section. | ||
| 307 | |||
| 308 | =head2 cpan_maker_syntax_checking | ||
| 309 | |||
| 310 | Returns C<syntax-checking> from the C<[cpan-maker]> section. | ||
| 311 | |||
| 312 | |||
| 313 | =head1 ENVIRONMENT | ||
| 314 | |||
| 315 | =over 4 | ||
| 316 | |||
| 317 | =item C<CPAN_MAKER_CONFIG> | ||
| 318 | |||
| 319 | Path to the configuration file. Used when no file is passed to | ||
| 320 | C<new>. Allows the config file location to be set once in your shell | ||
| 321 | profile: | ||
| 322 | |||
| 323 | export CPAN_MAKER_CONFIG=~/.cpan-makerrc | ||
| 324 | |||
| 325 | =back | ||
| 326 | |||
| 327 | =head1 SEE ALSO | ||
| 328 | |||
| 329 | L<CPAN::Maker::Bootstrapper> - the scaffolding tool that uses this module | ||
| 330 | |||
| 331 | L<Config::Tiny> - the INI file parser underlying this module | ||
| 332 | |||
| 333 | =head1 AUTHOR | ||
| 334 | |||
| 335 | Rob Lauer - E<lt>rlauer@treasurersbriefcase.comE<gt> | ||
| 336 | |||
| 337 | =head1 LICENSE | ||
| 338 | |||
| 339 | This library is free software; you can redistribute it and/or modify | ||
| 340 | it under the same terms as Perl itself. | ||
| 341 | |||
| 342 | =cut |