| f6bda77e Andy McCall ↶ |
1 |
// The API this extension exports to others (spec §9, "extension API"): the |
| 2 |
// same idea as the built-in Git extension's `getAPI()`. A forge extension |
| 3 |
// such as oxygenhub-vscode consumes it instead of running oxy itself, and |
| 4 |
// registers a remote provider so this extension can offer "Open on …" |
| 5 |
// links without knowing any hub. |
| 6 |
// |
| 7 |
// Pure functions live here so they can be tested with plain Node; the |
| 8 |
// vscode-facing object is assembled in extension.ts. |
| 9 |
|
| 10 |
/** What a remote-aware extension registers. */ |
| 11 |
export interface RemoteProvider { |
| 12 |
/** A stable id, e.g. `oxygenhub`. */ |
| 13 |
readonly id: string; |
| 9627171e Andy McCall ↶ |
14 |
/** Shown in menus: "Open on OxygenHub". */ |
| f6bda77e Andy McCall ↶ |
15 |
readonly label: string; |
| 16 |
/** Whether this provider understands a remote URL. */ |
| 17 |
matches(remoteUrl: string): boolean; |
| 18 |
/** A web URL for a thing in that remote, or undefined when the provider has none. */ |
| 19 |
urlFor(remoteUrl: string, target: RemoteTarget): string | undefined; |
| 20 |
} |
| 21 |
|
| 22 |
export type RemoteTarget = |
| 23 |
| { kind: "repository" } |
| 24 |
| { kind: "bookmark"; name: string } |
| 25 |
| { kind: "change"; hash: string } |
| 26 |
| { kind: "file"; reference: string; path: string; line?: number } |
| 27 |
| { kind: "proposals" } |
| 28 |
| { kind: "issues" }; |
| 29 |
|
| 30 |
/** A minimal view of one repository, stable across versions. */ |
| 31 |
export interface RepositoryHandle { |
| 32 |
readonly root: string; |
| 33 |
/** `oxy status --json` as last refreshed, or undefined before the first refresh. */ |
| 34 |
readonly status: unknown; |
| 35 |
readonly bookmarks: { name: string; heads: string[]; current?: boolean }[]; |
| 36 |
readonly currentBookmark: string | undefined; |
| 37 |
remotes(): Promise<{ name: string; url: string }[]>; |
| 38 |
/** Run an oxy command in this repository and parse its JSON output. */ |
| 39 |
json<T>(args: string[]): Promise<T>; |
| 40 |
refresh(): Promise<void>; |
| 41 |
} |
| 42 |
|
| 43 |
export interface OxygenAPI { |
| 44 |
readonly version: 1; |
| 45 |
readonly repositories: readonly RepositoryHandle[]; |
| 46 |
/** Fires with the repository whose status changed. */ |
| 47 |
readonly onDidChangeStatus: (listener: (repo: RepositoryHandle) => void) => { dispose(): void }; |
| 48 |
/** Fires when repositories are discovered or removed. */ |
| 49 |
readonly onDidChangeRepositories: (listener: () => void) => { dispose(): void }; |
| 50 |
getRepository(fsPath: string): RepositoryHandle | undefined; |
| 51 |
registerRemoteProvider(provider: RemoteProvider): { dispose(): void }; |
| 52 |
/** The providers registered so far, for callers that build their own menus. */ |
| 53 |
remoteProviders(): readonly RemoteProvider[]; |
| 54 |
} |
| 55 |
|
| 56 |
/** The provider (and remote) to use for a repository's remotes, if any. */ |
| 57 |
export function pickProvider(providers: readonly RemoteProvider[], remotes: { name: string; url: string }[]): { provider: RemoteProvider; remote: { name: string; url: string } } | undefined { |
| 58 |
// Prefer a remote called origin or earth, then any. |
| 59 |
const ordered = [...remotes].sort((a, b) => rank(a.name) - rank(b.name)); |
| 60 |
for (const remote of ordered) { |
| 61 |
for (const provider of providers) { |
| 62 |
if (provider.matches(remote.url)) { |
| 63 |
return { provider, remote }; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
return undefined; |
| 68 |
} |
| 69 |
|
| 70 |
function rank(name: string): number { |
| 71 |
return name === "origin" ? 0 : name === "hub" ? 1 : 2; |
| 72 |
} |
| 73 |
|
| 74 |
/** The repository handle a folder path belongs to: the longest root that prefixes it. */ |
| 75 |
export function repositoryFor(roots: string[], fsPath: string): string | undefined { |
| 76 |
let best: string | undefined; |
| 77 |
for (const r of roots) { |
| 78 |
if ((fsPath === r || fsPath.startsWith(r.endsWith("/") ? r : r + "/")) && (!best || r.length > best.length)) { |
| 79 |
best = r; |
| 80 |
} |
| 81 |
} |
| 82 |
return best; |
| 83 |
} |