This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discovery.go
167 lines (143 loc) · 3.49 KB
/
discovery.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package discovery
import (
"encoding/json"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
"github.com/eduardooliveira/stLib/core/models"
"github.com/eduardooliveira/stLib/core/runtime"
"github.com/eduardooliveira/stLib/core/state"
"github.com/eduardooliveira/stLib/core/utils"
"golang.org/x/exp/slices"
)
func Run(path string) {
err := filepath.WalkDir(path, walker)
if err != nil {
fmt.Printf("error walking the path %q: %v\n", path, err)
return
}
j, _ := json.Marshal(state.Projects)
log.Println(string(j))
}
func walker(path string, d fs.DirEntry, err error) error {
if err != nil {
fmt.Printf("prevent panic by handling failure accessing a path %q: %v\n", path, err)
return err
}
log.Println(path)
if !d.IsDir() {
return nil
}
log.Printf("walking the path %q\n", path)
project := models.NewProjectFromPath(path)
err = DiscoverProjectAssets(project)
if err != nil {
return err
}
if len(project.Assets) > 0 {
state.Projects[project.UUID] = project
}
return nil
}
func DiscoverProjectAssets(project *models.Project) error {
libPath := utils.ToLibPath(project.Path)
files, err := ioutil.ReadDir(libPath)
if err != nil {
return err
}
fNames, err := getDirFileSlice(files)
if err != nil {
log.Printf("error reading the directory %q: %v\n", libPath, err)
return err
}
if slices.Contains(fNames, ".project.stlib") {
log.Println("found project", project.Path)
err = initProject(project)
if err != nil {
log.Printf("error loading the project %q: %v\n", project.Path, err)
return err
}
if !project.Initialized {
project.Tags = pathToTags(project.Path)
}
}
err = initProjectAssets(project, files)
if err != nil {
log.Printf("error loading the project %q: %v\n", project.Path, err)
return err
}
if project.DefaultImagePath == "" {
for _, asset := range project.Assets {
if asset.AssetType == models.ProjectImageType {
project.DefaultImagePath = asset.SHA1
break
}
}
}
return nil
}
func pathToTags(path string) []string {
log.Println("pathToTags", path)
tags := strings.Split(path, "/")
if len(tags) > 1 {
tags = tags[1:]
} else {
tags = make([]string, 0)
}
log.Println("pathToTags", tags)
return tags
}
func initProject(project *models.Project) error {
_, err := toml.DecodeFile(utils.ToLibPath(fmt.Sprintf("%s/.project.stlib", project.Path)), &project)
if err != nil {
log.Printf("error decoding the project %q: %v\n", project.Path, err)
return err
}
return nil
}
func initProjectAssets(project *models.Project, files []fs.FileInfo) error {
for _, file := range files {
if file.IsDir() {
continue
}
blacklisted := false
for _, blacklist := range runtime.Cfg.FileBlacklist {
if strings.HasSuffix(file.Name(), blacklist) {
blacklisted = true
break
}
}
if blacklisted {
continue
}
f, err := os.Open(utils.ToLibPath(fmt.Sprintf("%s/%s", project.Path, file.Name())))
if err != nil {
return err
}
defer f.Close()
asset, err := models.NewProjectAsset(file.Name(), project, f)
if err != nil {
return err
}
if asset.AssetType == models.ProjectSliceType {
if asset.Slice.Image != nil {
project.Assets[asset.Slice.Image.SHA1] = asset.Slice.Image
}
}
project.Assets[asset.SHA1] = asset
state.Assets[asset.SHA1] = asset
}
return nil
}
func getDirFileSlice(files []fs.FileInfo) ([]string, error) {
fNames := make([]string, 0)
for _, file := range files {
fNames = append(fNames, file.Name())
}
return fNames, nil
}