main.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. // Copyright 2016 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. //go:generate go build -o gotext.latest
  5. //go:generate ./gotext.latest help gendocumentation
  6. //go:generate rm gotext.latest
  7. package main
  8. import (
  9. "bufio"
  10. "bytes"
  11. "flag"
  12. "fmt"
  13. "go/build"
  14. "go/format"
  15. "io"
  16. "io/ioutil"
  17. "log"
  18. "os"
  19. "strings"
  20. "sync"
  21. "text/template"
  22. "unicode"
  23. "unicode/utf8"
  24. "golang.org/x/text/message/pipeline"
  25. "golang.org/x/text/language"
  26. "golang.org/x/tools/go/buildutil"
  27. )
  28. func init() {
  29. flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
  30. }
  31. var (
  32. srcLang = flag.String("srclang", "en-US", "the source-code language")
  33. dir = flag.String("dir", "locales", "default subdirectory to store translation files")
  34. )
  35. func config() (*pipeline.Config, error) {
  36. tag, err := language.Parse(*srcLang)
  37. if err != nil {
  38. return nil, wrap(err, "invalid srclang")
  39. }
  40. return &pipeline.Config{
  41. SourceLanguage: tag,
  42. Supported: getLangs(),
  43. TranslationsPattern: `messages\.(.*)\.json`,
  44. GenFile: *out,
  45. }, nil
  46. }
  47. // NOTE: the Command struct is copied from the go tool in core.
  48. // A Command is an implementation of a go command
  49. // like go build or go fix.
  50. type Command struct {
  51. // Run runs the command.
  52. // The args are the arguments after the command name.
  53. Run func(cmd *Command, c *pipeline.Config, args []string) error
  54. // UsageLine is the one-line usage message.
  55. // The first word in the line is taken to be the command name.
  56. UsageLine string
  57. // Short is the short description shown in the 'go help' output.
  58. Short string
  59. // Long is the long message shown in the 'go help <this-command>' output.
  60. Long string
  61. // Flag is a set of flags specific to this command.
  62. Flag flag.FlagSet
  63. }
  64. // Name returns the command's name: the first word in the usage line.
  65. func (c *Command) Name() string {
  66. name := c.UsageLine
  67. i := strings.Index(name, " ")
  68. if i >= 0 {
  69. name = name[:i]
  70. }
  71. return name
  72. }
  73. func (c *Command) Usage() {
  74. fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
  75. fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
  76. os.Exit(2)
  77. }
  78. // Runnable reports whether the command can be run; otherwise
  79. // it is a documentation pseudo-command such as importpath.
  80. func (c *Command) Runnable() bool {
  81. return c.Run != nil
  82. }
  83. // Commands lists the available commands and help topics.
  84. // The order here is the order in which they are printed by 'go help'.
  85. var commands = []*Command{
  86. cmdUpdate,
  87. cmdExtract,
  88. cmdRewrite,
  89. cmdGenerate,
  90. // TODO:
  91. // - update: full-cycle update of extraction, sending, and integration
  92. // - report: report of freshness of translations
  93. }
  94. var exitStatus = 0
  95. var exitMu sync.Mutex
  96. func setExitStatus(n int) {
  97. exitMu.Lock()
  98. if exitStatus < n {
  99. exitStatus = n
  100. }
  101. exitMu.Unlock()
  102. }
  103. var origEnv []string
  104. func main() {
  105. flag.Usage = usage
  106. flag.Parse()
  107. log.SetFlags(0)
  108. args := flag.Args()
  109. if len(args) < 1 {
  110. usage()
  111. }
  112. if args[0] == "help" {
  113. help(args[1:])
  114. return
  115. }
  116. for _, cmd := range commands {
  117. if cmd.Name() == args[0] && cmd.Runnable() {
  118. cmd.Flag.Usage = func() { cmd.Usage() }
  119. cmd.Flag.Parse(args[1:])
  120. args = cmd.Flag.Args()
  121. config, err := config()
  122. if err != nil {
  123. fatalf("gotext: %+v", err)
  124. }
  125. if err := cmd.Run(cmd, config, args); err != nil {
  126. fatalf("gotext: %+v", err)
  127. }
  128. exit()
  129. return
  130. }
  131. }
  132. fmt.Fprintf(os.Stderr, "gotext: unknown subcommand %q\nRun 'go help' for usage.\n", args[0])
  133. setExitStatus(2)
  134. exit()
  135. }
  136. var usageTemplate = `gotext is a tool for managing text in Go source code.
  137. Usage:
  138. gotext command [arguments]
  139. The commands are:
  140. {{range .}}{{if .Runnable}}
  141. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  142. Use "gotext help [command]" for more information about a command.
  143. Additional help topics:
  144. {{range .}}{{if not .Runnable}}
  145. {{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
  146. Use "gotext help [topic]" for more information about that topic.
  147. `
  148. var helpTemplate = `{{if .Runnable}}usage: gotext {{.UsageLine}}
  149. {{end}}{{.Long | trim}}
  150. `
  151. var documentationTemplate = `{{range .}}{{if .Short}}{{.Short | capitalize}}
  152. {{end}}{{if .Runnable}}Usage:
  153. gotext {{.UsageLine}}
  154. {{end}}{{.Long | trim}}
  155. {{end}}`
  156. // commentWriter writes a Go comment to the underlying io.Writer,
  157. // using line comment form (//).
  158. type commentWriter struct {
  159. W io.Writer
  160. wroteSlashes bool // Wrote "//" at the beginning of the current line.
  161. }
  162. func (c *commentWriter) Write(p []byte) (int, error) {
  163. var n int
  164. for i, b := range p {
  165. if !c.wroteSlashes {
  166. s := "//"
  167. if b != '\n' {
  168. s = "// "
  169. }
  170. if _, err := io.WriteString(c.W, s); err != nil {
  171. return n, err
  172. }
  173. c.wroteSlashes = true
  174. }
  175. n0, err := c.W.Write(p[i : i+1])
  176. n += n0
  177. if err != nil {
  178. return n, err
  179. }
  180. if b == '\n' {
  181. c.wroteSlashes = false
  182. }
  183. }
  184. return len(p), nil
  185. }
  186. // An errWriter wraps a writer, recording whether a write error occurred.
  187. type errWriter struct {
  188. w io.Writer
  189. err error
  190. }
  191. func (w *errWriter) Write(b []byte) (int, error) {
  192. n, err := w.w.Write(b)
  193. if err != nil {
  194. w.err = err
  195. }
  196. return n, err
  197. }
  198. // tmpl executes the given template text on data, writing the result to w.
  199. func tmpl(w io.Writer, text string, data interface{}) {
  200. t := template.New("top")
  201. t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
  202. template.Must(t.Parse(text))
  203. ew := &errWriter{w: w}
  204. err := t.Execute(ew, data)
  205. if ew.err != nil {
  206. // I/O error writing. Ignore write on closed pipe.
  207. if strings.Contains(ew.err.Error(), "pipe") {
  208. os.Exit(1)
  209. }
  210. fatalf("writing output: %v", ew.err)
  211. }
  212. if err != nil {
  213. panic(err)
  214. }
  215. }
  216. func capitalize(s string) string {
  217. if s == "" {
  218. return s
  219. }
  220. r, n := utf8.DecodeRuneInString(s)
  221. return string(unicode.ToTitle(r)) + s[n:]
  222. }
  223. func printUsage(w io.Writer) {
  224. bw := bufio.NewWriter(w)
  225. tmpl(bw, usageTemplate, commands)
  226. bw.Flush()
  227. }
  228. func usage() {
  229. printUsage(os.Stderr)
  230. os.Exit(2)
  231. }
  232. // help implements the 'help' command.
  233. func help(args []string) {
  234. if len(args) == 0 {
  235. printUsage(os.Stdout)
  236. // not exit 2: succeeded at 'go help'.
  237. return
  238. }
  239. if len(args) != 1 {
  240. fmt.Fprintf(os.Stderr, "usage: go help command\n\nToo many arguments given.\n")
  241. os.Exit(2) // failed at 'go help'
  242. }
  243. arg := args[0]
  244. // 'go help documentation' generates doc.go.
  245. if strings.HasSuffix(arg, "documentation") {
  246. w := &bytes.Buffer{}
  247. fmt.Fprintln(w, "// Code generated by go generate. DO NOT EDIT.")
  248. fmt.Fprintln(w)
  249. buf := new(bytes.Buffer)
  250. printUsage(buf)
  251. usage := &Command{Long: buf.String()}
  252. tmpl(&commentWriter{W: w}, documentationTemplate, append([]*Command{usage}, commands...))
  253. fmt.Fprintln(w, "package main")
  254. if arg == "gendocumentation" {
  255. b, err := format.Source(w.Bytes())
  256. if err != nil {
  257. logf("Could not format generated docs: %v\n", err)
  258. }
  259. if err := ioutil.WriteFile("doc.go", b, 0666); err != nil {
  260. logf("Could not create file alldocs.go: %v\n", err)
  261. }
  262. } else {
  263. fmt.Println(w.String())
  264. }
  265. return
  266. }
  267. for _, cmd := range commands {
  268. if cmd.Name() == arg {
  269. tmpl(os.Stdout, helpTemplate, cmd)
  270. // not exit 2: succeeded at 'go help cmd'.
  271. return
  272. }
  273. }
  274. fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run 'gotext help'.\n", arg)
  275. os.Exit(2) // failed at 'go help cmd'
  276. }
  277. func getLangs() (tags []language.Tag) {
  278. for _, t := range strings.Split(*lang, ",") {
  279. if t == "" {
  280. continue
  281. }
  282. tag, err := language.Parse(t)
  283. if err != nil {
  284. fatalf("gotext: could not parse language %q: %v", t, err)
  285. }
  286. tags = append(tags, tag)
  287. }
  288. return tags
  289. }
  290. var atexitFuncs []func()
  291. func atexit(f func()) {
  292. atexitFuncs = append(atexitFuncs, f)
  293. }
  294. func exit() {
  295. for _, f := range atexitFuncs {
  296. f()
  297. }
  298. os.Exit(exitStatus)
  299. }
  300. func fatalf(format string, args ...interface{}) {
  301. logf(format, args...)
  302. exit()
  303. }
  304. func logf(format string, args ...interface{}) {
  305. log.Printf(format, args...)
  306. setExitStatus(1)
  307. }
  308. func exitIfErrors() {
  309. if exitStatus != 0 {
  310. exit()
  311. }
  312. }