github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/cmd/gomobile/version.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 import ( 8 "bytes" 9 "fmt" 10 "os" 11 "os/exec" 12 "path/filepath" 13 ) 14 15 var cmdVersion = &command{ 16 run: runVersion, 17 Name: "version", 18 Usage: "", 19 Short: "print version", 20 Long: ` 21 Version prints versions of the gomobile binary and tools 22 `, 23 } 24 25 func runVersion(cmd *command) (err error) { 26 // Check this binary matches the version in github.com/c-darwin/mobile/cmd/gomobile 27 // source code in GOPATH. If they don't match, currently there is no 28 // way to reliably identify the revision number this binary was built 29 // against. 30 version := func() string { 31 bin, err := exec.LookPath(os.Args[0]) 32 if err != nil { 33 return "" 34 } 35 bindir := filepath.Dir(bin) 36 cmd := exec.Command("go", "install", "-x", "-n", "github.com/c-darwin/mobile/cmd/gomobile") 37 cmd.Env = append(os.Environ(), "GOBIN="+bindir) 38 out, err := cmd.CombinedOutput() 39 if err != nil || len(out) != 0 { 40 return "" 41 } 42 if rev, err := mobileRepoRevision(); err == nil { 43 return rev 44 } 45 return "" 46 }() 47 if version == "" { 48 fmt.Println("gomobile version unknown") 49 return nil 50 } 51 52 // Supported platforms 53 platforms := "android" 54 if goos == "darwin" { 55 platforms = "android,ios" 56 } 57 58 // ANDROID_HOME, sdk build tool version 59 androidapi, _ := androidAPIPath() 60 61 fmt.Printf("gomobile version %s (%s); androidSDK=%s\n", version, platforms, androidapi) 62 return nil 63 } 64 65 func mobileRepoRevision() (rev string, err error) { 66 b, err := exec.Command("go", "list", "-f", "{{.Dir}}", "github.com/c-darwin/mobile/app").CombinedOutput() 67 if err != nil { 68 return "", fmt.Errorf("mobile repo not found: %v", err) 69 } 70 71 repo := filepath.Dir(string(b)) 72 if err := os.Chdir(repo); err != nil { 73 return "", fmt.Errorf("mobile repo %q not accessible: %v", repo, err) 74 } 75 revision, err := exec.Command("git", "log", "-n", "1", "--format=format: +%h %cd", "HEAD").CombinedOutput() 76 if err != nil { 77 return "", err 78 } 79 return string(bytes.Trim(revision, " \t\r\n")), nil 80 }