Skip to content

Commit

Permalink
Expose more installation info
Browse files Browse the repository at this point in the history
  • Loading branch information
salamaashoush committed Apr 28, 2024
1 parent 1c2a14f commit bacd231
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 22 deletions.
7 changes: 7 additions & 0 deletions apps/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# pact-toolbox

## 0.0.12

### Patch Changes

- Updated dependencies
- @pact-toolbox/installer@0.0.9

## 0.0.11

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pact-toolbox",
"version": "0.0.11",
"version": "0.0.12",
"description": "A tool to help you build, test and deploy your Pact smart contracts",
"keywords": [
"pact",
Expand Down
6 changes: 6 additions & 0 deletions packages/installer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @pact-toolbox/installer

## 0.0.9

### Patch Changes

- Expose more installation info

## 0.0.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/installer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pact-toolbox/installer",
"version": "0.0.8",
"version": "0.0.9",
"repository": "kadena-community/pact-toolbox",
"author": "Salama Ashoush <[email protected]>",
"license": "MIT",
Expand Down
9 changes: 5 additions & 4 deletions packages/installer/src/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { PactRemoteAssetInfo } from './releaseInfo';

export interface InstalledPactVersionMetadata extends PactRemoteAssetInfo {
version: string;
binary: string;
pactExecutable: string;
pactExecutablePath: string;
files: string[];
}
export async function activatePactVersion(version: string, binary?: string) {
export async function activatePactVersion(version: string, pactExecutable?: string) {
const managedVersions = await listInstalledPactVersions();
const installedVersion = managedVersions.find((v) => normalizeVersion(v.version).includes(normalizeVersion(version)));
if (!installedVersion) {
Expand All @@ -27,8 +28,8 @@ export async function activatePactVersion(version: string, binary?: string) {
.catch(() => {
throw new Error(`Could not find metadata for version ${version}`);
});
binary = binary || metadata.binary;
const pactBinary = binary.startsWith('/') ? binary : join(versionPath, binary);
pactExecutable = pactExecutable || metadata.pactExecutable;
const pactBinary = pactExecutable.startsWith('/') ? pactExecutable : join(versionPath, pactExecutable);
if (!existsSync(pactBinary)) {
throw new Error(`Could not find binary ${pactBinary}`);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/installer/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export async function extractTarball(
}
await directory.extract({ path: dest, verbose: true });
for (const entry of directory.files) {
files.push(entry.path);
if (entry.type === 'File' && (!filter || filter.some((f) => entry.path.includes(f)))) {
files.push(entry.path);
}
}
}
// make it executable
Expand Down
50 changes: 40 additions & 10 deletions packages/installer/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { activatePactVersion } from './activate';
import { KADENA_BIN_DIR, PACT4X_REPO, PACT5X_REPO, PACT_ROOT_DIR, Z3_URL } from './constants';
import { updateShellProfileScript } from './env';
import { extractTarball } from './extract';
import { listInstalledPactVersions } from './list';
import { InstalledPactVersion, listInstalledPactVersions } from './list';
import {
PactRemoteAssetInfo,
downloadTarball,
Expand Down Expand Up @@ -57,7 +57,23 @@ export async function isPactVersionInstalled(version: string) {
return installedVersions.some((v) => areVersionMatching(v.version, version)) || !!currentVersion;
}

export async function getPactInstallationInfo({ version, nightly }: PactInstallCommonOptions = {}) {
export interface PactInstallationInfo {
foundAnyVersion: boolean;
updatedAt: string;
isNightlyVersion: boolean;
isInstalled: boolean;
currentVersion?: string;
latestVersion: string;
isManagedVersion: boolean;
isCurrentVersion: boolean;
isUpToDate: boolean;
versionInfo: InstalledPactVersion;
}

export async function getPactInstallationInfo({
version,
nightly,
}: PactInstallCommonOptions = {}): Promise<PactInstallationInfo> {
const latestRelease = await fetchLatestPactGithubRelease(nightly ? PACT5X_REPO : PACT4X_REPO);
const latestVersion = latestRelease.tag_name;
if (!version) {
Expand All @@ -71,7 +87,7 @@ export async function getPactInstallationInfo({ version, nightly }: PactInstallC
const isCurrentVersion = await isActivePactVersion(version, currentVersion);
const isInstalled = isCurrentVersion || managedVersions.some((mv) => areVersionMatching(mv.version, version));
const isNightlyVersion = !!currentVersion && isNightlyPactVersion(currentVersion);
const foundManagedVersion = managedVersions.find((mv) => areVersionMatching(mv.version, version));
const foundManagedVersion = managedVersions.find((mv) => areVersionMatching(mv.version, version))!;
let isUpToDate = false;
if (latestRelease.assets.length > 0) {
const latestUpdate = latestRelease.assets[0].updated_at;
Expand All @@ -93,6 +109,7 @@ export async function getPactInstallationInfo({ version, nightly }: PactInstallC
isManagedVersion,
isCurrentVersion,
isUpToDate,
versionInfo: foundManagedVersion ?? {},
};
}

Expand All @@ -112,16 +129,21 @@ export async function installZ3() {
logger.success(`Z3 installed successfully 🎉`);
}

export async function writeVersionMetadata(asset: PactRemoteAssetInfo, binary?: string, files: string[] = []) {
export async function writeVersionMetadata(
asset: PactRemoteAssetInfo,
pactExecutable: string = 'pact',
files: string[] = [],
) {
const dest = join(PACT_ROOT_DIR, asset.version);
// write metadata file
await writeFileAtPath(
join(dest, 'metadata.json'),
JSON.stringify(
{
...asset,
binary,
files,
pactExecutable,
pactExecutablePath: join(dest, pactExecutable),
},
null,
2,
Expand All @@ -133,12 +155,13 @@ export interface InstallPactOptions extends PactInstallCommonOptions {
activate?: boolean;
force?: boolean;
}

export async function installPact({
version,
nightly = false,
force = false,
activate = false,
}: InstallPactOptions = {}) {
}: InstallPactOptions = {}): Promise<InstalledPactVersion> {
const repo = nightly ? PACT5X_REPO : PACT4X_REPO;
const releases = await fetchPactGithubReleases(repo);
if (!version) {
Expand All @@ -147,14 +170,14 @@ export async function installPact({
version = (await findPactRelease({ version, nightly })).tag_name;
}
const assetInfo = await getPactRemoteAssetInfo(releases, version, nightly);
const { isInstalled, currentVersion } = await getPactInstallationInfo({
const { isInstalled, currentVersion, versionInfo } = await getPactInstallationInfo({
version: assetInfo.version,
nightly,
});
const versionDir = join(PACT_ROOT_DIR, assetInfo.version);
if (isInstalled && !force) {
logger.info(`Pact version ${assetInfo.version} is already installed at ${versionDir}`);
return;
return versionInfo;
}
logger.start(`Installing Pact version ${assetInfo.version}`);
const path = await downloadTarball(assetInfo.downloadUrl);
Expand All @@ -173,9 +196,16 @@ export async function installPact({
await updateShellProfileScript();

return {
binaryPath: join(versionDir, binary ?? 'pact'),
...versionInfo,
pactExecutablePath: join(versionDir, binary ?? 'pact'),
pactExecutable: binary ?? 'pact',
files,
...assetInfo,
version: assetInfo.version,
updatedAt: assetInfo.updatedAt,
createdAt: assetInfo.createdAt,
downloadUrl: assetInfo.downloadUrl,
isActive: Boolean(activate || !currentVersion || versionInfo?.isActive),
path: versionDir,
};
}

Expand Down
9 changes: 4 additions & 5 deletions packages/installer/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ export async function listInstalledPactVersions(includeNightly = true) {
const areNightly = isNightlyPactVersion(f) && currentVersion && isNightlyPactVersion(currentVersion);
const isActive = await isActivePactVersion(f, currentVersion);
const path = join(PACT_ROOT_DIR, f);
const metadata = await readFile(join(path, 'metadata.json'), 'utf-8')
.then(JSON.parse)
.catch(() => undefined);
const metadata: InstalledPactVersionMetadata = await readFile(join(path, 'metadata.json'), 'utf-8').then(
JSON.parse,
);
versions.push({
path,
version: f,
isActive: areNightly || isActive,
...metadata,
...(metadata ?? {}),
});
}

Expand Down

0 comments on commit bacd231

Please sign in to comment.