common.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "fmt"
  7. "go/build"
  8. "go/parser"
  9. "golang.org/x/tools/go/loader"
  10. )
  11. const (
  12. extractFile = "extracted.gotext.json"
  13. outFile = "out.gotext.json"
  14. gotextSuffix = ".gotext.json"
  15. )
  16. // NOTE: The command line tool already prefixes with "gotext:".
  17. var (
  18. wrap = func(err error, msg string) error {
  19. if err == nil {
  20. return nil
  21. }
  22. return fmt.Errorf("%s: %v", msg, err)
  23. }
  24. errorf = fmt.Errorf
  25. )
  26. // TODO: still used. Remove when possible.
  27. func loadPackages(conf *loader.Config, args []string) (*loader.Program, error) {
  28. if len(args) == 0 {
  29. args = []string{"."}
  30. }
  31. conf.Build = &build.Default
  32. conf.ParserMode = parser.ParseComments
  33. // Use the initial packages from the command line.
  34. args, err := conf.FromArgs(args, false)
  35. if err != nil {
  36. return nil, wrap(err, "loading packages failed")
  37. }
  38. // Load, parse and type-check the whole program.
  39. return conf.Load()
  40. }