| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use FindBin; | ||
| 6 | use File::Spec; | ||
| 7 | use Cwd qw(abs_path); | ||
| 8 | |||
| 9 | my $SELF = abs_path($FindBin::Bin); | ||
| 10 | my $ROOT = abs_path(File::Spec->catdir($SELF, '..', '..')); | ||
| 11 | |||
| 12 | sub read_text { | ||
| 13 | my ($path) = @_; | ||
| 14 | open my $fh, '<:raw', $path or die "read $path: $!\n"; | ||
| 15 | local $/; | ||
| 16 | my $text = <$fh>; | ||
| 17 | close $fh; | ||
| 18 | return $text; | ||
| 19 | } | ||
| 20 | |||
| 21 | my $version = read_text(File::Spec->catfile($SELF, 'VERSION')); | ||
| 22 | $version =~ s/^\s+|\s+$//g; | ||
| 23 | my $spec = read_text(File::Spec->catfile($ROOT, 'spec', 'SPEC_VERSION')); | ||
| 24 | $spec =~ s/^\s+|\s+$//g; | ||
| 25 | |||
| 26 | my @entries; | ||
| 27 | for my $path (sort glob(File::Spec->catfile($SELF, 'lib', 'Praxo', '*.pm'))) { | ||
| 28 | my $text = read_text($path); | ||
| 29 | my $package = 'main'; | ||
| 30 | for my $line (split /\n/, $text) { | ||
| 31 | $package = $1 if $line =~ /^package\s+([A-Za-z0-9_:]+)/; | ||
| 32 | if ($line =~ /^sub\s+([A-Za-z_][A-Za-z0-9_]*)/) { | ||
| 33 | next if $1 =~ /^_/; | ||
| 34 | push @entries, "$package\::$1"; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | } | ||
| 38 | @entries = sort @entries; | ||
| 39 | my %seen; | ||
| 40 | @entries = grep { !$seen{$_}++ } @entries; | ||
| 41 | |||
| 42 | my $content = join "\n", | ||
| 43 | "# PRAXO-CORE Perl API $version", | ||
| 44 | "# SPEC_VERSION $spec", | ||
| 45 | "", | ||
| 46 | @entries, | ||
| 47 | ""; | ||
| 48 | |||
| 49 | my $check = grep { $_ eq '--check' } @ARGV; | ||
| 50 | my $target = File::Spec->catfile($SELF, 'api', "$version.txt"); | ||
| 51 | if ($check) { | ||
| 52 | if (!-f $target) { | ||
| 53 | print STDERR "missing snapshot: $target\n"; | ||
| 54 | exit 1; | ||
| 55 | } | ||
| 56 | if (read_text($target) ne $content) { | ||
| 57 | print STDERR "snapshot is stale: $target\n"; | ||
| 58 | exit 1; | ||
| 59 | } | ||
| 60 | print "snapshot is current: $target\n"; | ||
| 61 | } else { | ||
| 62 | my $dir = File::Spec->catdir($SELF, 'api'); | ||
| 63 | mkdir $dir unless -d $dir; | ||
| 64 | open my $fh, '>:raw', $target or die "write $target: $!\n"; | ||
| 65 | print {$fh} $content; | ||
| 66 | close $fh; | ||
| 67 | print "wrote $target\n"; | ||
| 68 | } |