Skip to content

Commit

Permalink
Automatically generate apps listing in README
Browse files Browse the repository at this point in the history
  • Loading branch information
xarantolus committed Oct 12, 2021
1 parent 595e1a1 commit 58a335c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ This repository hosts an [F-Droid](https://f-droid.org/) repo for my apps. This
3. Open the link in F-Droid. It will ask you to add the repository. Everything should already be filled in correctly, so just press "OK".
4. You can now install my apps, e.g. start by searching for "Notality" in the F-Droid client.
### Apps
<!-- This table is auto-generated. Do not edit -->
<!-- end apps table -->
### For developers
If you are a developer and want to publish your own apps right from GitHub Actions as an F-Droid repo, you can fork/copy this repo and see [the documentation](setup.md) for more information on how to set it up.
Expand Down
8 changes: 8 additions & 0 deletions metascoop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"metascoop/apps"
"metascoop/file"
"metascoop/git"
"metascoop/md"
"net/http"
"os"
"os/exec"
Expand Down Expand Up @@ -385,6 +386,13 @@ func main() {
}
}

// We can now generate the README file
readmePath := filepath.Join(filepath.Dir(filepath.Dir(*repoDir)), "README.md")
err = md.RegenerateReadme(readmePath, fdroidIndex)
if err != nil {
log.Fatalf("error generating %q: %s\n", readmePath, err.Error())
}

cpath, haveSignificantChanges := apps.HasSignificantChanges(initialFdroidIndex, fdroidIndex)
if haveSignificantChanges {
log.Printf("The index %q had a significant change at JSON path %q", fdroidIndexFilePath, cpath)
Expand Down
57 changes: 57 additions & 0 deletions metascoop/md/generate_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package md

import (
"bytes"
"fmt"
"html/template"
"metascoop/apps"
"os"
)

const (
tableStart = "<!-- This table is auto-generated. Do not edit -->"

tableEnd = "<!-- end apps table -->"

tableTmpl = `
| Icon | Name | Description | Version |
| --- | --- | --- | --- |{{range .Apps}}
| <a href="{{.sourceCode}}"><img src="fdroid/repo/icons/{{.icon}}" alt="{{.name}} icon" width="36px" height="36px"></a> | [**{{.name}}**]({{.sourceCode}}) | {{.summary}} | {{.suggestedVersionName}} ({{.suggestedVersionCode}}) |{{end}}
` + tableEnd
)

var tmpl = template.Must(template.New("").Parse(tableTmpl))

func RegenerateReadme(readMePath string, index *apps.RepoIndex) (err error) {
content, err := os.ReadFile(readMePath)
if err != nil {
return
}

var tableStartIndex = bytes.Index(content, []byte(tableStart))
if tableStartIndex < 0 {
return fmt.Errorf("cannot find table start in %q", readMePath)
}

var tableEndIndex = bytes.Index(content, []byte(tableEnd))
if tableEndIndex < 0 {
return fmt.Errorf("cannot find table end in %q", readMePath)
}

var table bytes.Buffer

table.WriteString(tableStart)

err = tmpl.Execute(&table, index)
if err != nil {
return err
}

newContent := []byte{}

newContent = append(newContent, content[:tableStartIndex]...)
newContent = append(newContent, table.Bytes()...)
newContent = append(newContent, content[tableEndIndex:]...)

return os.WriteFile(readMePath, newContent, os.ModePerm)
}

0 comments on commit 58a335c

Please sign in to comment.