Skip to content

Commit

Permalink
fix(mgmake): generate makefile with variables for the generated makef…
Browse files Browse the repository at this point in the history
…iles for convenience
  • Loading branch information
Viktor Voltaire authored and viktorvoltaire committed Dec 22, 2021
1 parent 52720c2 commit ac06dbc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 21 deletions.
16 changes: 0 additions & 16 deletions .mage/gen.mk

This file was deleted.

18 changes: 18 additions & 0 deletions .mage/tools.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mage_folder := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
mage_generated_path := $(mage_folder)/gen
mage_targets_file := $(mage_generated_path)/targets.mk
mage := $(mage_generated_path)/local-mage

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)
@cd $(mage_folder) && \
go mod tidy && \
go run main.go -compile $(mage) && \
$(mage) generateMakefile $(@)

.PHONY: mage-clean
mage-clean:
@git clean -fdx $(mage_generated_path) ./.tools
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: all
all: $(mage_target)
all: $(mage_targets)

include .mage/gen.mk
include .mage/tools.mk
include $(mage_targets)
34 changes: 31 additions & 3 deletions mgmake/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,43 @@ type templateTargets struct {

// GenerateMakefile is a mage target that ...
func GenerateMakefile(makefile string) error {
const defaultTargets = "mage_targets"
genDir := filepath.Dir(makefile)
mgDefaultTargets := fmt.Sprintf("%s.mk", filepath.Join(genDir, strcase.ToKebab(defaultTargets)))
if makefile == mgDefaultTargets {
return fmt.Errorf("%s has the same name as the default %s makefile", makefile, mgDefaultTargets)
}
mglog.Logger("generate-makefile").Info("generating Makefile...")
targets, err := listTargets()
if err != nil {
return err
}

// Create map which holds variables for each makefile being generated
mgMakefiles := make(map[string]string)

for _, target := range targets {
var f *os.File
args, _ := getTargetArguments(target)
if strings.Contains(target, ":") {
// Create unique makefile if target is namespaced
filename := fmt.Sprintf("%s.mk", filepath.Join(filepath.Dir(makefile), strings.Split(target, ":")[0]))
name := "mage_" + strcase.ToSnake(strings.Split(target, ":")[0])
filename := fmt.Sprintf("%s.mk", filepath.Join(genDir, strcase.ToKebab(name)))
f, err = os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
if _, ok := mgMakefiles[name]; !ok {
mgMakefiles[name] = filename
}
} else {
f, err = os.OpenFile(makefile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
f, err = os.OpenFile(mgDefaultTargets, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return err
}
if _, ok := mgMakefiles[defaultTargets]; !ok {
mgMakefiles[defaultTargets] = mgDefaultTargets
}
}
templateTarget := templateTargets{
MakeTarget: toMakeTarget(target),
Expand All @@ -60,12 +76,24 @@ endif{{end}}
return err
}
}
err = os.WriteFile(makefile, createMakefileVariablesFromMap(mgMakefiles), 0o644)
if err != nil {
return err
}
return nil
}

func createMakefileVariablesFromMap(m map[string]string) []byte {
makefileVariables := make([]string, 0, len(m))
for key, value := range m {
makefileVariables = append(makefileVariables, fmt.Sprintf("%s := %s", key, value))
}
return []byte(strings.Join(makefileVariables, "\n"))
}

// toMakeVars converts input to make vars.
func toMakeVars(args []string) []string {
makeVars := make([]string, 0)
makeVars := make([]string, 0, len(args))
for _, arg := range args {
arg = strcase.ToSnake(arg)
arg = strings.ReplaceAll(arg, "<", "")
Expand Down

0 comments on commit ac06dbc

Please sign in to comment.