Skip to content

Commit

Permalink
WIP: deprecate type and platforms
Browse files Browse the repository at this point in the history
- use discrete os, arch, and PackageFormat to in place of combines Platform strings.
  • Loading branch information
dopry committed Sep 12, 2023
1 parent d3c770c commit 7fecd55
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
23 changes: 20 additions & 3 deletions src/models/PecansAsset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { extname } from "path";
import {
Architecture,
filenameToArchitecture,
filenameToArchitectureLegacy,
filenameToOperatingSystem,
filenameToPackageFormat,
filenameToSupportedArchitecture,
OperatingSystem,
PackageFormat,
Platform,
SupportedArchitecture,
} from "../utils";
import { SupportedFileExtension } from "../utils/SupportedFileExtension";
import { PecansAssetQuery } from "./PecansAssetQuery";
Expand All @@ -17,16 +19,19 @@ export interface PecansAssetDTO {
id: string;
raw: any;
size: number;
// TODO: use os, arch, and pkg in place of platform.
// @deprecated use os, arch, and pkg in place of platform.
type: Platform;
}

export class PecansAsset implements PecansAssetDTO {
os: OperatingSystem;
// @deprecated, should be using archs instead. remove once old arch/type is removed from pecans.
arch: Architecture;
archs: SupportedArchitecture[];
pkg?: PackageFormat;
id: string;
filename: string;
// @deprecated, use os, archs, and pkg instead for filtering.
type: Platform;
size: number;
content_type: string;
Expand All @@ -40,14 +45,16 @@ export class PecansAsset implements PecansAssetDTO {
this.size = dto.size;
this.type = dto.type;
this.os = filenameToOperatingSystem(this.filename);
this.arch = filenameToArchitecture(this.filename, this.os);
this.arch = filenameToArchitectureLegacy(this.filename, this.os);
this.archs = filenameToSupportedArchitecture(this.filename);
this.pkg = filenameToPackageFormat(this.filename);
}

satisfiesQuery(query: PecansAssetQuery) {
return (
this.satisfiesOS(query.os) &&
this.satisfiesArch(query.arch) &&
this.satisfiesSupportedArch(query.supportedArch) &&
this.satisfiesPkg(query.pkg) &&
this.satisfiesFilename(query.filename) &&
this.satisfiesExtensions(query.extensions)
Expand All @@ -58,10 +65,16 @@ export class PecansAsset implements PecansAssetDTO {
return os == undefined || this.os == os;
}

// @deprecated, use satisfiesSupportedArch instead.
satisfiesArch(arch?: Architecture) {
return arch == undefined || this.arch == arch;
}

// TODO: renamve to satisfiesArch after removing the old satisfiesArch.
satisfiesSupportedArch(arch?: SupportedArchitecture) {
return arch == undefined || this.archs.includes(arch);
}

satisfiesPkg(pkg?: PackageFormat) {
return pkg == undefined || this.pkg == pkg;
}
Expand All @@ -76,3 +89,7 @@ export class PecansAsset implements PecansAssetDTO {
return extensions.includes(ext as SupportedFileExtension);
}
}

export function isPecansAsset(obj: unknown): obj is PecansAsset {
return obj instanceof PecansAsset;
}
10 changes: 9 additions & 1 deletion src/models/PecansAssetQuery.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import { Architecture, OperatingSystem, PackageFormat } from "../utils";
import {
Architecture,
OperatingSystem,
PackageFormat,
SupportedArchitecture,
} from "../utils";
import { SupportedFileExtension } from "../utils/SupportedFileExtension";

export interface PecansAssetQuery {
os?: OperatingSystem;
// @deprecated, use supportedArch instead.
arch?: Architecture;
// TODO: rename to arch after removing the current arch.
supportedArch?: SupportedArchitecture;
pkg?: PackageFormat;
filename?: string;
extensions?: SupportedFileExtension[];
Expand Down
6 changes: 1 addition & 5 deletions src/models/PecansRelease.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { satisfies, validRange } from "semver";
import { channelFromVersion as channelFromVersion } from "../utils";
import { PecansAssetQuery } from "./PecansAssetQuery";
import { PecansAsset, PecansAssetDTO } from "./PecansAsset";
import { PecansAsset, PecansAssetDTO, isPecansAsset } from "./PecansAsset";
import { PecansReleaseQuery } from "./PecansReleaseQuery";

export interface PecansReleaseDTO {
Expand All @@ -14,10 +14,6 @@ export interface PecansReleaseDTO {
version: string;
}

export function isPecansAsset(obj: unknown): obj is PecansAsset {
return obj instanceof PecansAsset;
}

export class PecansRelease implements PecansReleaseDTO {
assets: PecansAsset[];
channel: string;
Expand Down
42 changes: 41 additions & 1 deletion src/utils/Architecture.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
import { OperatingSystem } from "./OperatingSystem";

/**
* These are based on NODE.js os.arch() values
* @see: https://nodejs.org/api/os.html#osarch
*/
export const SUPPORTED_ARCHITECTURES = ["arm64", "x32", "x64"];
export type SupportedArchitecture = (typeof SUPPORTED_ARCHITECTURES)[number];

export function isSupportedArchitecture(
obj: unknown
): obj is SupportedArchitecture {
return (
typeof obj == "string" &&
SUPPORTED_ARCHITECTURES.includes(obj as SupportedArchitecture)
);
}

export function filenameToSupportedArchitecture(
filename: string
): SupportedArchitecture[] {
const archs: SupportedArchitecture[] = [];
const name = filename.toLowerCase();
// this is the squirrel releases file.
// TODO: update to RELEASES-{arch} and modify pecans to have architecture specific release endpoints.
// TODO: Explore aggregating RELEASES from multiple Github Releases into a single release file and how that may interact with deltas.
if (name == "releases") return ["x32", "x64"];
if (name.includes("arm64")) archs.push("arm64");
if (name.includes("x32")) archs.push("x32");
if (name.includes("x64")) archs.push("x64");
if (name.includes("universal") || name.includes("univ"))
archs.push("x64", "x32", "arm64");
return archs;
}

/**
* Architecture string identifiers
*/
// @deprecated
export const ARCHITECTURES = ["32", "64", "arm64", "universal"] as const;
// @deprecated
export type Architecture = (typeof ARCHITECTURES)[number];

// check if a string is an architecture identifier
// @deprecated
export function isArchitecture(obj: unknown): obj is Architecture {
return typeof obj == "string" && ARCHITECTURES.includes(obj as Architecture);
}

export function filenameToArchitecture(
// deprecated
export function filenameToArchitectureLegacy(
filename: string,
os: OperatingSystem
): Architecture {
Expand All @@ -33,6 +71,7 @@ export function filenameToArchitecture(
return "64";
}

// @deprecated
export function getSupportedArchByOs(os: OperatingSystem): Architecture[] {
switch (os) {
case "osx":
Expand All @@ -45,6 +84,7 @@ export function getSupportedArchByOs(os: OperatingSystem): Architecture[] {
}
}

// @deprecated
export function isValidArchForOS(
os: OperatingSystem,
arch: string
Expand Down
8 changes: 4 additions & 4 deletions src/utils/platforms.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { platform } from "os";
import { filenameToArchitecture } from "./Architecture";
import { filenameToArchitectureLegacy } from "./Architecture";
import {
filenameToOperatingSystem,
isOperatingSystem,
Expand All @@ -9,7 +8,7 @@ import { filenameToPackageFormat } from "./PackageFormat";

// platform identifier
// {{os}}_{{packaging system}}_{{arch}}
// TODO: refactor so resolution works off of discrete os, arch, pkg rather than these composite strings.
// @deprecated, use discrete os, arch, and pkg properties rather than these composite strings.
export const PLATFORMS = [
"linux",
"linux_32",
Expand All @@ -30,6 +29,7 @@ export const PLATFORMS = [
"windows_64",
] as const;

// @deprecated user os, archs, and pkg instead.
export type Platform = (typeof PLATFORMS)[number];

export const platforms: Record<string, Platform> = {
Expand Down Expand Up @@ -110,7 +110,7 @@ export function filenameToPlatform(filename: string): Platform {
const pkg = filenameToPackageFormat(name);
// pkg is optional and typically only with linux.
pkg && parts.push(pkg);
const arch = filenameToArchitecture(name, os);
const arch = filenameToArchitectureLegacy(name, os);
parts.push(arch);
const platformKey = parts.join("_").toUpperCase();
return platforms[platformKey];
Expand Down

0 comments on commit 7fecd55

Please sign in to comment.