github.com/lestrrat-go/jwx/v2@v2.0.21/tools/autodoc.pl (about)

     1  #!perl
     2  use strict;
     3  use File::Temp;
     4  
     5  # Accept a list of filenames, and process them
     6  # if any of them has a diff, commit it
     7  
     8  # Use GITHUB_REF, but if the ref is develop/v\d, then use v\d
     9  my $link_ref = $ENV{GITHUB_REF};
    10  if ($link_ref =~ /^(?:refs\/heads\/)?develop\/(v\d+)$/) {
    11      $link_ref = $1;
    12  }
    13  
    14  my @files = @ARGV;
    15  my @has_diff;
    16  for my $filename (@files) {
    17      open(my $src, '<', $filename) or die $!;
    18  
    19      my $output = File::Temp->new(SUFFIX => '.md');
    20      my $skip_until_end;
    21      for my $line (<$src>) {
    22          if ($line =~ /^<!-- END INCLUDE -->$/) {
    23              $skip_until_end = 0;
    24          } elsif ($skip_until_end) {
    25              next;
    26          }
    27          if ($line !~ /(^<!-- INCLUDE\(([^\),]+)(?:,([^\)]+))?\) -->)$/) {
    28              $output->print($line);
    29              next;
    30          }
    31          $output->print("$1\n");
    32      
    33          my $include_filename = $2;
    34          my $options = $3;
    35      
    36          $output->print("```go\n");
    37          my $content = do {
    38              open(my $file, '<', $include_filename) or die "failed to include file $include_filename from source file $filename: $!";
    39              local $/;
    40              <$file>;
    41          };
    42          $content =~ s{^(\t+)}{"  " x length($1)}gsme;
    43          $output->print($content);
    44          $output->print("```\n");
    45  
    46          $output->print("source: [$include_filename](https://github.com/lestrrat-go/jwx/blob/$link_ref/$include_filename)\n");
    47      
    48          # now we need to skip copying until the end of INCLUDE
    49          $skip_until_end = 1;
    50      }
    51      $output->close();
    52      close($src);
    53  
    54      if (!$ENV{AUTODOC_DRYRUN}) {
    55          rename $output->filename, $filename or die $!;
    56          my $diff = `git diff $filename`;
    57          if ($diff) {
    58              push @has_diff, $filename;
    59          }
    60      }
    61  }
    62  
    63  if (!$ENV{AUTODOC_DRYRUN}) {
    64      if (@has_diff) {
    65          # Write multi-line commit message in a file
    66          my $commit_message_file = File::Temp->new(SUFFIX => '.txt');
    67          print $commit_message_file "autodoc updates\n\n";
    68          print "  - $_\n" for @has_diff;
    69          $commit_message_file->close();
    70          system("git", "remote", "set-url", "origin", "https://github-actions:$ENV{GITHUB_TOKEN}\@github.com/$ENV{GITHUB_REPOSITORY}") == 0 or die $!;
    71          system("git", "config", "--global", "user.name", "$ENV{GITHUB_ACTOR}") == 0 or die $!;
    72          system("git", "config", "--global", "user.email", "$ENV{GITHUB_ACTOR}\@users.noreply.github.com") == 0 or die $!;
    73          system("git", "switch", "-c", "autodoc-pr-$ENV{GITHUB_HEAD_REF}") == 0 or die $!;
    74          system("git", "commit", "-F", $commit_message_file->filename, @files) == 0 or die $!;
    75          system("git", "push", "origin", "HEAD:autodoc-pr-$ENV{GITHUB_HEAD_REF}") == 0 or die $!;
    76          system("gh", "pr", "create", "--fill") == 0 or die $!;
    77      }
    78  }