| 1 | use 5.008; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package Dist::Zilla::Plugin::SVK::Tag; | ||
| 6 | # ABSTRACT: tag the new version | ||
| 7 | |||
| 8 | use SVK; | ||
| 9 | use SVK::XD; | ||
| 10 | use SVK::Util qw/find_dotsvk/; | ||
| 11 | use List::MoreUtils qw/any/; | ||
| 12 | use File::Basename; | ||
| 13 | |||
| 14 | use Moose; | ||
| 15 | use MooseX::Has::Sugar; | ||
| 16 | use MooseX::Types::Moose qw{ Str }; | ||
| 17 | use String::Formatter method_stringf => { | ||
| 18 | -as => '_format_tag', | ||
| 19 | codes => { | ||
| 20 | d => sub { require DateTime; | ||
| 21 | DateTime->now->format_cldr($_[1] || 'dd-MMM-yyyy') }, | ||
| 22 | n => sub { "\n" }, | ||
| 23 | N => sub { $_[0]->name }, | ||
| 24 | v => sub { $_[0]->version }, | ||
| 25 | }, | ||
| 26 | }; | ||
| 27 | |||
| 28 | with 'Dist::Zilla::Role::BeforeRelease'; | ||
| 29 | with 'Dist::Zilla::Role::AfterRelease'; | ||
| 30 | |||
| 31 | |||
| 32 | # -- attributes | ||
| 33 | |||
| 34 | has tag_format => ( ro, isa=>Str, default => 'v%v' ); | ||
| 35 | has tag_message => ( ro, isa=>Str, default => 'v%v' ); | ||
| 36 | has project => ( ro, isa=>Str ); | ||
| 37 | has tag_directory => ( ro, isa=>Str, default => 'tags' ); | ||
| 38 | |||
| 39 | # -- role implementation | ||
| 40 | |||
| 41 | sub before_release { | ||
| 42 | my $self = shift; | ||
| 43 | my $firstpart = qr|^/([^/]*)|; | ||
| 44 | my $info = qx "svk info"; | ||
| 45 | $info =~ m/^.*\n[^\/]*(\/.*)$/m; my $depotpath = $1; | ||
| 46 | ( my $depotname = $depotpath ) =~ s|$firstpart.*$|$1|; | ||
| 47 | my $project = $self->project || $self->zilla->name; | ||
| 48 | my $project_dir = lc $project; | ||
| 49 | $project_dir =~ s/::/-/g; | ||
| 50 | my $tag_dir = $self->tag_directory; | ||
| 51 | |||
| 52 | # Make sure a tag with the new version doesn't exist yet: | ||
| 53 | my $tag = _format_tag($self->tag_format, $self->zilla); | ||
| 54 | my $output = qx "svk ls /$depotname/$project_dir/$tag_dir"; | ||
| 55 | my @tags = split "\n", $output; | ||
| 56 | $self->log_fatal("tag $tag already exists") if any { m/^$tag/ } @tags; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub after_release { | ||
| 60 | my $self = shift; | ||
| 61 | my $firstpart = qr|^/([^/]*)|; | ||
| 62 | my $info = qx "svk info"; | ||
| 63 | $info =~ m/^.*\n[^\/]*(\/.*)$/m; my $depotpath = $1; | ||
| 64 | ( my $depotname = $depotpath ) =~ s|$firstpart.*$|$1|; | ||
| 65 | my $project = $self->project || $self->zilla->name; | ||
| 66 | my $project_dir = lc $project; | ||
| 67 | $project_dir =~ s/::/-/g; | ||
| 68 | my $tag_dir = $self->tag_directory; | ||
| 69 | |||
| 70 | # create a tag with the new version | ||
| 71 | my $tag = _format_tag($self->tag_format, $self->zilla); | ||
| 72 | my $message = _format_tag($self->tag_message, $self->zilla); | ||
| 73 | my $tagpath = $depotpath; | ||
| 74 | $tagpath = dirname( $tagpath ) until basename( $tagpath ) eq | ||
| 75 | $project_dir or basename( $tagpath ) eq $depotname;; | ||
| 76 | $tagpath .= "/$tag_dir"; | ||
| 77 | system( "svk copy $depotpath $tagpath/$tag -m $message" ); | ||
| 78 | $self->log("Tagged $tag"); | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | |||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =head1 NAME | ||
| 87 | |||
| 88 | Dist::Zilla::Plugin::SVK::Tag - tag the new version | ||
| 89 | |||
| 90 | =head1 VERSION | ||
| 91 | |||
| 92 | version 0.10 | ||
| 93 | |||
| 94 | =head1 SYNOPSIS | ||
| 95 | |||
| 96 | In your F<dist.ini>: | ||
| 97 | |||
| 98 | [SVK::Tag] | ||
| 99 | tag_format = v%v ; this is the default | ||
| 100 | tag_message = v%v ; this is the default | ||
| 101 | project = someid ; the default is lc $dzilla->name, | ||
| 102 | tag_directory = tags ; the default is 'tags', as in /$project/tags | ||
| 103 | |||
| 104 | =head1 DESCRIPTION | ||
| 105 | |||
| 106 | Once the release is done, this plugin will record this fact by creating a tag of the present branch. You can set the C<tag_message> attribute to change the message. | ||
| 107 | |||
| 108 | It also checks before the release to ensure the tag to be created doesn't already exist. (You would have to manually delete the existing tag before you could release the same version again, but that is almost never a good idea.) | ||
| 109 | |||
| 110 | The plugin accepts the following options: | ||
| 111 | |||
| 112 | =over 4 | ||
| 113 | |||
| 114 | =item * tag_format - format of the tag to apply. Defaults to C<v%v>. | ||
| 115 | |||
| 116 | =item * tag_message - format of the commit message. Defaults to C<v%v>. | ||
| 117 | |||
| 118 | =item * project - the project directory, below which typically are 'trunk', 'branches' and 'tags' subdirectories. Defaults to C<$dzilla->name>, lowercased. | ||
| 119 | |||
| 120 | =item * tag_directory - location of the tags directory, below the project directory. Defaults to C<tags>. | ||
| 121 | |||
| 122 | =back | ||
| 123 | |||
| 124 | You can use the following codes in both options: | ||
| 125 | |||
| 126 | =over 4 | ||
| 127 | |||
| 128 | =item C<%{dd-MMM-yyyy}d> | ||
| 129 | |||
| 130 | The current date. You can use any CLDR format supported by | ||
| 131 | L<DateTime>. A bare C<%d> means C<%{dd-MMM-yyyy}d>. | ||
| 132 | |||
| 133 | =item C<%n> | ||
| 134 | |||
| 135 | a newline | ||
| 136 | |||
| 137 | =item C<%N> | ||
| 138 | |||
| 139 | the distribution name | ||
| 140 | |||
| 141 | =item C<%v> | ||
| 142 | |||
| 143 | the distribution version | ||
| 144 | |||
| 145 | =back | ||
| 146 | |||
| 147 | -- role implementation | ||
| 148 | |||
| 149 | =over 4 | ||
| 150 | |||
| 151 | =item before_release | ||
| 152 | |||
| 153 | Depotpath from second line of 'svk info'. Depotname from after first slash. Project from dist.ini, is directory under depotname. | ||
| 154 | |||
| 155 | =back | ||
| 156 | |||
| 157 | =for Pod::Coverage after_release | ||
| 158 | before_release | ||
| 159 | |||
| 160 | =head1 AUTHOR | ||
| 161 | |||
| 162 | Dr Bean <drbean at (a) cpan dot (.) org> | ||
| 163 | |||
| 164 | =head1 COPYRIGHT AND LICENSE | ||
| 165 | |||
| 166 | This software is copyright (c) 2010 by Dr Bean. | ||
| 167 | |||
| 168 | This is free software; you can redistribute it and/or modify it under | ||
| 169 | the same terms as the Perl 5 programming language system itself. | ||
| 170 | |||
| 171 | =cut | ||
| 172 | |||
| 173 | |||
| 174 | __END__ |