Debian Codegraph @

cmd/gomobile/main.go

raw symbols SCIP
1 // Copyright 2015 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
5 package main
6
7 //go:generate gomobile help documentation doc.go
8
9 import (
10 "bufio"
11 "bytes"
12 "flag"
13 "fmt"
14 "html/template"
15 "io"
16 "log"
17 "os"
18 "os/exec"
19 "unicode"
20 "unicode/utf8"
21 )
22
23 var (
24 gomobileName = "gomobile"
25 goVersion string
26 )
27
28 const minimumGoMinorVersion = 18
29
30 func printUsage(w io.Writer) {
31 bufw := bufio.NewWriter(w)
32 if err := usageTmpl.Execute(bufw, commands); err != nil {
33 panic(err)
34 }
35 bufw.Flush()
36 }
37
38 func main() {
39 gomobileName = os.Args[0]
40 flag.Usage = func() {
41 printUsage(os.Stderr)
42 os.Exit(2)
43 }
44 flag.Parse()
45 log.SetFlags(0)
46
47 args := flag.Args()
48 if len(args) < 1 {
49 flag.Usage()
50 }
51
52 if args[0] == "help" {
53 if len(args) == 3 && args[1] == "documentation" {
54 helpDocumentation(args[2])
55 return
56 }
57 help(args[1:])
58 return
59 }
60
61 if _, err := ensureGoVersion(); err != nil {
62 fmt.Fprintf(os.Stderr, "%s: %v\n", gomobileName, err)
63 os.Exit(1)
64 }
65
66 for _, cmd := range commands {
67 if cmd.Name == args[0] {
68 cmd.flag.Usage = func() {
69 cmd.usage()
70 os.Exit(1)
71 }
72 cmd.flag.Parse(args[1:])
73 if err := cmd.run(cmd); err != nil {
74 msg := err.Error()
75 if msg != "" {
76 fmt.Fprintf(os.Stderr, "%s: %v\n", os.Args[0], err)
77 }
78 os.Exit(1)
79 }
80 return
81 }
82 }
83 fmt.Fprintf(os.Stderr, "%s: unknown subcommand %q\nRun 'gomobile help' for usage.\n", os.Args[0], args[0])
84 os.Exit(2)
85 }
86
87 func ensureGoVersion() (string, error) {
88 if goVersion != "" {
89 return goVersion, nil
90 }
91
92 goVersionOut, err := exec.Command("go", "version").CombinedOutput()
93 if err != nil {
94 return "", fmt.Errorf("'go version' failed: %v, %s", err, goVersionOut)
95 }
96 var minor int
97 if _, err := fmt.Sscanf(string(goVersionOut), "go version go1.%d", &minor); err != nil {
98 // Ignore unknown versions; it's probably a devel version.
99 return "", nil
100 }
101 goVersion = fmt.Sprintf("go1.%d", minor)
102 if minor < minimumGoMinorVersion {
103 return "", fmt.Errorf("Go 1.%d or newer is required", minimumGoMinorVersion)
104 }
105 return goVersion, nil
106 }
107
108 func help(args []string) {
109 if len(args) == 0 {
110 printUsage(os.Stdout)
111 return // succeeded at helping
112 }
113 if len(args) != 1 {
114 fmt.Fprintf(os.Stderr, "usage: %s help command\n\nToo many arguments given.\n", gomobileName)
115 os.Exit(2) // failed to help
116 }
117
118 arg := args[0]
119 for _, cmd := range commands {
120 if cmd.Name == arg {
121 cmd.usage()
122 return // succeeded at helping
123 }
124 }
125
126 fmt.Fprintf(os.Stderr, "Unknown help topic %#q. Run '%s help'.\n", arg, gomobileName)
127 os.Exit(2)
128 }
129
130 const documentationHeader = `// Copyright 2015 The Go Authors. All rights reserved.
131 // Use of this source code is governed by a BSD-style
132 // license that can be found in the LICENSE file.
133
134 // Code generated by 'gomobile help documentation doc.go'. DO NOT EDIT.
135 `
136
137 func helpDocumentation(path string) {
138 w := new(bytes.Buffer)
139 w.WriteString(documentationHeader)
140 w.WriteString("\n/*\n")
141 if err := usageTmpl.Execute(w, commands); err != nil {
142 log.Fatal(err)
143 }
144
145 for _, cmd := range commands {
146 r, rlen := utf8.DecodeRuneInString(cmd.Short)
147 w.WriteString("\n\n")
148 w.WriteRune(unicode.ToUpper(r))
149 w.WriteString(cmd.Short[rlen:])
150 w.WriteString("\n\nUsage:\n\n\tgomobile " + cmd.Name)
151 if cmd.Usage != "" {
152 w.WriteRune(' ')
153 w.WriteString(cmd.Usage)
154 }
155 w.WriteRune('\n')
156 w.WriteString(cmd.Long)
157 }
158
159 w.WriteString("*/\npackage main // import \"golang.org/x/mobile/cmd/gomobile\"\n")
160
161 if err := os.WriteFile(path, w.Bytes(), 0666); err != nil {
162 log.Fatal(err)
163 }
164 }
165
166 var commands = []*command{
167 // TODO(crawshaw): cmdRun
168 cmdBind,
169 cmdBuild,
170 cmdClean,
171 cmdInit,
172 cmdInstall,
173 cmdVersion,
174 }
175
176 type command struct {
177 run func(*command) error
178 flag flag.FlagSet
179 Name string
180 Usage string
181 Short string
182 Long string
183 }
184
185 func (cmd *command) usage() {
186 fmt.Fprintf(os.Stdout, "usage: %s %s %s\n%s", gomobileName, cmd.Name, cmd.Usage, cmd.Long)
187 }
188
189 var usageTmpl = template.Must(template.New("usage").Parse(
190 `Gomobile is a tool for building and running mobile apps written in Go.
191
192 To install:
193
194 $ go install golang.org/x/mobile/cmd/gomobile@latest
195 $ gomobile init
196
197 At least Go 1.16 is required.
198 For detailed instructions, see https://golang.org/wiki/Mobile.
199
200 Usage:
201
202 gomobile command [arguments]
203
204 Commands:
205 {{range .}}
206 {{.Name | printf "%-11s"}} {{.Short}}{{end}}
207
208 Use 'gomobile help [command]' for more information about that command.
209 `))