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