Skip to content

Commit

Permalink
feat: revert changes to tree building, to adjust when application fla…
Browse files Browse the repository at this point in the history
…vors is adjusted
  • Loading branch information
seankwarren committed Feb 8, 2024
1 parent 565e2db commit 46424df
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 64 deletions.
1 change: 0 additions & 1 deletion src/flavor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from "@exabyte-io/code.js/dist/entity";

import { Template as AdeTemplate } from "./template";
import { NamedTemplate } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";
import { AnyObject } from "@exabyte-io/code.js/dist/entity/in_memory";
import { ExecutionUnitInputItemSchemaForPhysicsBasedSimulationEngines, TemplateSchema1 } from "@exabyte-io/code.js/dist/types";
Expand Down
97 changes: 42 additions & 55 deletions src/tree.ts
Original file line number Diff line number Diff line change
@@ -1,93 +1,80 @@
// @ts-ignore
// @ts-expect-error application-flavors.js is not typed
import { allApplications, getAppData, getAppTree } from "@exabyte-io/application-flavors.js";
import { getOneMatchFromObject } from "@exabyte-io/code.js/dist/utils";
import { AppTree, ApplicationArray, ApplicationConfig, ApplicationData, ApplicationTree, ExecutableData, VersionData } from "./types";
import { AppTree, ExecutableData } from "./types";
import { Constructor } from "@exabyte-io/code.js/dist/context";

/**
* @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: InstanceType<any> | null) {
const applicationsTree: ApplicationTree = {};
const applicationsArray: ApplicationArray = [];
export function getAllApplications(cls?: Constructor<any>) {
const applicationsTree = {};
// @ts-expect-error application-flavors is not typed
const applicationsArray = [];
allApplications.forEach((appName: string) => {
// @ts-expect-error instantiating an empty object
// @ts-ignore
applicationsTree[appName] = {};
const { versions, defaultVersion, ...appData } = getAppData(appName) as ApplicationData;
const { versions, defaultVersion, build = "Default", ...appData } = getAppData(appName);
// @ts-ignore
applicationsTree[appName].defaultVersion = defaultVersion;
versions.forEach(({ version, build = "Default", ...versionData }) => {
// add version to applicationsTree if it doesn't exist
// @ts-ignore
versions.forEach((options) => {
const { version } = options;
// @ts-ignore
if (!(version in applicationsTree[appName])) applicationsTree[appName][version] = {};
// convert to class instance if cls is provided otherwise use the config
const config: ApplicationConfig = { ...appData, version, build, ...versionData };
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 };
}

/**
* @summary Get an application from the constructed applications
* @param applicationsTree See getAllApplications applicationsTree object structure
* @param name name of the application
* @param version version of the application (optional, defaults to defaultVersion)
* @param build the build to use (optional, defaults to Default)
* @return an application
* @param applicationsTree {Object} See getAllApplications applicationsTree object structure
* @param name {String} name of the application
* @param version {String|null} version of the application (optional, defaults to defaultVersion)
* @param build {String} the build to use (optional, defaults to Default)
* @return {*} an application
*/
export function getApplication({
applicationsTree,
name,
version = undefined,
build = "Default"
}: {
applicationsTree: ApplicationTree,
name: string
version?: string
build?: string
}): ApplicationConfig | undefined {
// @ts-ignore applicationsTree is not typed
export function getApplication({ applicationsTree, name, version = null, build = "Default" }:{
applicationsTree: object,
name: string,
version?: string,
build?: string,
}) {
// @ts-ignore applicationsTree is not typed
const app = applicationsTree[name];
if (!app) {
console.log(`Application ${name} not found!`);
return undefined;
}
const version_ = version || app.defaultVersion;
const appData = app[version_][build];
if (!appData) {
console.log(`Version ${version_} not available for ${name}!`);
return undefined;
}
return {
...app,
...appData,
version: appData.version, // Explicitly set version and build in case they are different in app and versionData
build: appData.build || build
};
if (!app[version_]) console.log(`Version ${version_} not available for ${name} !`);
return app[version_]?.[build];
}

const { applicationsTree } = getAllApplications(null);
const { applicationsTree } = getAllApplications();

/**
* @summary Get pre-defined application config from an already generated applicationsTree of configs
* @param name name of the application
* @param version version of the application (optional, defaults to defaultVersion)
* @param build the build to use (optional, defaults to Default)
* @returns an application config
* @param name {String}
* @param version {String|undefined}
* @param build {String}
* @returns {*}
*/
export function getApplicationConfig({
name,
version = undefined,
build = "Default"
}: {
name: string,
version?: string,
build?: string
export function getApplicationConfig({ name, version = undefined, build = "Default" }: {
name: string;
version?: string;
build?: string;
}) {
return getApplication({
applicationsTree,
Expand Down
8 changes: 0 additions & 8 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ export type ApplicationData = {
versions: VersionData[];
}

export type ApplicationTree = Record<string, {
defaultVersion: string;
} & Record<string, Record<string, ApplicationConfig>>>;

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

export type ApplicationArray = ApplicationConfig[];

export type AppTree = {
[applicationName: string]: ExecutableData;
}
Expand Down Expand Up @@ -54,5 +48,3 @@ export type TemplateData = {
applicationName?: string;
executableName?: string;
}

export type NamedTemplate = {name: string} | {templateName: string};

0 comments on commit 46424df

Please sign in to comment.