Skip to content

Commit

Permalink
Updating gen.go to also update dburl documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Apr 12, 2023
1 parent fddc7d7 commit 5e07f38
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions gen.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build tools
//go:build ignore

package main

Expand All @@ -11,7 +11,6 @@ import (
"go/parser"
"go/token"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -21,6 +20,7 @@ import (

"github.com/mattn/go-runewidth"
"github.com/xo/dburl"
"github.com/yookoala/realpath"
)

type DriverInfo struct {
Expand Down Expand Up @@ -66,14 +66,17 @@ var wireDrivers = map[string]DriverInfo{}
func main() {
licenseStart := flag.Int("license-start", 2016, "license start year")
licenseAuthor := flag.String("license-author", "Kenneth Shaw", "license author")
dburlGen := flag.Bool("dburl-gen", false, "enable dburl generation")
dburlDir := flag.String("dburl-dir", getDburlDir(), "dburl dir")
dburlLicenseStart := flag.Int("dburl-license-start", 2015, "dburl license start year")
flag.Parse()
if err := run(*licenseStart, *licenseAuthor); err != nil {
if err := run(*licenseStart, *licenseAuthor, *dburlGen, *dburlDir, *dburlLicenseStart); err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

func run(licenseStart int, licenseAuthor string) error {
func run(licenseStart int, licenseAuthor string, dburlGen bool, dburlDir string, dburlLicenseStart int) error {
wd, err := os.Getwd()
if err != nil {
return err
Expand All @@ -84,15 +87,32 @@ func run(licenseStart int, licenseAuthor string) error {
if err := writeInternal(filepath.Join(wd, "internal"), baseDrivers, mostDrivers, allDrivers, badDrivers); err != nil {
return err
}
if err := writeReadme(wd); err != nil {
if err := writeReadme(wd, true); err != nil {
return err
}
if err := writeLicenseFiles(licenseStart, licenseAuthor); err != nil {
return err
}
if dburlGen {
if err := writeReadme(dburlDir, false); err != nil {
return err
}
if err := writeDburlLicense(dburlDir, dburlLicenseStart, licenseAuthor); err != nil {
return err
}
}
return nil
}

func getDburlDir() string {
dir := filepath.Join(os.Getenv("GOPATH"), "src/github.com/xo/dburl")
var err error
if dir, err = realpath.Realpath(dir); err != nil {
panic(err)
}
return dir
}

var dirRE = regexp.MustCompile(`^([^/]+)/([^\./]+)\.go$`)

// loadDrivers loads the driver descriptions.
Expand Down Expand Up @@ -235,7 +255,7 @@ func writeInternal(wd string, drivers ...map[string]DriverInfo) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(wd, "internal.go"), buf, 0644); err != nil {
if err := os.WriteFile(filepath.Join(wd, "internal.go"), buf, 0o644); err != nil {
return err
}
// write <tag>.go
Expand All @@ -258,7 +278,7 @@ func writeInternal(wd string, drivers ...map[string]DriverInfo) error {
if err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(wd, v.Tag+".go"), buf, 0644); err != nil {
if err := os.WriteFile(filepath.Join(wd, v.Tag+".go"), buf, 0o644); err != nil {
return err
}
}
Expand Down Expand Up @@ -292,9 +312,9 @@ const (
driverTableEnd = "<!-- DRIVER DETAILS END -->"
)

func writeReadme(wd string) error {
readme := filepath.Join(wd, "README.md")
buf, err := ioutil.ReadFile(readme)
func writeReadme(dir string, includeTagSummary bool) error {
readme := filepath.Join(dir, "README.md")
buf, err := os.ReadFile(readme)
if err != nil {
return err
}
Expand All @@ -307,16 +327,16 @@ func writeReadme(wd string) error {
if _, err := b.Write(append(buf[:start+len(driverTableStart)], '\n')); err != nil {
return err
}
if _, err := b.Write([]byte(buildDriverTable())); err != nil {
if _, err := b.Write([]byte(buildDriverTable(includeTagSummary))); err != nil {
return err
}
if _, err := b.Write(buf[end:]); err != nil {
return err
}
return ioutil.WriteFile(readme, b.Bytes(), 0644)
return os.WriteFile(readme, b.Bytes(), 0o644)
}

func buildDriverTable() string {
func buildDriverTable(includeTagSummary bool) string {
hdr := []string{"Database", "Scheme / Tag", "Scheme Aliases", "Driver Package / Notes"}
widths := []int{len(hdr[0]), len(hdr[1]), len(hdr[2]), len(hdr[3])}
baseRows, widths := buildRows(baseDrivers, widths)
Expand All @@ -335,14 +355,16 @@ func buildDriverTable() string {
s += tableRows(widths, ' ', wireRows...)
s += tableRows(widths, ' ')
s += tableRows(widths, ' ', badRows...)
s += tableRows(widths, ' ')
s += tableRows(widths, ' ',
[]string{"**NO DRIVERS**", "`no_base`", "", "_no base drivers (useful for development)_"},
[]string{"**MOST DRIVERS**", "`most`", "", "_all stable drivers_"},
[]string{"**ALL DRIVERS**", "`all`", "", "_all drivers, excluding bad drivers_"},
[]string{"**BAD DRIVERS**", "`bad`", "", "_bad drivers (broken/non-working drivers)_"},
[]string{"**NO &lt;TAG&gt;**", "`no_<tag>`", "", "_exclude driver with `<tag>`_"},
)
if includeTagSummary {
s += tableRows(widths, ' ')
s += tableRows(widths, ' ',
[]string{"**NO DRIVERS**", "`no_base`", "", "_no base drivers (useful for development)_"},
[]string{"**MOST DRIVERS**", "`most`", "", "_all stable drivers_"},
[]string{"**ALL DRIVERS**", "`all`", "", "_all drivers, excluding bad drivers_"},
[]string{"**BAD DRIVERS**", "`bad`", "", "_bad drivers (broken/non-working drivers)_"},
[]string{"**NO &lt;TAG&gt;**", "`no_<tag>`", "", "_exclude driver with `<tag>`_"},
)
}
return s + "\n" + buildTableLinks(baseDrivers, mostDrivers, allDrivers, badDrivers)
}

Expand Down Expand Up @@ -444,11 +466,19 @@ func buildTableLinks(drivers ...map[string]DriverInfo) string {

func writeLicenseFiles(licenseStart int, licenseAuthor string) error {
s := fmt.Sprintf(license, licenseStart, time.Now().Year(), licenseAuthor)
if err := ioutil.WriteFile("LICENSE", append([]byte(s), '\n'), 0644); err != nil {
if err := os.WriteFile("LICENSE", append([]byte(s), '\n'), 0o644); err != nil {
return err
}
textGo := fmt.Sprintf(licenseTextGo, s)
if err := ioutil.WriteFile("text/license.go", []byte(textGo), 0644); err != nil {
if err := os.WriteFile("text/license.go", []byte(textGo), 0o644); err != nil {
return err
}
return nil
}

func writeDburlLicense(dir string, licenseStart int, licenseAuthor string) error {
s := fmt.Sprintf(license, licenseStart, time.Now().Year(), licenseAuthor)
if err := os.WriteFile(filepath.Join(dir, "LICENSE"), append([]byte(s), '\n'), 0o644); err != nil {
return err
}
return nil
Expand Down

0 comments on commit 5e07f38

Please sign in to comment.