Skip to content

Commit 9d0ab3b

Browse files
authored
Merge pull request #1633 from JosephMoniz/add-jvm-coursier-support
add coursier (jvm) as a language
2 parents eee891c + 70ab1c3 commit 9d0ab3b

File tree

10 files changed

+133
-1
lines changed

10 files changed

+133
-1
lines changed

azure-pipelines.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ jobs:
3434
pre_test:
3535
- task: UseRubyVersion@0
3636
- template: step--git-install.yml
37+
- bash: |
38+
testing/get-coursier.sh
39+
echo '##vso[task.prependpath]/tmp/coursier'
40+
displayName: install coursier
3741
- bash: |
3842
testing/get-swift.sh
3943
echo '##vso[task.prependpath]/tmp/swift/usr/bin'
@@ -44,6 +48,10 @@ jobs:
4448
os: linux
4549
pre_test:
4650
- task: UseRubyVersion@0
51+
- bash: |
52+
testing/get-coursier.sh
53+
echo '##vso[task.prependpath]/tmp/coursier'
54+
displayName: install coursier
4755
- bash: |
4856
testing/get-swift.sh
4957
echo '##vso[task.prependpath]/tmp/swift/usr/bin'

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from pre_commit.hook import Hook
88
from pre_commit.languages import conda
9+
from pre_commit.languages import coursier
910
from pre_commit.languages import docker
1011
from pre_commit.languages import docker_image
1112
from pre_commit.languages import dotnet
@@ -41,6 +42,7 @@ class Language(NamedTuple):
4142
languages = {
4243
# BEGIN GENERATED (testing/gen-languages-all)
4344
'conda': Language(name='conda', ENVIRONMENT_DIR=conda.ENVIRONMENT_DIR, get_default_version=conda.get_default_version, healthy=conda.healthy, install_environment=conda.install_environment, run_hook=conda.run_hook), # noqa: E501
45+
'coursier': Language(name='coursier', ENVIRONMENT_DIR=coursier.ENVIRONMENT_DIR, get_default_version=coursier.get_default_version, healthy=coursier.healthy, install_environment=coursier.install_environment, run_hook=coursier.run_hook), # noqa: E501
4446
'docker': Language(name='docker', ENVIRONMENT_DIR=docker.ENVIRONMENT_DIR, get_default_version=docker.get_default_version, healthy=docker.healthy, install_environment=docker.install_environment, run_hook=docker.run_hook), # noqa: E501
4547
'docker_image': Language(name='docker_image', ENVIRONMENT_DIR=docker_image.ENVIRONMENT_DIR, get_default_version=docker_image.get_default_version, healthy=docker_image.healthy, install_environment=docker_image.install_environment, run_hook=docker_image.run_hook), # noqa: E501
4648
'dotnet': Language(name='dotnet', ENVIRONMENT_DIR=dotnet.ENVIRONMENT_DIR, get_default_version=dotnet.get_default_version, healthy=dotnet.healthy, install_environment=dotnet.install_environment, run_hook=dotnet.run_hook), # noqa: E501

pre_commit/languages/coursier.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import contextlib
2+
import os
3+
from typing import Generator
4+
from typing import Sequence
5+
from typing import Tuple
6+
7+
from pre_commit.envcontext import envcontext
8+
from pre_commit.envcontext import PatchesT
9+
from pre_commit.envcontext import Var
10+
from pre_commit.hook import Hook
11+
from pre_commit.languages import helpers
12+
from pre_commit.prefix import Prefix
13+
from pre_commit.util import clean_path_on_failure
14+
15+
ENVIRONMENT_DIR = 'coursier'
16+
17+
get_default_version = helpers.basic_get_default_version
18+
healthy = helpers.basic_healthy
19+
20+
21+
def install_environment(
22+
prefix: Prefix,
23+
version: str,
24+
additional_dependencies: Sequence[str],
25+
) -> None: # pragma: win32 no cover
26+
helpers.assert_version_default('coursier', version)
27+
helpers.assert_no_additional_deps('coursier', additional_dependencies)
28+
29+
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
30+
channel = prefix.path('.pre-commit-channel')
31+
with clean_path_on_failure(envdir):
32+
for app_descriptor in os.listdir(channel):
33+
_, app_file = os.path.split(app_descriptor)
34+
app, _ = os.path.splitext(app_file)
35+
helpers.run_setup_cmd(
36+
prefix,
37+
(
38+
'cs',
39+
'install',
40+
'--default-channels=false',
41+
f'--channel={channel}',
42+
app,
43+
f'--dir={envdir}',
44+
),
45+
)
46+
47+
48+
def get_env_patch(target_dir: str) -> PatchesT: # pragma: win32 no cover
49+
return (
50+
('PATH', (target_dir, os.pathsep, Var('PATH'))),
51+
)
52+
53+
54+
@contextlib.contextmanager
55+
def in_env(
56+
prefix: Prefix,
57+
) -> Generator[None, None, None]: # pragma: win32 no cover
58+
target_dir = prefix.path(
59+
helpers.environment_dir(ENVIRONMENT_DIR, get_default_version()),
60+
)
61+
with envcontext(get_env_patch(target_dir)):
62+
yield
63+
64+
65+
def run_hook(
66+
hook: Hook,
67+
file_args: Sequence[str],
68+
color: bool,
69+
) -> Tuple[int, bytes]: # pragma: win32 no cover
70+
with in_env(hook.prefix):
71+
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

testing/gen-languages-all

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys
33

44
LANGUAGES = [
5-
'conda', 'docker', 'dotnet', 'docker_image', 'fail', 'golang',
5+
'conda', 'coursier', 'docker', 'dotnet', 'docker_image', 'fail', 'golang',
66
'node', 'perl', 'pygrep', 'python', 'ruby', 'rust', 'script', 'swift',
77
'system',
88
]

testing/get-coursier.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
$wc = New-Object System.Net.WebClient
2+
3+
$coursier_url = "https://github.com/coursier/coursier/releases/download/v2.0.5/cs-x86_64-pc-win32.exe"
4+
$coursier_dest = "C:\coursier\cs.exe"
5+
$coursier_hash ="d63d497f7805261e1cd657b8aaa626f6b8f7264cdb68219b2e6be9dd882033a9"
6+
7+
New-Item -Path "C:\" -Name "coursier" -ItemType "directory"
8+
$wc.DownloadFile($coursier_url, $coursier_dest)
9+
if ((Get-FileHash $coursier_dest -Algorithm SHA256).Hash -ne $coursier_hash) {
10+
throw "Invalid coursier file"
11+
}

testing/get-coursier.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
# This is a script used in CI to install coursier
3+
set -euxo pipefail
4+
5+
COURSIER_URL="https://github.com/coursier/coursier/releases/download/v2.0.0/cs-x86_64-pc-linux"
6+
COURSIER_HASH="e2e838b75bc71b16bcb77ce951ad65660c89bda7957c79a0628ec7146d35122f"
7+
ARTIFACT="/tmp/coursier/cs"
8+
9+
mkdir -p /tmp/coursier
10+
rm -f "$ARTIFACT"
11+
curl --location --silent --output "$ARTIFACT" "$COURSIER_URL"
12+
echo "$COURSIER_HASH $ARTIFACT" | sha256sum --check
13+
chmod ugo+x /tmp/coursier/cs
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"repositories": [
3+
"central"
4+
],
5+
"dependencies": [
6+
"io.get-coursier:echo:latest.stable"
7+
]
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: echo-java
2+
name: echo-java
3+
description: echo from java
4+
entry: echo-java
5+
language: coursier

testing/util.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def cmd_output_mocked_pre_commit_home(
4040
return ret, out.replace('\r\n', '\n'), None
4141

4242

43+
skipif_cant_run_coursier = pytest.mark.skipif(
44+
os.name == 'nt' or parse_shebang.find_executable('cs') is None,
45+
reason="coursier isn't installed or can't be found",
46+
)
4347
skipif_cant_run_docker = pytest.mark.skipif(
4448
os.name == 'nt' or not docker_is_running(),
4549
reason="Docker isn't running or can't be accessed",

tests/repository_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from testing.fixtures import modify_manifest
3232
from testing.util import cwd
3333
from testing.util import get_resource_path
34+
from testing.util import skipif_cant_run_coursier
3435
from testing.util import skipif_cant_run_docker
3536
from testing.util import skipif_cant_run_swift
3637
from testing.util import xfailif_windows
@@ -195,6 +196,15 @@ def test_versioned_python_hook(tempdir_factory, store):
195196
)
196197

197198

199+
@skipif_cant_run_coursier # pragma: win32 no cover
200+
def test_run_a_coursier_hook(tempdir_factory, store):
201+
_test_hook_repo(
202+
tempdir_factory, store, 'coursier_hooks_repo',
203+
'echo-java',
204+
['Hello World from coursier'], b'Hello World from coursier\n',
205+
)
206+
207+
198208
@skipif_cant_run_docker # pragma: win32 no cover
199209
def test_run_a_docker_hook(tempdir_factory, store):
200210
_test_hook_repo(

0 commit comments

Comments
 (0)