xref: /aosp_15_r20/external/skia/infra/bots/assets/mockery/create.py (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1#!/usr/bin/env python
2#
3# Copyright 2020 Google LLC.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""Create the asset."""
10
11
12import argparse
13import subprocess
14import os
15import shutil
16
17
18REPO_URL = "https://github.com/vektra/mockery.git"
19REPO_DIR = "mockery_repo"
20BINARY_NAME = "mockery"
21
22
23def create_asset(target_dir):
24  """Create the asset."""
25  os.chdir(target_dir)
26
27  # We build mockery 2.4.0 from source to fix an issue with Go 1.18. Read the
28  # comments below for details.
29  output = subprocess.check_output(["git", "clone", REPO_URL, REPO_DIR])
30  print(output)
31  os.chdir(os.path.join(target_dir, REPO_DIR))
32  output = subprocess.check_output(["git", "checkout", "v2.4.0"])
33
34  # Under Go 1.18, mockery v2.4.0 through v2.10.0 fails with errors such as:
35  #
36  #     internal error: package "fmt" without types was imported from ...
37  #
38  # This can be fixed by updating golang.org/x/tools to a more recent version.
39  # For more details, please see https://github.com/vektra/mockery/issues/434
40  # and https://github.com/golang/go/issues/49608.
41  output = subprocess.check_output(["go", "get", "golang.org/x/tools@v0.1.10"])
42  print(output)
43  output = subprocess.check_output(["go", "mod", "tidy"])
44  print(output)
45
46  # Build mockery with the same flags as in release builds.
47  #
48  # If we don't specify a SemVer value, mockery will generate files with a
49  # "Code generated by mockery v0.0.0-dev. DO NOT EDIT." comment at the top,
50  # which causes diffs due to the changed version. Thus, it is important to set
51  # the SemVer value to the correct version.
52  #
53  # See
54  # https://github.com/vektra/mockery/blob/271c74610ef710a4c30e19a42733796c50e7ea3f/.goreleaser.yml#L9.
55  ldflags = "-s -w -X github.com/vektra/mockery/v2/pkg/config.SemVer=2.4.0"
56  build_command = ["go", "build", "-ldflags=\"%s\"" % ldflags]
57  print("Building with command:", build_command)
58  output = subprocess.check_output(["go", "build", "-ldflags=" + ldflags])
59  print(output)
60
61  # Copy binary outside of the cloned repository directory and clean up.
62  output = subprocess.check_output(["cp", BINARY_NAME, ".."])
63  shutil.copy(os.path.join(target_dir, REPO_DIR, BINARY_NAME), target_dir)
64  shutil.rmtree(os.path.join(target_dir, REPO_DIR))
65  os.chdir(target_dir)
66
67
68def main():
69  parser = argparse.ArgumentParser()
70  parser.add_argument('--target_dir', '-t', required=True)
71  args = parser.parse_args()
72  create_asset(args.target_dir)
73
74
75if __name__ == '__main__':
76  main()
77