| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use 5.024; | ||
| 4 | use feature qw/signatures postderef/; | ||
| 5 | no warnings qw/experimental uninitialized/; | ||
| 6 | use utf8::all; | ||
| 7 | use Try::Tiny; | ||
| 8 | use Data::Printer; | ||
| 9 | |||
| 10 | =pod | ||
| 11 | |||
| 12 | =head1 buildpod.pl | ||
| 13 | |||
| 14 | This is a utility script for the Vote::Count Distribution. | ||
| 15 | Method Documentation is being written conventionally as inline POD, | ||
| 16 | Documentation files are being written in MarkDown. This utility | ||
| 17 | will convert markdown files to pod files, and insert pod into modules | ||
| 18 | that have a markdown file. The insertion will be between lines that | ||
| 19 | have #buildpod comments. | ||
| 20 | |||
| 21 | As an added convenience buildpod will read the version from dist.ini and | ||
| 22 | replace the version strings in modules. | ||
| 23 | |||
| 24 | Someday I may make a Dist::Zilla plugin out of this. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | ./buildpod.pl | ||
| 29 | |||
| 30 | ./buildpod.pl fixfooter | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | |||
| 35 | # use Carp::Always; | ||
| 36 | use Carp; | ||
| 37 | use Path::Tiny; | ||
| 38 | use Markdown::Pod;; | ||
| 39 | # use Time::Piece; | ||
| 40 | # use Time::Moment; | ||
| 41 | # use Search::Tools::UTF8; | ||
| 42 | # use Unicode::Normalize; | ||
| 43 | # use List::Util qw(max); | ||
| 44 | # use Unicode::Collate::Locale; | ||
| 45 | # use feature 'unicode_strings'; | ||
| 46 | # use Encode qw (from_to decode_utf8 encode_utf8 decode encode); | ||
| 47 | # use Cpanel::JSON::XS; # qw( encode_json decode_json ); | ||
| 48 | use Data::Printer; | ||
| 49 | # use Data::Dumper; | ||
| 50 | # use charnames ':full'; | ||
| 51 | # use Unicode::UCD 'charinfo'; | ||
| 52 | # use Test::More; | ||
| 53 | # use Getopt::Long::Descriptive; | ||
| 54 | |||
| 55 | our $VERSION='2021.0427'; | ||
| 56 | |||
| 57 | my $m2p = Markdown::Pod->new; | ||
| 58 | |||
| 59 | sub fix_version ( $text, $version ) { | ||
| 60 | $text =~ s/our \$VERSION(| )=.*;/our \$VERSION='$version';/g; | ||
| 61 | $text =~ s/=head1 VERSION.*\n/=head1 VERSION $version\n/; | ||
| 62 | return $text; | ||
| 63 | } | ||
| 64 | |||
| 65 | # sub add_pod ( $text, $markdown ) { | ||
| 66 | # my $pod = $m2p->markdown_to_pod( | ||
| 67 | # markdown => $markdown, | ||
| 68 | # ); | ||
| 69 | # my $markerstr = '#buildpod'; | ||
| 70 | # my $num_markers = () = $text =~ /$markerstr/g; | ||
| 71 | # say "Counted Markers: $num_markers"; | ||
| 72 | # return $text unless $num_markers; | ||
| 73 | # if ( $num_markers >2 ) { | ||
| 74 | # die "There are too many $markerstr markers in the current file\\n" | ||
| 75 | # } | ||
| 76 | # my @beforepod = (); | ||
| 77 | # my @afterpod = (); | ||
| 78 | # my $aftermarker = 0; | ||
| 79 | # for my $l ( split /\n/, $text ) { | ||
| 80 | # if ($aftermarker == $num_markers ) { push @afterpod, $l } | ||
| 81 | # elsif ($aftermarker == 1 ) { | ||
| 82 | # $aftermarker++ if $l =~ /$markerstr/; | ||
| 83 | # } else { | ||
| 84 | # if ( $l =~ /$markerstr/ ) { | ||
| 85 | # $aftermarker++ if $l =~ /$markerstr/; | ||
| 86 | # } else { | ||
| 87 | # push @beforepod, $l; | ||
| 88 | # } | ||
| 89 | # } | ||
| 90 | # } | ||
| 91 | # return join( "\n", | ||
| 92 | # ( @beforepod, $markerstr, "\n=pod\n$pod\n=cut\n", $markerstr, @afterpod ) | ||
| 93 | # ) | ||
| 94 | # } | ||
| 95 | |||
| 96 | my $footer = <<'FOOTER'; | ||
| 97 | |||
| 98 | #FOOTER | ||
| 99 | |||
| 100 | =pod | ||
| 101 | |||
| 102 | BUG TRACKER | ||
| 103 | |||
| 104 | L<https://github.com/brainbuz/Vote-Count/issues> | ||
| 105 | |||
| 106 | AUTHOR | ||
| 107 | |||
| 108 | John Karr (BRAINBUZ) brainbuz@cpan.org | ||
| 109 | |||
| 110 | CONTRIBUTORS | ||
| 111 | |||
| 112 | Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org. | ||
| 113 | |||
| 114 | LICENSE | ||
| 115 | |||
| 116 | This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>. | ||
| 117 | |||
| 118 | SUPPORT | ||
| 119 | |||
| 120 | This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | FOOTER | ||
| 125 | |||
| 126 | my $fixfooter = 0; | ||
| 127 | for (@ARGV) { | ||
| 128 | $fixfooter = 1 if $_ eq 'fixfooter'; | ||
| 129 | } | ||
| 130 | |||
| 131 | my $dist = path( './dist.ini')->slurp; | ||
| 132 | $dist =~ /version\s? =\s?(\d+\.\d+)/; | ||
| 133 | my $version = $1; | ||
| 134 | |||
| 135 | # my @mdfiles = path(".")->children( qr/md$/ ); | ||
| 136 | |||
| 137 | my @pmfiles1 = path("./lib/Vote/Count")->children( qr/pm$|pod$/); | ||
| 138 | my @pmfiles2 = path("./lib/Vote/Count/Method")->children( qr/pm$/); | ||
| 139 | my @pmfiles3 = path("./lib/Vote/Count/Charge")->children( qr/pm$/); | ||
| 140 | my @pmfiles4 = path("./lib/Vote/Count/Helper")->children( qr/pm$/); | ||
| 141 | my $countpm = path( "./lib/Vote/Count.pm"); | ||
| 142 | my @pmfiles = ( @pmfiles1, @pmfiles2, @pmfiles3, @pmfiles4,$countpm); | ||
| 143 | my %pmkeys = (); | ||
| 144 | for my $pm (@pmfiles ) { | ||
| 145 | next if $pm =~ /TextTableTiny/; | ||
| 146 | $pm =~ /(.*)\.(pm$|pod$)/; # extract the part of the string before .pm | ||
| 147 | my @bits = split /\//, $1; # split extracted on /, llast bit is basename | ||
| 148 | $pmkeys{ $bits[-1] } = $pm ; # put the path object in the hash keyed on the basename. | ||
| 149 | } | ||
| 150 | # FORMD: | ||
| 151 | # for my $md ( @mdfiles ) { | ||
| 152 | # my $name = $md->basename; | ||
| 153 | # say "ORI base name $name"; | ||
| 154 | # $name =~ s/\.md$//; | ||
| 155 | # say "base name $name"; | ||
| 156 | # if ( $pmkeys{ $name }) { | ||
| 157 | # my $pm = delete $pmkeys{ $name }; # remove from keys, so we dont repeat it later. | ||
| 158 | # my $mdtext = path($md)->slurp(); | ||
| 159 | # my $pmtext = path($pm)->slurp(); | ||
| 160 | # $pmtext = add_pod( $pmtext, $mdtext ); | ||
| 161 | # $pmtext = fix_version( $pmtext, $version); | ||
| 162 | # $pm->spew( $pmtext ); | ||
| 163 | # say "updated $pm added pod from $md"; | ||
| 164 | # } else { | ||
| 165 | # next FORMD if $md =~ /README/ ; | ||
| 166 | # my $mdtext = try { path($md)->slurp(); } | ||
| 167 | # catch { warn "failed to slurp $md"}; | ||
| 168 | # $mdtext =~ s/(\# ABSTRACT.*)\n//; | ||
| 169 | # my $abstract = $1; | ||
| 170 | # my $pod = $m2p->markdown_to_pod( markdown => $mdtext ); | ||
| 171 | # my $versionline = "=head1 VERSION $version"; | ||
| 172 | # warn $versionline; | ||
| 173 | # # $pod = "$abstract\n$versionline\n$pod\n$footer"; | ||
| 174 | # $pod = "$abstract\n\n=pod\n\n$versionline\n$pod\n$footer\n=cut\n"; | ||
| 175 | # path( "./lib/Vote/$name.pod")->spew($pod); | ||
| 176 | # } | ||
| 177 | |||
| 178 | sub updateCountIndex { | ||
| 179 | my @modules = sort map { | ||
| 180 | my $p = $_->canonpath ; | ||
| 181 | my $d = path( $p)->slurp(); | ||
| 182 | $d =~ /head1 NAME\s+(.*)/i; | ||
| 183 | my $n = $1; | ||
| 184 | unless ( $n ) { | ||
| 185 | $n = $_->basename; | ||
| 186 | } | ||
| 187 | $n; | ||
| 188 | } @pmfiles; | ||
| 189 | my $countmod = path('lib/Vote/Count.pm')->slurp(); | ||
| 190 | my $index = join("\n", map {"=item *\n\nL<$_>\n"} @modules); | ||
| 191 | # p @modules; | ||
| 192 | # $countmod =~ /(?s)#BEGININDEX(.*)/; | ||
| 193 | my ( $part1, $part2, $part3 ) = split( /#INDEXSECTION|#FOOTER/, $countmod ); | ||
| 194 | |||
| 195 | $countmod = qq|$part1\n | ||
| 196 | #INDEXSECTION\n | ||
| 197 | =pod\n | ||
| 198 | =head1 INDEX of Vote::Count Modules and Documentation\n | ||
| 199 | =over\n | ||
| 200 | $index\n | ||
| 201 | =back\n | ||
| 202 | =cut\n | ||
| 203 | #FOOTER\n | ||
| 204 | $part3|; | ||
| 205 | $countmod =~ s/\n\n+/\n\n/g; | ||
| 206 | |||
| 207 | path('lib/Vote/Count.pm')->spew($countmod); | ||
| 208 | } | ||
| 209 | |||
| 210 | updateCountIndex(); | ||
| 211 | |||
| 212 | for my $pm ( values @pmfiles ) { | ||
| 213 | # for my $pm ( values %pmkeys ) { | ||
| 214 | warn "fixing $pm" ; | ||
| 215 | my $pmtext = path($pm)->slurp; | ||
| 216 | unless ($pmtext =~ /\#FOOTER/) { $pmtext .= $footer ; } | ||
| 217 | elsif ( $fixfooter ) { | ||
| 218 | say "updateding footer $pm" ; | ||
| 219 | # $pmtext =~ s/(?s)\#FOOTER.*/$footer/; | ||
| 220 | my $splitstr = "#FOOTER\n"; | ||
| 221 | my ($body, @trim) = split /$splitstr/, $pmtext; | ||
| 222 | $pmtext = $body . $footer; | ||
| 223 | } | ||
| 224 | $pmtext =~ s/\n\n\n+/\n\n/g; | ||
| 225 | path($pm)->spew( fix_version( $pmtext, $version) ); | ||
| 226 | say "updated version in $pm"; | ||
| 227 | } |