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