Skip to content

Commit

Permalink
Merge pull request #271 from Bedrock-OSS/export-refactor
Browse files Browse the repository at this point in the history
Refactor and improve exporting
  • Loading branch information
stirante authored Feb 28, 2024
2 parents 86b64a2 + 178c483 commit 7619d3e
Show file tree
Hide file tree
Showing 62 changed files with 361 additions and 144 deletions.
69 changes: 65 additions & 4 deletions docs/docs/guide/export-targets.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@ 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 @@ -47,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 @@ -63,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 @@ -74,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.
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
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 7619d3e

Please sign in to comment.