Skip to content

Commit f966b14

Browse files
committed
magefile: attempting to recreate make file dependencies
this is sloppy/clunky :-\ Signed-off-by: Vincent Batts <[email protected]>
1 parent 724d595 commit f966b14

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

magefile.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
package main
55

66
import (
7+
"errors"
78
"fmt"
89
"os"
910
"os/exec"
11+
"time"
1012

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

2224
golangcilintVersion = "v1.51.2"
25+
26+
cleanFiles = []string{}
2327
)
2428

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

83+
func init() {
84+
cleanFiles = append(cleanFiles, ".install.deps") // sloppy
85+
}
86+
7987
// Manage your deps, or running package managers.
8088
func InstallDeps() error {
89+
const fpath = ".install.deps"
90+
success := false
91+
defer func() {
92+
if success {
93+
fd, err := os.Create(fpath)
94+
if err != nil {
95+
fmt.Fprintln(os.Stderr, err)
96+
}
97+
fd.Close()
98+
}
99+
}()
100+
if IsFresh(fpath, time.Now()) {
101+
return nil
102+
}
103+
81104
mg.Deps(Tidy)
82105
fmt.Println("Installing Deps...")
83106
cmd := exec.Command("go", "get", "./...")
84107
cmd.Stdout = Stdout
85108
cmd.Stderr = Stderr
86-
return cmd.Run()
109+
err := cmd.Run()
110+
if err != nil {
111+
return err
112+
}
113+
success = true
114+
return nil
87115
}
88116

89117
// Tools used during build/dev/test
@@ -113,4 +141,17 @@ func Tidy() error {
113141
func Clean() {
114142
fmt.Println("Cleaning...")
115143
os.RemoveAll(app)
144+
for _, fpath := range cleanFiles {
145+
os.RemoveAll(fpath)
146+
}
147+
}
148+
149+
// IsFresh checks if `fpath` exists (therefore `false`, it is not fresh) or if
150+
// `fpath` is _newer_ than `t` (true, as in it's freshly built)
151+
func IsFresh(fpath string, t time.Time) bool {
152+
fi, err := os.Stat(fpath)
153+
if err != nil && errors.Is(err, os.ErrNotExist) {
154+
return false
155+
}
156+
return fi.ModTime().Before(t)
116157
}

0 commit comments

Comments
 (0)