Файли
openidea/src/features/dotnet/NugetView.tsx
T
LU XIANGRUIтаwildcatDownstairs 115003f0b6 feat(nuget): central package management awareness and one-click update (#7434)
The NuGet Packages window reads Directory.Packages.props: each package shows where its version is defined or pinned and any VersionOverride, and Check NuGet Package Updates offers an update to the newest version.

Metadata and update checks are scoped to the selected project, configuration and framework and run independently; one-click updates are withheld when compatibility across target frameworks is not verified.

Co-authored-by: wildcatDownstairs <[email protected]>
2026-12-02 18:23:33 +08:00

189 рядки
7.3 KiB
TypeScript

/**
* NuGet tool window: the selected .NET project's packages, with search and
* install / update / remove. The package management that used to live in the
* standalone .NET tool window (#5635).
*/
import { useEffect, useState } from "react";
import { executeAction } from "@/platform/actions";
import { useTx } from "@/platform/i18n";
import { Button, Input } from "@/ui";
import { compareVersions, useCentral } from "./central";
import { ensureDotnetModel, useDotnet } from "./store";
import { SourcesPanel } from "./SourcesPanel";
import styles from "./Nuget.module.css";
const baseName = (path: string) => path.replace(/\\/g, "/").split("/").pop() ?? path;
export function NugetView() {
const s = useDotnet();
const tx = useTx();
const [versions, setVersions] = useState<Record<string, string>>({});
const results = new Map<string, string[]>();
for (const p of s.found) results.set(p.id, [...new Set([...(results.get(p.id) ?? []), p.version])]);
const project = s.model.projects.find((p) => p.path === s.selected);
const central = useCentral();
const matches =
central.project === s.selected && central.framework === s.framework && central.configuration === s.configuration;
const info = matches ? central.info : null;
const working = s.busy || central.busy;
const knownLatest = (p: { id: string; version: string }) => {
const latest = matches ? central.latest[p.id.toLowerCase()] : undefined;
return latest && compareVersions(latest, p.version) > 0 ? latest : undefined;
};
const note = (p: { id: string; version: string; transitive?: boolean }) => {
const key = p.id.toLowerCase();
const override = info?.overrides[key];
const file = baseName(info?.versions[key]?.file ?? "");
const latest = knownLatest(p);
return [
override ? tx`Version override ${override} in the project` : "",
!override && file ? (p.transitive ? tx`Pinned in ${file}` : tx`Defined in ${file}`) : "",
latest ? tx`Newer version available: ${latest}` : "",
]
.filter(Boolean)
.join(" · ");
};
const action = (id: string, arg?: unknown) => void executeAction(`dotnet.${id}`, arg);
// The workspace scan used to run when the standalone .NET window opened; without it the
// window shows no project to manage packages for.
useEffect(() => {
void ensureDotnetModel();
}, []);
// Where versions live (Directory.Packages.props, VersionOverride) follows the selected project.
useEffect(() => {
if (s.selected) void executeAction("dotnet.centralRefresh");
}, [s.selected, s.framework, s.configuration]);
return (
<div className={styles.view}>
<div className={styles.toolbar}>
<Button disabled={s.busy} onClick={() => action("packages")}>
{tx("Refresh packages")}
</Button>
<Button disabled={working || !project} onClick={() => action("checkUpdates")}>
{tx("Check for updates")}
</Button>
<label className={styles.inline}>
<input
type="checkbox"
checked={central.prerelease}
disabled={working}
onChange={(e) => useCentral.setState({ prerelease: e.target.checked })}
/>
{tx("Include prereleases")}
</label>
{working && <Button onClick={() => action("cancel")}>{tx("Cancel")}</Button>}
</div>
{matches && central.error && (
<div role="alert" className={styles.error}>
{central.error}
</div>
)}
{info?.enabled && (
<div role="status">{tx("Central package management is on: versions are set in Directory.Packages.props.")}</div>
)}
{s.error && (
<div role="alert" className={styles.error}>
{s.error}
</div>
)}
<label>
{tx(".NET project")}
<select
aria-label={tx(".NET project")}
value={s.selected}
disabled={s.busy}
onChange={(e) => action("select", { path: e.target.value })}
>
{!project && <option value="">{tx("Select a .NET project")}</option>}
{s.model.projects.map((p) => (
<option key={p.path} value={p.path}>
{p.name}
</option>
))}
</select>
</label>
{project && (
<>
<SourcesPanel project={project.path} />
<form
className={styles.toolbar}
onSubmit={(e) => {
e.preventDefault();
action("search", { query: s.query });
}}
>
<Input
aria-label={tx("Search NuGet packages")}
value={s.query}
onChange={(e) => useDotnet.setState({ query: e.target.value })}
/>
<Button type="submit" disabled={s.busy || !s.query.trim()}>
{tx("Search")}
</Button>
</form>
<ul className={styles.list}>
{[...results].map(([id, available]) => {
const version = available.includes(versions[id] ?? "") ? versions[id]! : available[0]!;
return (
<li key={id}>
<span>{id}</span>
<select
aria-label={tx("Version")}
value={version}
disabled={s.busy}
onChange={(e) => setVersions({ ...versions, [id]: e.target.value })}
>
{available.map((v) => (
<option key={v}>{v}</option>
))}
</select>
<Button disabled={s.busy} onClick={() => action("packageChange", { id, version })}>
{tx("Install / Update")}
</Button>
<Button disabled={s.busy} onClick={() => action("search", { query: id, exact: true })}>
{tx("Versions")}
</Button>
</li>
);
})}
</ul>
<ul className={styles.list}>
{s.packages.map((p) => {
const latest = knownLatest(p);
return (
<li key={`${p.id}:${p.version}`}>
<span>
{p.id} {p.version} {p.transitive ? tx("Transitive") : ""}
{note(p) && ` · ${note(p)}`}
</span>
{latest && !p.transitive && (project?.frameworks.length ?? 0) <= 1 && (
<Button disabled={working} onClick={() => action("packageChange", { id: p.id, version: latest })}>
{tx`Update to ${latest}`}
</Button>
)}
{!p.transitive && (
<>
<Button disabled={s.busy} onClick={() => action("search", { query: p.id, exact: true })}>
{tx("Versions")}
</Button>
<Button disabled={s.busy} onClick={() => action("packageChange", { id: p.id, version: null })}>
{tx("Remove")}
</Button>
</>
)}
</li>
);
})}
</ul>
</>
)}
{s.output && (
<details>
<summary>{tx("Output")}</summary>
<pre className={styles.output}>{s.output}</pre>
</details>
)}
</div>
);
}