Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix artifact search to work well with multiple versions of artifacts and registries #608

Merged
merged 4 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ce/ce/artifacts/artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class Artifact extends ArtifactBase {
if (!installer) {
fail(i`Unknown installer type ${installInfo!.installerKind}`);
}
await installer(this.session, this.id, this.targetLocation, installInfo, events, options);
await installer(this.session, this.id, this.version, this.targetLocation, installInfo, events, options);
}

// after we unpack it, write out the installed manifest
Expand Down
32 changes: 25 additions & 7 deletions ce/ce/cli/commands/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// Licensed under the MIT License.


import { cyan } from 'chalk';
import { i } from '../../i18n';
import { session } from '../../main';
import { Registries } from '../../registries/registries';
import { Command } from '../command';
import { artifactIdentity } from '../format';
import { Table } from '../markdown-table';
import { debug, log } from '../styling';
import { debug, error, log } from '../styling';
import { Project } from '../switches/project';
import { Registry } from '../switches/registry';
import { Version } from '../switches/version';
Expand Down Expand Up @@ -42,17 +43,34 @@ export class FindCommand extends Command {
const table = new Table('Artifact', 'Version', 'Summary');

for (const each of this.inputs) {
for (const [registry, id, artifacts] of await registries.search({ keyword: each, version: this.version.value })) {
const latest = artifacts[0];
if (!latest.metadata.info.dependencyOnly) {
const name = artifactIdentity(latest.registryId, id, latest.shortName);
table.push(name, latest.metadata.info.version, latest.metadata.info.summary || '');
const hasColon = each.indexOf(':') > -1;
// eslint-disable-next-line prefer-const
for (let [registry, id, artifacts] of await registries.search({
// use keyword search if no registry is specified
keyword: hasColon ? undefined : each,
// otherwise use the criteria as an id
idOrShortName: hasColon ? each : undefined,
version: this.version.value
})) {
if (!this.version.isRangeOfVersions) {
// if the user didn't specify a range, just show the latest version that was returned
artifacts = [artifacts[0]];
}
for (const result of artifacts) {
if (!result.metadata.info.dependencyOnly) {
const name = artifactIdentity(result.registryId, id, result.shortName);
table.push(name, result.metadata.info.version, result.metadata.info.summary || '');
}
}
}
}
if (!table.anyRows) {
error(i`No artifacts found matching criteria: ${cyan.bold(this.inputs.join(', '))}`);
return false;
}

log(table.toString());
log();
return true;
}
}
}
2 changes: 1 addition & 1 deletion ce/ce/cli/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function artifactIdentity(registryName: string, identity: string, alias?:
}

export function artifactReference(registryName: string, identity: string, version: string) {
return version && version !== '*' ? `${artifactIdentity(registryName, identity)}-v${gray(version)}` : artifactIdentity(registryName, identity);
return version && version !== '*' ? `${artifactIdentity(registryName, identity)}-${gray(version)}` : artifactIdentity(registryName, identity);
}

