This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.go
219 lines (174 loc) · 5.59 KB
/
object.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* gomake
* Copyright (C) 2024 gomake contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package object
import (
"errors"
"os"
"strings"
"github.com/ricochhet/gomake/scanner"
"github.com/ricochhet/gomake/token"
)
type Command struct {
OS string `json:"os"`
Directory string `json:"directory"`
Command string `json:"command"`
Expression Expression `json:"expression"`
Environment []string `json:"environment"`
}
type StatefulFunctionBlock struct {
Name string `json:"name"`
Params []string `json:"params"`
Commands []Command `json:"commands"`
OS string `json:"os"`
Directory string `json:"directory"`
Expression Expression `json:"expression"`
Environment []string `json:"environment"`
}
type FunctionBlock struct {
Name string `json:"name"`
Params []string `json:"params"`
Commands []Command `json:"commands"`
}
type Expression struct {
OperandA string `json:"operandA"`
OperandB string `json:"operandB"`
Operation int `json:"operation"`
Result bool `json:"result"`
}
var (
ErrBlockNotFound = errors.New("function block was not found or is malformed")
ErrInvalidKeyValuePair = errors.New("invalid key=value pair")
)
func GetBlock(blocks []StatefulFunctionBlock, blockName string) (StatefulFunctionBlock, error) {
for _, block := range blocks {
if block.Name == blockName {
return block, nil
}
}
return StatefulFunctionBlock{}, ErrBlockNotFound
}
//nolint:cyclop // wontfix
func (currentBlock *StatefulFunctionBlock) SetCallerBlock(blocks []StatefulFunctionBlock, callerName string, callerParams []string) error {
for _, block := range blocks {
//nolint:nestif // wontfix
if block.Name == callerName {
directory, err := SetBlockDirectory(block)
if err != nil {
return err
}
for _, cmd := range block.Commands {
commandText := cmd.Command
commandExpr := cmd.Expression
if len(callerParams) != 0 {
commandText = SetFunctionParams(cmd.Command, block.Params, callerParams)
commandExpr = SetOperandFunctionParams(cmd.Expression, block.Params, callerParams)
}
commandDirectory := cmd.Directory
if commandDirectory == "" {
commandDirectory = directory
}
commandOS := cmd.OS
if commandOS == "" {
commandOS = block.Directory
}
if commandExpr.OperandA == "" && commandExpr.OperandB == "" {
commandExpr = block.Expression
}
commandEnv := cmd.Environment
if len(commandEnv) == 0 {
commandEnv = block.Environment
}
currentBlock.Commands = append(currentBlock.Commands, Command{
Command: commandText,
Directory: commandDirectory,
OS: commandOS,
Expression: commandExpr,
Environment: commandEnv,
})
}
continue
}
}
return nil
}
func SetBlockDirectory(block StatefulFunctionBlock) (string, error) {
if block.Directory == "" {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return cwd, nil
}
return block.Directory, nil
}
func SetBlockOperatingSystem(block StatefulFunctionBlock) string {
if block.OS == "" {
return "all"
}
return block.OS
}
func SetFunctionParams(original string, oldArray []string, newArray []string) string {
replacements := make(map[string]string)
for i := range oldArray {
replacements[oldArray[i]] = newArray[i]
}
for old, new := range replacements {
original = strings.ReplaceAll(original, string(token.TokenLeftBracket)+old+string(token.TokenRightBracket), new)
}
return SetEnvironmentVariables(original)
}
func SetOperandFunctionParams(expr Expression, oldArray []string, newArray []string) Expression {
expr.OperandA = SetFunctionParams(expr.OperandA, oldArray, newArray)
expr.OperandB = SetFunctionParams(expr.OperandA, oldArray, newArray)
return expr
}
func SetEnvironmentVariables(original string) string {
variables := scanner.ScanVariables(original)
for _, variable := range variables {
replacement := string(token.TokenString) + string(token.TokenLeftBracket) + variable + string(token.TokenRightBracket)
env := os.Getenv(variable)
if env == "" {
env = replacement
}
original = strings.ReplaceAll(original, replacement, env)
}
return original
}
func SetArrayFunctionParams(original, oldArray, newArray []string) []string {
replacements := []string{}
for _, str := range original {
replacements = append(replacements, SetFunctionParams(str, oldArray, newArray))
}
return replacements
}
func SetKeyValueVariables(original string, pairs []string) (string, error) {
variables := scanner.ScanVariables(original)
for _, variable := range variables {
replacement := string(token.TokenString) + string(token.TokenLeftBracket) + variable + string(token.TokenRightBracket)
//nolint:mnd // wontfix
for _, pair := range pairs {
kvp := strings.SplitN(pair, "=", 2)
if len(kvp) != 2 {
return "", ErrInvalidKeyValuePair
}
if variable == kvp[0] {
original = strings.ReplaceAll(original, replacement, kvp[1])
}
}
}
return original, nil
}