This repository has been archived by the owner on May 9, 2021. It is now read-only.
/
asset-root.go
133 lines (125 loc) · 2.82 KB
/
asset-root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package assetmatrix
import (
"crypto/md5"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
log "github.com/inconshreveable/log15"
)
type AssetRoot struct {
Path string
GitRepo string
GitBranch string
GitRef string
ESLintConfigPath string
Log log.Logger
cacheBreaker string
indexMtx sync.Mutex
assetIndex map[string]Asset
findAsset func(string) (Asset, error)
transformerJSPath string
scssJSPath string
erbRBPath string
assetURLPrefix string
}
func (r *AssetRoot) SetCacheBreaker(cacheBreaker string) {
r.cacheBreaker = cacheBreaker
}
func (r *AssetRoot) CloneRepo() error {
var cmd *exec.Cmd
repoHash := md5.Sum([]byte(r.GitRepo))
if err := os.MkdirAll(".gitrepos", os.ModePerm); err != nil {
return err
}
cloneDir := filepath.Join(".gitrepos", fmt.Sprintf("%x", repoHash))
r.Path = filepath.Join(cloneDir, r.Path)
if _, err := os.Stat(cloneDir); err != nil {
cmd = exec.Command("git", "clone", r.GitRepo, "--branch", r.GitBranch, cloneDir)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
}
if r.GitRef == "" {
return nil
}
cmd = exec.Command("bash", "-c", strings.Join([]string{
"cd", cloneDir, "&&", "git", "checkout", r.GitRef,
"&&", "cd", "../../",
}, " "))
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (r *AssetRoot) enumerateAssets() error {
var walkFunc filepath.WalkFunc
errChan := make(chan error)
numAssets := 0
initAsset := func(a Asset) {
errChan <- a.Initialize()
}
r.assetIndex = make(map[string]Asset, 0)
walkFunc = func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
numAssets++
a := NewAsset(r, path)
r.indexAsset(a)
go initAsset(a)
return nil
}
filepath.Walk(r.Path, walkFunc)
for i := 0; i < numAssets; i++ {
if err := <-errChan; err != nil {
return err
}
}
return nil
}
func (r *AssetRoot) indexAsset(a Asset) error {
r.indexMtx.Lock()
defer r.indexMtx.Unlock()
rel, err := filepath.Rel(r.Path, a.Path())
if err != nil {
return err
}
a.SetIndexKey(rel)
r.assetIndex[rel] = a
return nil
}
func (r *AssetRoot) buildOutputTrees() ([][]Asset, error) {
trees := [][]Asset{}
var buildTree func(*[]Asset, Asset) error
buildTree = func(t *[]Asset, a Asset) error {
key := a.IndexKey()
for _, i := range *t {
if i.IndexKey() == key {
return nil
}
}
for _, p := range a.ImportPaths() {
ia, err := r.findAsset(p)
if err != nil {
return err
}
if err := buildTree(t, ia); err != nil {
return err
}
}
*t = append(*t, a)
return nil
}
for _, a := range r.assetIndex {
t := []Asset{}
if err := buildTree(&t, a); err != nil {
return nil, err
}
trees = append(trees, t)
}
return trees, nil
}