Skip to content

Commit

Permalink
Refactor GTK documentation parser (#140)
Browse files Browse the repository at this point in the history
* Migrate from github.com/pkg/errors.Wrap to stdlib %w

* gir/girgen/cmt: Comment which things in "go/doc" are deprecated

* gir/girgen/cmt: Tidy up

This is tidying up in prep for me making real changes.  None of the
changes here should be functional changes.

* gir-generate/gendata: Drop the unused Package type

* Rethink pkgconfig

The main purpose of this is to let me get other variables out of
pkgconfig, which is functionality that I will use in later work.

* gir/girgen/cmt: Don't print param/ret labels if we don't print the list

* gir/girgen/cmt: Use the new go/doc/comment package for formatting

As you can see, I got its formatting to exactly match the old formatting.
This allows us to be sure that this doesn't introduce any regressions.
We can make improvements to the formatting in subsequent commits.

* gir/girgen/cmt: Drop back-compat whitespace hacks

I added these in the previous commit so that the formatting would match
exactly.  Now that we've verified that there are no regressions, let's
drop them.

This brings the formatting in-line with `gofmt`, so go ahead and have the
"lint" CI job check the generated files too.
  • Loading branch information
LukeShu committed Jul 9, 2024
1 parent 5109f19 commit 0add5fd
Show file tree
Hide file tree
Showing 29 changed files with 3,903 additions and 13,536 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/qa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:

- name: Run goimports
run: |
goimports -w gir/ pkg/core/ pkg/cairo/
goimports -w .
git add .
if [[ -n "$(git status --porcelain)" ]]; then
PAGER= git diff --cached
Expand Down
22 changes: 0 additions & 22 deletions gir/cmd/gir-generate/gendata/gendata.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ var Main = genmain.Data{
SingleFile: true,
}

type Package struct {
PkgName string // pkg-config name
Namespaces []string // refer to ./cmd/gir_namespaces
}

// HasNamespace returns true if the package allows all namespaces or has the
// given namespace in the list.
func (pkg *Package) HasNamespace(n *gir.Namespace) bool {
if pkg.Namespaces == nil {
return true
}

namespace := gir.VersionedNamespace(n)
for _, name := range pkg.Namespaces {
if name == namespace {
return true
}
}

return false
}

// PkgExceptions contains a list of file names that won't be deleted off of
// pkg/.
var PkgExceptions = []string{
Expand Down
17 changes: 8 additions & 9 deletions gir/cmd/gir-generate/genmain/genutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/diamondburned/gotk4/gir"
"github.com/diamondburned/gotk4/gir/girgen"
"github.com/pkg/errors"
"golang.org/x/sync/semaphore"
)

Expand Down Expand Up @@ -94,15 +93,15 @@ func WriteNamespace(ng *girgen.NamespaceGenerator, basePath string) error {
}

if err := os.MkdirAll(dir, 0777); err != nil {
return errors.Wrapf(err, "failed to mkdir -p %q", dir)
return fmt.Errorf("failed to mkdir -p %q: %w", dir, err)
}

files, err := ng.Generate()

for name, file := range files {
dst := filepath.Join(dir, name)
if err := os.WriteFile(dst, file, 0666); err != nil {
return errors.Wrapf(err, "failed to write to %s", dst)
return fmt.Errorf("failed to write to %q: %w", dst, err)
}
}

Expand Down Expand Up @@ -143,7 +142,7 @@ func AddPackages(repos *gir.Repositories, pkgs []Package) error {
}

if err != nil {
return errors.Wrapf(err, "error adding package %q", pkg.Name)
return fmt.Errorf("error adding package %q: %w", pkg.Name, err)
}
}

Expand Down Expand Up @@ -297,7 +296,7 @@ func CleanDirectory(path string, except []string) error {

fullPath := filepath.Join(path, oldFile.Name())
if err := os.RemoveAll(fullPath); err != nil {
return errors.Wrapf(err, "failed to rm -rf %s", oldFile)
return fmt.Errorf("failed to rm -rf %q: %w", oldFile, err)
}
}

