| 1 | package App::Multigit::Script; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use 5.014; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Getopt::Long qw(:config gnu_getopt pass_through require_order); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Pod::Usage qw(pod2usage); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '0.18'; | |||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | App::Multigit::Script - Common behaviour for scripts | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | If you use App::Multigit::Script, some things are taken care of for you, and | ||||||||||
| 19 | some things can be done on request. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 AUTOMATIC BEHAVIOUR | ||||||||||
| 22 | |||||||||||
| 23 | The default behaviour is to | ||||||||||
| 24 | |||||||||||
| 25 | =over | ||||||||||
| 26 | |||||||||||
| 27 | =item Use L<Getopt::Long> to handle C<--workdir>. This changes directory and is | ||||||||||
| 28 | part of the internal communication between C<mg> itself and your script. | ||||||||||
| 29 | |||||||||||
| 30 | =item Also handle C<--help>, by running Pod::Usage against your script. | ||||||||||
| 31 | |||||||||||
| 32 | =item Read C<STDIN> for a list of directories to work against, if appropriate | ||||||||||
| 33 | |||||||||||
| 34 | =back | ||||||||||
| 35 | |||||||||||
| 36 | =head1 FUNCTIONS | ||||||||||
| 37 | |||||||||||
| 38 | =head2 import | ||||||||||
| 39 | |||||||||||
| 40 | Not your common or garden import. This does the aforementioned things, and | ||||||||||
| 41 | exports a C<%options> into your script's namespace (i.e. C<main>). | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | sub import { | ||||||||
|
Subroutine
import: not called CC=1 SCAR=6.9
| |||||||||||
| 46 | 0 | my $package = (caller)[0]; | |||||||||
| 47 | 0 | read_stdin(); | |||||||||
| 48 | 0 | my %options = get_default_options($package); | |||||||||
| 49 | 0 | chdir $options{workdir}; | |||||||||
| 50 | 0 | _install_symbol( \%options, $package, 'options'); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | =head2 get_default_options | ||||||||||
| 54 | |||||||||||
| 55 | Returns a hash containing the standard options for all C<mg> scripts. | ||||||||||
| 56 | |||||||||||
| 57 | =cut | ||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub get_default_options { | ||||||||
|
Subroutine
get_default_options: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | 0 | my $package = shift; | |||||||||
| 61 | my %options = ( | ||||||||||
| 62 | 0 | ▶ | help => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 63 | 0 | pod2usage({ | |||||||||
| 64 | -exitval => 0, | ||||||||||
| 65 | -verbose => 1, | ||||||||||
| 66 | }); | ||||||||||
| 67 | } | ||||||||||
| 68 | 0 | ); | |||||||||
| 69 | |||||||||||
| 70 | 0 | GetOptions( | |||||||||
| 71 | \%options, | ||||||||||
| 72 | 'workdir=s', | ||||||||||
| 73 | 'help' | ||||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | 0 | return %options; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head2 read_stdin | ||||||||||
| 80 | |||||||||||
| 81 | If STDIN is connected to a pipe or something, this slurps it and uses it as the | ||||||||||
| 82 | list of selected repos. See | ||||||||||
| 83 | L<@SELECTED_REPOS in App::Multigit|App::Multigit/@SELECTED_REPOS>. | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | 0 | ▶ | sub read_stdin { | ||||||||
|
Subroutine
read_stdin: not called CC=2 SCAR=10.1
Pod
documented
| |||||||||||
| 88 | 0 | ▶ | if (-p STDIN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 1 | ▶ | no warnings 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 90 | 0 | chomp(@App::Multigit::SELECTED_REPOS = <STDIN>); | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | # installs symbol $ref into package $package as symbol name $name. | ||||||||||
| 95 | 0 | ▶ | sub _install_symbol { | ||||||||
|
Subroutine
_install_symbol: not called CC=1 SCAR=1.2
| |||||||||||
| 96 | 0 | my ($ref, $package, $name) = @_; | |||||||||
| 97 | |||||||||||
| 98 | 1 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 99 | 0 | *{ $package . '::' . $name } = $ref; | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 1 | 1; | |||||||||
| 103 | |||||||||||
| 104 | =head1 AUTHOR | ||||||||||
| 105 | |||||||||||
| 106 | Alastair McGowan-Douglas, C<< <altreus at perl.org> >> | ||||||||||
| 107 | |||||||||||
| 108 | =head1 BUGS | ||||||||||
| 109 | |||||||||||
| 110 | Please report bugs on the github repository L<https://github.com/Altreus/App-Multigit>. | ||||||||||
| 111 | |||||||||||
| 112 | =head1 LICENSE | ||||||||||
| 113 | |||||||||||
| 114 | Copyright 2015 Alastair McGowan-Douglas. | ||||||||||
| 115 | |||||||||||
| 116 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 117 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 118 | copy of the full license at: | ||||||||||
| 119 | |||||||||||
| 120 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||