Skip to content

Commit

Permalink
wip: remove separation of custom versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 22, 2024
1 parent 254ac0a commit 4fbe42d
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 312 deletions.
9 changes: 2 additions & 7 deletions companion/lib/Instance/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ export class InstanceController extends CoreBase<InstanceControllerEvents> {
const moduleDirs: ModuleDirs = {
bundledLegacyModulesDir: path.resolve(generatePath('modules')),
bundledModulesDir: path.resolve(generatePath('bundled-modules')),
storeModulesDir: path.join(registry.appInfo.modulesDir, 'store'),
customModulesDir: path.join(registry.appInfo.modulesDir, 'custom'),
installedModulesDir: path.join(registry.appInfo.modulesDir, 'store'),
}

this.definitions = new InstanceDefinitions(registry)
Expand Down Expand Up @@ -148,11 +147,7 @@ export class InstanceController extends CoreBase<InstanceControllerEvents> {
this.emit('connection_added')
}

async reloadUsesOfModule(
moduleId: string,
mode: 'release' | 'custom' | 'dev',
versionId: string | null
): Promise<void> {
async reloadUsesOfModule(moduleId: string, mode: 'release' | 'dev', versionId: string | null): Promise<void> {
// TODO - use the version!

// restart usages of this module
Expand Down
47 changes: 14 additions & 33 deletions companion/lib/Instance/InstalledModulesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,20 @@ export class InstanceInstalledModulesManager {
/**
* Absolute path for storing store modules on disk
*/
readonly #storeModulesDir: string

/**
* Absolute path for storing custom modules on disk
*/
readonly #customModulesDir: string
readonly #modulesDir: string

constructor(appInfo: AppInfo, modulesManager: InstanceModules, modulesStore: ModuleStoreService, dirs: ModuleDirs) {
this.#appInfo = appInfo
this.#modulesManager = modulesManager
this.#modulesStore = modulesStore
this.#storeModulesDir = dirs.storeModulesDir
this.#customModulesDir = dirs.customModulesDir
this.#modulesDir = dirs.installedModulesDir
}

/**
* Initialise the user modules manager
*/
async init() {
await fs.mkdirp(this.#storeModulesDir)
await fs.mkdirp(this.#customModulesDir)
await fs.mkdirp(this.#modulesDir)
}

/**
Expand Down Expand Up @@ -88,17 +81,17 @@ export class InstanceInstalledModulesManager {
return "Doesn't look like a valid module, missing manifest"
}

const moduleDir = path.join(this.#customModulesDir, `${manifestJson.id}-${manifestJson.version}`)
const moduleDir = path.join(this.#modulesDir, `${manifestJson.id}-${manifestJson.version}`)
if (fs.existsSync(moduleDir)) {
this.#logger.warn(`Module ${manifestJson.id} v${manifestJson.version} already exists on disk`)
return `Module ${manifestJson.id} v${manifestJson.version} already exists`
}

return this.#installModuleFromTarBuffer('custom', moduleDir, manifestJson, decompressedData, false)
return this.#installModuleFromTarBuffer(moduleDir, manifestJson, decompressedData, false)
})

client.onPromise('modules:uninstall-custom-module', async (moduleId, versionId) => {
return this.#uninstallModule('custom', this.#customModulesDir, moduleId, versionId)
return this.#uninstallModule(moduleId, versionId)
})

client.onPromise('modules:install-store-module', async (moduleId, moduleVersion) => {
Expand Down Expand Up @@ -128,7 +121,7 @@ export class InstanceInstalledModulesManager {
})

client.onPromise('modules:uninstall-store-module', async (moduleId, versionId) => {
return this.#uninstallModule('release', this.#storeModulesDir, moduleId, versionId)
return this.#uninstallModule(moduleId, versionId)
})
}

Expand All @@ -138,7 +131,7 @@ export class InstanceInstalledModulesManager {
): Promise<string | null> {
const moduleVersion = versionInfo.id

const moduleDir = path.join(this.#storeModulesDir, `${moduleId}-${moduleVersion}`)
const moduleDir = path.join(this.#modulesDir, `${moduleId}-${moduleVersion}`)
if (fs.existsSync(moduleDir)) {
this.#logger.warn(`Module ${moduleId} v${moduleVersion} already exists on disk`)
return `Module ${moduleId} v${moduleVersion} already exists`
Expand Down Expand Up @@ -199,17 +192,10 @@ export class InstanceInstalledModulesManager {
return 'Module manifest does not match requested module'
}

return this.#installModuleFromTarBuffer(
'release',
moduleDir,
manifestJson,
decompressedData,
versionInfo.isPrerelease
)
return this.#installModuleFromTarBuffer(moduleDir, manifestJson, decompressedData, versionInfo.isPrerelease)
}

async #installModuleFromTarBuffer(
type: 'release' | 'custom',
moduleDir: string,
manifestJson: ModuleManifest,
uncompressedData: Buffer,
Expand Down Expand Up @@ -237,25 +223,20 @@ export class InstanceInstalledModulesManager {
this.#logger.info(`Installed module ${manifestJson.id} v${manifestJson.version}`)

// Let other interested parties know that a module has been installed
await this.#modulesManager.loadInstalledModule(moduleDir, type, manifestJson)
await this.#modulesManager.loadInstalledModule(moduleDir, manifestJson)

return null
}

async #uninstallModule(
type: 'release' | 'custom',
modulesDir: string,
moduleId: string,
versionId: string
): Promise<string | null> {
this.#logger.info(`Uninstalling ${type} ${moduleId} v${versionId}`)
async #uninstallModule(moduleId: string, versionId: string): Promise<string | null> {
this.#logger.info(`Uninstalling ${moduleId} v${versionId}`)

try {
const moduleDir = path.join(modulesDir, `${moduleId}-${versionId}`)
const moduleDir = path.join(this.#modulesDir, `${moduleId}-${versionId}`)
if (!fs.existsSync(moduleDir)) return `Module ${moduleId} v${versionId} doesn't exist`

// Stop any usages of the module
await this.#modulesManager.uninstallModule(moduleId, type, versionId)
await this.#modulesManager.uninstallModule(moduleId, versionId)

// Delete the module code
await fs.rm(moduleDir, { recursive: true }).catch(() => null)
Expand Down
41 changes: 7 additions & 34 deletions companion/lib/Instance/ModuleInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ import type {
NewClientModuleVersionInfo2,
NewClientModuleVersionInfo2Ext,
} from '@companion-app/shared/Model/ModuleInfo.js'
import type {
CustomModuleVersionInfo,
DevModuleVersionInfo,
ReleaseModuleVersionInfo,
SomeModuleVersionInfo,
} from './Types.js'
import type { DevModuleVersionInfo, ReleaseModuleVersionInfo, SomeModuleVersionInfo } from './Types.js'
import semver from 'semver'
import { compact } from 'lodash-es'

Expand All @@ -23,8 +18,7 @@ export class InstanceModuleInfo {

devModule: DevModuleVersionInfo | null = null

releaseVersions: Record<string, ReleaseModuleVersionInfo | undefined> = {}
customVersions: Record<string, CustomModuleVersionInfo | undefined> = {}
installedVersions: Record<string, ReleaseModuleVersionInfo | undefined> = {}

constructor(id: string) {
this.id = id
Expand All @@ -36,7 +30,7 @@ export class InstanceModuleInfo {
if (this.devModule) return this.devModule

let latest: ReleaseModuleVersionInfo | null = null
for (const version of Object.values(this.releaseVersions)) {
for (const version of Object.values(this.installedVersions)) {
if (!version || version.releaseType !== 'stable') continue
if (!latest || semver.compare(version.display.version, latest.display.version) > 0) {
latest = version
Expand All @@ -49,7 +43,7 @@ export class InstanceModuleInfo {
if (this.devModule) return this.devModule

let latest: ReleaseModuleVersionInfo | null = null
for (const version of Object.values(this.releaseVersions)) {
for (const version of Object.values(this.installedVersions)) {
if (!version || version.releaseType !== 'prerelease') continue
if (!latest || semver.compare(version.display.version, latest.display.version) > 0) {
latest = version
Expand All @@ -59,9 +53,7 @@ export class InstanceModuleInfo {
return latest
}
case 'specific-version':
return versionId ? (this.releaseVersions[versionId] ?? null) : null
case 'custom':
return versionId ? (this.customVersions[versionId] ?? null) : null
return versionId ? (this.installedVersions[versionId] ?? null) : null
default:
return null
}
Expand All @@ -71,11 +63,7 @@ export class InstanceModuleInfo {
const stableVersion = this.getVersion('stable', null)
const prereleaseVersion = this.getVersion('prerelease', null)

const baseVersion =
stableVersion ??
prereleaseVersion ??
Object.values(this.releaseVersions)[0] ??
Object.values(this.customVersions)[0]
const baseVersion = stableVersion ?? prereleaseVersion ?? Object.values(this.installedVersions)[0]
if (!baseVersion) return null

return {
Expand All @@ -86,8 +74,7 @@ export class InstanceModuleInfo {
stableVersion: translateStableVersion(stableVersion),
prereleaseVersion: translatePrereleaseVersion(prereleaseVersion),

releaseVersions: compact(Object.values(this.releaseVersions)).map(translateReleaseVersion),
customVersions: compact(Object.values(this.customVersions)).map(translateCustomVersion),
installedVersions: compact(Object.values(this.installedVersions)).map(translateReleaseVersion),
}
}
}
Expand Down Expand Up @@ -169,17 +156,3 @@ function translateReleaseVersion(version: ReleaseModuleVersionInfo): NewClientMo
},
}
}

function translateCustomVersion(version: CustomModuleVersionInfo): NewClientModuleVersionInfo2 {
return {
displayName: `Custom XXX v${version.versionId}`,
isLegacy: false,
isDev: false,
isBuiltin: false,
hasHelp: version.helpPath !== null,
version: {
mode: 'custom',
id: version.versionId,
},
}
}
Loading

0 comments on commit 4fbe42d

Please sign in to comment.