Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate template dependency file #33

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/gentpldeps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gentpldeps
102 changes: 102 additions & 0 deletions cmd/gentpldeps/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// gentpldeps generates a command dependency Go file.
//
// Usage:
// - go get $module...
// - create .mkuimage.yaml file with commands
// - gentpldeps -t <tag> -o deps.go -p <pkg>
package main

import (
"bytes"
"flag"
"log"
"os"
"sort"
"text/template"

"github.com/u-root/gobusybox/src/pkg/bb/findpkg"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uimage/mkuimage"
"golang.org/x/exp/maps"
)

var (
pkg = flag.String("p", "", "Package")
o = flag.String("o", "", "Output file name")
tag = flag.String("t", "", "Go build tag for the file")
)

func main() {
tf := &mkuimage.TemplateFlags{}
tf.RegisterFlags(flag.CommandLine)
flag.Parse()

Check warning on line 36 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L33-L36

Added lines #L33 - L36 were not covered by tests

if *pkg == "" {
log.Fatal("Must specify package name")

Check warning on line 39 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L38-L39

Added lines #L38 - L39 were not covered by tests
}
if *tag == "" {
log.Fatal("Must specify Go build tag")

Check warning on line 42 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}
if *o == "" {
log.Fatal("Must specify output file name")

Check warning on line 45 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

tpls, err := tf.Get()
if err != nil {
log.Fatal(err)

Check warning on line 50 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L48-L50

Added lines #L48 - L50 were not covered by tests
}
if tpls == nil {
log.Fatalf("No template found")

Check warning on line 53 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L52-L53

Added lines #L52 - L53 were not covered by tests
}

var cmds []string
for _, c := range tpls.Commands {
cmds = append(cmds, c...)

Check warning on line 58 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L56-L58

Added lines #L56 - L58 were not covered by tests
}
for _, conf := range tpls.Configs {
for _, c := range conf.Commands {
cmds = append(cmds, tpls.CommandsFor(c.Commands...)...)

Check warning on line 62 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L60-L62

Added lines #L60 - L62 were not covered by tests
}
}
paths, err := findpkg.ResolveGlobs(nil, golang.Default(), findpkg.DefaultEnv(), cmds)
if err != nil {
log.Fatal(err)

Check warning on line 67 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}
dedup := map[string]struct{}{}
for _, p := range paths {
dedup[p] = struct{}{}

Check warning on line 71 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L69-L71

Added lines #L69 - L71 were not covered by tests
}
c := maps.Keys(dedup)
sort.Strings(c)

Check warning on line 74 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L73-L74

Added lines #L73 - L74 were not covered by tests

tpl := `//go:build {{.Tag}}

Check warning on line 76 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L76

Added line #L76 was not covered by tests

package {{.Package}}

Check warning on line 78 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L78

Added line #L78 was not covered by tests

import ({{range .Imports}}
_ "{{.}}"{{end}}
)
`

Check warning on line 83 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L80-L83

Added lines #L80 - L83 were not covered by tests

vars := struct {
Tag string
Package string
Imports []string
}{
Tag: *tag,
Package: *pkg,
Imports: c,

Check warning on line 92 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L85-L92

Added lines #L85 - L92 were not covered by tests
}
t := template.Must(template.New("tpl").Parse(tpl))
var b bytes.Buffer
if err := t.Execute(&b, vars); err != nil {
log.Fatal(err)

Check warning on line 97 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L94-L97

Added lines #L94 - L97 were not covered by tests
}
if err := os.WriteFile(*o, b.Bytes(), 0o644); err != nil {
log.Fatal(err)

Check warning on line 100 in cmd/gentpldeps/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/gentpldeps/main.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}
}
Loading