Expand scope of behaviour for updating using tray also contain checking for updates

Co-authored-by: Mikko Aspiala <mikko.aspiala@gmail.com>

Signed-off-by: Janne Savolainen <janne.savolainen@live.fi>
This commit is contained in:
Janne SavolainenandMikko Aspiala committed 2022-05-20 14:08:34 +03:00
1 parent 35d6b54ed4
commit 4b2aaa8923
14 files changed
+823 -184

No files matched your search

@@ -0,0 +1,30 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import electronUpdaterInjectable from "../electron-app/features/electron-updater.injectable";
import type { UpdateChannel } from "./update-channels";
export type CheckForPlatformUpdates = (updateChannel: UpdateChannel) => Promise<{ updateWasDiscovered: boolean; version?: string }>;
const checkForPlatformUpdatesInjectable = getInjectable({
id: "check-for-platform-updates",
instantiate: (di): CheckForPlatformUpdates => {
const electronUpdater = di.inject(electronUpdaterInjectable);
return async (updateChannel) => {
electronUpdater.channel = updateChannel.id;
await electronUpdater.checkForUpdates();
return {
updateWasDiscovered: true,
version: "42",
};
};
},
});
export default checkForPlatformUpdatesInjectable;
@@ -0,0 +1,59 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import updatingIsEnabledInjectable from "./updating-is-enabled.injectable";
import { trayMenuItemInjectionToken } from "../tray/tray-menu-item/tray-menu-item-injection-token";
import versionUpdateInjectable from "./version-update.injectable";
import progressOfUpdateDownloadInjectable from "./progress-of-update-download.injectable";
import showApplicationWindowInjectable from "../start-main-application/lens-window/show-application-window.injectable";
const checkForUpdatesTrayItemInjectable = getInjectable({
id: "check-for-updates-tray-item",
instantiate: (di) => {
const showApplicationWindow = di.inject(showApplicationWindowInjectable);
const updatingIsEnabled = di.inject(updatingIsEnabledInjectable);
const versionUpdate = di.inject(versionUpdateInjectable);
const progressOfUpdateDownload = di.inject(progressOfUpdateDownloadInjectable);
return {
id: "check-for-updates",
parentId: null,
orderNumber: 30,
label: computed(() => {
if (versionUpdate.downloading.get()) {
return `Downloading update "${versionUpdate.discoveredVersion.get()}" (${progressOfUpdateDownload.value.get()}%)...`;
}
if (versionUpdate.checking.get()) {
return "Checking for updates...";
}
return "Check for updates";
}),
enabled: computed(() => !versionUpdate.checking.get() && !versionUpdate.downloading.get()),
visible: computed(() => updatingIsEnabled),
click: async () => {
const { updateWasDiscovered } = await versionUpdate.checkForUpdates();
if (updateWasDiscovered) {
versionUpdate.downloadUpdate();
}
// await showApplicationWindow();
},
};
},
injectionToken: trayMenuItemInjectionToken,
});
export default checkForUpdatesTrayItemInjectable;
@@ -0,0 +1,15 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
const downloadPlatformUpdateInjectable = getInjectable({
id: "download-platform-update",
instantiate: () => {
return async () => {};
},
});
export default downloadPlatformUpdateInjectable;
@@ -5,25 +5,34 @@
import { getInjectable } from "@ogre-tools/injectable";
import { computed } from "mobx";
import { trayMenuItemInjectionToken } from "../tray/tray-menu-item/tray-menu-item-injection-token";
import updateIsReadyToBeInstalledInjectable from "./update-is-ready-to-be-installed.injectable";
import quitAndInstallUpdateInjectable from "../electron-app/features/quit-and-install-update.injectable";
import versionUpdateInjectable from "./version-update.injectable";
const triggerApplicationUpdateTrayItemInjectable = getInjectable({
id: "trigger-application-update-tray-item",
const installApplicationUpdateTrayItemInjectable = getInjectable({
id: "install-update-tray-item",
instantiate: (di) => {
const updateIsReadyToBeInstalled = di.inject(updateIsReadyToBeInstalledInjectable);
const quitAndInstallUpdate = di.inject(quitAndInstallUpdateInjectable);
const versionUpdate = di.inject(versionUpdateInjectable);
return {
id: "trigger-application-update",
id: "install-update",
parentId: null,
orderNumber: 50,
label: "Trigger update",
enabled: computed(() => true),
visible: computed(() => updateIsReadyToBeInstalled.get()),
click: () => {
label: computed(() => {
const versionToBeInstalled = versionUpdate.discoveredVersion.get();
return `Install update "${versionToBeInstalled}"`;
}),
enabled: computed(() => true),
visible: computed(
() => versionUpdate.discoveredVersion.get() && !versionUpdate.downloading.get(),
),
click: () => {
quitAndInstallUpdate();
},
};
@@ -32,4 +41,4 @@ const triggerApplicationUpdateTrayItemInjectable = getInjectable({
injectionToken: trayMenuItemInjectionToken,
});
export default triggerApplicationUpdateTrayItemInjectable;
export default installApplicationUpdateTrayItemInjectable;
@@ -0,0 +1,24 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { action, computed, observable } from "mobx";
const progressOfUpdateDownloadInjectable = getInjectable({
id: "progress-of-update-download",
instantiate: () => {
const state = observable.box(0);
return {
value: computed(() => state.get()),
setValue: action((percentage: number) => {
state.set(percentage);
}),
};
},
});
export default progressOfUpdateDownloadInjectable;
@@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import { action, computed, observable } from "mobx";
import type { UpdateChannelId } from "./update-channels";
import { updateChannels } from "./update-channels";
const selectedUpdateChannelInjectable = getInjectable({
id: "selected-update-channel",
instantiate: () => {
const state = observable.box(updateChannels.latest);
return {
value: computed(() => state.get()),
setValue: action((channelId: UpdateChannelId) => {
state.set(updateChannels[channelId]);
}),
};
},
});
export default selectedUpdateChannelInjectable;
+28
View File
@@ -0,0 +1,28 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export type UpdateChannelId = "alpha" | "beta" | "latest";
export const updateChannels: Record<UpdateChannelId, UpdateChannel> = {
alpha: {
id: "alpha",
label: "Alpha",
},
beta: {
id: "beta",
label: "Beta",
},
latest: {
id: "latest",
label: "Stable",
},
};
export interface UpdateChannel {
readonly id: UpdateChannelId;
readonly label: string;
}
@@ -0,0 +1,87 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { IComputedValue, IObservableValue } from "mobx";
import { computed, observable, runInAction } from "mobx";
import selectedUpdateChannelInjectable from "./selected-update-channel.injectable";
import downloadPlatformUpdateInjectable from "./download-platform-update.injectable";
import type { CheckForPlatformUpdates } from "./check-for-platform-updates.injectable";
import checkForPlatformUpdatesInjectable from "./check-for-platform-updates.injectable";
import type { UpdateChannel } from "./update-channels";
const versionUpdateInjectable = getInjectable({
id: "version-update",
instantiate: (di) => {
const selectedUpdateChannel = di.inject(selectedUpdateChannelInjectable);
const downloadPlatformUpdate = di.inject(downloadPlatformUpdateInjectable);
const checkForPlatformUpdates = di.inject(checkForPlatformUpdatesInjectable);
const discoveredVersionState = observable.box<string>();
const downloadingState = observable.box<boolean>(false);
const checkingState = observable.box<boolean>(false);
return {
discoveredVersion: computed(() => discoveredVersionState.get()),
downloading: computed(() => downloadingState.get()),
checking: computed(() => checkingState.get()),
checkForUpdates: checkForUpdatesFor(
checkForPlatformUpdates,
discoveredVersionState,
checkingState,
selectedUpdateChannel.value,
),
downloadUpdate: downloadUpdateFor(
downloadPlatformUpdate,
downloadingState,
),
};
},
});
export default versionUpdateInjectable;
const downloadUpdateFor =
(
downloadPlatformUpdate: () => Promise<void>,
downloadingState: IObservableValue<boolean>,
) =>
async () => {
runInAction(() => {
downloadingState.set(true);
});
await downloadPlatformUpdate();
runInAction(() => {
downloadingState.set(false);
});
};
const checkForUpdatesFor = (
checkForPlatformUpdates: CheckForPlatformUpdates,
discoveredVersionState: IObservableValue<string>,
checkingState: IObservableValue<boolean>,
selectedUpdateChannel: IComputedValue<UpdateChannel>,
) => {
return async () => {
runInAction(() => {
checkingState.set(true);
});
const { updateWasDiscovered, version } = await checkForPlatformUpdates(
selectedUpdateChannel.get(),
);
runInAction(() => {
discoveredVersionState.set(version);
checkingState.set(false);
});
return { updateWasDiscovered };
};
};