|
7 | 7 | from typing import Sequence |
8 | 8 |
|
9 | 9 | import pre_commit.constants as C |
10 | | -from pre_commit import git |
11 | 10 | from pre_commit.envcontext import envcontext |
12 | 11 | from pre_commit.envcontext import PatchesT |
13 | 12 | from pre_commit.envcontext import Var |
14 | 13 | from pre_commit.hook import Hook |
15 | 14 | from pre_commit.languages import helpers |
16 | 15 | from pre_commit.prefix import Prefix |
17 | 16 | from pre_commit.util import cmd_output |
18 | | -from pre_commit.util import cmd_output_b |
19 | 17 | from pre_commit.util import rmtree |
20 | 18 |
|
21 | 19 | ENVIRONMENT_DIR = 'golangenv' |
@@ -36,53 +34,28 @@ def in_env(prefix: Prefix) -> Generator[None, None, None]: |
36 | 34 | yield |
37 | 35 |
|
38 | 36 |
|
39 | | -def guess_go_dir(remote_url: str) -> str: |
40 | | - if remote_url.endswith('.git'): |
41 | | - remote_url = remote_url[:-1 * len('.git')] |
42 | | - looks_like_url = ( |
43 | | - not remote_url.startswith('file://') and |
44 | | - ('//' in remote_url or '@' in remote_url) |
45 | | - ) |
46 | | - remote_url = remote_url.replace(':', '/') |
47 | | - if looks_like_url: |
48 | | - _, _, remote_url = remote_url.rpartition('//') |
49 | | - _, _, remote_url = remote_url.rpartition('@') |
50 | | - return remote_url |
51 | | - else: |
52 | | - return 'unknown_src_dir' |
53 | | - |
54 | | - |
55 | 37 | def install_environment( |
56 | 38 | prefix: Prefix, |
57 | 39 | version: str, |
58 | 40 | additional_dependencies: Sequence[str], |
59 | 41 | ) -> None: |
60 | 42 | helpers.assert_version_default('golang', version) |
61 | | - directory = helpers.environment_dir(prefix, ENVIRONMENT_DIR, C.DEFAULT) |
62 | | - |
63 | | - remote = git.get_remote_url(prefix.prefix_dir) |
64 | | - repo_src_dir = os.path.join(directory, 'src', guess_go_dir(remote)) |
65 | | - |
66 | | - # Clone into the goenv we'll create |
67 | | - cmd = ('git', 'clone', '--recursive', '.', repo_src_dir) |
68 | | - helpers.run_setup_cmd(prefix, cmd) |
| 43 | + env_dir = helpers.environment_dir(prefix, ENVIRONMENT_DIR, version) |
69 | 44 |
|
70 | 45 | if sys.platform == 'cygwin': # pragma: no cover |
71 | | - _, gopath, _ = cmd_output('cygpath', '-w', directory) |
72 | | - gopath = gopath.strip() |
| 46 | + gopath = cmd_output('cygpath', '-w', env_dir)[1].strip() |
73 | 47 | else: |
74 | | - gopath = directory |
| 48 | + gopath = env_dir |
75 | 49 | env = dict(os.environ, GOPATH=gopath) |
76 | 50 | env.pop('GOBIN', None) |
77 | | - cmd_output_b('go', 'install', './...', cwd=repo_src_dir, env=env) |
| 51 | + |
| 52 | + helpers.run_setup_cmd(prefix, ('go', 'install', './...'), env=env) |
78 | 53 | for dependency in additional_dependencies: |
79 | | - cmd_output_b( |
80 | | - 'go', 'install', dependency, cwd=repo_src_dir, env=env, |
81 | | - ) |
82 | | - # Same some disk space, we don't need these after installation |
83 | | - rmtree(prefix.path(directory, 'src')) |
84 | | - pkgdir = prefix.path(directory, 'pkg') |
85 | | - if os.path.exists(pkgdir): # pragma: no cover (go<1.10) |
| 54 | + helpers.run_setup_cmd(prefix, ('go', 'install', dependency), env=env) |
| 55 | + |
| 56 | + # save some disk space -- we don't need this after installation |
| 57 | + pkgdir = os.path.join(env_dir, 'pkg') |
| 58 | + if os.path.exists(pkgdir): # pragma: no branch (always true on windows?) |
86 | 59 | rmtree(pkgdir) |
87 | 60 |
|
88 | 61 |
|
|