LCFG-Build-Tools

 view release on metacpan or  search on metacpan

lib/LCFG/Build/Utils/RPM.pm  view on Meta::CPAN

        $logfile = File::Spec->catfile( $dir, $logfile );
        if ( -f $logfile ) {

            my $changelog = format_changelog($logfile);
            $extra->{LCFG_CHANGELOG} = $changelog;
        }
    }

    my $specname = $pkgspec->rpmspec_name;
    my $output   = File::Spec->catfile( $outdir, $specname );

    LCFG::Build::Utils::translate_file( $pkgspec, $specfile,
                                        $output,
                                        $extra );

    # Do this so the generated tar-file contains a usable specfile

    File::Copy::copy( $output, $specfile );

    return;
}

sub format_changelog {
  my ($file) = @_;

  my @entries = parse_changelog($file);

  if ( scalar @entries == 0 ) {
    my $dt = DateTime->now();
    my $entry = {
      year  => $dt->year,
      month => $dt->month,
      day   => $dt->day,
    };
    return format_entry($entry);
  }

  my $changelog = q{};
  for my $entry (@entries) {
    $changelog .= format_entry($entry);
  }

  return $changelog;
}

sub format_entry {
  my ($entry) = @_;

  my $dt = eval { DateTime->new( year  => $entry->{year},
                                 month => $entry->{month},
                                 day   => $entry->{day} ) };

  if ( $EVAL_ERROR || !defined $dt ) {
    return q{};
  }

  my $formatted_date = $dt->strftime('%a %b %d %Y');

  my $title = $entry->{title};
  if ( !defined $title ) {
    $title = $ENV{EMAIL} || getpwuid $UID;
  }

  if ( $title =~ /\s*cvs:\s*new release/i && defined $entry->{release} ) {
    $title = "<<<< Release: $entry->{release} >>>>";
  }

  my $output = q{* } . $formatted_date . q{ } . $title . "\n";

  my @body;
  if ( defined $entry->{body} ) {
    @body = @{$entry->{body}};
  }

  if ( scalar @body == 0 ) {
    push @body, 'No release information available';
  }

  for my $item (@body) {
    $output .= Text::Wrap::wrap( '- ', '  ', $item ) . "\n";
  }

  $output .= "\n";

  return $output;
}

sub parse_changelog {
  my ($file) = @_;

  my @data;
  if ( !-f $file || -z $file ) {
    return @data;
  }

  my $fh = IO::File->new( $file, 'r' )
    or die "Could not open file '$file': $OS_ERROR\n";

  my $current;
  while ( defined( my $line = <$fh> ) ) {
    chomp $line;

    if ( $line =~ m/^\s*$/ ) {
      next;
    } elsif ( $line =~ m/^(\d+)-(\d+)-(\d+)\s*(.*)$/ ) {
      $current = $data[$#data + 1] = { year  => $1,
                                       month => $2,
                                       day   => $3,
                                       title => $4,
                                       body  => [] };
    } else {
      $line =~ s/^\s+//;
      $line =~ s/\s+$//;

      my $body = $current->{body};
      if ( $line =~ m/^\*\s*(.+)/ ) {
        my $entry = $1;
        if ( $entry =~ m/^release:\s*(.+)$/i ) {
          $current->{release} = $1;
        }



( run in 0.613 second using v1.01-cache-2.11-cpan-39bf76dae61 )