gensrcmods.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // +build ignore
  2. package main
  3. import (
  4. "bytes"
  5. "io/ioutil"
  6. "log"
  7. "regexp"
  8. "strconv"
  9. )
  10. var tengoModFileRE = regexp.MustCompile(`^srcmod_(\w+).tengo$`)
  11. func main() {
  12. modules := make(map[string]string)
  13. // enumerate all Tengo module files
  14. files, err := ioutil.ReadDir(".")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. for _, file := range files {
  19. m := tengoModFileRE.FindStringSubmatch(file.Name())
  20. if m != nil {
  21. modName := m[1]
  22. src, err := ioutil.ReadFile(file.Name())
  23. if err != nil {
  24. log.Fatalf("file '%s' read error: %s",
  25. file.Name(), err.Error())
  26. }
  27. modules[modName] = string(src)
  28. }
  29. }
  30. var out bytes.Buffer
  31. out.WriteString(`// Code generated using gensrcmods.go; DO NOT EDIT.
  32. package stdlib
  33. // SourceModules are source type standard library modules.
  34. var SourceModules = map[string]string{` + "\n")
  35. for modName, modSrc := range modules {
  36. out.WriteString("\t\"" + modName + "\": " +
  37. strconv.Quote(modSrc) + ",\n")
  38. }
  39. out.WriteString("}\n")
  40. const target = "source_modules.go"
  41. if err := ioutil.WriteFile(target, out.Bytes(), 0644); err != nil {
  42. log.Fatal(err)
  43. }
  44. }