export function heading(text: string, level = 1) {
Expand Down
2 changes: 2 additions & 0 deletions ce/ce/cli/markdown-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ marked.setOptions({
export class Table {
private readonly rows = new Array<string>();
private numberOfColumns = 0;
public anyRows = false;
constructor(...columnNames: Array<string>) {
this.numberOfColumns = columnNames.length;
this.rows.push(`|${columnNames.join('|')}|`);
Expand All @@ -44,6 +45,7 @@ export class Table {
push(...values: Array<string>) {
strict.equal(values.length, this.numberOfColumns, 'unexpected number of arguments in table row');
this.rows.push(`|${values.join('|')}|`);
this.anyRows = true;
}
toString() {
return marked.marked(this.rows.join('\n'));
Expand Down
3 changes: 3 additions & 0 deletions ce/ce/cli/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ export abstract class Switch implements Help {
const v = this.values;
return !!v && v.length > 0 && v[0] !== 'false';
}
get isRangeOfVersions() {
return !!/[*[\]()~^]/.exec(this.value);
}
}
4 changes: 1 addition & 3 deletions ce/ce/installers/espidf.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { platform } from 'os';
import { delimiter } from 'path';
import { log } from '../cli/styling';
import { i } from '../i18n';
import { InstallEvents } from '../interfaces/events';
import { Session } from '../session';
Expand Down Expand Up @@ -89,7 +87,7 @@ export async function activateEspIdf(session: Session, targetLocation: Uri) {
const pathValues = splitLine[1].split(delimiter);
for (const path of pathValues) {
if (path.trim() !== '%PATH%' && path.trim() !== '$PATH') {
// we actually want to use the artifacts we installed, not the ones that are being bundled.
// we actually want to use the artifacts we installed, not the ones that are being bundled.
// when espressif supports artifacts properly, we shouldn't need this filter.
if (! /\.espressif.tools/ig.exec(path)) {
session.activation.addPath(splitLine[0].trim(), session.fileSystem.file(path));
Expand Down
2 changes: 1 addition & 1 deletion ce/ce/installers/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Session } from '../session';
import { Uri } from '../util/uri';
import { Vcpkg } from '../vcpkg';

export async function installGit(session: Session, name: string, targetLocation: Uri, install: GitInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions & CloneOptions & CloneSettings>): Promise<void> {
export async function installGit(session: Session, name: string, version: string, targetLocation: Uri, install: GitInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions & CloneOptions & CloneSettings>): Promise<void> {
const vcpkg = new Vcpkg(session);

const gitPath = await vcpkg.fetch('git');
Expand Down
2 changes: 1 addition & 1 deletion ce/ce/installers/nuget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Session } from '../session';
import { Uri } from '../util/uri';
import { applyAcquireOptions } from './util';

export async function installNuGet(session: Session, name: string, targetLocation: Uri, install: NupkgInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
export async function installNuGet(session: Session, name: string, version: string, targetLocation: Uri, install: NupkgInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
const file = await acquireNugetFile(session, install.location, `${name}.zip`, events, applyAcquireOptions(options, install));

return new ZipUnpacker(session).unpack(
Expand Down
4 changes: 2 additions & 2 deletions ce/ce/installers/untar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { Uri } from '../util/uri';
import { applyAcquireOptions, artifactFileName } from './util';


export async function installUnTar(session: Session, name: string, targetLocation: Uri, install: UnTarInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
const file = await acquireArtifactFile(session, [...install.location].map(each => session.parseUri(each)), artifactFileName(name, install, '.tar'), events, applyAcquireOptions(options, install));
export async function installUnTar(session: Session, name: string, version: string, targetLocation: Uri, install: UnTarInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
const file = await acquireArtifactFile(session, [...install.location].map(each => session.parseUri(each)), artifactFileName(name, version, install, '.tar'), events, applyAcquireOptions(options, install));
const x = await file.readBlock(0, 128);
let unpacker: Unpacker;
if (x[0] === 0x1f && x[1] === 0x8b) {
Expand Down
4 changes: 2 additions & 2 deletions ce/ce/installers/unzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import { Session } from '../session';
import { Uri } from '../util/uri';
import { applyAcquireOptions, artifactFileName } from './util';

export async function installUnZip(session: Session, name: string, targetLocation: Uri, install: UnZipInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
const file = await acquireArtifactFile(session, [...install.location].map(each => session.parseUri(each)), artifactFileName(name, install, '.zip'), events, applyAcquireOptions(options, install));
export async function installUnZip(session: Session, name: string, version: string, targetLocation: Uri, install: UnZipInstaller, events: Partial<InstallEvents>, options: Partial<InstallOptions>): Promise<void> {
const file = await acquireArtifactFile(session, [...install.location].map(each => session.parseUri(each)), artifactFileName(name, version, install, '.zip'), events, applyAcquireOptions(options, install));
await new ZipUnpacker(session).unpack(
file,
targetLocation,
Expand Down
12 changes: 10 additions & 2 deletions ce/ce/installers/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AcquireOptions } from '../fs/acquire';
import { Installer } from '../interfaces/metadata/installers/Installer';
import { Verifiable } from '../interfaces/metadata/installers/verifiable';

export function artifactFileName(name: string, install: Installer, extension: string): string {
export function artifactFileName(name: string, version: string, install: Installer & Verifiable, extension: string): string {
let result = name;
if (install.nametag) {
result += '-';
Expand All @@ -16,9 +16,17 @@ export function artifactFileName(name: string, install: Installer, extension: st
result += '-';
result += install.lang;
}
// add the version number into the filename too.
result += '-' + version;

// if there is a sha256 or sha512 hash in the install, add it to the filename
const hash = (install.sha256 || install.sha512 || '');
if (hash) {
result += `-(${hash})`;
}

result += extension;
return result.replace(/[^\w]+/g, '.');
return result.replace(/[^\w()-]+/g, '.');
}

export function applyAcquireOptions(options: AcquireOptions, install: Verifiable): AcquireOptions {
Expand Down
7 changes: 4 additions & 3 deletions ce/ce/registries/ArtifactRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,16 @@ export abstract class ArtifactRegistry implements Registry {

if (criteria?.idOrShortName) {
query.id.nameOrShortNameIs(criteria.idOrShortName);
if (criteria.version) {
query.version.rangeMatch(criteria.version);
}
}

if (criteria?.keyword) {
query.id.contains(criteria.keyword);
}

if (criteria?.version) {
query.version.rangeMatch(criteria.version);
}

return [...(await this.openArtifacts(query.items, parent)).entries()].map(each => [this, ...each]);
}

Expand Down
4 changes: 2 additions & 2 deletions ce/ce/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { isYAML } from './yaml/yaml';
type InstallerTool<T extends Installer = any> = (
session: Session,
name: string,
version: string,
targetLocation: Uri,
install: T,
events: Partial<InstallEvents>,
Expand Down Expand Up @@ -151,9 +152,8 @@ export class Session {
return undefined;
}


async loadRegistry(registryLocation: Uri | string | undefined, registryKind = 'artifact'): Promise<Registry | undefined> {
// normalize the location first.
// normalize the location first.

registryLocation = typeof registryLocation === 'string' ? await this.parseLocation(registryLocation) || this.parseUri(registryLocation) : registryLocation;

Expand Down