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