Skip to content

Commit

Permalink
chore: ++appliaction-flavors
Browse files Browse the repository at this point in the history
  • Loading branch information
seankwarren committed Feb 9, 2024
1 parent 54207b0 commit b370947
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
}
],
"@babel/preset-react",
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-proposal-class-properties"
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@babel/preset-react": "7.16.7",
"@babel/register": "^7.16.0",
"@babel/runtime-corejs3": "7.16.8",
"@exabyte-io/application-flavors.js": "2024.1.23-1",
"@exabyte-io/application-flavors.js": "https://github.com/Exabyte-io/application-flavors.git#513e4daf188c1ef1fcfe8195e2c90e289f616296",
"@exabyte-io/code.js": "https://github.com/Exabyte-io/code.js.git#b3eaf74a11cf0b35a1f0bac32e74452fe0329953",
"@exabyte-io/eslint-config": "^2022.11.17-0",
"@exabyte-io/made.js": "^2024.1.8-0",
Expand All @@ -70,7 +70,7 @@
"ts-node": "^10.9.2"
},
"peerDependencies": {
"@exabyte-io/code.js": "https://github.com/Exabyte-io/code.js.git#b3eaf74a11cf0b35a1f0bac32e74452fe0329953",
"@exabyte-io/code.js": "*",
"@exabyte-io/made.js": "*",
"@exabyte-io/application-flavors.js": "*"
},
Expand Down
12 changes: 7 additions & 5 deletions src/application.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-expect-error application-flavors.js is not typed
import { allApplications, getAppData, getAppTree } from "@exabyte-io/application-flavors.js";
import { AllowedApplications, ApplicationTreeData, allApplications, getAppData, getAppTree } from "@exabyte-io/application-flavors.js";
import { NamedDefaultableInMemoryEntity } from "@exabyte-io/code.js/dist/entity";

import lodash from "lodash";
Expand All @@ -21,7 +20,8 @@ export function ApplicationMixin<

constructor(...args: any[]) {
const config = args[0] as ApplicationConfig;
if (!config || typeof config.name !== "string") throw new Error("Invalid application configuration object.");
if (!config || !config?.name) throw new Error("Invalid application configuration object.");
if (!allApplications.includes(config.name)) throw new Error(`Invalid application name: ${config.name}`);
const staticConfig = getApplicationConfig(config);
super({ ...staticConfig, ...config });
}
Expand All @@ -38,7 +38,7 @@ export function ApplicationMixin<
}

static create(config: {
name: string,
name: AllowedApplications,
version?: string,
build?: string
}) {
Expand All @@ -50,7 +50,7 @@ export function ApplicationMixin<
version = undefined,
build = "Default"
}: {
name: string,
name: AllowedApplications,
version?: string,
build?: string
}) {
Expand Down Expand Up @@ -81,6 +81,7 @@ export function ApplicationMixin<
}

