|
16 | 16 | from pre_commit.hook import Hook |
17 | 17 | from pre_commit.languages import helpers |
18 | 18 | from pre_commit.prefix import Prefix |
19 | | -from pre_commit.util import clean_path_on_failure |
20 | 19 |
|
21 | 20 | ENVIRONMENT_DIR = 'dotnetenv' |
22 | 21 | BIN_DIR = 'bin' |
@@ -64,59 +63,58 @@ def install_environment( |
64 | 63 | helpers.assert_no_additional_deps('dotnet', additional_dependencies) |
65 | 64 |
|
66 | 65 | envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version)) |
67 | | - with clean_path_on_failure(envdir): |
68 | | - build_dir = 'pre-commit-build' |
69 | | - |
70 | | - # Build & pack nupkg file |
71 | | - helpers.run_setup_cmd( |
72 | | - prefix, |
73 | | - ( |
74 | | - 'dotnet', 'pack', |
75 | | - '--configuration', 'Release', |
76 | | - '--output', build_dir, |
77 | | - ), |
78 | | - ) |
79 | | - |
80 | | - nupkg_dir = prefix.path(build_dir) |
81 | | - nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')] |
82 | | - |
83 | | - if not nupkgs: |
84 | | - raise AssertionError('could not find any build outputs to install') |
85 | | - |
86 | | - for nupkg in nupkgs: |
87 | | - with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f: |
88 | | - nuspec, = (x for x in f.namelist() if x.endswith('.nuspec')) |
89 | | - with f.open(nuspec) as spec: |
90 | | - tree = xml.etree.ElementTree.parse(spec) |
91 | | - |
92 | | - namespace = re.match(r'{.*}', tree.getroot().tag) |
93 | | - if not namespace: |
94 | | - raise AssertionError('could not parse namespace from nuspec') |
95 | | - |
96 | | - tool_id_element = tree.find(f'.//{namespace[0]}id') |
97 | | - if tool_id_element is None: |
98 | | - raise AssertionError('expected to find an "id" element') |
99 | | - |
100 | | - tool_id = tool_id_element.text |
101 | | - if not tool_id: |
102 | | - raise AssertionError('"id" element missing tool name') |
103 | | - |
104 | | - # Install to bin dir |
105 | | - with _nuget_config_no_sources() as nuget_config: |
106 | | - helpers.run_setup_cmd( |
107 | | - prefix, |
108 | | - ( |
109 | | - 'dotnet', 'tool', 'install', |
110 | | - '--configfile', nuget_config, |
111 | | - '--tool-path', os.path.join(envdir, BIN_DIR), |
112 | | - '--add-source', build_dir, |
113 | | - tool_id, |
114 | | - ), |
115 | | - ) |
116 | | - |
117 | | - # Clean the git dir, ignoring the environment dir |
118 | | - clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*') |
119 | | - helpers.run_setup_cmd(prefix, clean_cmd) |
| 66 | + build_dir = 'pre-commit-build' |
| 67 | + |
| 68 | + # Build & pack nupkg file |
| 69 | + helpers.run_setup_cmd( |
| 70 | + prefix, |
| 71 | + ( |
| 72 | + 'dotnet', 'pack', |
| 73 | + '--configuration', 'Release', |
| 74 | + '--output', build_dir, |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + nupkg_dir = prefix.path(build_dir) |
| 79 | + nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')] |
| 80 | + |
| 81 | + if not nupkgs: |
| 82 | + raise AssertionError('could not find any build outputs to install') |
| 83 | + |
| 84 | + for nupkg in nupkgs: |
| 85 | + with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f: |
| 86 | + nuspec, = (x for x in f.namelist() if x.endswith('.nuspec')) |
| 87 | + with f.open(nuspec) as spec: |
| 88 | + tree = xml.etree.ElementTree.parse(spec) |
| 89 | + |
| 90 | + namespace = re.match(r'{.*}', tree.getroot().tag) |
| 91 | + if not namespace: |
| 92 | + raise AssertionError('could not parse namespace from nuspec') |
| 93 | + |
| 94 | + tool_id_element = tree.find(f'.//{namespace[0]}id') |
| 95 | + if tool_id_element is None: |
| 96 | + raise AssertionError('expected to find an "id" element') |
| 97 | + |
| 98 | + tool_id = tool_id_element.text |
| 99 | + if not tool_id: |
| 100 | + raise AssertionError('"id" element missing tool name') |
| 101 | + |
| 102 | + # Install to bin dir |
| 103 | + with _nuget_config_no_sources() as nuget_config: |
| 104 | + helpers.run_setup_cmd( |
| 105 | + prefix, |
| 106 | + ( |
| 107 | + 'dotnet', 'tool', 'install', |
| 108 | + '--configfile', nuget_config, |
| 109 | + '--tool-path', os.path.join(envdir, BIN_DIR), |
| 110 | + '--add-source', build_dir, |
| 111 | + tool_id, |
| 112 | + ), |
| 113 | + ) |
| 114 | + |
| 115 | + # Clean the git dir, ignoring the environment dir |
| 116 | + clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*') |
| 117 | + helpers.run_setup_cmd(prefix, clean_cmd) |
120 | 118 |
|
121 | 119 |
|
122 | 120 | def run_hook( |
|
0 commit comments