|
| 1 | +import { inject, injectable } from 'inversify'; |
| 2 | +import { remote } from 'electron'; |
| 3 | +import { ArduinoMenus } from '../menu/arduino-menus'; |
| 4 | +import { SketchContribution, Command, CommandRegistry, MenuModelRegistry } from './contribution'; |
| 5 | +import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; |
| 6 | +import URI from '@theia/core/lib/common/uri'; |
| 7 | +import { InstallationProgressDialog } from '../widgets/progress-dialog'; |
| 8 | +import { LibraryService } from '../../common/protocol'; |
| 9 | + |
| 10 | +@injectable() |
| 11 | +export class AddZipLibrary extends SketchContribution { |
| 12 | + |
| 13 | + @inject(EnvVariablesServer) |
| 14 | + protected readonly envVariableServer: EnvVariablesServer; |
| 15 | + |
| 16 | + @inject(LibraryService) |
| 17 | + protected readonly libraryService: LibraryService; |
| 18 | + |
| 19 | + registerCommands(registry: CommandRegistry): void { |
| 20 | + registry.registerCommand(AddZipLibrary.Commands.ADD_ZIP_LIBRARY, { |
| 21 | + execute: () => this.addZipLibrary() |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + registerMenus(registry: MenuModelRegistry): void { |
| 26 | + const includeLibMenuPath = [...ArduinoMenus.SKETCH__UTILS_GROUP, '0_include']; |
| 27 | + // TODO: do we need it? calling `registerSubmenu` multiple times is noop, so it does not hurt. |
| 28 | + registry.registerSubmenu(includeLibMenuPath, 'Include Library', { order: '1' }); |
| 29 | + registry.registerMenuAction([...includeLibMenuPath, '1_install'], { |
| 30 | + commandId: AddZipLibrary.Commands.ADD_ZIP_LIBRARY.id, |
| 31 | + label: 'Add .ZIP Library...', |
| 32 | + order: '1' |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + async addZipLibrary(): Promise<void> { |
| 37 | + const homeUri = await this.envVariableServer.getHomeDirUri(); |
| 38 | + const defaultPath = await this.fileService.fsPath(new URI(homeUri)); |
| 39 | + const { canceled, filePaths } = await remote.dialog.showOpenDialog({ |
| 40 | + title: "Select a zip file containing the library you'd like to add", |
| 41 | + defaultPath, |
| 42 | + properties: ['openFile'], |
| 43 | + filters: [ |
| 44 | + { |
| 45 | + name: 'Library', |
| 46 | + extensions: ['zip'] |
| 47 | + } |
| 48 | + ] |
| 49 | + }); |
| 50 | + if (!canceled && filePaths.length) { |
| 51 | + const zipUri = await this.fileSystemExt.getUri(filePaths[0]); |
| 52 | + const dialog = new InstallationProgressDialog('Installing library', 'zip'); |
| 53 | + try { |
| 54 | + this.outputChannelManager.getChannel('Arduino').clear(); |
| 55 | + dialog.open(); |
| 56 | + await this.libraryService.installZip({ zipUri }); |
| 57 | + } catch (e) { |
| 58 | + this.messageService.error(e.toString()); |
| 59 | + } finally { |
| 60 | + dialog.close(); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +export namespace AddZipLibrary { |
| 68 | + export namespace Commands { |
| 69 | + export const ADD_ZIP_LIBRARY: Command = { |
| 70 | + id: 'arduino-add-zip-library' |
| 71 | + }; |
| 72 | + } |
| 73 | +} |
0 commit comments