mirror of
https://github.com/d9pouces/id_configurator.git
synced 2026-11-09 01:47:21 +01:00
bf56cdfcd2
Signed-off-by: d9pouces <github@19pouces.net>
79 lines
3.2 KiB
Python
79 lines
3.2 KiB
Python
"""Configure NuGet to use the configured NuGetRepository entries as its
|
|
package sources, replacing the default (nuget.org).
|
|
|
|
NuGet's own HTTPS client (.NET's HttpClient, backed by OpenSSL on Linux)
|
|
already trusts whatever the `certificates` module installs into the system
|
|
CA store, so no separate certificate configuration is needed here, unlike
|
|
npm's.
|
|
|
|
Microsoft documents a "Computer"-wide config file for this purpose,
|
|
`/etc/opt/NuGet/Config` on Linux (or `$NUGET_COMMON_APPLICATION_DATA/NuGet/Config`
|
|
should that variable be set) - but the .NET SDK does not actually read it in
|
|
practice (confirmed empirically; a known, still-open upstream gap, see
|
|
dotnet/sdk#12674), so it would silently have no effect. This instead writes
|
|
the "user" level config, `~/.nuget/NuGet/NuGet.Config` (the location the
|
|
.NET CLI itself uses; Mono/NuGet.exe use `~/.config/NuGet/NuGet.Config`
|
|
instead). This tool targets Docker images, which are built and run as root,
|
|
so `~` is assumed to be `/root`, exactly like a fresh dotnet install does
|
|
when not overridden - the same tradeoff the `php` module's Composer config
|
|
already accepts. A `<clear />` element is included so the configured
|
|
sources fully replace nuget.org rather than being merged alongside it,
|
|
matching every other package-manager module's behaviour.
|
|
|
|
The same file is additionally written under each configured `--user`'s own
|
|
home directory (`context.users`), since a non-root user invoking `dotnet`
|
|
reads its config from there instead.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import xml.etree.ElementTree as ET
|
|
from urllib.parse import urlparse
|
|
|
|
from id_configurator.config import NuGetRepository
|
|
from id_configurator.context import Context
|
|
from id_configurator.modules.base import Module, register
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_NUGET_CONFIG = "root/.nuget/NuGet/NuGet.Config"
|
|
|
|
|
|
def _render_xml(root: ET.Element) -> bytes:
|
|
ET.indent(root)
|
|
content = '<?xml version="1.0" encoding="utf-8"?>\n' + ET.tostring(
|
|
root, encoding="unicode"
|
|
)
|
|
return (content + "\n").encode("utf-8")
|
|
|
|
|
|
def _write_nuget_config(context: Context, repositories: list[NuGetRepository]) -> None:
|
|
configuration = ET.Element("configuration")
|
|
package_sources = ET.SubElement(configuration, "packageSources")
|
|
ET.SubElement(package_sources, "clear")
|
|
for repository in repositories:
|
|
attributes = {"key": repository.name, "value": repository.url}
|
|
if urlparse(repository.url).scheme.lower() != "https":
|
|
attributes["allowInsecureConnections"] = "true"
|
|
ET.SubElement(package_sources, "add", attributes)
|
|
content = _render_xml(configuration)
|
|
context.write_file(_NUGET_CONFIG, content)
|
|
for user in context.users:
|
|
context.write_file(
|
|
f"{context.home_dir(user)}/.nuget/NuGet/NuGet.Config", content
|
|
)
|
|
logger.info("configured nuget for user %s", user)
|
|
logger.info("configured nuget to use %d package source(s)", len(repositories))
|
|
|
|
|
|
@register
|
|
class NuGetModule(Module):
|
|
name = "nuget"
|
|
|
|
def is_relevant(self, context: Context) -> bool:
|
|
return bool(context.config.configurations.nuget.repositories)
|
|
|
|
def apply(self, context: Context) -> None:
|
|
_write_nuget_config(context, context.config.configurations.nuget.repositories)
|