@@ -3,28 +3,57 @@ package files
3
3
import (
4
4
"os"
5
5
"path"
6
+ "strings"
7
+ "sync"
6
8
7
9
"github.com/magefile/mage/mg"
8
10
"github.com/wavesoftware/go-ensure"
9
11
"github.com/wavesoftware/go-magetasks/config"
10
12
"github.com/wavesoftware/go-magetasks/pkg/dotenv"
11
13
"github.com/wavesoftware/go-magetasks/pkg/output"
14
+ "k8s.io/apimachinery/pkg/util/rand"
15
+ )
16
+
17
+ const randomDirLength = 12
18
+
19
+ var (
20
+ randomDir = "mage-build-" + rand .String (randomDirLength )
21
+ buildDirOnce sync.Once
12
22
)
13
23
14
24
// EnsureBuildDir creates a build directory.
15
25
func EnsureBuildDir () {
16
- mg .Deps (dotenv .Load , output .Setup )
17
- d := path .Join (BuildDir (), "bin" )
18
- ensure .NoError (os .MkdirAll (d , os .ModePerm ))
26
+ buildDirOnce .Do (func () {
27
+ mg .Deps (dotenv .Load , output .Setup )
28
+ d := path .Join (BuildDir (), "bin" )
29
+ ensure .NoError (os .MkdirAll (d , os .ModePerm ))
30
+ ensure .NoError (os .MkdirAll (ReportsDir (), os .ModePerm ))
31
+ if strings .Contains (ReportsDir (), randomDir ) {
32
+ output .Println ("📁 Reports directory: " , ReportsDir ())
33
+ }
34
+ })
19
35
}
20
36
21
37
// BuildDir returns project build dir.
22
38
func BuildDir () string {
39
+ buildDir := os .Getenv ("MAGE_BUILD_DIR" )
40
+ if buildDir != "" {
41
+ return buildDir
42
+ }
43
+ buildDir = os .Getenv ("BUILD_DIR" )
44
+ if buildDir != "" {
45
+ return buildDir
46
+ }
47
+ return relativeTo (ProjectDir (), config .Actual ().BuildDirPath ... )
48
+ }
49
+
50
+ // ReportsDir returns project reports directory.
51
+ func ReportsDir () string {
23
52
artifacts := os .Getenv ("ARTIFACTS" )
24
53
if artifacts != "" {
25
- return artifacts
54
+ return path . Join ( artifacts , randomDir )
26
55
}
27
- return relativeToProjectRoot ( config . Actual (). BuildDirPath )
56
+ return relativeTo ( BuildDir (), "reports" )
28
57
}
29
58
30
59
// ProjectDir returns project repo directory.
@@ -37,9 +66,9 @@ func ProjectDir() string {
37
66
return repoDir
38
67
}
39
68
40
- func relativeToProjectRoot ( paths [] string ) string {
69
+ func relativeTo ( to string , paths ... string ) string {
41
70
fullpath := make ([]string , len (paths )+ 1 )
42
- fullpath [0 ] = ProjectDir ()
71
+ fullpath [0 ] = to
43
72
for ix , elem := range paths {
44
73
fullpath [ix + 1 ] = elem
45
74
}
0 commit comments