mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-31 17:51:49 +00:00
139 lines
3.8 KiB
TypeScript
139 lines
3.8 KiB
TypeScript
import { isNonEmptyString } from '@sindresorhus/is';
|
|
import * as hostRules from '../../../util/host-rules.ts';
|
|
import { regEx } from '../../../util/regex.ts';
|
|
import { parseRegistryUrl } from '../../datasource/nuget/common.ts';
|
|
import { NugetDatasource } from '../../datasource/nuget/index.ts';
|
|
import type { ParsedRegistryUrl } from '../../datasource/nuget/types.ts';
|
|
import type {
|
|
PackageSourceCredential,
|
|
PackageSourceMap,
|
|
Registry,
|
|
} from './types.ts';
|
|
|
|
export function createNuGetConfigXml(registries: Registry[]): string {
|
|
let contents = `<?xml version="1.0" encoding="utf-8"?>\n<configuration>\n<packageSources>\n`;
|
|
let unnamedRegistryCount = 0;
|
|
|
|
const credentials: PackageSourceCredential[] = [];
|
|
const packageSourceMaps: PackageSourceMap[] = [];
|
|
const seenUrls = new Set<string>();
|
|
|
|
for (const registry of registries) {
|
|
// Skip if we've already seen this URL
|
|
if (seenUrls.has(registry.url)) {
|
|
continue;
|
|
}
|
|
seenUrls.add(registry.url);
|
|
|
|
const registryName =
|
|
registry.name ?? `Package source ${++unnamedRegistryCount}`;
|
|
const registryInfo = parseRegistryUrl(registry.url);
|
|
|
|
contents += formatPackageSourceElement(registryInfo, registryName);
|
|
|
|
const { password, username } = hostRules.find({
|
|
hostType: NugetDatasource.id,
|
|
url: registry.url,
|
|
});
|
|
|
|
if (isNonEmptyString(password) || isNonEmptyString(username)) {
|
|
credentials.push({
|
|
name: registryName,
|
|
password,
|
|
username,
|
|
});
|
|
}
|
|
|
|
if (registry.sourceMappedPackagePatterns) {
|
|
packageSourceMaps.push({
|
|
name: registryName,
|
|
patterns: registry.sourceMappedPackagePatterns,
|
|
});
|
|
}
|
|
}
|
|
|
|
contents += '</packageSources>\n';
|
|
|
|
if (credentials.length > 0) {
|
|
contents += '<packageSourceCredentials>\n';
|
|
|
|
for (const credential of credentials) {
|
|
contents += formatPackageSourceCredentialElement(credential);
|
|
}
|
|
|
|
contents += '</packageSourceCredentials>\n';
|
|
}
|
|
|
|
if (packageSourceMaps.length > 0) {
|
|
contents += '<packageSourceMapping>\n';
|
|
for (const packageSourceMap of packageSourceMaps) {
|
|
contents += formatPackageSource(packageSourceMap);
|
|
}
|
|
contents += '</packageSourceMapping>';
|
|
}
|
|
|
|
contents += '</configuration>\n';
|
|
|
|
return contents;
|
|
}
|
|
|
|
function formatPackageSourceElement(
|
|
registryInfo: ParsedRegistryUrl,
|
|
name: string,
|
|
): string {
|
|
let element = `<add key="${name}" value="${registryInfo.feedUrl}" `;
|
|
|
|
if (registryInfo.protocolVersion) {
|
|
element += `protocolVersion="${registryInfo.protocolVersion}" `;
|
|
}
|
|
|
|
return `${element}/>\n`;
|
|
}
|
|
|
|
function formatPackageSourceCredentialElement(
|
|
credential: PackageSourceCredential,
|
|
): string {
|
|
const escapedName = escapeName(credential.name);
|
|
|
|
let packageSourceCredential = `<${escapedName}>\n`;
|
|
|
|
if (credential.username) {
|
|
packageSourceCredential += `<add key="Username" value="${credential.username}" />\n`;
|
|
}
|
|
|
|
if (credential.password) {
|
|
packageSourceCredential += `<add key="ClearTextPassword" value="${credential.password}" />\n`;
|
|
}
|
|
|
|
packageSourceCredential += `<add key="ValidAuthenticationTypes" value="basic" />`;
|
|
|
|
packageSourceCredential += `</${escapedName}>\n`;
|
|
|
|
return packageSourceCredential;
|
|
}
|
|
|
|
function formatPackageSource(packageSourceMap: PackageSourceMap): string {
|
|
let packageSource = `<packageSource key="${packageSourceMap.name}">\n`;
|
|
|
|
for (const pattern of packageSourceMap.patterns) {
|
|
packageSource += `<package pattern="${pattern}" />\n`;
|
|
}
|
|
|
|
return `${packageSource}</packageSource>\n`;
|
|
}
|
|
|
|
const charactersToEscape = regEx(/[^A-Za-z0-9\-_.]/);
|
|
|
|
function escapeName(name: string): string {
|
|
let escapedName = '';
|
|
for (const char of name) {
|
|
if (char.match(charactersToEscape)) {
|
|
escapedName += `_x${char.codePointAt(0)!.toString(16).padStart(4, '0')}_`;
|
|
} else {
|
|
escapedName += char;
|
|
}
|
|
}
|
|
|
|
return escapedName;
|
|
}
|