version.go 2.3 KB

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