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