|
| 1 | +import { LocalStorageService } from '@theia/core/lib/browser'; |
| 2 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 3 | +import { BoardsService, LibraryService } from '../../common/protocol'; |
| 4 | +import { Contribution } from './contribution'; |
| 5 | + |
| 6 | +@injectable() |
| 7 | +export class FirstStartupInstaller extends Contribution { |
| 8 | + @inject(LocalStorageService) |
| 9 | + private readonly localStorageService: LocalStorageService; |
| 10 | + @inject(BoardsService) |
| 11 | + private readonly boardsService: BoardsService; |
| 12 | + @inject(LibraryService) |
| 13 | + private readonly libraryService: LibraryService; |
| 14 | + |
| 15 | + override async onReady(): Promise<void> { |
| 16 | + const isFirstStartup = !(await this.localStorageService.getData( |
| 17 | + FirstStartupInstaller.INIT_LIBS_AND_PACKAGES |
| 18 | + )); |
| 19 | + if (isFirstStartup) { |
| 20 | + const avrPackage = await this.boardsService.getBoardPackage({ |
| 21 | + id: 'arduino:avr', |
| 22 | + }); |
| 23 | + const builtInLibrary = ( |
| 24 | + await this.libraryService.search({ query: 'Arduino_BuiltIn' }) |
| 25 | + )[0]; |
| 26 | + |
| 27 | + let avrPackageError: Error | undefined; |
| 28 | + let builtInLibraryError: Error | undefined; |
| 29 | + |
| 30 | + if (avrPackage) { |
| 31 | + try { |
| 32 | + await this.boardsService.install({ |
| 33 | + item: avrPackage, |
| 34 | + noOverwrite: true, // We don't want to automatically replace custom platforms the user might already have in place |
| 35 | + }); |
| 36 | + } catch (e) { |
| 37 | + // There's no error code, I need to parse the error message: https://github.com/arduino/arduino-cli/commit/ffe4232b359fcfa87238d68acf1c3b64a1621f14#diff-10ffbdde46838dd9caa881fd1f2a5326a49f8061f6cfd7c9d430b4875a6b6895R62 |
| 38 | + if ( |
| 39 | + e.message.includes( |
| 40 | + `Platform ${avrPackage.id}@${avrPackage.installedVersion} already installed` |
| 41 | + ) |
| 42 | + ) { |
| 43 | + // If arduino:avr installation fails because it's already installed we don't want to retry on next start-up |
| 44 | + console.error(e); |
| 45 | + } else { |
| 46 | + // But if there is any other error (e.g.: no interntet cconnection), we want to retry next time |
| 47 | + avrPackageError = e; |
| 48 | + } |
| 49 | + } |
| 50 | + } else { |
| 51 | + avrPackageError = new Error('Could not find platform.'); |
| 52 | + } |
| 53 | + |
| 54 | + if (builtInLibrary) { |
| 55 | + try { |
| 56 | + await this.libraryService.install({ |
| 57 | + item: builtInLibrary, |
| 58 | + installDependencies: true, |
| 59 | + noOverwrite: true, // We don't want to automatically replace custom libraries the user might already have in place |
| 60 | + }); |
| 61 | + } catch (e) { |
| 62 | + // There's no error code, I need to parse the error message: https://github.com/arduino/arduino-cli/commit/2ea3608453b17b1157f8a1dc892af2e13e40f4f0#diff-1de7569144d4e260f8dde0e0d00a4e2a218c57966d583da1687a70d518986649R95 |
| 63 | + if (/Library (.*) is already installed/.test(e.message)) { |
| 64 | + // If Arduino_BuiltIn installation fails because it's already installed we don't want to retry on next start-up |
| 65 | + console.log('error installing core', e); |
| 66 | + } else { |
| 67 | + // But if there is any other error (e.g.: no interntet cconnection), we want to retry next time |
| 68 | + builtInLibraryError = e; |
| 69 | + } |
| 70 | + } |
| 71 | + } else { |
| 72 | + builtInLibraryError = new Error('Could not find library'); |
| 73 | + } |
| 74 | + |
| 75 | + if (avrPackageError) { |
| 76 | + this.messageService.error( |
| 77 | + `Could not install Arduino AVR platform: ${avrPackageError}` |
| 78 | + ); |
| 79 | + } |
| 80 | + if (builtInLibraryError) { |
| 81 | + this.messageService.error( |
| 82 | + `Could not install ${builtInLibrary.name} library: ${builtInLibraryError}` |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + if (!avrPackageError && !builtInLibraryError) { |
| 87 | + await this.localStorageService.setData( |
| 88 | + FirstStartupInstaller.INIT_LIBS_AND_PACKAGES, |
| 89 | + true |
| 90 | + ); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +export namespace FirstStartupInstaller { |
| 96 | + export const INIT_LIBS_AND_PACKAGES = 'initializedLibsAndPackages'; |
| 97 | +} |
0 commit comments