-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
399 lines (361 loc) · 16.3 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package main
import (
"fmt"
"os"
"strings"
"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/stirante/go-simple-eval/eval"
"github.com/fatih/color"
"github.com/Bedrock-OSS/regolith/regolith"
"github.com/spf13/cobra"
)
var (
commit string
version = "unversioned"
date string
buildSource = "DEV"
)
const regolithDesc = `
Regolith is a tool designed to make it easier to work on add-ons for Minecraft: Bedrock Edition
using scripts and executables called filters. Regolith serves as a platform for installing and
running filters.
`
const regolithRunDesc = `
This command runs Regolith using the profile specified in arguments. The profile must be defined in
the "config.json" file of the project. If the profile name is not specified, Regolith uses "default"
profile.
`
const regolithWatchDesc = `
This command starts Regolith in the watch mode. This mode will trigger the "regolith run" command
every time a change in files of the project's RP, BP, or data folders is detected. "regolith watch"
uses the same syntax as "regolith run". You can use "regolith help run" to learn more about the
command.
`
const regolithApplyFilter = `
This command runs single selected filter and applies its changes to the project source files. Running
this is a destructive operation that modifies RP, BP and data folders, so it is recommended to be
cautious when using this command and to have a way to revert the changes (e.g. using Git).
Every filter can be used this way as long as it's defined in the "config.json" file in the
"filterDefinitions" section.
The "regolith apply-filter" command runs on a copy of the project's files and copies them back to the
project only if the filter is successful. This means that if the filter fails, the project's files
aren't modified.
`
const regolithInstallDesc = `
Downloads and installs Regolith filters from the internet, and adds them to the "filterDefinitions"
list of the project's "config.json" file. This command accepts multiple arguments, each of which
specifies what filter to install and what version to install. The syntax of the argument can have
the following forms:
- <FILTER_NAME>
- <FILTER_NAME>==<VERSION>
- <FILTER_URL>
- <FILTER_URL>==<VERSION>
Where:
- <FILTER_NAME> is the name of the filter to be resolved to URL using the Bedrock-OSS filter
resolver repository (github.com/Bedrock-OSS/regolith-filter-resolver).
- <FILTER_URL> is the URL to the filter. Using this instead of <FILTER_NAME> lets you skip the
resolver step and download the filters which aren't known to the resolver (for example from
private repositories).
The URL should follow format: <FILTER_REPOSITORY_URL>/<FILTER_NAME>. Note that this is not a
valid URL on GitHub because it's not how GitHub creates the URLs to subdirectories.
To access a "name_ninja" filter from the Bedrock-OSS repository using <FILTER_URL> you would use:
"github.com/Bedrock-OSS/regolith-filters/name_ninja" because the filter is on
"Bedrock-OSS/regolith-filters" repository in "name_ninja" folder but this is not a valid URL on
the GitHub website.
- <VERSION> is an optional part of the argument that you can add to specify what version of the
filter you want to install. You can specify the version you want in multiple ways:
- Using a semantic version of the filter (like 1.2.3)
- Using the "latest" keyword. This option searches for the latest commit that tags the version of
the filter.
- Using SHA of the commit on the GitHub repository.
- Using the "HEAD" keyword. This option looks for the latest SHA of the main branch of the
repository.
- Using a git tag used on the repository.
The semantic version format is internally changed into a tag that contains two parts separated
with a dash (-) symbol. For example argument "name_ninja==1.0.0" would be resolved by Regolith
into: "github.com/Bedrock-OSS/regolith-filters/name_ninja==name_ninja-1.0.0".
If no <VERSION> is specified, Regolith tries to download the filter using "latest" mode first, and
when it fails (due to not being able to find any tags that refer to the version of the filter on
the repository), it tries to download using "HEAD".
The "regolith install" combined with the "--force" flag can be used to change/update filters saved
in the "config.json".
`
const regolithInstallAllDesc = `
This commands installs or updates all of the filters specified in the "filterDefinitions" list of
the "config.json" file. It makes sure that the versions of the filters defined in the
"filterDefinitions" list in sync with the actual versions of the installed filters.
It is useful when you're starting to work on a project which already has a "config.json" file with
a bunch of filters defined in it or when the config file was modified by someone else and you want
to make sure that your local copies of the filters is up to date.
By default, the filters that are already installed with a correct version are ignored. You can
change that by using the "--force" flag. "regolith install-all --force" forcefully reinstalls every
filter on the project.
`
const regolithInitDesc = `
Initializes a new Regolith project in the current directory. The folder used for a new project must
be an empty directory. This command creates "config.json" and a few empty folders to be used for
RP, BP, data, and Regolith cache (.regolith folder).
`
const regolithCleanDesc = `
This command clears the Regolith cache files for the currently open project. With the default
Regolith configuration, the Regolith cache is stored in the ".regolith" folder (which you can
find in the root of the project). If your user configuration has the "use_project_app_data_storage"
setting set to "true", the cache will be stored in the user data folder, in a path based on a hash
of the project root path. "regolith clean" will always clean both cache folders, regardless of the
"use_project_app_data_storage" setting.
Cache files include scripts/executables of Regolith filters, their virtual environments, and a list
of files recognized by Regolith as previous outputs.
After "regolith clean", usually two additional steps need to be done before running the project
again:
- the filters need to be reinstalled if there are any
- the output paths of Regolith must be deleted if the project ran before
The second step is necessary because the clean command deletes the file that stores the list of the
files created by Regolith. As a safety measure, Regolith never deletes the files that it can't
recognize so running it after "clean" would result in an error saying that Regolith stopped to
protect your files.
If you're using the "use_project_app_data_storage" setting in your user configuration. It is
recommended to periodically clean the Regolith data folder to remove the cache files of the
projects that you don't work on anymore. You can clear caches of all projects stored in user data
by using the "--user-cache" flag.
`
const regolithConfigDesc = `
The config command is used to manage the user configuration of Regolith. It can access and modify
the user configuration file. The data is stored in the application data folder in the
"user_config.json" file.
The behavior of the command changes based on the used flags and the number of provided arguments.
The cheatsheet below shows the possible combinations of flags and arguments and what they do:
Printing all properties: regolith config
Printing specified property: regolith config <key>
Setting property value: regolith config <key> <value>
Deleting a property: regolith config <key> --delete
Appending to a list property: regolith config <key> <value> --append
Replacing item in a list property: regolith config <key> <value> --index <index>
Deleting item in a list property: regolith config <key> --index <index> --delete
The printing commands can take the --full flag to print configuration with the default values
included (if they're not defined in the config file). Without the flag, the undefined properties
will be printed as null or empty list.
`
const regolithUpdateResolversDesc = `
Updates every resolver repository in the "resolvers" list in the user configuration. This command
is particularly useful if you are adding a new filter to the resolver file and want to ensure that
the new filter is available in the Regolith.
`
func main() {
// Schedule error handling
var err error
defer func() {
if regolith.Logger == nil { // Logger is nil when the command is 'help' or 'completion'
return
}
if err != nil {
regolith.Logger.Error(err)
os.Exit(1)
} else {
regolith.Logger.Info(color.GreenString("Finished"))
}
}()
// Schedule update status check
status := make(chan regolith.UpdateStatus)
go regolith.CheckUpdate(version, status)
defer func() {
if regolith.Logger == nil { // Logger is nil when the command is 'help' or 'completion'
return
}
updateStatus := <-status
if updateStatus.Err != nil {
regolith.Logger.Warn("Update check failed")
regolith.Logger.Debug(*updateStatus.Err)
} else if updateStatus.ShouldUpdate {
_, _ = fmt.Fprintln(color.Output, color.GreenString("New version available!"))
_, _ = fmt.Fprintln(color.Output, color.GreenString(*updateStatus.Url))
}
}()
// Initialize simple eval
eval.Init()
// Root command
versionTitle := "Regolith "
if buildSource != "DEV" {
versionTitle += version
} else {
versionTitle += "Dev Build"
}
if commit != "" {
versionTitle += " (#" + commit[0:7]
if date != "" {
versionTitle += " built at " + date
}
versionTitle += ")"
}
var rootCmd = &cobra.Command{
Use: "regolith",
Short: "Addon Compiler for the Bedrock Edition of Minecraft",
Long: versionTitle + regolithDesc,
Version: version,
}
subcommands := make([]*cobra.Command, 0)
var force bool
// regolith init
cmdInit := &cobra.Command{
Use: "init",
Short: "Initializes a Regolith project in current directory",
Long: regolithInitDesc,
Run: func(cmd *cobra.Command, _ []string) {
err = regolith.Init(burrito.PrintStackTrace, force)
},
}
cmdInit.Flags().BoolVarP(
&force, "force", "f", false, "Force the operation, overriding potential safeguards.")
subcommands = append(subcommands, cmdInit)
profiles := []string{}
// regolith install
var update, resolverRefresh, filterRefresh bool
cmdInstall := &cobra.Command{
Use: "install [filters...]",
Short: "Downloads and installs filters from the internet and adds them to the filterDefinitions list",
Long: regolithInstallDesc,
Run: func(cmd *cobra.Command, filters []string) {
if len(filters) == 0 {
cmd.Help()
return
}
if cmd.Flags().Lookup("profile").Changed && len(profiles) == 0 {
profiles = append(profiles, "default")
}
err = regolith.Install(filters, force || update, resolverRefresh, filterRefresh, profiles, burrito.PrintStackTrace)
},
}
cmdInstall.Flags().BoolVarP(
&force, "force", "f", false, "Force the operation, overriding potential safeguards.")
cmdInstall.Flags().BoolVar(
&resolverRefresh, "force-resolver-refresh", false, "Force resolvers refresh.")
cmdInstall.Flags().BoolVar(
&filterRefresh, "force-filter-refresh", false, "Force filter cache refresh.")
cmdInstall.Flags().BoolVarP(
&force, "update", "u", false, "An alias for --force flag. Use this flag to update filters.")
cmdInstall.Flags().StringSliceVarP(&profiles, "profile", "p", profiles, "Adds installed filters to the specified profiles. If no profile is provided, the filter will be added to the default profile.")
cmdInstall.Flags().Lookup("profile").NoOptDefVal = "default"
subcommands = append(subcommands, cmdInstall)
// regolith install-all
cmdInstallAll := &cobra.Command{
Use: "install-all",
Short: "Installs all nonexistent or outdated filters defined in filterDefinitions list",
Long: regolithInstallAllDesc,
Run: func(cmd *cobra.Command, _ []string) {
err = regolith.InstallAll(force, update, burrito.PrintStackTrace, filterRefresh)
},
}
cmdInstallAll.Flags().BoolVarP(
&force, "force", "f", false, "Force the operation, overriding potential safeguards.")
cmdInstallAll.Flags().BoolVarP(
&update, "update", "u", false, "Updates the remote filters to the latest stable version available.")
cmdInstallAll.Flags().BoolVar(
&filterRefresh, "force-filter-refresh", false, "Force filter cache refresh.")
subcommands = append(subcommands, cmdInstallAll)
// regolith run
cmdRun := &cobra.Command{
Use: "run [profile_name]",
Short: "Runs Regolith using specified profile",
Long: regolithRunDesc,
Run: func(cmd *cobra.Command, args []string) {
var profile string
if len(args) != 0 {
profile = args[0]
}
err = regolith.Run(profile, burrito.PrintStackTrace)
},
}
subcommands = append(subcommands, cmdRun)
// regolith watch
cmdWatch := &cobra.Command{
Use: "watch [profile_name]",
Short: "Watches project files and automatically runs Regolith when they change",
Long: regolithWatchDesc,
Run: func(cmd *cobra.Command, args []string) {
var profile string
if len(args) != 0 {
profile = args[0]
}
err = regolith.Watch(profile, burrito.PrintStackTrace)
},
}
subcommands = append(subcommands, cmdWatch)
// regolith apply-filter
cmdApplyFilter := &cobra.Command{
Use: "apply-filter <filter_name> [filter_args...]",
Short: "Runs selected filter to destructively modify the project files",
Long: regolithApplyFilter,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Help()
return
}
filter := args[0]
filterArgs := args[1:] // First arg is the filter name
err = regolith.ApplyFilter(filter, filterArgs, burrito.PrintStackTrace)
},
}
subcommands = append(subcommands, cmdApplyFilter)
// regolith config
cmdConfig := &cobra.Command{
Use: "config [key] [value]",
Short: " Print or modify the user configuration.",
Long: regolithConfigDesc,
Run: func(cmd *cobra.Command, args []string) {
regolith.InitLogging(burrito.PrintStackTrace)
full, _ := cmd.Flags().GetBool("full")
delete, _ := cmd.Flags().GetBool("delete")
append, _ := cmd.Flags().GetBool("append")
index, _ := cmd.Flags().GetInt("index")
err = regolith.ManageConfig(burrito.PrintStackTrace, full, delete, append, index, args)
},
}
cmdConfig.Flags().BoolP("full", "f", false, "When printing, prints the full configuration including default values.")
cmdConfig.Flags().BoolP("delete", "d", false, "Delete property")
cmdConfig.Flags().BoolP("append", "a", false, "Append value to array property")
cmdConfig.Flags().IntP("index", "i", -1, "The index of the array property on which to act")
subcommands = append(subcommands, cmdConfig)
// regolith clean
var userCache, filterCache bool
cmdClean := &cobra.Command{
Use: "clean",
Short: "Cleans Regolith cache",
Long: regolithCleanDesc,
Run: func(cmd *cobra.Command, _ []string) {
err = regolith.Clean(burrito.PrintStackTrace, userCache, filterCache)
},
}
cmdClean.Flags().BoolVarP(
&userCache, "user-cache", "u", false, "Clears all caches stored in user data, instead of the cache of "+
"the current project")
cmdClean.Flags().BoolVar(
&filterCache, "filter-cache", false, "Clears filter cache stored in user data, instead of the cache of "+
"the current project")
subcommands = append(subcommands, cmdClean)
// regolith update-resolvers
cmdUpdateResolvers := &cobra.Command{
Use: "update-resolvers",
Short: "Updates cached resolver repositories",
Long: regolithUpdateResolversDesc,
Run: func(cmd *cobra.Command, _ []string) {
err = regolith.UpdateResolvers(burrito.PrintStackTrace)
},
}
subcommands = append(subcommands, cmdUpdateResolvers)
// Generate the description for the experiments
experimentDescs := make([]string, len(regolith.AvailableExperiments))
for i, experiment := range regolith.AvailableExperiments {
experimentDescs[i] = "- " + experiment.Name + " - " + strings.Trim(experiment.Description, "\n")
}
// add --debug, --timings and --experiment flag to every command
for _, cmd := range subcommands {
cmd.Flags().BoolVarP(&burrito.PrintStackTrace, "debug", "", false, "Enables debugging")
cmd.Flags().BoolVarP(®olith.EnableTimings, "timings", "", false, "Enables timing information")
cmd.Flags().StringSliceVar(
®olith.EnabledExperiments, "experiments", nil,
"Enables experimental features. Currently supported experiments:\n"+
strings.Join(experimentDescs, "\n"))
}
// Build and run CLI
rootCmd.AddCommand(subcommands...)
rootCmd.Execute()
}