| 1 | # | ||
| 2 | # This file is part of Dist-Zilla-Plugin-Git | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2009 by Jerome Quelin. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | use 5.010; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | package Dist::Zilla::Plugin::Git::Init; | ||
| 14 | # ABSTRACT: Initialize git repository on dzil new | ||
| 15 | |||
| 16 | our $VERSION = '2.052'; | ||
| 17 | |||
| 18 | our %transform = ( | ||
| 19 | lc => sub { lc shift }, | ||
| 20 | uc => sub { uc shift }, | ||
| 21 | '' => sub { shift }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | use Moose; | ||
| 25 | use Git::Wrapper; | ||
| 26 | use String::Formatter method_stringf => { | ||
| 27 | -as => '_format_string', | ||
| 28 | codes => { | ||
| 29 | n => sub { "\n" }, | ||
| 30 | N => sub { $transform{$_[1] || ''}->( $_[0]->zilla->name ) }, | ||
| 31 | }, | ||
| 32 | }; | ||
| 33 | |||
| 34 | use Types::Standard qw(Str Bool ArrayRef); | ||
| 35 | with 'Dist::Zilla::Role::AfterMint'; | ||
| 36 | use namespace::autoclean; | ||
| 37 | |||
| 38 | has commit_message => ( | ||
| 39 | is => 'ro', | ||
| 40 | isa => Str, | ||
| 41 | default => 'initial commit', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has stage => ( | ||
| 45 | is => 'ro', | ||
| 46 | isa => Bool, | ||
| 47 | default => 1, | ||
| 48 | ); | ||
| 49 | |||
| 50 | has commit => ( | ||
| 51 | is => 'ro', | ||
| 52 | isa => Bool, | ||
| 53 | default => 1, | ||
| 54 | ); | ||
| 55 | |||
| 56 | has branch => ( | ||
| 57 | is => 'ro', | ||
| 58 | isa => Str, | ||
| 59 | default => '', | ||
| 60 | ); | ||
| 61 | |||
| 62 | has remotes => ( | ||
| 63 | is => 'ro', | ||
| 64 | isa => ArrayRef[Str], | ||
| 65 | default => sub { [] }, | ||
| 66 | ); | ||
| 67 | |||
| 68 | has push_urls => ( | ||
| 69 | is => 'ro', | ||
| 70 | isa => ArrayRef[Str], | ||
| 71 | default => sub { [] }, | ||
| 72 | ); | ||
| 73 | |||
| 74 | has config_entries => ( | ||
| 75 | is => 'ro', | ||
| 76 | isa => ArrayRef[Str], | ||
| 77 | default => sub { [] }, | ||
| 78 | ); | ||
| 79 | |||
| 80 | sub mvp_multivalue_args { qw(config_entries remotes push_urls) } | ||
| 81 | sub mvp_aliases { return { config => 'config_entries', remote => 'remotes', push_url => 'push_urls' } } | ||
| 82 | |||
| 83 | sub after_mint { | ||
| 84 | my $self = shift; | ||
| 85 | my ($opts) = @_; | ||
| 86 | my $git = Git::Wrapper->new("$opts->{mint_root}"); | ||
| 87 | $self->log("Initializing a new git repository in " . $opts->{mint_root}); | ||
| 88 | $git->init; | ||
| 89 | |||
| 90 | foreach my $configSpec (@{ $self->config_entries }) { | ||
| 91 | my ($option, $value) = split ' ', _format_string($configSpec, $self), 2; | ||
| 92 | $self->log_fatal("invalid syntax for $configSpec!") if not defined $value; | ||
| 93 | $self->log_debug("Configuring $option $value"); | ||
| 94 | $git->config($option, $value); | ||
| 95 | } | ||
| 96 | |||
| 97 | $git->add("$opts->{mint_root}") unless ! $self->stage; | ||
| 98 | if ($self->commit) { | ||
| 99 | my $message = 'Made initial commit'; | ||
| 100 | if (length $self->branch) { | ||
| 101 | $git->checkout('-b', $self->branch); | ||
| 102 | $message .= ' on branch ' . $self->branch; | ||
| 103 | } | ||
| 104 | $git->commit({ | ||
| 105 | message => _format_string($self->commit_message, $self), | ||
| 106 | 'allow-empty' => 1, | ||
| 107 | }); | ||
| 108 | $self->log($message); | ||
| 109 | } | ||
| 110 | |||
| 111 | foreach my $remoteSpec (@{ $self->remotes }) { | ||
| 112 | my ($remote, $url) = split ' ', _format_string($remoteSpec, $self), 2; | ||
| 113 | $self->log_fatal("invalid syntax for $remoteSpec!") if not defined $url; | ||
| 114 | $self->log_debug("Adding remote $remote as $url"); | ||
| 115 | $git->remote(add => $remote, $url); | ||
| 116 | } | ||
| 117 | |||
| 118 | foreach my $remoteSpec (@{ $self->push_urls }) { | ||
| 119 | my ($remote, $url) = split ' ', _format_string($remoteSpec, $self), 2; | ||
| 120 | $self->log_fatal("invalid syntax for $remoteSpec!") if not defined $url; | ||
| 121 | $self->log_debug("Setting push URL for remote $remote to $url"); | ||
| 122 | $git->remote('set-url' => $remote, '--push' => $url); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | |||
| 126 | __PACKAGE__->meta->make_immutable; | ||
| 127 | 1; | ||
| 128 | |||
| 129 | __END__ |