support.go 804 B

123456789101112131415161718192021222324252627282930313233
  1. package evaluator
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. var (
  7. TOKEN_ESCAPED_QUOTE = regexp.MustCompile(`\\"`)
  8. TOKEN_ESCAPED_ESCAPE = regexp.MustCompile(`\\\\`)
  9. TOKEN_ENV_INTERPOLATION = regexp.MustCompile("(\\\\\\\\+|[^\\\\]|\\b|\\A)\\$({?)([[:alnum:]_]+)(}?)")
  10. )
  11. func stripQuotes(str string) string {
  12. str = str[1 : len(str)-1]
  13. str = TOKEN_ESCAPED_QUOTE.ReplaceAllString(str, `"`)
  14. return TOKEN_ESCAPED_ESCAPE.ReplaceAllString(str, `\`)
  15. }
  16. func replaceEnv(b *buildFile, str string) string {
  17. for _, match := range TOKEN_ENV_INTERPOLATION.FindAllString(str, -1) {
  18. match = match[strings.Index(match, "$"):]
  19. matchKey := strings.Trim(match, "${}")
  20. for envKey, envValue := range b.env {
  21. if matchKey == envKey {
  22. str = strings.Replace(str, match, envValue, -1)
  23. }
  24. }
  25. }
  26. return str
  27. }