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