Expand All @@ -320,18 +319,18 @@ func appendGoFile(path, filename, content string) error {

b, err := os.ReadFile(fullPath)
if err != nil {
return errors.Wrapf(err, "failed to read file %q", filename)
return fmt.Errorf("failed to read file %q: %w", filename, err)
}

b = append(b, []byte(content)...)

b, err = format.Source(b)
if err != nil {
return errors.Wrapf(err, "failed to go fmt file %q", filename)
return fmt.Errorf("failed to go fmt file %q: %w", filename, err)
}

if err := os.WriteFile(fullPath, b, os.ModePerm); err != nil {
return errors.Wrapf(err, "failed to write file %q", filename)
return fmt.Errorf("failed to write file %q: %w", filename, err)
}

return nil
Expand All @@ -344,7 +343,7 @@ func EnsureDirectory(path string, expects ...[]string) error {

files, err := os.ReadDir(path)
if err != nil {
return errors.Wrap(err, "failed to read dir")
return fmt.Errorf("failed to read dir: %w", err)
}

for _, file := range files {
Expand Down
5 changes: 2 additions & 3 deletions gir/cmd/gir-namespaces/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"

"github.com/diamondburned/gotk4/gir"
"github.com/diamondburned/gotk4/gir/pkgconfig"
)

func main() {
Expand All @@ -17,9 +16,9 @@ func main() {
log.Fatalln("missing name; usage: gir_namespaces <pkg-config name>")
}

files, err := pkgconfig.FindGIRFiles(name)
files, err := gir.FindGIRFiles(name)
if err != nil {
log.Fatalln("failed to get gir files for", name)
log.Fatalf("failed to get gir files for %q: %v", name, err)
}

for _, file := range files {
Expand Down
55 changes: 46 additions & 9 deletions gir/gir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package gir
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
Expand All @@ -15,7 +16,6 @@ import (
"unicode"

"github.com/diamondburned/gotk4/gir/pkgconfig"
"github.com/pkg/errors"
"golang.org/x/sync/singleflight"
)

Expand Down Expand Up @@ -114,6 +114,43 @@ type PkgRepository struct {
Path string // gir file path
}

// FindGIRFiles finds gir files from the given list of pkgs.
func FindGIRFiles(pkgs ...string) ([]string, error) {
girDirs, err := pkgconfig.GIRDirs(pkgs...)
if err != nil {
return nil, err
}
visited := make(map[string]struct{}, len(girDirs))
var girFiles []string
// Iterate over `pkgs` instead of over `girDirs` directly, so
// that the order of `girFiles` is predictable based on order
// of the input arguments. At least this was important to me
// for debugability, but I feel like I had another reason that
// I no longer remember.
for _, pkg := range pkgs {
girDir := girDirs[pkg]

// Avoid visiting the same directory twice. Sorting +
// slices.Compact(pkgs) can't save us because multiple
// pkgs might have the same girDir.
if _, ok := visited[girDir]; ok {
continue
}
visited[girDir] = struct{}{}

dirEnts, err := os.ReadDir(girDir)
if err != nil {
return nil, err
}
for _, dirEnt := range dirEnts {
if filepath.Ext(dirEnt.Name()) == ".gir" {
girFiles = append(girFiles, filepath.Join(girDir, dirEnt.Name()))
}
}
}
return girFiles, nil
}

// AddSelected adds a single package but only searches for the given list of
// GIR files.
func (repos *Repositories) AddSelected(pkg string, wantedNames []string) error {
Expand All @@ -139,23 +176,23 @@ func (repos *Repositories) AddSelected(pkg string, wantedNames []string) error {
return len(r.Namespaces) > 0
}

girs, err := pkgconfig.FindGIRFiles(pkg)
girs, err := FindGIRFiles(pkg)
if err != nil {
return errors.Wrapf(err, "failed to get gir files for %q", pkg)
return fmt.Errorf("failed to get gir files for %q: %w", pkg, err)
}

for _, gir := range girs {
repo, err := ParseRepository(gir)
if err != nil {
return errors.Wrapf(err, "failed to parse file %q", gir)
return fmt.Errorf("failed to parse file %q: %w", gir, err)
}

if !filter(repo) {
continue
}

if err := repos.add(*repo, pkg, gir); err != nil {
return errors.Wrapf(err, "failed to add file %q", gir)
return fmt.Errorf("failed to add file %q: %w", gir, err)
}
}

Expand All @@ -169,19 +206,19 @@ func (repos *Repositories) AddSelected(pkg string, wantedNames []string) error {
// Add finds the given pkg name to be searched using pkg-config and added into
// the list of repositories.
func (repos *Repositories) Add(pkg string) error {
girs, err := pkgconfig.FindGIRFiles(pkg)
girs, err := FindGIRFiles(pkg)
if err != nil {
return errors.Wrapf(err, "failed to get gir files for %q", pkg)
return fmt.Errorf("failed to get gir files for %q: %w", pkg, err)
}

for _, gir := range girs {
repo, err := ParseRepository(gir)
if err != nil {
return errors.Wrapf(err, "failed to parse file %q", gir)
return fmt.Errorf("failed to parse file %q: %w", gir, err)
}

if err := repos.add(*repo, pkg, gir); err != nil {
return errors.Wrapf(err, "failed to add file %q", gir)
return fmt.Errorf("failed to add file %q: %w", gir, err)
}
}

Expand Down
Loading

0 comments on commit 0add5fd

Please sign in to comment.