-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from Bedrock-OSS/export-refactor
Refactor and improve exporting
- Loading branch information
Showing
62 changed files
with
361 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
Oops, something went wrong.