Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jvm] Support setting additional repositories for coursier #13606

Merged
merged 1 commit into from
Nov 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions src/python/pants/jvm/resolve/coursier_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import os
import shlex
import textwrap
from dataclasses import dataclass
from typing import ClassVar, Iterable
Expand All @@ -17,6 +18,7 @@
from pants.engine.fs import CreateDigest, Digest, FileContent, MergeDigests
from pants.engine.platform import Platform
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.python.binaries import PythonBinary

COURSIER_POST_PROCESSING_SCRIPT = textwrap.dedent(
"""\
Expand All @@ -38,24 +40,8 @@
"""
)

COURSIER_WRAPPER_SCRIPT = textwrap.dedent(
"""\
set -eux

coursier_exe="$1"
shift
json_output_file="$1"
shift

"$coursier_exe" fetch --json-output-file="$json_output_file" "$@"

/bin/mkdir -p classpath
/usr/bin/python3 coursier_post_processing_script.py "$json_output_file"
"""
)


class CoursierBinary(TemplatedExternalTool):
class CoursierSubsystem(TemplatedExternalTool):
options_scope = "coursier"
name = "coursier"
help = "A dependency resolver for the Maven ecosystem."
Expand All @@ -77,6 +63,20 @@ class CoursierBinary(TemplatedExternalTool):
"linux_x86_64": "x86_64-pc-linux",
}

@classmethod
def register_options(cls, register) -> None:
super().register_options(register)
register(
"--repos",
type=list,
member_type=str,
default=[
"https://maven-central.storage-download.googleapis.com/maven2",
"https://repo1.maven.org/maven2",
],
help=("Maven style repositories to resolve artifacts from."),
)

def generate_exe(self, plat: Platform) -> str:
archive_filename = os.path.basename(self.generate_url(plat))
filename = os.path.splitext(archive_filename)[0]
Expand All @@ -85,7 +85,7 @@ def generate_exe(self, plat: Platform) -> str:

@dataclass(frozen=True)
class Coursier:
"""The Coursier tool and various utilities, materialzed to a `Digest` and ready to use."""
"""The Coursier tool and various utilities, materialized to a `Digest` and ready to use."""

coursier: DownloadedExternalTool
digest: Digest
Expand Down Expand Up @@ -114,17 +114,39 @@ def append_only_caches(self) -> dict[str, str]:


@rule
async def setup_coursier(coursier_binary: CoursierBinary) -> Coursier:
async def setup_coursier(
coursier_subsystem: CoursierSubsystem,
python: PythonBinary,
) -> Coursier:
repos_args = " ".join(f"-r={shlex.quote(repo)}" for repo in coursier_subsystem.options.repos)
coursier_wrapper_script = textwrap.dedent(
f"""\
set -eux

coursier_exe="$1"
shift
json_output_file="$1"
shift

"$coursier_exe" fetch {repos_args} --json-output-file="$json_output_file" "$@"

/bin/mkdir -p classpath
{python.path} coursier_post_processing_script.py "$json_output_file"
"""
)

downloaded_coursier_get = Get(
DownloadedExternalTool, ExternalToolRequest, coursier_binary.get_request(Platform.current)
DownloadedExternalTool,
ExternalToolRequest,
coursier_subsystem.get_request(Platform.current),
)
wrapper_scripts_digest_get = Get(
Digest,
CreateDigest(
[
FileContent(
Coursier.wrapper_script,
COURSIER_WRAPPER_SCRIPT.encode("utf-8"),
coursier_wrapper_script.encode("utf-8"),
is_executable=True,
),
FileContent(
Expand Down