Dist-Zilla-Plugin-SignReleaseNotes

 view release on metacpan or  search on metacpan

lib/Dist/Zilla/Plugin/SignReleaseNotes.pm  view on Meta::CPAN

use strict;
use warnings;

package Dist::Zilla::Plugin::SignReleaseNotes;

our $VERSION = '0.0008';

# ABSTRACT: Create and signs a 'Release' notes file
use Moose;
use Exporter qw(import);
with 'Dist::Zilla::Role::AfterRelease';

has sign => (is => 'ro', default => 'always');
has hash_alg => (is => 'ro', default => 'sha256');

sub do_sign {
  my $self      = shift;
  my $dir       = shift;
  my $plaintext = shift;

  use Module::Signature qw/ $SIGNATURE $Preamble /;

  $SIGNATURE = 'Release' . "-" . $self->get_version();
  $Preamble = '';
  require File::chdir;

  local $File::chdir::CWD = $dir;

  my $signed;
  if (my $version = Module::Signature::_has_gpg()) {
    $signed = Module::Signature::_sign_gpg($SIGNATURE, $plaintext, $version);
  }
  elsif (eval {require Crypt::OpenPGP; 1}) {
    $signed = Module::Signature::_sign_crypt_openpgp($SIGNATURE, $plaintext);
  }
}

sub after_release {
  my $self     = shift;
  my $filename = shift;

  my $digest = $self->get_checksum($filename);

  my @sha1s_and_titles = get_git_checksums_and_titles();

  my $file = $self->create_release_file (
    $digest,
    $filename,
    @sha1s_and_titles
  );

  $self->do_sign('./', $file)
    if $self->sign =~ /always/i;
}

sub get_git_checksums_and_titles {
  my $self     = shift;

  use Git::Wrapper;
  my $git = Git::Wrapper->new('./');

  my @tags = $git->RUN('for-each-ref', 'refs/tags/*', '--sort=-taggerdate', '--count=2', '--format=%(refname:short)');

  my @sha1s_and_titles;

  if ((scalar @tags) lt 2) {
    @sha1s_and_titles = $git->RUN('log', {pretty=>'%h %s' });
  } else {
    my $range = "$tags[1]...$tags[0]";
    @sha1s_and_titles = $git->RUN('log', {pretty=>'%h %s' }, $range);
  }

  return @sha1s_and_titles;

}

sub get_checksum {
  my $self     = shift;
  my $filename = shift;

  use Digest::SHA;
  my $sha = Digest::SHA->new($self->{hash_alg});
  my $digest;
  if ( -e $filename ) {
      open my $fh, '<:raw', $filename  or die "$filename: $!";
      $sha->addfile($fh);
      $digest = $sha->hexdigest;
  }
  return $digest;
}



( run in 0.710 second using v1.01-cache-2.11-cpan-df04353d9ac )