Skip to content

Commit

Permalink
Merge pull request #281 from Bedrock-OSS/develop
Browse files Browse the repository at this point in the history
Merge develop for 1.2.0 Update
  • Loading branch information
Nusiq authored Mar 3, 2024
2 parents b340a6f + ddf62eb commit 0b3a3a9
Show file tree
Hide file tree
Showing 125 changed files with 1,379 additions and 780 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ docs/scraper/__pycache__/
docs/node_modules
docs/docs/.vitepress/dist
test/local_test.go
test/test_results
4 changes: 4 additions & 0 deletions docs/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ export default defineConfig({
text: 'Profiles',
link: '/guide/profiles'
},
{
text: 'Experiments',
link: '/guide/experiments'
},
{
text: 'Safety',
link: '/guide/safety'
Expand Down
23 changes: 23 additions & 0 deletions docs/docs/guide/experiments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: Experiments
---

# Experiments

Experiments are new experimental features of Regolith to be released in the future versions, once proven to be stable and useful. The experiments can be enabled with the `--experiments` flag.

## Currently Available Experiments

### `size_time_check`

The `size_time_check` is an experiment that aims to speed up `regolith run` and `regolith watch` commands. It achieves this by checking the size and modification time of the files before moving them between working and output directories. If the source file is the same size and has the same modification time as the destination file, the target file will remain untouched (Regolith assumes that the files are the same).

The `size_time_check` should greatly speed up the exports of large projects.

The downside of this approach is that on the first run, the export will be slower, but on subsequent runs, the export will be much faster. This means that the `size_time_check` is not recommended for CI where Regolith is run only once.

Usage:
```
regolith run --experiments size_time_check
regolith watch --experiments size_time_check
```
76 changes: 72 additions & 4 deletions docs/docs/guide/export-targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ Some configuration properties may be used with all export targets.

`readOnly` changes the permissions of exported files to read-only. The default value is `false`. This property can be used to protect against accidental editing of files that should only be edited by Regolith!

## Additional Configuration for Non-Windows Users

Some of the export targets listed below wouldn't make sense on systems other than Windows with Minecraft installed. They often rely on finding the `com.mojang` path first, and then placing the files in a path relative to that. This problem can be solved by setting environment variables that Regolith will use instead of the `com.mojang` path.

- `COM_MOJANG_PREVIEW` - A fake path to the `com.mojang` folder in Minecraft preview releases. This is used by the `preview` export target.
- `COM_MOJANG` - A fake path to the `com.mojang` folder in regular Minecraft releases.

# Export Targets

These are the export targets that Regolith offers.

## Development

The development export target will place the compiled packs into your `com.mojang` `development_*_packs` folder, in a new folder called `<name>_BP` or `<name>_RP`.
The development export target will place the compiled packs into your `com.mojang` `development_*_packs` folders.

```json
"export": {
"target": "development"
}
```

Optionally, you can use `rpName` and `bpName` to specify the names of the folders that will be created in the `development_*_packs` folders. You can read more about these options at the end of this page of the documentation.

```json
"export": {
"target": "development",
"rpName": "'my_rp'",
"bpName": "'my_bp'"
}
```

## Local

This export target will place the compiled packs into a folder called `build`, created in your regolith project. This export target is mostly useful for quick testing.
Expand All @@ -40,6 +57,18 @@ This export target will place the compiled packs into a folder called `build`, c
}
```

Local export optionally accepts `rpName` and `bpName` to specify the names of the folders that will be created in the `build` folders. You can read more about these options at the end of this page of the documentation.

```json
"export": {
"target": "local",
"rpName": "'my_rp'",
"bpName": "'my_bp'"
}
```



## Exact

The Exact export target will place the files to specific, user specified locations. This is useful when you need absolute control over Regoliths export functionality.
Expand All @@ -56,6 +85,8 @@ Example:
}
```

The exact export target doesn't support using `rpName` and `bpName`. The `rpPath` and `bpPath` should provide full paths to the desired locations.

## World

The World export target will place the compiled files into a specific world. This is useful for teams that prefer working in-world, as opposed to in the development pack folders.
Expand All @@ -67,17 +98,54 @@ Example:
```json
"export": {
"target": "world",
"worldName": "...", // This
"worldPath": "..." // OR this
"worldName": "..." // This
// "worldPath": "..." // OR this
}
```

Optionally, you can use `rpName` and `bpName` to specify the names of the folders that will be created in the world. You can read more about these options at the end of this page of the documentation.

```json
"export": {
"target": "world",
"worldPath": "...",
"rpName": "'my_rp'",
"bpName": "'my_bp'"
}
```


## Preview

The development export target will place the compiled packs into your (minecraft preview) `com.mojang` `development_*_packs` folder, in a new folder called `<name>_bp` or `<name>_rp`.
The development export target will place the compiled packs into your **(minecraft preview)** `com.mojang` `development_*_packs` folder.

```json
"export": {
"target": "preview"
}
```

Optionally, you can use `rpName` and `bpName` to specify the names of the folders that will be created in the `development_*_packs` folders. You can read more about these options at the end of this page of the documentation.
```json
"export": {
"target": "preview",
"rpName": "'my_rp'",
"bpName": "'my_bp'"
}
```

# The `rpName` and `bpName` expressions

The `rpName` and `bpName` are expressions evaulated using the [go-simple-eval](https://github.com/stirante/go-simple-eval/) library. They let you specify the names of the folders of the exported packs in some of the export targets.

The go-simple-eval library allows you to use simple expressions to generate the names of the folders. The expressions can use the following variables:

- `project.name` - The name of the project.
- `project.author` - The author of the project.
- `os` - The host operating system.
- `arch` - The host architecture.
- `debug` - whether regolith is running in debug mode or not.
- `version` - The version of regolith.
- `profile` - The name of the profile being run.

Go-simple-eval can concatenate strings using the `+` operator. The strings must be enclosed in single quotes.
14 changes: 13 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/stirante/go-simple-eval/eval"
Expand Down Expand Up @@ -358,11 +359,22 @@ func main() {
}
subcommands = append(subcommands, cmdUpdateResolvers)

// add --debug and --timings flag to every command
// 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(&regolith.EnableTimings, "timings", "", false, "Enables timing information")
cmd.Flags().StringSliceVar(
&regolith.EnabledExperiments, "experiments", nil,
"Enables experimental features. Currently supported experiments:\n"+
strings.Join(experimentDescs, "\n"))
}

// Build and run CLI
rootCmd.AddCommand(subcommands...)
rootCmd.Execute()
Expand Down
17 changes: 14 additions & 3 deletions regolith/compatibility_other_os.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package regolith

import "github.com/Bedrock-OSS/go-burrito/burrito"
import (
"os"
"github.com/Bedrock-OSS/go-burrito/burrito"
)

// pythonExeNames is the list of strings with possible names of the Python
// executable. The order of the names determines the order in which they are
Expand Down Expand Up @@ -48,11 +51,19 @@ func (d *DirWatcher) Close() error {
}

func FindMojangDir() (string, error) {
return "", burrito.WrappedError(notImplementedOnThisSystemError)
comMojang := os.Getenv("COM_MOJANG")
if comMojang == "" {
return "", burrito.WrappedError(comMojangEnvUnsetError)
}
return comMojang, nil
}

func FindPreviewDir() (string, error) {
return "", burrito.WrappedError(notImplementedOnThisSystemError)
comMojangPreview := os.Getenv("COM_MOJANG_PREVIEW")
if comMojangPreview == "" {
return "", burrito.WrappedError(comMojangPreviewEnvUnsetError)
}
return comMojangPreview, nil
}

func CheckSuspiciousLocation() error {
Expand Down
37 changes: 0 additions & 37 deletions regolith/condition_evaluator.go

This file was deleted.

9 changes: 9 additions & 0 deletions regolith/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ type Config struct {

// ExportTarget is a part of "config.json" that contains export information
// for a profile, which denotes where compiled files will go.
// When editing, adjust ExportTargetFromObject function as well.
type ExportTarget struct {
Target string `json:"target,omitempty"` // The mode of exporting. "develop" or "exact"
RpPath string `json:"rpPath,omitempty"` // Relative or absolute path to resource pack for "exact" export target
BpPath string `json:"bpPath,omitempty"` // Relative or absolute path to resource pack for "exact" export target
RpName string `json:"rpName,omitempty"`
BpName string `json:"bpName,omitempty"`
WorldName string `json:"worldName,omitempty"`
WorldPath string `json:"worldPath,omitempty"`
ReadOnly bool `json:"readOnly"` // Whether the exported files should be read-only
Expand Down Expand Up @@ -179,6 +182,12 @@ func ExportTargetFromObject(obj map[string]interface{}) (ExportTarget, error) {
// BpPath - can be empty
bpPath, _ := obj["bpPath"].(string)
result.BpPath = bpPath
// RpName - can be empty
rpName, _ := obj["rpName"].(string)
result.RpName = rpName
// BpName - can be empty
bpName, _ := obj["bpName"].(string)
result.BpName = bpName
// WorldName - can be empty
worldName, _ := obj["worldName"].(string)
result.WorldName = worldName
Expand Down
11 changes: 10 additions & 1 deletion regolith/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const (
// Error message for filepath.Abs() function.
filepathAbsError = "Failed to get absolute path.\nBase path: %s"

// Error message for os.ReadDir() failure
osReadDirError = "Failed to list files in the directory.\nPath: %s"

// Error message for os.Stat failure
osStatErrorAny = "Failed to access file info.\nPath: %s"

Expand Down Expand Up @@ -40,7 +43,7 @@ const (
isDirEmptyError = "Failed to check if path is an empty directory.\nPath: %s"

// Error used when an empty directory is expected, but it's not
isDirEmptyNotEmptyError = "Path is an empty directory.\nPath: %s"
isDirEmptyNotEmptyError = "Path is not an empty directory.\nPath: %s"

// Error used when copyFileSecurityInfo fails
copyFileSecurityInfoError = "Failed to copy ACL.\nSource: %s\nTarget: %s"
Expand Down Expand Up @@ -140,6 +143,12 @@ const (
// Error used when certain function is not implemented on this system
notImplementedOnThisSystemError = "Not implemented for this system."

// Error used when env variable COM_MOJANG is not set on non Windows system
comMojangEnvUnsetError = "COM_MOJANG environment variable is not set."

// Error used when env variable COM_MOJANG_PREVIEW is not set on non Windows system
comMojangPreviewEnvUnsetError = "COM_MOJANG_PREVIEW environment variable is not set."

// Error used when SetupTmpFiles function fails
setupTmpFilesError = "Failed to setup temporary files.\n" +
"Regolith files path: %s" // .regolith
Expand Down
60 changes: 60 additions & 0 deletions regolith/evaluator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package regolith

import (
"runtime"

"github.com/Bedrock-OSS/go-burrito/burrito"
"github.com/stirante/go-simple-eval/eval"
"github.com/stirante/go-simple-eval/eval/utils"
)

// EvalCondition evaluates a condition expression with the given context.
func EvalCondition(expression string, ctx RunContext) (bool, error) {
Logger.Debugf("Evaluating condition: %s", expression)
t := prepareScope(ctx)
Logger.Debugf("Evaluation scope: %s", utils.ToString(t))
e, err := eval.Eval(expression, t)
if err != nil {
return false, burrito.WrapErrorf(err, "Failed to evaluate condition: %s", expression)
}
Logger.Debugf("Condition evaluated to: %s", utils.ToString(e))
return utils.ToBoolean(e), nil
}

// EvalString evaluates an expression with the given context and returns the
// result as a string.
func EvalString(expression string, ctx RunContext) (string, error) {
Logger.Debugf("Evaluating expression: %s", expression)
t := prepareScope(ctx)
Logger.Debugf("Evaluation scope: %s", utils.ToString(t))
e, err := eval.Eval(expression, t)
if err != nil {
return "", burrito.WrapErrorf(err, "Failed to evaluate condition: %s", expression)
}
Logger.Debugf("Expression evaluated to: %s", utils.ToString(e))
if v, ok := e.(string); ok {
return v, nil
}
return "", burrito.WrapErrorf(err, "Expression evaluated to non-string value: %s", expression)
}

func prepareScope(ctx RunContext) map[string]interface{} {
semverString, err := utils.ParseSemverString(Version)
if err != nil {
semverString = utils.Semver{}
}
projectData := map[string]interface{}{
"name": ctx.Config.Name,
"author": ctx.Config.Author,
}
return map[string]interface{}{
"os": runtime.GOOS,
"arch": runtime.GOARCH,
"debug": burrito.PrintStackTrace,
"version": semverString,
"profile": ctx.Profile,
"filterLocation": ctx.AbsoluteLocation,
"settings": ctx.Settings,
"project": projectData,
}
}
Loading

0 comments on commit 0b3a3a9

Please sign in to comment.