Skip to content

Commit

Permalink
use n sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Sep 15, 2024
1 parent 5c0777f commit 8b892ae
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 55 deletions.
55 changes: 28 additions & 27 deletions src/app/add-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomError } from "ts-custom-error";
import type { Err } from "ts-results-es";
import { Err, Result } from "ts-results-es";
import { logResolvedDependency } from "../cli/dependency-logging";
import { PackumentNotFoundError } from "../domain/common-errors";
import {
Expand Down Expand Up @@ -31,7 +31,7 @@ import {
mapScopedRegistry,
setDependency,
} from "../domain/project-manifest";
import { type Registry, unityRegistry } from "../domain/registry";
import { type Registry } from "../domain/registry";
import { type RegistryUrl, unityRegistryUrl } from "../domain/registry-url";
import {
addScope,
Expand All @@ -40,10 +40,13 @@ import {
import { SemanticVersion } from "../domain/semantic-version";
import { isZod } from "../domain/zod-utils";
import type { ReadTextFile, WriteTextFile } from "../io/fs";
import type { GetRegistryPackument } from "../io/registry";
import { type GetRegistryPackument } from "../io/registry";
import type { CheckUrlExists } from "../io/www";
import { loadProjectManifestUsing } from "./get-dependencies";
import { FetchRegistryPackumentVersion } from "./get-registry-packument-version";
import {
FetchRegistryPackumentVersion,
type ResolvedPackumentVersion,
} from "./get-registry-packument-version";
import { resolveDependenciesUsing } from "./resolve-dependencies";
import { saveProjectManifestUsing } from "./write-dependencies";

Expand Down Expand Up @@ -187,9 +190,7 @@ function pickMostFixable(
* @param projectDirectory The projects root directory.
* @param editorVersion The projects editor version. Will be used to check
* compatibility. If set to null then compatibility will not be checked.
* @param primaryRegistry The primary registry from which to resolve
* dependencies.
* @param useUnity Whether to fall back to the Unity registry.
* @param sources The sources from which to resolve the packuments.
* @param force Whether to force add the dependencies.
* @param shouldAddTestable Whether to also add dependencies to the `testables`.
* @param pkgs References to the dependencies to add.
Expand All @@ -203,8 +204,7 @@ export async function addDependenciesUsing(
debugLog: DebugLog,
projectDirectory: string,
editorVersion: ReleaseVersion | null,
primaryRegistry: Registry,
useUnity: boolean,
sources: ReadonlyArray<Registry>,
force: boolean,
shouldAddTestable: boolean,
pkgs: ReadonlyArray<PackageReference>
Expand Down Expand Up @@ -236,7 +236,7 @@ export async function addDependenciesUsing(
const dependencyGraph = await resolveDependenciesUsing(
checkUrlExists,
fetchPackument,
[primaryRegistry, unityRegistry],
sources,
packageName,
verison,
true
Expand Down Expand Up @@ -330,28 +330,30 @@ export async function addDependenciesUsing(
): Promise<[SemanticVersion, RegistryUrl]> {
let versionToAdd = requestedVersion;

let resolveResult = await getRegistryPackumentVersion(
packageName,
requestedVersion,
primaryRegistry
).promise;
if (resolveResult.isErr() && useUnity) {
const unityResult = await getRegistryPackumentVersion(
let totalResolveResult: Result<
ResolvedPackumentVersion,
ResolvePackumentVersionError
> = Err(new PackumentNotFoundError(packageName));

for (const source of sources) {
const resolveResult = await getRegistryPackumentVersion(
packageName,
requestedVersion,
unityRegistry
source
).promise;
if (unityResult.isOk()) {
resolveResult = unityResult;

if (resolveResult.isOk()) {
totalResolveResult = resolveResult;
break;
} else {
resolveResult = pickMostFixable(resolveResult, unityResult);
totalResolveResult = pickMostFixable(totalResolveResult, resolveResult);
}
}

if (resolveResult.isErr()) throw resolveResult.error;
if (totalResolveResult.isErr()) throw totalResolveResult.error;

const packumentVersion = resolveResult.value.packumentVersion;
const source = resolveResult.value.source;
const packumentVersion = totalResolveResult.value.packumentVersion;
const source = totalResolveResult.value.source;
versionToAdd = packumentVersion.version;

// Only do compatibility check when we have a editor version to check against
Expand Down Expand Up @@ -408,9 +410,8 @@ export async function addDependenciesUsing(
);

if (!isUnityPackage && packagesInScope.length > 0) {
manifest = mapScopedRegistry(manifest, primaryRegistry.url, (initial) => {
let updated =
initial ?? makeEmptyScopedRegistryFor(primaryRegistry.url);
manifest = mapScopedRegistry(manifest, source, (initial) => {
let updated = initial ?? makeEmptyScopedRegistryFor(source);

updated = packagesInScope.reduce(addScope, updated!);

Expand Down
7 changes: 5 additions & 2 deletions src/cli/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PackageReference,
} from "../domain/package-reference";
import { recordEntries } from "../domain/record-utils";
import { unityRegistry } from "../domain/registry";
import { getHomePathFromEnv } from "../domain/special-paths";
import { getUserUpmConfigPathFor } from "../domain/upm-config";
import type { ReadTextFile, WriteTextFile } from "../io/fs";
Expand Down Expand Up @@ -92,6 +93,9 @@ export function makeAddCmd(
env.primaryRegistryUrl
);

const sources = [primaryRegistry];
if (env.useUnityRegistry) sources.push(unityRegistry);

const addResults = await addDependenciesUsing(
readTextFile,
writeTextFile,
Expand All @@ -100,8 +104,7 @@ export function makeAddCmd(
debugLog,
projectDirectory,
typeof editorVersion === "string" ? null : editorVersion,
primaryRegistry,
env.useUnityRegistry,
sources,
options.force === true,
options.test === true,
pkgs
Expand Down
46 changes: 20 additions & 26 deletions test/integration/app/add-dependencies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { makeEditorVersion } from "../../../src/domain/editor-version";
import { partialApply } from "../../../src/domain/fp-utils";
import { noopLogger } from "../../../src/domain/logging";
import { emptyProjectManifest } from "../../../src/domain/project-manifest";
import type { Registry } from "../../../src/domain/registry";
import { unityRegistry, type Registry } from "../../../src/domain/registry";
import { unityRegistryUrl } from "../../../src/domain/registry-url";
import { SemanticVersion } from "../../../src/domain/semantic-version";
import { getRegistryPackumentUsing } from "../../../src/io/registry";
Expand Down Expand Up @@ -117,8 +117,7 @@ describe("add dependencies", () => {
const result = await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
true,
false,
[badEditorPackument.name]
Expand All @@ -138,8 +137,7 @@ describe("add dependencies", () => {
const result = await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
true,
false,
[incompatiblePackument.name]
Expand All @@ -160,8 +158,7 @@ describe("add dependencies", () => {
addDependencies(
someProjectDir,
makeEditorVersion(2020, 1, 1, "f", 1),
someRegistry,
true,
[someRegistry, unityRegistry],
false,
false,
[incompatiblePackument.name]
Expand All @@ -176,8 +173,7 @@ describe("add dependencies", () => {
addDependencies(
someProjectDir,
makeEditorVersion(2020, 1, 1, "f", 1),
someRegistry,
true,
[someRegistry, unityRegistry],
true,
false,
[unknownPackage]
Expand All @@ -192,8 +188,7 @@ describe("add dependencies", () => {
addDependencies(
someProjectDir,
makeEditorVersion(2020, 1, 1, "f", 1),
someRegistry,
true,
[someRegistry, unityRegistry],
false,
false,
[badEditorPackument.name]
Expand All @@ -205,9 +200,14 @@ describe("add dependencies", () => {
const { addDependencies } = makeDependencies();

await expect(() =>
addDependencies(someProjectDir, null, someRegistry, true, false, false, [
packumentWithBadDependency.name,
])
addDependencies(
someProjectDir,
null,
[someRegistry, unityRegistry],
false,
false,
[packumentWithBadDependency.name]
)
).rejects.toBeInstanceOf(UnresolvedDependenciesError);
});

Expand All @@ -217,8 +217,7 @@ describe("add dependencies", () => {
const result = await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
true,
false,
[packumentWithBadDependency.name]
Expand All @@ -238,8 +237,7 @@ describe("add dependencies", () => {
await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
false,
false,
[somePackument.name]
Expand All @@ -265,8 +263,7 @@ describe("add dependencies", () => {
await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
false,
false,
[somePackument.name]
Expand All @@ -286,8 +283,7 @@ describe("add dependencies", () => {
await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
false,
false,
[somePackument.name]
Expand All @@ -313,8 +309,7 @@ describe("add dependencies", () => {
await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
false,
true,
[somePackument.name]
Expand All @@ -334,8 +329,7 @@ describe("add dependencies", () => {
await addDependencies(
someProjectDir,
null,
someRegistry,
true,
[someRegistry, unityRegistry],
false,
true,
// The second package can not be added
Expand Down

0 comments on commit 8b892ae

Please sign in to comment.