This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
/
builder.go
179 lines (146 loc) · 3.68 KB
/
builder.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
168
169
170
171
172
173
174
175
176
177
178
179
package gasx
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"path/filepath"
"github.com/fatih/color"
copyPkg "github.com/otiai10/copy"
)
// Builder GOS files builder
type Builder struct {
// BlockCompilers pipeline of special blocks compilers
BlockCompilers []BlockCompiler
}
// BlockInfo information about special block
type BlockInfo struct {
// Name File name
Name string
// Value special block value
Value string
// FileInfo isExternal, file path, extension, e.t.c.
FileInfo File
// FileBytes full GOS file value
FileBytes string
}
// BlockCompiler node for render pipeline
type BlockCompiler func(*BlockInfo) (string, error)
// RenderBlock compile special block by render pipeline
func (builder *Builder) RenderBlock(block *BlockInfo) (string, error) {
for i, renderer := range builder.BlockCompilers {
newVal, err := renderer(block)
if err != nil {
return "", fmt.Errorf("error in block renderer %d: \n%s", i, err.Error())
}
block.Value = newVal
}
return block.Value, nil
}
// Log println message with "Builder" prefix
func Log(msg string) {
fmt.Println(color.BlueString("Builder:") + " " + msg)
}
// Must compact "if err != nil"
func Must(err error) {
if err != nil {
ErrorMsg(err.Error())
}
}
// ErrorMsg print message with ERROR tag
func ErrorMsg(msg string) {
fmt.Println(color.RedString("ERROR") + ": " + msg)
panic(msg)
}
// RunCommand execute command
func RunCommand(command string) {
cmd := exec.Command("sh", "-c", command)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Must(cmd.Run())
}
// NewFile create new file
func NewFile(path, body string) {
Must(ioutil.WriteFile(path, []byte(body), 0644))
}
// DeleteFile delete file
func DeleteFile(path string) {
Must(os.Remove(path))
}
// CopyFile copy file from pathA to file in pathB
func CopyFile(pathA, pathB string) {
file, err := ioutil.ReadFile(pathA)
Must(err)
err = os.Remove(pathB)
if err != nil && !os.IsNotExist(err) {
Must(err)
}
NewFile(pathB, string(file))
}
// ClearDir reacreate directory or create if it doesn't exists
func ClearDir(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
Must(os.Mkdir(dir, os.ModePerm))
} else {
Must(os.RemoveAll(dir))
}
}
// CopyDir copy dirA to dirB
func CopyDir(dirA, dirB string) {
Must(copyPkg.Copy(dirA, dirB))
}
// Exists return true if file exisits
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
// InArrayString return is string in array of strings
func InArrayString(a string, arr []string) bool {
for _, el := range arr {
if el == a {
return true
}
}
return false
}
// FilesByPattern return files path matched by mattern in directory
func FilesByPattern(dir string, pattern string) ([]string, error) {
var matched []string
pattern = dir+"/"+pattern
err := filepath.Walk(dir+"/", func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("cannot access path %q: %s\n", path, err.Error())
}
if info.IsDir() {
return nil
}
pathIsMatched, err := filepath.Match(pattern, path)
if err != nil {
return fmt.Errorf("error matching path: %s", err.Error())
}
if pathIsMatched {
matched = append(matched, path)
}
return nil
})
if err != nil {
return []string{}, fmt.Errorf("error walking the path %q: %s\n", dir, err.Error())
}
return matched, nil
}
func UniteFilesByPaths(paths []string) (string, error) {
outFile := strings.Builder{}
for _, path := range paths {
fileFromPath, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("cannot open file: \"%s\": %s", path, err.Error())
}
outFile.Write(fileFromPath)
}
return outFile.String(), nil
}