Skip to content

Commit

Permalink
feat: make mage-tools easy to understand
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Voltaire authored and viktorvoltaire committed Jan 13, 2022
1 parent 6bdd4c3 commit 3444493
Show file tree
Hide file tree
Showing 30 changed files with 396 additions and 293 deletions.
11 changes: 11 additions & 0 deletions .mage/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import (
"github.com/magefile/mage/mg"
"go.einride.tech/mage-tools/mgmake"
"go.einride.tech/mage-tools/mgpath"

// mage:import
"go.einride.tech/mage-tools/targets/mgmarkdownfmt"
Expand All @@ -25,6 +27,15 @@ import (
"go.einride.tech/mage-tools/targets/mggoreview"
)

func init() {
mgmake.GenerateMakefiles(
mgmake.Makefile{
Path: mgpath.FromGitRoot("Makefile"),
DefaultTarget: All,
},
)
}

func All() {
mg.Deps(
mgcocogitto.CogCheck,
Expand Down
12 changes: 0 additions & 12 deletions .mage/mgmain_gen.go

This file was deleted.

51 changes: 6 additions & 45 deletions .mage/tools.mk
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
mage_folder := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
mage_generated_path := $(mage_folder)/tools/mgmake
mage_tools_path := $(mage_folder)/tools
mage_targets_file := $(mage_generated_path)/targets.mk
mage := $(mage_generated_path)/local-mage
mgmake := $(mage_folder)/mgmake_gen.go
mgmain := $(mage_folder)/mgmain_gen.go
mage_dir := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
mage_tools_path := $(mage_dir)/tools
mage := $(mage_tools_path)/mgmake/magefile

define mgmake_content
// Code generated by Mage-tools. DO NOT EDIT.
//go:build mage
// +build mage

package main

// mage:import
import _ "go.einride.tech/mage-tools/mgmake"
endef

define mgmain_content
// Code generated by Mage-tools. DO NOT EDIT.
//go:build ignore
// +build ignore

package main

import (
"os"
"github.com/magefile/mage/mage"
)

func main() { os.Exit(mage.Main()) }
endef

include $(mage_targets_file)

$(mage_targets_file): $(mage_folder)/go.mod $(shell find $(mage_folder)/.. -type f -name '*.go')
@git clean -fdx $(mage_generated_path)
@mkdir -p $(mage_generated_path)
$(file > $(mgmake),$(mgmake_content))
$(file > $(mgmain),$(mgmain_content))
@cd $(mage_folder) && \
go mod tidy && \
go run $(notdir $(mgmain)) -compile $(mage) && \
$(mage) generateMakefile $(@)
$(mage): $(mage_dir)/go.mod $(shell find $(mage_dir)/.. -type f -name '*.go')
@cd $(mage_dir) && go run ../main.go gen

.PHONY: mage-clean
mage-clean:
@git clean -fdx $(mage_generated_path) $(mage_tools_path)
@git clean -fdx $(mage_dir)
38 changes: 35 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
.PHONY: all
all: $(mage_targets)
# Code generated by Mage-tools. DO NOT EDIT.

.DEFAULT_GOAL := all

include .mage/tools.mk
include $(mage_targets)

.PHONY: all
all: $(mage)
@$(mage) all

.PHONY: cog-check
cog-check: $(mage)
@$(mage) cogCheck

.PHONY: format-markdown
format-markdown: $(mage)
@$(mage) formatMarkdown

.PHONY: git-verify-no-diff
git-verify-no-diff: $(mage)
@$(mage) gitVerifyNoDiff

.PHONY: go-mod-tidy
go-mod-tidy: $(mage)
@$(mage) goModTidy

.PHONY: go-test
go-test: $(mage)
@$(mage) goTest

.PHONY: golangci-lint
golangci-lint: $(mage)
@$(mage) golangciLint

.PHONY: goreview
goreview: $(mage)
@$(mage) goreview
43 changes: 29 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Run `make`
Usage
-----

All mage imports, and targets within the magefiles, gets written to a default Makefile which is always imported. It can also generate specific makefiles to only be imported in certain places like in a Makefile that should only have terraform targets.
Mage imports, and targets within the magefiles, can be written to Makefiles, you can generate as many Makefiles as you want, see more at [Makefiles / Mage namespaces](https://github.com/einride/mage-tools#makefiles--mage-namespaces).

### Magefiles

Expand Down Expand Up @@ -64,15 +64,37 @@ func All() {
}
```

#### Specific makefiles / Mage namespaces
#### Makefiles / Mage namespaces

We utilize mage namespaces to group targets and write them to unique Makefiles. This can be done via import or for local targets. The below would generate a `semantic-release.mk` and a `terraform.mk`
To generate makefiles, an `init` method needs to exist in one of the magefiles where we call the `mgmake.GenerateMakefiles` method.

```golang
func init() {
mgmake.GenerateMakefiles(
mgmake.Makefile{
Path: mgpath.FromGitRoot("Makefile"),
DefaultTarget: All,
},
)
}
```

If another makefile is desired, lets say one that only includes Terraform targets, we utilize the `mg.Namespace` type and just add another `Makefile` to the `GenerateMakefiles` method and specify the namespace, path and default target.

```golang
import (
// mage:import semantic-release
_ "go.einride.tech/mage-tools/semantic-release"
)

func init() {
mgmake.GenerateMakefiles(
mgmake.Makefile{
Path: mgpath.FromGitRoot("Makefile"),
DefaultTarget: All,
},
mgmake.Makefile{
Path: mgpath.FromGitRoot("terraform/Makefile"),
Namespace: Terraform{},
},
)
}

type Terraform mg.Namespace

Expand All @@ -84,13 +106,6 @@ func (Terraform) TerraformInitDev() {
}
```

Which we can then import like below, where the `$(mage_terraform)` variable gets generated by the tooling

```Makefile
include ./.mage/tools.mk
include $(mage_terraform)
```

#### Dependencies

Dependencies can be defined just by specificing the function, or with `mg.F` if the function takes arguments. `Deps` runs in parallel while `Serial` runs serially
Expand Down
11 changes: 11 additions & 0 deletions example/.mage/magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import (
"github.com/magefile/mage/mg"
"go.einride.tech/mage-tools/mgmake"
"go.einride.tech/mage-tools/mgpath"

// mage:import
"go.einride.tech/mage-tools/targets/mgcocogitto"
Expand All @@ -13,6 +15,15 @@ import (
"go.einride.tech/mage-tools/targets/mggitverifynodiff"
)

func init() {
mgmake.GenerateMakefiles(
mgmake.Makefile{
Path: mgpath.FromGitRoot("Makefile"),
DefaultTarget: All,
},
)
}

func All() {
mg.Deps(
mgcocogitto.CogCheck,
Expand Down
8 changes: 8 additions & 0 deletions example/.mage/mgmake_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 6 additions & 45 deletions example/.mage/tools.mk
Original file line number Diff line number Diff line change
@@ -1,49 +1,10 @@
mage_folder := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
mage_generated_path := $(mage_folder)/tools/mgmake
mage_tools_path := $(mage_folder)/tools
mage_targets_file := $(mage_generated_path)/targets.mk
mage := $(mage_generated_path)/local-mage
mgmake := $(mage_folder)/mgmake_gen.go
mgmain := $(mage_folder)/mgmain_gen.go
mage_dir := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
mage_tools_path := $(mage_dir)/tools
mage := $(mage_tools_path)/mgmake/magefile

define mgmake_content
// Code generated by Mage-tools. DO NOT EDIT.
//go:build mage
// +build mage

package main

// mage:import
import _ "go.einride.tech/mage-tools/mgmake"
endef

define mgmain_content
// Code generated by Mage-tools. DO NOT EDIT.
//go:build ignore
// +build ignore

package main

import (
"os"
"github.com/magefile/mage/mage"
)

func main() { os.Exit(mage.Main()) }
endef

include $(mage_targets_file)

$(mage_targets_file): $(mage_folder)/go.mod $(mage_folder)/*.go
@git clean -fdx $(mage_generated_path)
@mkdir -p $(mage_generated_path)
$(file > $(mgmake),$(mgmake_content))
$(file > $(mgmain),$(mgmain_content))
@cd $(mage_folder) && \
go mod tidy && \
go run $(notdir $(mgmain)) -compile $(mage) && \
$(mage) generateMakefile $(@)
$(mage): $(mage_dir)/go.mod $(mage_dir)/*.go
@cd $(mage_dir) && go run go.einride.tech/mage-tools gen

.PHONY: mage-clean
mage-clean:
@git clean -fdx $(mage_generated_path) $(mage_tools_path)
@git clean -fdx $(mage_dir)
4 changes: 1 addition & 3 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.PHONY: all
all: $(mage_targets)
# Code generated by Mage-tools. DO NOT EDIT.

include .mage/tools.mk
include $(mage_targets)
Loading

0 comments on commit 3444493

Please sign in to comment.