getExecutableByName(name?: string) {
console.log(`getExecutableByName: ${name}`)
return new Application.Executable(
getExecutableConfig({
appName: this.prop("name"),
Expand Down Expand Up @@ -123,6 +124,7 @@ export function ApplicationMixin<
const tree = getAppTree(this.prop("name"));
return Object.keys(tree)
.filter((key) => {
// @ts-ignore
const { supportedApplicationVersions } = tree[key];
return (
!supportedApplicationVersions ||
Expand Down
3 changes: 1 addition & 2 deletions src/template.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-expect-error application-flavors.js is not typed
import { allTemplates } from "@exabyte-io/application-flavors.js";
import {
NamedInMemoryEntity,
Expand Down Expand Up @@ -96,7 +95,7 @@ export function TemplateMixin<

static fromFlavor(appName: string, execName: string, inputName: string): Template {
const filtered = allTemplates.filter(
(temp: TemplateData) =>
(temp) =>
temp.applicationName === appName &&
temp.executableName === execName &&
temp.name === inputName,
Expand Down
87 changes: 56 additions & 31 deletions src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,61 @@
// @ts-expect-error application-flavors.js is not typed
import { allApplications, getAppData, getAppTree } from "@exabyte-io/application-flavors.js";
import { AllowedApplications, allApplications, getAppData, getAppTree } from "@exabyte-io/application-flavors.js";
import { getOneMatchFromObject } from "@exabyte-io/code.js/dist/utils";
import { AppTree, ExecutableData } from "./types";
import { ExecutableData } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";
import { ApplicationData, ApplicationTree } from "@exabyte-io/application-flavors.js/lib/js/build_templates";

type AppTree = {
// application
[appName in AllowedApplications]: {
// version
defaultVersion: string
} & {
[version: string]: {
// build
[build: string]: {
build: string;
name: string;
summary: string;
shortName: string;
}
}
};
}

type AppArray = {
version: string;
build: string;
name: string;
summary: string;
shortName: string;
isDefault: boolean;
}[];

/**
* @summary Return all applications as both a nested object of Applications and an array of config objects
* @param cls optional class to use to create applications
* @returns containing applications and applicationConfigs
*/
export function getAllApplications(cls?: Constructor<any>) {
const applicationsTree = {};
// @ts-expect-error application-flavors is not typed
const applicationsArray = [];
allApplications.forEach((appName: string) => {
// @ts-ignore
applicationsTree[appName] = {};
const { versions, defaultVersion, build = "Default", ...appData } = getAppData(appName);
// @ts-ignore
applicationsTree[appName].defaultVersion = defaultVersion;
const applicationsTree: AppTree = {} as AppTree;
const applicationsArray: AppArray = [];
allApplications.forEach((appName) => {
const { versions, defaultVersion, ...appData } = getAppData(appName);
// @ts-ignore
applicationsTree[appName] = { defaultVersion };
versions.forEach((options) => {
const { version } = options;
// @ts-ignore
const { version, build = "Default" } = options;
if (!(version in applicationsTree[appName])) applicationsTree[appName][version] = {};
const config = { ...appData, build, ...options };
if (cls) {
// @ts-ignore
applicationsTree[appName][version][build] = new cls(config);
applicationsArray.push(new cls(config));
} else {
// @ts-ignore
applicationsTree[appName][version][build] = config;
applicationsArray.push(config);
}
});
});
// @ts-ignore
return { applicationsTree, applicationsArray };
}

Expand All @@ -48,14 +67,12 @@ export function getAllApplications(cls?: Constructor<any>) {
* @param build {String} the build to use (optional, defaults to Default)
* @return {*} an application
*/
// @ts-ignore applicationsTree is not typed
export function getApplication({ applicationsTree, name, version = null, build = "Default" }:{
applicationsTree: object,
name: string,
export function getApplication({ applicationsTree, name, version, build = "Default" }:{
applicationsTree: AppTree,
name: AllowedApplications,
version?: string,
build?: string,
}) {
// @ts-ignore applicationsTree is not typed
const app = applicationsTree[name];
const version_ = version || app.defaultVersion;
if (!app[version_]) console.log(`Version ${version_} not available for ${name} !`);
Expand All @@ -71,8 +88,8 @@ const { applicationsTree } = getAllApplications();
* @param build {String}
* @returns {*}
*/
export function getApplicationConfig({ name, version = undefined, build = "Default" }: {
name: string;
export function getApplicationConfig({ name, version, build = "Default" }: {
name: AllowedApplications;
version?: string;
build?: string;
}) {
Expand All @@ -90,18 +107,26 @@ export function getApplicationConfig({ name, version = undefined, build = "Defau
* @param execName if not provided, find the executable with isDefault === true
* @returns an executable config
*/
export function getExecutableConfig({ appName, execName = undefined }: { appName: string, execName: string | undefined }): ExecutableData | undefined {
const appTree: AppTree = getAppTree(appName);
Object.entries(appTree).forEach(([name, exec]) => {
exec.name = name;
});
export function getExecutableConfig({ appName, execName }: { appName: AllowedApplications, execName?: string }) {
const appTree = getAppTree(appName);
console.log(appTree)
if (!execName) {
console.log("No executable name provided, using default executable");
return getOneMatchFromObject(appTree, "isDefault", true) as ExecutableData | undefined;
// iterate over properties of appTree and find the one with isDefault === true
Object.entries(appTree).forEach(([key, value]) => {
if (value.isDefault) {
console.log(`Found default executable: ${key}`);
execName = key;
}
});
if (!execName) {
console.log(`No default executable found for application ${appName}`);
return undefined;
}
}
if (!appTree[execName]) {
console.log(`Executable ${execName} not found for application ${appName}`);
return undefined;
}
return appTree[execName];
return {...appTree[execName], name: execName};
}
8 changes: 3 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { AllowedApplications } from "@exabyte-io/application-flavors.js";

// TODO: remove these types once application-flavors is moved to TypeScript
export type VersionData = {
version: string;
Expand All @@ -7,7 +9,7 @@ export type VersionData = {
}

export type ApplicationData = {
name: string;
name: AllowedApplications;
shortName: string;
summary: string;
defaultVersion: string;
Expand All @@ -16,10 +18,6 @@ export type ApplicationData = {

export type ApplicationConfig = Omit<ApplicationData, "versions" | "defaultVersion"> & VersionData;

export type AppTree = {
[applicationName: string]: ExecutableData;
}

export type ExecutableData = {
name?: string;
isDefault: boolean;
Expand Down
2 changes: 2 additions & 0 deletions tests/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ describe("Application", () => {
const obj = { name: "espresso" };
it("can be created", () => {
const app = new Application(obj);
console.log(app);
console.log(app.executables);
expect(app.name).to.equal("espresso");
expect(app.executables.map((e) => e.name).includes("pw.x")).to.equal(true);
});
Expand Down

0 comments on commit b370947

Please sign in to comment.