|
| 1 | +import { nls } from '@theia/core/lib/common/nls'; |
| 2 | +import { LocalStorageService } from '@theia/core/lib/browser/storage-service'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { |
| 5 | + IDEUpdater, |
| 6 | + SKIP_IDE_VERSION, |
| 7 | +} from '../../common/protocol/ide-updater'; |
| 8 | +import { IDEUpdaterDialog } from '../dialogs/ide-updater/ide-updater-dialog'; |
| 9 | +import { Contribution } from './contribution'; |
| 10 | + |
| 11 | +@injectable() |
| 12 | +export class CheckForIDEUpdates extends Contribution { |
| 13 | + @inject(IDEUpdater) |
| 14 | + private readonly updater: IDEUpdater; |
| 15 | + |
| 16 | + @inject(IDEUpdaterDialog) |
| 17 | + private readonly updaterDialog: IDEUpdaterDialog; |
| 18 | + |
| 19 | + @inject(LocalStorageService) |
| 20 | + private readonly localStorage: LocalStorageService; |
| 21 | + |
| 22 | + override onStart(): void { |
| 23 | + this.preferences.onPreferenceChanged( |
| 24 | + ({ preferenceName, newValue, oldValue }) => { |
| 25 | + if (newValue !== oldValue) { |
| 26 | + switch (preferenceName) { |
| 27 | + case 'arduino.ide.updateChannel': |
| 28 | + case 'arduino.ide.updateBaseUrl': |
| 29 | + this.updater.init( |
| 30 | + this.preferences.get('arduino.ide.updateChannel'), |
| 31 | + this.preferences.get('arduino.ide.updateBaseUrl') |
| 32 | + ); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + override onReady(): void { |
| 40 | + const checkForUpdates = this.preferences['arduino.checkForUpdates']; |
| 41 | + if (!checkForUpdates) { |
| 42 | + return; |
| 43 | + } |
| 44 | + this.updater |
| 45 | + .init( |
| 46 | + this.preferences.get('arduino.ide.updateChannel'), |
| 47 | + this.preferences.get('arduino.ide.updateBaseUrl') |
| 48 | + ) |
| 49 | + .then(() => this.updater.checkForUpdates(true)) |
| 50 | + .then(async (updateInfo) => { |
| 51 | + if (!updateInfo) return; |
| 52 | + const versionToSkip = await this.localStorage.getData<string>( |
| 53 | + SKIP_IDE_VERSION |
| 54 | + ); |
| 55 | + if (versionToSkip === updateInfo.version) return; |
| 56 | + this.updaterDialog.open(updateInfo); |
| 57 | + }) |
| 58 | + .catch((e) => { |
| 59 | + this.messageService.error( |
| 60 | + nls.localize( |
| 61 | + 'arduino/ide-updater/errorCheckingForUpdates', |
| 62 | + 'Error while checking for Arduino IDE updates.\n{0}', |
| 63 | + e.message |
| 64 | + ) |
| 65 | + ); |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
0 commit comments