https://github.com/sagikazarmark/please-go-modules
https://github.com/sagikazarmark/please-go-modules
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/sagikazarmark/please-go-modules
- Owner: sagikazarmark
- Created: 2020-06-24T17:24:27.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-09-15T12:09:18.000Z (about 3 years ago)
- Last Synced: 2025-06-05T12:08:49.147Z (4 months ago)
- Language: Go
- Size: 505 KB
- Stars: 16
- Watchers: 5
- Forks: 6
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: HISTORY.md
Awesome Lists containing this project
README
# Please Go Modules rule generator
[](https://github.com/sagikazarmark/please-go-modules/actions?query=workflow%3ACI)
**Generate Please `go_module` targets from your `go.mod` file.**
## Introduction
Please added official Go modules support in v16. However, it still requires you to manually write `go_module` targets.
For large projects, that can be a heavy task.This project helps with that task, by automatically generating a `third_party/go/BUILD.plz` file containing all modules required for your project.
## Usage
### Generate `BUILD` file from `go.mod`
Add the following snippet to your `tools/BUILD` file in the root of your repository:
```starlark
remote_file(
name = "godeps",
url = f"https://github.com/sagikazarmark/please-go-modules/releases/latest/download/godeps_{CONFIG.HOSTOS}_{CONFIG.HOSTARCH}.tar.gz",
extract = True,
exported_files = ["godeps"],
binary = True,
)
```Add the following snippet to your `.plzconfig`:
```
[please]
version = 16.22.0[alias "godeps"]
desc = Generate third-party dependency rules for a Go project
cmd = run //tools:godeps -- -dir third_party/go -clean -builtin
```Run the following:
```bash
plz godeps
```The above command will generate build targets in `third_party/go` for your third party dependencies.
### Update BUILD files to use dependencies
You can combine the above with [wollemi](https://github.com/tcncloud/wollemi) that can generate/update
`BUILD` files in your project to use third-party dependencies.Add the following content to your `tools/BUILD` file:
```starlark
go_toolchain(
name = "go_toolchain",
version = "1.16.3",
)WOLLEMI_VERSION = "v0.8.1"
remote_file(
name = "wollemi",
url = f"https://github.com/tcncloud/wollemi/releases/download/{WOLLEMI_VERSION}/wollemi-{WOLLEMI_VERSION}-{CONFIG.HOSTOS}-{CONFIG.HOSTARCH}.tar.gz",
extract = True,
exported_files = ["wollemi"],
binary = True,
)sh_cmd(
name = "plz-tidy",
cmd = [
"export GOROOT=\\\\$($(out_exe :go_toolchain|go) env GOROOT)",
"$(out_exe :godeps) -dir third_party/go -clean -builtin -wollemi",
"$(out_exe :wollemi) gofmt ./...",
],
deps = [
":godeps",
":wollemi",
":go_toolchain",
],
)
```**Note:** You can remove any references to `go_toolchain` if you want to use Go installed on your system.
Finally, add an alias:
```
[alias "tidy"]
desc = Tidy generates build targets for dependencies and makes sure that BUILD files are up-to-date.
cmd = run //tools:plz-tidy
```and run:
```bash
plz tidy
```**Note:** the wollemi command might not work perfectly with Go submodules. You need to run wollemi for each module separately.