Skip to content

Commit 1d4ded5

Browse files
authored
Improve clean target (#5392)
Improve clean target, remove test binaries and improve logging for build:clean.
1 parent 60f66df commit 1d4ded5

File tree

2 files changed

+42
-19
lines changed

2 files changed

+42
-19
lines changed

dev-tools/mage/target/common/clean.go

-12
This file was deleted.

magefile.go

+42-7
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,50 @@ func (Build) Binary() error {
323323
}
324324

325325
// Clean up dev environment.
326-
func (Build) Clean() {
327-
os.RemoveAll(buildDir)
326+
func (Build) Clean() error {
327+
absBuildDir, err := filepath.Abs(buildDir)
328+
if err != nil {
329+
return fmt.Errorf("cannot get absolute path of build dir: %w", err)
330+
}
331+
if err := os.RemoveAll(absBuildDir); err != nil {
332+
return fmt.Errorf("cannot remove build dir '%s': %w", absBuildDir, err)
333+
}
334+
335+
testBinariesPath, err := getTestBinariesPath()
336+
if err != nil {
337+
return fmt.Errorf("cannot remove test binaries: %w", err)
338+
}
339+
340+
if mg.Verbose() {
341+
fmt.Println("removed", absBuildDir)
342+
for _, b := range testBinariesPath {
343+
fmt.Println("removed", b)
344+
}
345+
}
346+
347+
return nil
328348
}
329349

330-
// TestBinaries build the required binaries for the test suite.
331-
func (Build) TestBinaries() error {
332-
wd, _ := os.Getwd()
350+
func getTestBinariesPath() ([]string, error) {
351+
wd, err := os.Getwd()
352+
if err != nil {
353+
return nil, fmt.Errorf("could not get working directory: %w", err)
354+
}
355+
333356
testBinaryPkgs := []string{
334357
filepath.Join(wd, "pkg", "component", "fake", "component"),
335358
filepath.Join(wd, "internal", "pkg", "agent", "install", "testblocking"),
336359
}
360+
return testBinaryPkgs, nil
361+
}
362+
363+
// TestBinaries build the required binaries for the test suite.
364+
func (Build) TestBinaries() error {
365+
testBinaryPkgs, err := getTestBinariesPath()
366+
if err != nil {
367+
fmt.Errorf("cannot build test binaries: %w", err)
368+
}
369+
337370
for _, pkg := range testBinaryPkgs {
338371
binary := filepath.Base(pkg)
339372
if runtime.GOOS == "windows" {
@@ -809,6 +842,10 @@ func (Cloud) Push() error {
809842
return nil
810843
}
811844

845+
func Clean() {
846+
mg.Deps(devtools.Clean, Build.Clean)
847+
}
848+
812849
func dockerCommitHash() string {
813850
commit, err := devtools.CommitHash()
814851
if err == nil && len(commit) > commitLen {
@@ -2973,8 +3010,6 @@ func stringPrompt(prompt string) (string, error) {
29733010
return s, nil
29743011
}
29753012
}
2976-
2977-
return "", nil
29783013
}
29793014

29803015
func writeFile(name string, data []byte, perm os.FileMode) error {

0 commit comments

Comments
 (0)