|
4 | 4 | package main |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "errors" |
7 | 8 | "fmt" |
8 | 9 | "os" |
9 | 10 | "os/exec" |
| 11 | + "time" |
10 | 12 |
|
11 | 13 | "github.com/magefile/mage/mg" // mg contains helpful utility functions, like Deps |
12 | 14 | ) |
|
20 | 22 | Stderr = ourStderr |
21 | 23 |
|
22 | 24 | golangcilintVersion = "v1.51.2" |
| 25 | + |
| 26 | + cleanFiles = []string{} |
23 | 27 | ) |
24 | 28 |
|
25 | 29 | // Run all-the-things |
@@ -76,14 +80,38 @@ func Install() error { |
76 | 80 | return os.Rename(app, "/usr/local/bin/"+app) |
77 | 81 | } |
78 | 82 |
|
| 83 | +func init() { |
| 84 | + cleanFiles = append(cleanFiles, ".install.deps") // sloppy |
| 85 | +} |
| 86 | + |
79 | 87 | // Manage your deps, or running package managers. |
80 | 88 | 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 | + |
81 | 104 | mg.Deps(Tidy) |
82 | 105 | fmt.Println("Installing Deps...") |
83 | 106 | cmd := exec.Command("go", "get", "./...") |
84 | 107 | cmd.Stdout = Stdout |
85 | 108 | 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 |
87 | 115 | } |
88 | 116 |
|
89 | 117 | // Tools used during build/dev/test |
@@ -113,4 +141,17 @@ func Tidy() error { |
113 | 141 | func Clean() { |
114 | 142 | fmt.Println("Cleaning...") |
115 | 143 | 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) |
116 | 157 | } |
0 commit comments