Skip to content

Commit

Permalink
magefile: attempting to recreate make file dependencies
Browse files Browse the repository at this point in the history
this is sloppy/clunky :-\

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
  • Loading branch information
vbatts committed Apr 27, 2023
1 parent 724d595 commit f966b14
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
"time"

"github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps
)
Expand All @@ -20,6 +22,8 @@ var (
Stderr = ourStderr

golangcilintVersion = "v1.51.2"

cleanFiles = []string{}
)

// Run all-the-things
Expand Down Expand Up @@ -76,14 +80,38 @@ func Install() error {
return os.Rename(app, "/usr/local/bin/"+app)
}

func init() {
cleanFiles = append(cleanFiles, ".install.deps") // sloppy
}

// Manage your deps, or running package managers.
func InstallDeps() error {
const fpath = ".install.deps"
success := false
defer func() {
if success {
fd, err := os.Create(fpath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
fd.Close()
}
}()
if IsFresh(fpath, time.Now()) {
return nil
}

mg.Deps(Tidy)
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "./...")
cmd.Stdout = Stdout
cmd.Stderr = Stderr
return cmd.Run()
err := cmd.Run()
if err != nil {
return err
}
success = true
return nil
}

// Tools used during build/dev/test
Expand Down Expand Up @@ -113,4 +141,17 @@ func Tidy() error {
func Clean() {
fmt.Println("Cleaning...")
os.RemoveAll(app)
for _, fpath := range cleanFiles {
os.RemoveAll(fpath)
}
}

// IsFresh checks if `fpath` exists (therefore `false`, it is not fresh) or if
// `fpath` is _newer_ than `t` (true, as in it's freshly built)
func IsFresh(fpath string, t time.Time) bool {
fi, err := os.Stat(fpath)
if err != nil && errors.Is(err, os.ErrNotExist) {
return false
}
return fi.ModTime().Before(t)
}

0 comments on commit f966b14

Please sign in to comment.