Skip to content

Commit

Permalink
Remove pkger task in favor of Go 1.16 embed package (#75)
Browse files Browse the repository at this point in the history
In #52 [1] a task for the github.com/markbates/pkger [2] Go module was
added, a tool for embedding static files into Go binaries.
The issue also includes the "Official Static Assets Embedding" section
which mentions that the task might be removed later on again as soon as
Go 1.16 [3] will be released which comes with toolchain support for
embedding static assets (files) [4] through the `embed` package [5].
Also see markbates/pkger#114 [6] for more details about the project
future of `pkger`.

The `pkger` package [7] has been removed and the `//go:embed` directive
should be used instead.

[1]: #52
[2]: https://pkg.go.dev/github.com/markbates/pkger
[3]: https://blog.golang.org/go1.16
[4]: https://golang.org/doc/go1.16#library-embed
[5]: https://pkg.go.dev/embed
[6]: markbates/pkger#114
[7]: https://pkg.go.dev/github.com/svengreb/[email protected]/pkg/task/pkger

Closes GH-74
  • Loading branch information
svengreb authored Apr 21, 2021
1 parent 53fd75e commit 1fc1f25
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 345 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ The package also already provides tasks for basic [Go toolchain][go-pkg-cmd/go]
- **`test`** — to run the [`test` command of the Go toolchain][go-pkg-cmd/go#test] the task of the [`test`][go-pkg-task/golang/test] package can be used.
- **`golangci-lint`** — the [`golangcilint`][go-pkg-task/golangcilint] package provides a task for the [`github.com/golangci/golangci-lint/cmd/golangci-lint`][go-pkg-github.com/golangci/golangci-lint/cmd/golangci-lint] Go module command. `golangci-lint` is a fast, parallel runner for dozens of Go linters that uses caching, supports YAML configurations and has integrations with all major IDEs. The source code of `golangci-lint` is [available in the GitHub repository][gh-golangci/golangci-lint].
- **`gox`** — the [`gox`][go-pkg-task/gox] package provides a task for the [`github.com/mitchellh/gox`][go-pkg-github.com/mitchellh/gox] Go module command. `gox` is a dead simple, no frills Go cross compile tool that behaves a lot like the standard [Go toolchain `build` command][go-pkg-cmd/go#build]. The source code of `gox` is [available in the GitHub repository][gh-mitchellh/gox].
- **`pkger`** — the [`pkger`][go-pkg-task/pkger] package provides a task for the [`github.com/markbates/pkger`][go-pkg-github.com/markbates/pkger] Go module command. `pkger` is a tool for embedding static files into Go binaries.

There are also tasks that don‘t need to implement the task API but make use of some “loose“ features like information about a project application are shared as well as the dynamic option system. They can be used without a `task.Runner`, just like a “normal“ package, and provide Go functions/methods that can be called directly:

Expand Down Expand Up @@ -337,7 +336,6 @@ The guide also includes information about [minimal, complete, and verifiable exa
[go-pkg-func-app#newstore]: https://pkg.go.dev/github.com/svengreb/wand/pkg/app#NewStore
[go-pkg-func-os#usercachedir]: https://pkg.go.dev/os/#UserCacheDir
[go-pkg-github.com/golangci/golangci-lint/cmd/golangci-lint]: https://pkg.go.dev/github.com/golangci/golangci-lint/cmd/golangci-lint
[go-pkg-github.com/markbates/pkger]: https://pkg.go.dev/github.com/markbates/pkger
[go-pkg-github.com/mitchellh/gox]: https://pkg.go.dev/github.com/mitchellh/gox
[go-pkg-github.com/myitcv/gobin]: https://pkg.go.dev/github.com/myitcv/gobin
[go-pkg-golang.org/x/tools/cmd/goimports]: https://pkg.go.dev/golang.org/x/tools/cmd/goimports
Expand Down Expand Up @@ -368,7 +366,6 @@ The guide also includes information about [minimal, complete, and verifiable exa
[go-pkg-task/golang/test]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/golang/test
[go-pkg-task/golangcilint]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/golangcilint
[go-pkg-task/gox]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/gox
[go-pkg-task/pkger]: https://pkg.go.dev/github.com/svengreb/wand/pkg/task/pkger
[go-pkg-wand]: https://pkg.go.dev/github.com/svengreb/wand
[go-ref-mod]: https://golang.org/ref/mod
[go-ref-mod#go.mod]: https://golang.org/ref/mod#go-mod-file
Expand Down
70 changes: 0 additions & 70 deletions pkg/elder/elder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
package elder

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

Expand All @@ -30,7 +28,6 @@ import (
taskGoTest "github.com/svengreb/wand/pkg/task/golang/test"
taskGolangCILint "github.com/svengreb/wand/pkg/task/golangcilint"
taskGox "github.com/svengreb/wand/pkg/task/gox"
taskPkger "github.com/svengreb/wand/pkg/task/pkger"
)

// Elder is a wand.Wand reference implementation that provides common Mage tasks and stores configurations and metadata
Expand Down Expand Up @@ -250,73 +247,6 @@ func (e *Elder) Gox(appName string, opts ...taskGox.Option) error {
return e.gobinRunner.Run(t)
}

// Pkger is a task to run the "github.com/markbates/pkger/cmd/pkger" Go module command.
// "pkger" is a tool for embedding static files into Go binaries.
// When any error occurs it will be of type *app.ErrApp or *task.ErrRunner.
//
// See the "github.com/svengreb/wand/pkg/task/pkger" package for all available options.
//
// See https://pkg.go.dev/github.com/markbates/pkger for more details about "pkger".
// The source code of the "pkger" is available at github.com/markbates/pkger.
//
// Official "Static Assets Embedding"
//
// Please note that the "pkger" project might be superseded and discontinued due to the official Go toolchain support
// for embedding static assets (files) that will most probably be released with Go version 1.16.
//
// Please see https://go.googlesource.com/proposal/+/master/design/draft-embed.md and
// https://github.com/markbates/pkger/issues/114 for more details.
//
// "Monorepo" Workaround
//
// "pkger" tries to mimic the Go standard library and the way how the Go toolchain handles modules, but is therefore
// also affected by its problems and edge cases.
// When the "pkger" command is used from the root of a Go module repository, the directory where the "go.mod" file is
// located, and there is no valid Go source file, the command will fail because it internally uses the same logic like
// the "list" command of the Go toolchain ("go list").
// Therefore a "dummy" Go source file may need to be created as a workaround. This is mostly only required for
// repositories that use a "monorepo" layout where one or more "main" packages are placed in a subdirectory relative to
// the root directory, e.g. "apps" or "cmd". For repositories where the root directory already has a Go package,
// that does not contain any build constraints/tags, or uses a "library" layout, a "dummy" file is probably not needed.
//
// Please see https://github.com/markbates/pkger/issues/109 and https://github.com/markbates/pkger/issues/121 for more
// details.
func (e *Elder) Pkger(appName string, opts ...taskPkger.Option) error {
ac, acErr := e.GetAppConfig(appName)
if acErr != nil {
return fmt.Errorf("get %q application configuration: %w", appName, acErr)
}

t, tErr := taskPkger.New(e, ac, opts...)
if tErr != nil {
return fmt.Errorf("create %q task: %w", taskPkger.TaskName, tErr)
}

dummyWorkaroundFilePath := filepath.Join(
e.GetProjectMetadata().Options().RootDirPathAbs,
fmt.Sprintf("%s.go", taskPkger.MonorepoWorkaroundDummyFileName),
)

cleanDummyFile := func(path string) {
if osErr := os.Remove(path); osErr != nil && !errors.Is(osErr, os.ErrNotExist) {
e.Warnf("Failed to delete \"pkger\" dummy workaround file %q: %w", path, osErr)
e.Warnf("Please remove %q manually", path)
return
}
e.Debugf("Removed \"pkger\" dummy workaround file %q", path)
}

cleanDummyFile(dummyWorkaroundFilePath)
defer cleanDummyFile(dummyWorkaroundFilePath)

wErr := ioutil.WriteFile(dummyWorkaroundFilePath, []byte(taskPkger.MonorepoWorkaroundDummyFileContent), os.ModePerm)
if wErr != nil {
return fmt.Errorf("write \"pkger\" dummy workaround file %q: %w", dummyWorkaroundFilePath, wErr)
}

return e.gobinRunner.Run(t)
}

// RegisterApp creates and stores a new application configuration.
// Note that the package path must be relative to the project root directory!
//
Expand Down
120 changes: 0 additions & 120 deletions pkg/task/pkger/options.go

This file was deleted.

152 changes: 0 additions & 152 deletions pkg/task/pkger/pkger.go

This file was deleted.

0 comments on commit 1fc1f25

Please sign in to comment.