Skip to content

Commit

Permalink
stuff for generating docs
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Mar 22, 2022
1 parent b511258 commit 25f091c
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .github/workflows/autodoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Auto-Doc
on:
pull_request:
branches:
- main
types:
- closed

jobs:
autodoc:
runs-on: ubuntu-latest
name: "Run commands to generate documentation"
if: github.event.pull_request.merged == true
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Process markdown files
run: |
find . -name '*.md' | xargs perl tools/autodoc.pl
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Check documentation generator
run: |
find . -name '*.md' | xargs env AUTODOC_DRYRUN=1 perl tools/autodoc.pl
- name: Install Go stable version
uses: actions/setup-go@v2
with:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
can be fetched via HTTP, but keep the cached content up-to-date based on periodic
refreshing.

# SYNOPSIS

<!-- INCLUDE(httprc_example_test.go) -->
<!-- END INCLUDE -->

# Sequence Diagram

```mermaid
Expand Down
76 changes: 76 additions & 0 deletions httprc_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package httprc_test

import (
"context"
"fmt"
"log"
"net/http"
"net/http/httptest"
"sync"
"time"

"github.com/lestrrat-go/httprc"
)

func Example() {
var mu sync.RWMutex

msg := `Hello World!`

srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(`Cache-Control`, fmt.Sprintf(`max-age=%d`, 3))
w.WriteHeader(http.StatusOK)
mu.RLock()
fmt.Fprintf(w, msg)
mu.RUnlock()
}))
defer srv.Close()

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

errSink := httprc.ErrSinkFunc(func(err error) {
log.Printf("%s", err)
})

c := httprc.New(ctx,
httprc.WithErrSink(errSink),
httprc.WithRefreshWindow(time.Second), // force checks every second
)

c.Register(srv.URL, httprc.WithHTTPClient(srv.Client()))

payload, err := c.Get(ctx, srv.URL)
if err != nil {
log.Printf("%s", err)
return
}

//nolint:forcetypeassert
if string(payload.([]byte)) != `Hello World!` {
log.Printf("payload mismatch: %s", payload)
return
}

mu.Lock()
msg = `Goodbye World!`
mu.Unlock()

time.Sleep(4 * time.Second)

payload, err = c.Get(ctx, srv.URL)
if err != nil {
log.Printf("%s", err)
return
}

//nolint:forcetypeassert
if string(payload.([]byte)) != `Hello World!` {
log.Printf("payload mismatch: %s", payload)
return
}

cancel()

// OUTPUT:
}
2 changes: 1 addition & 1 deletion httprc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCache(t *testing.T) {
defer cancel()

var called int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-ctx.Done():
return
Expand Down
6 changes: 6 additions & 0 deletions queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ type ErrSink interface {
Error(error)
}

type ErrSinkFunc func(err error)

func (f ErrSinkFunc) Error(err error) {
f(err)
}

// Transformer is responsible for converting an HTTP response
// into an appropriate form of your choosing.
type Transformer interface {
Expand Down
69 changes: 69 additions & 0 deletions tools/autodoc.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!perl
use strict;
use File::Temp;

# Accept a list of filenames, and process them
# if any of them has a diff, commit it

my @files = @ARGV;
my @has_diff;
for my $filename (@files) {
open(my $src, '<', $filename) or die $!;

my $output = File::Temp->new(SUFFIX => '.md');
my $skip_until_end;
for my $line (<$src>) {
if ($line =~ /^<!-- END INCLUDE -->$/) {
$skip_until_end = 0;
} elsif ($skip_until_end) {
next;
}
if ($line !~ /(^<!-- INCLUDE\(([^\),]+)(?:,([^\)]+))?\) -->)$/) {
$output->print($line);
next;
}
$output->print("$1\n");

my $include_filename = $2;
my $options = $3;

$output->print("```go\n");
my $content = do {
open(my $file, '<', $include_filename) or die "failed to include file $include_filename from source file $filename: $!";
local $/;
<$file>;
};
$content =~ s{^(\t+)}{" " x length($1)}gsme;
$output->print($content);
$output->print("```\n");
$output->print("source: [$include_filename](https://github.com/lestrrat-go/jwx/blob/$ENV{GITHUB_REF}/$include_filename)\n");

# now we need to skip copying until the end of INCLUDE
$skip_until_end = 1;
}
$output->close();
close($src);

if (!$ENV{AUTODOC_DRYRUN}) {
rename $output->filename, $filename or die $!;
my $diff = `git diff $filename`;
if ($diff) {
push @has_diff, $filename;
}
}
}

if (!$ENV{AUTODOC_DRYRUN}) {
if (@has_diff) {
# Write multi-line commit message in a file
my $commit_message_file = File::Temp->new(SUFFIX => '.txt');
print $commit_message_file "autodoc updates\n\n";
print " - $_\n" for @has_diff;
$commit_message_file->close();
system("git", "remote", "set-url", "origin", "https://github-actions:$ENV{GITHUB_TOKEN}\@github.com/$ENV{GITHUB_REPOSITORY}") == 0 or die $!;
system("git", "config", "--global", "user.name", "$ENV{GITHUB_ACTOR}") == 0 or die $!;
system("git", "config", "--global", "user.email", "$ENV{GITHUB_ACTOR}\@users.noreply.github.com") == 0 or die $!;
system("git", "commit", "-F", $commit_message_file->filename, @files) == 0 or die $!;
system("git", "push", "origin", "HEAD:$ENV{GITHUB_REF}") == 0 or die $!;
}
}

0 comments on commit 25f091c

Please sign in to comment.