Skip to content

Commit

Permalink
chore(tabler): update tabler to use go (#61)
Browse files Browse the repository at this point in the history
* rm scripts update tools

* update

* ci

* rm toolchain

* update workflow

* workflow

* workflow

* reset workflow
  • Loading branch information
leohhhn authored Oct 4, 2024
1 parent ab3edb9 commit 5287039
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 72 deletions.
17 changes: 11 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ ROWS=15
FILE="README.md"

build:
./scripts/del-existing-rows.sh $(ROWS) $(FILE)
# Delete existing table if it exists
go run ./scripts format --rmtable --readme README.md

go run scripts/generate-table.go -path ./presentations -out scripts/data.csv -rows $(ROWS)
cat scripts/data.csv | go run moul.io/mdtable csv > scripts/table.md
# Generate new table
go run ./scripts -path ./presentations -out ./scripts/data.csv
cat ./scripts/data.csv | go run moul.io/mdtable csv > ./scripts/table.md
go run github.com/campoy/embedmd -w README.md

# Remove codeblocks from embedmd
go run ./scripts format --readme README.md

# Clean up
rm scripts/data.csv scripts/table.md
rm ./scripts/data.csv ./scripts/table.md

./scripts/del-codeblock.sh $(FILE)

lint:
cd ./scripts && go run lint.go
go run ./scripts lint --readme README.md --path ./presentations
53 changes: 36 additions & 17 deletions README.md

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/gnolang/workshops

go 1.22.0

toolchain go1.22.3
go 1.22

require (
github.com/campoy/embedmd v1.0.0
Expand Down
15 changes: 15 additions & 0 deletions presentations/2023-03-13--gophercon_submission--jae/metadata.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Date of the workshop
date: "2023-03-13"
# Title of the workshop
title: "GopherCon Submission"
# GitHub usernames of the speakers
speakers:
- "jaekwon"
# Location of the workshop
location: ""
# At which event the workshop took place, if any
event: ""
# Workshop slides link. If the link is local, only put the file name, without any other path parts.
slides: "README.md"
# Workshop recording
recording: ""
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ location: "Turin, Italy"
# At which event the workshop took place, if any
event: ""
# Workshop slides link. If the link is local, only put the file name, without any other path parts.
slides: "slides.reveal.pdf"
slides: "https://gnolang.github.io/workshops/presentations/2024-09-23--distributed-communities/slides.html"
# Workshop recording
recording: ""
11 changes: 0 additions & 11 deletions scripts/del-codeblock.sh

This file was deleted.

13 changes: 0 additions & 13 deletions scripts/del-existing-rows.sh

This file was deleted.

66 changes: 66 additions & 0 deletions scripts/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"context"
"fmt"
"os"
"strings"

"github.com/gnolang/gno/tm2/pkg/commands"
)

func newFormatCmd(cfg *cfg) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Name: "format",
ShortHelp: "remove codeblocks from the README table",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execFormat(cfg)
},
)
return cmd
}

// execFormat removes an existing table if rmTable is set, and removes the codeblock after table generation happened
func execFormat(cfg *cfg) error {
read, err := os.ReadFile(cfg.readmePath)
if err != nil {
panic(err)
}

cts := string(read)
newContents := ""

// Called to remove the old table
if cfg.rmTable {
lines := strings.Split(cts, "\n")
newLines := make([]string, 0, len(lines))
for _, line := range lines {
if strings.Index(line, "|") != 0 {
newLines = append(newLines, line)
}
}

newContents = strings.Join(newLines, "\n")

} else {
// called to remove the codeblock
newContents = strings.Replace(cts, "```md", "", 1)
newContents = strings.Replace(newContents, "```", "", 1)
}

if cts == newContents {
fmt.Println("Nothing to format.")
return nil
}

err = os.WriteFile(cfg.readmePath, []byte(newContents), 0)
if err != nil {
panic(err)
}

fmt.Println("Formatted README.md")
return nil
}
32 changes: 23 additions & 9 deletions scripts/lint.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package main

import (
"context"
"fmt"
"os"
"slices"
"strings"
)

const (
presFolder = "../presentations"
readme = "../README.md"
"github.com/gnolang/gno/tm2/pkg/commands"
)

func main() {
dirs, err := os.ReadDir(presFolder)
func newLintCmd(cfg *cfg) *commands.Command {
cmd := commands.NewCommand(
commands.Metadata{
Name: "lint",
ShortHelp: "lint the README table",
},
commands.NewEmptyConfig(),
func(_ context.Context, args []string) error {
return execLint(cfg)
},
)
return cmd
}

func execLint(cfg *cfg) error {
dirs, err := os.ReadDir(cfg.presentationsPath)
if err != nil {
panic(err)
}
Expand All @@ -31,18 +43,20 @@ func main() {
slices.Sort(dates)
slices.Reverse(dates)

rawContents, err := os.ReadFile(readme)
rawContents, err := os.ReadFile(cfg.readmePath)
if err != nil {
panic(err)
}

cts := string(rawContents)

for _, date := range dates[:15] {
for _, date := range dates {
if !strings.Contains(cts, date) {
panic("could not find latest item in README table - did you run `make build`?")
panic("could not find some items in README table - did you run `make build`?")
}
}

fmt.Println("All good!")

return nil
}
39 changes: 27 additions & 12 deletions scripts/generate-table.go → scripts/table-tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (

type cfg struct {
presentationsPath string
outputPath string
rows int
csvOutPath string
readmePath string
rmTable bool
}

type Metadata struct {
Expand All @@ -32,6 +33,8 @@ var (
csvHeader = []string{"Date", "Title", "Speakers", "Presentation", "Recording"}
)

const ghLink = "https://github.com/"

func main() {
cfg := &cfg{}

Expand All @@ -45,6 +48,11 @@ func main() {
return execGen(cfg)
})

cmd.AddSubCommands(
newLintCmd(cfg),
newFormatCmd(cfg),
)

cmd.Execute(context.Background(), os.Args[1:])
}

Expand All @@ -56,22 +64,29 @@ func (c *cfg) RegisterFlags(fs *flag.FlagSet) {
"path to dir to walk for presentations files",
)
fs.StringVar(
&c.outputPath,
&c.csvOutPath,
"out",
"./data.csv",
"output csv path, including .csv",
)
fs.IntVar(
&c.rows,
"rows",
15,
"number of rows to generate",
fs.StringVar(
&c.readmePath,
"readme",
"",
"path to the main README",
)

fs.BoolVar(
&c.rmTable,
"rmtable",
false,
"rm the table",
)
}

func execGen(cfg *cfg) error {
searchDir := cfg.presentationsPath
outputCSV := cfg.outputPath // todo check for err
outputCSV := cfg.csvOutPath // todo check for err

// Create the CSV file
csvFile, err := os.Create(outputCSV)
Expand Down Expand Up @@ -137,8 +152,7 @@ func execGen(cfg *cfg) error {
})

// Write sorted rows to the CSV file
// Generate only the last N rows of data
for _, r := range rows[:cfg.rows] {
for _, r := range rows {
err = writer.Write(r.Format())
if err != nil {
return err
Expand Down Expand Up @@ -173,7 +187,8 @@ func (m Metadata) Format() []string {
func (m Metadata) parseSpeakers() string {
var speakers []string
for _, speaker := range m.Speakers {
speakers = append(speakers, "@"+speaker)
speaker = fmt.Sprintf("[@%s](%s%s)", speaker, ghLink, speaker)
speakers = append(speakers, speaker)
}

return strings.Join(speakers, ", ")
Expand Down

0 comments on commit 5287039

Please sign in to comment.