diff --git a/.mage/gen.mk b/.mage/gen.mk deleted file mode 100644 index 838d5022..00000000 --- a/.mage/gen.mk +++ /dev/null @@ -1,16 +0,0 @@ -mage_folder := $(abspath $(dir $(lastword $(MAKEFILE_LIST)))) -mage_targets_path := $(mage_folder)/gen -mage_targets := $(mage_targets_path)/targets.mk -mage := $(mage_targets_path)/local_mage - -$(mage_targets): $(mage_folder)/go.mod $(shell find $(mage_folder)/.. -type f -name '*.go') - @git clean $(mage_targets_path)/ -fdx - @mkdir -p $(mage_targets_path) - @cd $(mage_folder) && \ - go mod tidy && \ - go run main.go -compile $(mage) && \ - $(mage) generateMakefile $(@) - -.PHONY: mage-clean -mage-clean: - @git clean -fdx $(mage_targets) ./.tools diff --git a/.mage/tools.mk b/.mage/tools.mk new file mode 100644 index 00000000..96340178 --- /dev/null +++ b/.mage/tools.mk @@ -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 diff --git a/Makefile b/Makefile index 207aaa1a..7acf8754 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .PHONY: all -all: $(mage_target) +all: $(mage_targets) -include .mage/gen.mk +include .mage/tools.mk include $(mage_targets) diff --git a/mgmake/generate.go b/mgmake/generate.go index c64032b6..8a51d1ec 100644 --- a/mgmake/generate.go +++ b/mgmake/generate.go @@ -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), @@ -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, "<", "")