Use eslint&prettier for code linting&formatting

This commit is contained in:
Francesco Stasi
2021-06-16 15:08:48 +02:00
committed by Francesco Stasi
parent 2a3873a923
commit 0592199858
173 changed files with 8963 additions and 3841 deletions

View File

@@ -14,14 +14,19 @@ export interface ArduinoComponent {
readonly installedVersion?: Installable.Version;
}
export namespace ArduinoComponent {
export function is(arg: any): arg is ArduinoComponent {
return !!arg
&& 'name' in arg && typeof arg['name'] === 'string'
&& 'author' in arg && typeof arg['author'] === 'string'
&& 'summary' in arg && typeof arg['summary'] === 'string'
&& 'description' in arg && typeof arg['description'] === 'string'
&& 'installable' in arg && typeof arg['installable'] === 'boolean';
return (
!!arg &&
'name' in arg &&
typeof arg['name'] === 'string' &&
'author' in arg &&
typeof arg['author'] === 'string' &&
'summary' in arg &&
typeof arg['summary'] === 'string' &&
'description' in arg &&
typeof arg['description'] === 'string' &&
'installable' in arg &&
typeof arg['installable'] === 'boolean'
);
}
}

View File

@@ -6,7 +6,11 @@ import { ArduinoComponent } from './arduino-component';
export type AvailablePorts = Record<string, [Port, Array<Board>]>;
export namespace AvailablePorts {
export function groupByProtocol(availablePorts: AvailablePorts): { serial: AvailablePorts, network: AvailablePorts, unknown: AvailablePorts } {
export function groupByProtocol(availablePorts: AvailablePorts): {
serial: AvailablePorts;
network: AvailablePorts;
unknown: AvailablePorts;
} {
const serial: AvailablePorts = {};
const network: AvailablePorts = {};
const unknown: AvailablePorts = {};
@@ -26,44 +30,61 @@ export namespace AvailablePorts {
}
export interface AttachedBoardsChangeEvent {
readonly oldState: Readonly<{ boards: Board[], ports: Port[] }>;
readonly newState: Readonly<{ boards: Board[], ports: Port[] }>;
readonly oldState: Readonly<{ boards: Board[]; ports: Port[] }>;
readonly newState: Readonly<{ boards: Board[]; ports: Port[] }>;
}
export namespace AttachedBoardsChangeEvent {
export function isEmpty(event: AttachedBoardsChangeEvent): boolean {
const { detached, attached } = diff(event);
return !!detached.boards.length && !!detached.ports.length && !!attached.boards.length && !!attached.ports.length;
return (
!!detached.boards.length &&
!!detached.ports.length &&
!!attached.boards.length &&
!!attached.ports.length
);
}
export function toString(event: AttachedBoardsChangeEvent): string {
let rows: string[] = [];
const rows: string[] = [];
if (!isEmpty(event)) {
const { attached, detached } = diff(event);
const visitedAttachedPorts: Port[] = [];
const visitedDetachedPorts: Port[] = [];
for (const board of attached.boards) {
const port = board.port ? ` on ${Port.toString(board.port, { useLabel: true })}` : '';
const port = board.port
? ` on ${Port.toString(board.port, { useLabel: true })}`
: '';
rows.push(` - Attached board: ${Board.toString(board)}${port}`);
if (board.port) {
visitedAttachedPorts.push(board.port);
}
}
for (const board of detached.boards) {
const port = board.port ? ` from ${Port.toString(board.port, { useLabel: true })}` : '';
const port = board.port
? ` from ${Port.toString(board.port, { useLabel: true })}`
: '';
rows.push(` - Detached board: ${Board.toString(board)}${port}`);
if (board.port) {
visitedDetachedPorts.push(board.port);
}
}
for (const port of attached.ports) {
if (!visitedAttachedPorts.find(p => Port.sameAs(port, p))) {
rows.push(` - New port is available on ${Port.toString(port, { useLabel: true })}`);
if (!visitedAttachedPorts.find((p) => Port.sameAs(port, p))) {
rows.push(
` - New port is available on ${Port.toString(port, {
useLabel: true,
})}`
);
}
}
for (const port of detached.ports) {
if (!visitedDetachedPorts.find(p => Port.sameAs(port, p))) {
rows.push(` - Port is no longer available on ${Port.toString(port, { useLabel: true })}`);
if (!visitedDetachedPorts.find((p) => Port.sameAs(port, p))) {
rows.push(
` - Port is no longer available on ${Port.toString(
port,
{ useLabel: true }
)}`
);
}
}
}
@@ -72,41 +93,51 @@ export namespace AttachedBoardsChangeEvent {
export function diff(event: AttachedBoardsChangeEvent): Readonly<{
attached: {
boards: Board[],
ports: Port[]
},
boards: Board[];
ports: Port[];
};
detached: {
boards: Board[],
ports: Port[]
}
boards: Board[];
ports: Port[];
};
}> {
// In `lefts` AND not in `rights`.
const diff = <T>(lefts: T[], rights: T[], sameAs: (left: T, right: T) => boolean) => {
return lefts.filter(left => rights.findIndex(right => sameAs(left, right)) === -1);
}
const diff = <T>(
lefts: T[],
rights: T[],
sameAs: (left: T, right: T) => boolean
) => {
return lefts.filter(
(left) =>
rights.findIndex((right) => sameAs(left, right)) === -1
);
};
const { boards: newBoards } = event.newState;
const { boards: oldBoards } = event.oldState;
const { ports: newPorts } = event.newState;
const { ports: oldPorts } = event.oldState;
const boardSameAs = (left: Board, right: Board) => Board.sameAs(left, right);
const portSameAs = (left: Port, right: Port) => Port.sameAs(left, right);
const boardSameAs = (left: Board, right: Board) =>
Board.sameAs(left, right);
const portSameAs = (left: Port, right: Port) =>
Port.sameAs(left, right);
return {
detached: {
boards: diff(oldBoards, newBoards, boardSameAs),
ports: diff(oldPorts, newPorts, portSameAs)
ports: diff(oldPorts, newPorts, portSameAs),
},
attached: {
boards: diff(newBoards, oldBoards, boardSameAs),
ports: diff(newPorts, oldPorts, portSameAs)
}
ports: diff(newPorts, oldPorts, portSameAs),
},
};
}
}
export const BoardsServicePath = '/services/boards-service';
export const BoardsService = Symbol('BoardsService');
export interface BoardsService extends Installable<BoardsPackage>, Searchable<BoardsPackage> {
export interface BoardsService
extends Installable<BoardsPackage>,
Searchable<BoardsPackage> {
/**
* Deprecated. `getState` should be used to correctly map a board with a port.
* @deprecated
@@ -118,9 +149,15 @@ export interface BoardsService extends Installable<BoardsPackage>, Searchable<Bo
*/
getAvailablePorts(): Promise<Port[]>;
getState(): Promise<AvailablePorts>;
getBoardDetails(options: { fqbn: string }): Promise<BoardDetails | undefined>;
getBoardPackage(options: { id: string }): Promise<BoardsPackage | undefined>;
getContainerBoardPackage(options: { fqbn: string }): Promise<BoardsPackage | undefined>;
getBoardDetails(options: {
fqbn: string;
}): Promise<BoardDetails | undefined>;
getBoardPackage(options: {
id: string;
}): Promise<BoardsPackage | undefined>;
getContainerBoardPackage(options: {
fqbn: string;
}): Promise<BoardsPackage | undefined>;
searchBoards({ query }: { query?: string }): Promise<BoardWithPackage[]>;
}
@@ -133,7 +170,6 @@ export interface Port {
readonly label?: string;
}
export namespace Port {
export type Protocol = 'serial' | 'network' | 'unknown';
export namespace Protocol {
export function toProtocol(protocol: string | undefined): Protocol {
@@ -148,12 +184,21 @@ export namespace Port {
}
export function is(arg: any): arg is Port {
return !!arg && 'address' in arg && typeof arg['address'] === 'string' && 'protocol' in arg && typeof arg['protocol'] === 'string';
return (
!!arg &&
'address' in arg &&
typeof arg['address'] === 'string' &&
'protocol' in arg &&
typeof arg['protocol'] === 'string'
);
}
export function toString(port: Port, options: { useLabel: boolean } = { useLabel: false }): string {
export function toString(
port: Port,
options: { useLabel: boolean } = { useLabel: false }
): string {
if (options.useLabel && port.label) {
return `${port.address} ${port.label}`
return `${port.address} ${port.label}`;
}
return port.address;
}
@@ -166,7 +211,10 @@ export namespace Port {
if (!isBoardPort(left) && isBoardPort(right)) {
return 1;
}
let result = naturalCompare(left.protocol.toLocaleLowerCase(), right.protocol.toLocaleLowerCase());
let result = naturalCompare(
left.protocol.toLocaleLowerCase(),
right.protocol.toLocaleLowerCase()
);
if (result !== 0) {
return result;
}
@@ -177,16 +225,21 @@ export namespace Port {
return naturalCompare(left.label || '', right.label || '');
}
export function equals(left: Port | undefined, right: Port | undefined): boolean {
export function equals(
left: Port | undefined,
right: Port | undefined
): boolean {
if (left && right) {
return left.address === right.address
&& left.protocol === right.protocol
&& (left.label || '') === (right.label || '');
return (
left.address === right.address &&
left.protocol === right.protocol &&
(left.label || '') === (right.label || '')
);
}
return left === right;
}
// Based on: https://github.com/arduino/Arduino/blob/93581b03d723e55c60caedb4729ffc6ea808fe78/arduino-core/src/processing/app/SerialPortList.java#L48-L74
// Based on: https://github.com/arduino/Arduino/blob/93581b03d723e55c60caedb4729ffc6ea808fe78/arduino-core/src/processing/app/SerialPortList.java#L48-L74
export function isBoardPort(port: Port): boolean {
const address = port.address.toLocaleLowerCase();
if (isWindows) {
@@ -195,7 +248,7 @@ export namespace Port {
}
// On macOS and Linux, the port should start with `/dev/`.
if (!address.startsWith('/dev/')) {
return false
return false;
}
if (isOSX) {
// Example: `/dev/cu.usbmodem14401`
@@ -203,16 +256,25 @@ export namespace Port {
return [
'/dev/cu.MALS',
'/dev/cu.SOC',
'/dev/cu.Bluetooth-Incoming-Port'
].map(a => a.toLocaleLowerCase()).every(a => a !== address);
'/dev/cu.Bluetooth-Incoming-Port',
]
.map((a) => a.toLocaleLowerCase())
.every((a) => a !== address);
}
}
// Example: `/dev/ttyACM0`
if (/(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}/i.test(address.substring('/dev/'.length))) {
if (
/(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}/i.test(
address.substring('/dev/'.length)
)
) {
// Default ports were `/dev/ttyS0` -> `/dev/ttyS31` on Ubuntu 16.04.2.
if (address.startsWith('/dev/ttyS')) {
const index = Number.parseInt(address.substring('/dev/ttyS'.length), 10);
const index = Number.parseInt(
address.substring('/dev/ttyS'.length),
10
);
if (!Number.isNaN(index) && 0 <= index && 31 >= index) {
return false;
}
@@ -223,22 +285,36 @@ export namespace Port {
return false;
}
export function sameAs(left: Port | undefined, right: Port | string | undefined) {
export function sameAs(
left: Port | undefined,
right: Port | string | undefined
) {
if (left && right) {
if (left.protocol !== 'serial') {
console.log(`Unexpected protocol for 'left' port: ${JSON.stringify(left)}. Ignoring 'protocol', comparing 'addresses' with ${JSON.stringify(right)}.`);
console.log(
`Unexpected protocol for 'left' port: ${JSON.stringify(
left
)}. Ignoring 'protocol', comparing 'addresses' with ${JSON.stringify(
right
)}.`
);
}
if (typeof right === 'string') {
return left.address === right;
}
if (right.protocol !== 'serial') {
console.log(`Unexpected protocol for 'right' port: ${JSON.stringify(right)}. Ignoring 'protocol', comparing 'addresses' with ${JSON.stringify(left)}.`);
console.log(
`Unexpected protocol for 'right' port: ${JSON.stringify(
right
)}. Ignoring 'protocol', comparing 'addresses' with ${JSON.stringify(
left
)}.`
);
}
return left.address === right.address;
}
return false;
}
}
export interface BoardsPackage extends ArduinoComponent {
@@ -246,13 +322,15 @@ export interface BoardsPackage extends ArduinoComponent {
readonly boards: Board[];
}
export namespace BoardsPackage {
export function equals(left: BoardsPackage, right: BoardsPackage): boolean {
return left.id === right.id;
}
export function contains(selectedBoard: Board, { id, boards }: BoardsPackage): boolean {
if (boards.some(board => Board.sameAs(board, selectedBoard))) {
export function contains(
selectedBoard: Board,
{ id, boards }: BoardsPackage
): boolean {
if (boards.some((board) => Board.sameAs(board, selectedBoard))) {
return true;
}
if (selectedBoard.fqbn) {
@@ -263,7 +341,6 @@ export namespace BoardsPackage {
}
return false;
}
}
export interface Board {
@@ -277,22 +354,22 @@ export interface BoardWithPackage extends Board {
readonly packageId: string;
}
export namespace BoardWithPackage {
export function is(board: Board & Partial<{ packageName: string, packageId: string }>): board is BoardWithPackage {
export function is(
board: Board & Partial<{ packageName: string; packageId: string }>
): board is BoardWithPackage {
return !!board.packageId && !!board.packageName;
}
}
export interface InstalledBoardWithPackage extends BoardWithPackage {
readonly fqbn: string;
}
export namespace InstalledBoardWithPackage {
export function is(boardWithPackage: BoardWithPackage): boardWithPackage is InstalledBoardWithPackage {
export function is(
boardWithPackage: BoardWithPackage
): boardWithPackage is InstalledBoardWithPackage {
return !!boardWithPackage.fqbn;
}
}
export interface BoardDetails {
@@ -317,10 +394,16 @@ export interface ConfigOption {
readonly values: ConfigValue[];
}
export namespace ConfigOption {
export function is(arg: any): arg is ConfigOption {
return !!arg && 'option' in arg && 'label' in arg && 'values' in arg
&& typeof arg['option'] === 'string' && typeof arg['label'] === 'string' && Array.isArray(arg['values'])
return (
!!arg &&
'option' in arg &&
'label' in arg &&
'values' in arg &&
typeof arg['option'] === 'string' &&
typeof arg['label'] === 'string' &&
Array.isArray(arg['values'])
);
}
/**
@@ -328,7 +411,10 @@ export namespace ConfigOption {
* Throws an error if the `fqbn` does not have the `segment(':'segment)*` format.
* The provided output format is always segment(':'segment)*(':'option'='value(','option'='value)*)?
*/
export function decorate(fqbn: string, configOptions: ConfigOption[]): string {
export function decorate(
fqbn: string,
configOptions: ConfigOption[]
): string {
if (!configOptions.length) {
return fqbn;
}
@@ -336,7 +422,11 @@ export namespace ConfigOption {
const toValue = (values: ConfigValue[]) => {
const selectedValue = values.find(({ selected }) => selected);
if (!selectedValue) {
console.warn(`None of the config values was selected. Values were: ${JSON.stringify(values)}`);
console.warn(
`None of the config values was selected. Values were: ${JSON.stringify(
values
)}`
);
return undefined;
}
return selectedValue.value;
@@ -357,8 +447,11 @@ export namespace ConfigOption {
}
}
export const LABEL_COMPARATOR = (left: ConfigOption, right: ConfigOption) => naturalCompare(left.label.toLocaleLowerCase(), right.label.toLocaleLowerCase());
export const LABEL_COMPARATOR = (left: ConfigOption, right: ConfigOption) =>
naturalCompare(
left.label.toLocaleLowerCase(),
right.label.toLocaleLowerCase()
);
}
export interface ConfigValue {
@@ -373,21 +466,25 @@ export interface Programmer {
readonly id: string;
}
export namespace Programmer {
export function equals(left: Programmer | undefined, right: Programmer | undefined): boolean {
export function equals(
left: Programmer | undefined,
right: Programmer | undefined
): boolean {
if (!left) {
return !right;
}
if (!right) {
return !left;
}
return left.id === right.id && left.name === right.name && left.platform === right.platform;
return (
left.id === right.id &&
left.name === right.name &&
left.platform === right.platform
);
}
}
export namespace Board {
export function is(board: any): board is Board {
return !!board && 'name' in board;
}
@@ -404,7 +501,10 @@ export namespace Board {
if (left.fqbn && other.fqbn) {
return left.fqbn === other.fqbn;
}
return left.name.replace('/Genuino', '') === other.name.replace('/Genuino', '');
return (
left.name.replace('/Genuino', '') ===
other.name.replace('/Genuino', '')
);
}
export function compare(left: Board, right: Board): number {
@@ -419,15 +519,27 @@ export namespace Board {
return !!board.fqbn;
}
export function toString(board: Board, options: { useFqbn: boolean } = { useFqbn: true }): string {
const fqbn = options && options.useFqbn && board.fqbn ? ` [${board.fqbn}]` : '';
export function toString(
board: Board,
options: { useFqbn: boolean } = { useFqbn: true }
): string {
const fqbn =
options && options.useFqbn && board.fqbn ? ` [${board.fqbn}]` : '';
return `${board.name}${fqbn}`;
}
export type Detailed = Board & Readonly<{ selected: boolean, missing: boolean, packageName: string, packageId: string, details?: string }>;
export type Detailed = Board &
Readonly<{
selected: boolean;
missing: boolean;
packageName: string;
packageId: string;
details?: string;
}>;
export function decorateBoards(
selectedBoard: Board | undefined,
boards: Array<BoardWithPackage>): Array<Detailed> {
boards: Array<BoardWithPackage>
): Array<Detailed> {
// Board names are not unique. We show the corresponding core name as a detail.
// https://github.com/arduino/arduino-cli/pull/294#issuecomment-513764948
const distinctBoardNames = new Map<string, number>();
@@ -441,22 +553,29 @@ export namespace Board {
if (!!selectedBoard) {
if (Board.equals(board, selectedBoard)) {
if ('packageName' in selectedBoard) {
return board.packageName === (selectedBoard as any).packageName;
return (
board.packageName ===
(selectedBoard as any).packageName
);
}
if ('packageId' in selectedBoard) {
return board.packageId === (selectedBoard as any).packageId;
return (
board.packageId === (selectedBoard as any).packageId
);
}
return true;
}
}
return false;
}
return boards.map(board => ({
};
return boards.map((board) => ({
...board,
details: (distinctBoardNames.get(board.name) || 0) > 1 ? ` - ${board.packageName}` : undefined,
details:
(distinctBoardNames.get(board.name) || 0) > 1
? ` - ${board.packageName}`
: undefined,
selected: selected(board),
missing: !installed(board)
missing: !installed(board),
}));
}
}

View File

@@ -1,7 +1,9 @@
export const ConfigServicePath = '/services/config-service';
export const ConfigService = Symbol('ConfigService');
export interface ConfigService {
getVersion(): Promise<Readonly<{ version: string, commit: string, status?: string }>>;
getVersion(): Promise<
Readonly<{ version: string; commit: string; status?: string }>
>;
getCliConfigFileUri(): Promise<string>;
getConfiguration(): Promise<Config>;
setConfiguration(config: Config): Promise<void>;
@@ -18,15 +20,14 @@ export interface ProxySettings {
}
export type Network = 'none' | ProxySettings;
export namespace Network {
export function Default(): Network {
return {
protocol: 'http',
hostname: '',
port: '',
username: '',
password: ''
}
password: '',
};
}
export function parse(raw: string | undefined): Network {
@@ -35,18 +36,20 @@ export namespace Network {
}
try {
// Patter: PROTOCOL://USER:PASS@HOSTNAME:PORT/
const { protocol, hostname, password, username, port } = new URL(raw);
const { protocol, hostname, password, username, port } = new URL(
raw
);
return {
protocol,
hostname,
password,
username,
port
port,
};
} catch {
return 'none';
}
};
}
export function stringify(network: Network): string | undefined {
if (network === 'none') {
@@ -54,8 +57,16 @@ export namespace Network {
}
const { protocol, hostname, password, username, port } = network;
try {
const defaultUrl = new URL(`${protocol ? protocol : 'http'}://${hostname ? hostname : '_'}`);
return Object.assign(defaultUrl, { protocol, hostname, password, username, port }).toString();
const defaultUrl = new URL(
`${protocol ? protocol : 'http'}://${hostname ? hostname : '_'}`
);
return Object.assign(defaultUrl, {
protocol,
hostname,
password,
username,
port,
}).toString();
} catch {
return undefined;
}
@@ -68,12 +79,13 @@ export namespace Network {
if (right === 'none') {
return false;
}
return left.hostname === right.hostname
&& left.password === right.password
&& left.protocol === right.protocol
&& left.username === right.username;
};
return (
left.hostname === right.hostname &&
left.password === right.password &&
left.protocol === right.protocol &&
left.username === right.username
);
}
}
export interface Config {
@@ -95,9 +107,11 @@ export namespace Config {
return false;
}
}
return left.dataDirUri === right.dataDirUri
&& left.downloadsDirUri === right.downloadsDirUri
&& left.sketchDirUri === right.sketchDirUri
&& Network.sameAs(left.network, right.network);
return (
left.dataDirUri === right.dataDirUri &&
left.downloadsDirUri === right.downloadsDirUri &&
left.sketchDirUri === right.sketchDirUri &&
Network.sameAs(left.network, right.network)
);
}
}

View File

@@ -1,19 +1,29 @@
import { Programmer } from './boards-service';
export const CompilerWarningLiterals = ['None', 'Default', 'More', 'All'] as const;
export const CompilerWarningLiterals = [
'None',
'Default',
'More',
'All',
] as const;
export type CompilerWarnings = typeof CompilerWarningLiterals[number];
export const CoreServicePath = '/services/core-service';
export const CoreService = Symbol('CoreService');
export interface CoreService {
compile(options: CoreService.Compile.Options & Readonly<{ exportBinaries?: boolean, compilerWarnings?: CompilerWarnings }>): Promise<void>;
compile(
options: CoreService.Compile.Options &
Readonly<{
exportBinaries?: boolean;
compilerWarnings?: CompilerWarnings;
}>
): Promise<void>;
upload(options: CoreService.Upload.Options): Promise<void>;
uploadUsingProgrammer(options: CoreService.Upload.Options): Promise<void>;
burnBootloader(options: CoreService.Bootloader.Options): Promise<void>;
}
export namespace CoreService {
export namespace Compile {
export interface Options {
/**
@@ -44,5 +54,4 @@ export namespace CoreService {
readonly verify: boolean;
}
}
}

View File

@@ -4,7 +4,9 @@ export const ExamplesServicePath = '/services/example-service';
export const ExamplesService = Symbol('ExamplesService');
export interface ExamplesService {
builtIns(): Promise<SketchContainer[]>;
installed(options: { fqbn?: string }): Promise<{ user: SketchContainer[], current: SketchContainer[], any: SketchContainer[] }>;
installed(options: { fqbn?: string }): Promise<{
user: SketchContainer[];
current: SketchContainer[];
any: SketchContainer[];
}>;
}

View File

@@ -1,5 +1,5 @@
export const ExecutableServicePath = '/services/executable-service';
export const ExecutableService = Symbol('ExecutableService');
export interface ExecutableService {
list(): Promise<{ clangdUri: string, cliUri: string, lsUri: string }>;
list(): Promise<{ clangdUri: string; cliUri: string; lsUri: string }>;
}

View File

@@ -1,6 +1,9 @@
import * as semver from 'semver';
import { Progress } from '@theia/core/lib/common/message-service-protocol';
import { CancellationToken, CancellationTokenSource } from '@theia/core/lib/common/cancellation';
import {
CancellationToken,
CancellationTokenSource,
} from '@theia/core/lib/common/cancellation';
import { naturalCompare } from './../utils';
import { ArduinoComponent } from './arduino-component';
import { MessageService } from '@theia/core';
@@ -10,15 +13,18 @@ export interface Installable<T extends ArduinoComponent> {
/**
* If `options.version` is specified, that will be installed. Otherwise, `item.availableVersions[0]`.
*/
install(options: { item: T, progressId?: string, version?: Installable.Version }): Promise<void>;
install(options: {
item: T;
progressId?: string;
version?: Installable.Version;
}): Promise<void>;
/**
* Uninstalls the given component. It is a NOOP if not installed.
*/
uninstall(options: { item: T, progressId?: string }): Promise<void>;
uninstall(options: { item: T; progressId?: string }): Promise<void>;
}
export namespace Installable {
export type Version = string;
export namespace Version {
@@ -33,65 +39,90 @@ export namespace Installable {
};
}
export async function installWithProgress<T extends ArduinoComponent>(options: {
installable: Installable<T>,
messageService: MessageService,
responseService: ResponseServiceImpl,
item: T,
version: Installable.Version
export async function installWithProgress<
T extends ArduinoComponent
>(options: {
installable: Installable<T>;
messageService: MessageService;
responseService: ResponseServiceImpl;
item: T;
version: Installable.Version;
}): Promise<void> {
const { item, version } = options;
return doWithProgress({
...options,
progressText: `Processing ${item.name}:${version}`,
run: ({ progressId }) => options.installable.install({ item: options.item, version: options.version, progressId })
run: ({ progressId }) =>
options.installable.install({
item: options.item,
version: options.version,
progressId,
}),
});
}
export async function uninstallWithProgress<T extends ArduinoComponent>(options: {
installable: Installable<T>,
messageService: MessageService,
responseService: ResponseServiceImpl,
item: T
export async function uninstallWithProgress<
T extends ArduinoComponent
>(options: {
installable: Installable<T>;
messageService: MessageService;
responseService: ResponseServiceImpl;
item: T;
}): Promise<void> {
const { item } = options;
return doWithProgress({
...options,
progressText: `Processing ${item.name}${item.installedVersion ? `:${item.installedVersion}` : ''}`,
run: ({ progressId }) => options.installable.uninstall({ item: options.item, progressId })
progressText: `Processing ${item.name}${
item.installedVersion ? `:${item.installedVersion}` : ''
}`,
run: ({ progressId }) =>
options.installable.uninstall({
item: options.item,
progressId,
}),
});
}
export async function doWithProgress(options: {
run: ({ progressId }: { progressId: string }) => Promise<void>,
messageService: MessageService,
responseService: ResponseServiceImpl,
progressText: string
run: ({ progressId }: { progressId: string }) => Promise<void>;
messageService: MessageService;
responseService: ResponseServiceImpl;
progressText: string;
}): Promise<void> {
return withProgress(options.progressText, options.messageService, async (progress, _) => {
const progressId = progress.id;
const toDispose = options.responseService.onProgressDidChange(progressMessage => {
if (progressId === progressMessage.progressId) {
const { message, work } = progressMessage;
progress.report({ message, work });
return withProgress(
options.progressText,
options.messageService,
async (progress, _) => {
const progressId = progress.id;
const toDispose = options.responseService.onProgressDidChange(
(progressMessage) => {
if (progressId === progressMessage.progressId) {
const { message, work } = progressMessage;
progress.report({ message, work });
}
}
);
try {
options.responseService.clearArduinoChannel();
await options.run({ progressId });
} finally {
toDispose.dispose();
}
});
try {
options.responseService.clearArduinoChannel();
await options.run({ progressId });
} finally {
toDispose.dispose();
}
});
);
}
async function withProgress(text: string, messageService: MessageService, cb: (progress: Progress, token: CancellationToken) => Promise<void>): Promise<void> {
async function withProgress(
text: string,
messageService: MessageService,
cb: (progress: Progress, token: CancellationToken) => Promise<void>
): Promise<void> {
const cancellationSource = new CancellationTokenSource();
const { token } = cancellationSource;
const progress = await messageService.showProgress({ text, options: { cancelable: false } }, () => cancellationSource.cancel());
const progress = await messageService.showProgress(
{ text, options: { cancelable: false } },
() => cancellationSource.cancel()
);
try {
await cb(progress, token);
} finally {

View File

@@ -4,18 +4,37 @@ import { ArduinoComponent } from './arduino-component';
export const LibraryServicePath = '/services/library-service';
export const LibraryService = Symbol('LibraryService');
export interface LibraryService extends Installable<LibraryPackage>, Searchable<LibraryPackage> {
export interface LibraryService
extends Installable<LibraryPackage>,
Searchable<LibraryPackage> {
list(options: LibraryService.List.Options): Promise<LibraryPackage[]>;
/**
* When `installDependencies` is not set, it is `true` by default. If you want to skip the installation of required dependencies, set it to `false`.
*/
install(options: { item: LibraryPackage, progressId?: string, version?: Installable.Version, installDependencies?: boolean }): Promise<void>;
installZip(options: { zipUri: string, progressId?: string, overwrite?: boolean }): Promise<void>;
install(options: {
item: LibraryPackage;
progressId?: string;
version?: Installable.Version;
installDependencies?: boolean;
}): Promise<void>;
installZip(options: {
zipUri: string;
progressId?: string;
overwrite?: boolean;
}): Promise<void>;
/**
* Set `filterSelf` to `true` if you want to avoid having `item` in the result set.
* Note: as of today (22.02.2021), the CLI works like this: `./arduino-cli lib deps Adaino@0.1.0 ✕ Adaino 0.1.0 must be installed.`.
*/
listDependencies({ item, version, filterSelf }: { item: LibraryPackage, version: Installable.Version, filterSelf?: boolean }): Promise<LibraryDependency[]>;
listDependencies({
item,
version,
filterSelf,
}: {
item: LibraryPackage;
version: Installable.Version;
filterSelf?: boolean;
}): Promise<LibraryDependency[]>;
}
export namespace LibraryService {
@@ -27,7 +46,6 @@ export namespace LibraryService {
}
export enum LibraryLocation {
/**
* In the `libraries` subdirectory of the Arduino IDE installation.
*/
@@ -47,12 +65,10 @@ export enum LibraryLocation {
* When `LibraryLocation` is used in a context where a board is specified, this indicates the library is in the `libraries`
* subdirectory of a platform referenced by the board's platform.
*/
REFERENCED_PLATFORM_BUILTIN = 3
REFERENCED_PLATFORM_BUILTIN = 3,
}
export interface LibraryPackage extends ArduinoComponent {
/**
* Same as [`Library#real_name`](https://arduino.github.io/arduino-cli/latest/rpc/commands/#library).
* Should be used for the UI, and `name` is used to uniquely identify a library. It does not have an ID.
@@ -70,16 +86,25 @@ export interface LibraryPackage extends ArduinoComponent {
readonly installDirUri?: string;
}
export namespace LibraryPackage {
export function is(arg: any): arg is LibraryPackage {
return ArduinoComponent.is(arg) && 'includes' in arg && Array.isArray(arg['includes']);
return (
ArduinoComponent.is(arg) &&
'includes' in arg &&
Array.isArray(arg['includes'])
);
}
export function equals(left: LibraryPackage, right: LibraryPackage): boolean {
export function equals(
left: LibraryPackage,
right: LibraryPackage
): boolean {
return left.name === right.name && left.author === right.author;
}
export function groupByLocation(packages: LibraryPackage[]): { user: LibraryPackage[], rest: LibraryPackage[] } {
export function groupByLocation(packages: LibraryPackage[]): {
user: LibraryPackage[];
rest: LibraryPackage[];
} {
const user: LibraryPackage[] = [];
const rest: LibraryPackage[] = [];
for (const pkg of packages) {
@@ -91,7 +116,6 @@ export namespace LibraryPackage {
}
return { user, rest };
}
}
export interface LibraryDependency {

View File

@@ -1,8 +1,8 @@
import { JsonRpcServer } from '@theia/core/lib/common/messaging/proxy-factory';
import { Board, Port } from './boards-service';
export interface Status { }
export interface OK extends Status { }
export interface Status {}
export type OK = Status;
export interface ErrorStatus extends Status {
readonly message: string;
}
@@ -12,7 +12,9 @@ export namespace Status {
}
export const OK: OK = {};
export const NOT_CONNECTED: ErrorStatus = { message: 'Not connected.' };
export const ALREADY_CONNECTED: ErrorStatus = { message: 'Already connected.' };
export const ALREADY_CONNECTED: ErrorStatus = {
message: 'Already connected.',
};
}
export const MonitorServicePath = '/services/serial-monitor';
@@ -35,19 +37,25 @@ export interface MonitorConfig {
* Defaults to `9600`.
*/
readonly baudRate?: MonitorConfig.BaudRate;
}
export namespace MonitorConfig {
export type BaudRate = 300 | 1200 | 2400 | 4800 | 9600 | 19200 | 38400 | 57600 | 115200;
export type BaudRate =
| 300
| 1200
| 2400
| 4800
| 9600
| 19200
| 38400
| 57600
| 115200;
export namespace BaudRate {
export const DEFAULT: BaudRate = 9600;
}
export enum ConnectionType {
SERIAL = 0
SERIAL = 0,
}
}
export const MonitorServiceClient = Symbol('MonitorServiceClient');

View File

@@ -1,6 +1,11 @@
import { LibraryPackage } from './library-service';
import { JsonRpcServer } from '@theia/core/lib/common/messaging/proxy-factory';
import { Sketch, Config, BoardsPackage, AttachedBoardsChangeEvent } from '../protocol';
import {
Sketch,
Config,
BoardsPackage,
AttachedBoardsChangeEvent,
} from '../protocol';
export interface NotificationServiceClient {
notifyIndexUpdated(): void;
@@ -17,6 +22,8 @@ export interface NotificationServiceClient {
export const NotificationServicePath = '/services/notification-service';
export const NotificationServiceServer = Symbol('NotificationServiceServer');
export interface NotificationServiceServer extends Required<NotificationServiceClient>, JsonRpcServer<NotificationServiceClient> {
export interface NotificationServiceServer
extends Required<NotificationServiceClient>,
JsonRpcServer<NotificationServiceClient> {
disposeClient(client: NotificationServiceClient): void;
}

View File

@@ -13,8 +13,9 @@ import { ConfigService } from './config-service';
import { SketchContainer } from './sketches-service';
@injectable()
export class SketchesServiceClientImpl implements FrontendApplicationContribution {
export class SketchesServiceClientImpl
implements FrontendApplicationContribution
{
@inject(FileService)
protected readonly fileService: FileService;
@@ -32,45 +33,81 @@ export class SketchesServiceClientImpl implements FrontendApplicationContributio
protected toDispose = new DisposableCollection();
protected sketches = new Map<string, Sketch>();
protected sketchbookDidChangeEmitter = new Emitter<{ created: Sketch[], removed: Sketch[] }>();
protected sketchbookDidChangeEmitter = new Emitter<{
created: Sketch[];
removed: Sketch[];
}>();
readonly onSketchbookDidChange = this.sketchbookDidChangeEmitter.event;
onStart(): void {
this.configService.getConfiguration().then(({ sketchDirUri }) => {
this.sketchService.getSketches({ uri: sketchDirUri }).then(container => {
const sketchbookUri = new URI(sketchDirUri);
for (const sketch of SketchContainer.toArray(container)) {
this.sketches.set(sketch.uri, sketch);
}
this.toDispose.push(this.fileService.watch(new URI(sketchDirUri), { recursive: true, excludes: [] }));
this.toDispose.push(this.fileService.onDidFilesChange(async event => {
for (const { type, resource } of event.changes) {
// We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.
if (sketchbookUri.isEqualOrParent(resource)) {
if (Sketch.isSketchFile(resource)) {
if (type === FileChangeType.ADDED) {
try {
const toAdd = await this.sketchService.loadSketch(resource.parent.toString());
if (!this.sketches.has(toAdd.uri)) {
console.log(`New sketch '${toAdd.name}' was crated in sketchbook '${sketchDirUri}'.`);
this.sketches.set(toAdd.uri, toAdd);
this.fireSoon(toAdd, 'created');
this.sketchService
.getSketches({ uri: sketchDirUri })
.then((container) => {
const sketchbookUri = new URI(sketchDirUri);
for (const sketch of SketchContainer.toArray(container)) {
this.sketches.set(sketch.uri, sketch);
}
this.toDispose.push(
this.fileService.watch(new URI(sketchDirUri), {
recursive: true,
excludes: [],
})
);
this.toDispose.push(
this.fileService.onDidFilesChange(async (event) => {
for (const { type, resource } of event.changes) {
// We track main sketch files changes only. // TODO: check sketch folder changes. One can rename the folder without renaming the `.ino` file.
if (sketchbookUri.isEqualOrParent(resource)) {
if (Sketch.isSketchFile(resource)) {
if (type === FileChangeType.ADDED) {
try {
const toAdd =
await this.sketchService.loadSketch(
resource.parent.toString()
);
if (
!this.sketches.has(
toAdd.uri
)
) {
console.log(
`New sketch '${toAdd.name}' was crated in sketchbook '${sketchDirUri}'.`
);
this.sketches.set(
toAdd.uri,
toAdd
);
this.fireSoon(
toAdd,
'created'
);
}
} catch {}
} else if (
type === FileChangeType.DELETED
) {
const uri =
resource.parent.toString();
const toDelete =
this.sketches.get(uri);
if (toDelete) {
console.log(
`Sketch '${toDelete.name}' was removed from sketchbook '${sketchbookUri}'.`
);
this.sketches.delete(uri);
this.fireSoon(
toDelete,
'removed'
);
}
}
} catch { }
} else if (type === FileChangeType.DELETED) {
const uri = resource.parent.toString();
const toDelete = this.sketches.get(uri);
if (toDelete) {
console.log(`Sketch '${toDelete.name}' was removed from sketchbook '${sketchbookUri}'.`);
this.sketches.delete(uri);
this.fireSoon(toDelete, 'removed');
}
}
}
}
}
}));
});
})
);
});
});
}
@@ -79,12 +116,24 @@ export class SketchesServiceClientImpl implements FrontendApplicationContributio
}
async currentSketch(): Promise<Sketch | undefined> {
const sketches = (await Promise.all(this.workspaceService.tryGetRoots().map(({ resource }) => this.sketchService.getSketchFolder(resource.toString())))).filter(notEmpty);
const sketches = (
await Promise.all(
this.workspaceService
.tryGetRoots()
.map(({ resource }) =>
this.sketchService.getSketchFolder(resource.toString())
)
)
).filter(notEmpty);
if (!sketches.length) {
return undefined;
}
if (sketches.length > 1) {
console.log(`Multiple sketch folders were found in the workspace. Falling back to the first one. Sketch folders: ${JSON.stringify(sketches)}`);
console.log(
`Multiple sketch folders were found in the workspace. Falling back to the first one. Sketch folders: ${JSON.stringify(
sketches
)}`
);
}
return sketches[0];
}
@@ -104,7 +153,10 @@ export class SketchesServiceClientImpl implements FrontendApplicationContributio
}
private fireSoonHandle?: number;
private bufferedSketchbookEvents: { type: 'created' | 'removed', sketch: Sketch }[] = [];
private bufferedSketchbookEvents: {
type: 'created' | 'removed';
sketch: Sketch;
}[] = [];
private fireSoon(sketch: Sketch, type: 'created' | 'removed'): void {
this.bufferedSketchbookEvents.push({ type, sketch });
@@ -114,9 +166,9 @@ export class SketchesServiceClientImpl implements FrontendApplicationContributio
}
this.fireSoonHandle = window.setTimeout(() => {
const event: { created: Sketch[], removed: Sketch[] } = {
const event: { created: Sketch[]; removed: Sketch[] } = {
created: [],
removed: []
removed: [],
};
for (const { type, sketch } of this.bufferedSketchbookEvents) {
if (type === 'created') {
@@ -138,8 +190,9 @@ export class SketchesServiceClientImpl implements FrontendApplicationContributio
if (toCheck.scheme === 'user-storage') {
return false;
}
const readOnly = !this.workspaceService.tryGetRoots().some(({ resource }) => resource.isEqualOrParent(toCheck));
const readOnly = !this.workspaceService
.tryGetRoots()
.some(({ resource }) => resource.isEqualOrParent(toCheck));
return readOnly;
}
}

View File

@@ -3,13 +3,18 @@ import URI from '@theia/core/lib/common/uri';
export const SketchesServicePath = '/services/sketches-service';
export const SketchesService = Symbol('SketchesService');
export interface SketchesService {
/**
* Resolves to a sketch container representing the hierarchical structure of the sketches.
* If `uri` is not given, `directories.user` will be user instead. Specify `exclude` global patterns to filter folders from the sketch container.
* If `exclude` is not set `['**\/libraries\/**', '**\/hardware\/**']` will be used instead.
*/
getSketches({ uri, exclude }: { uri?: string, exclude?: string[] }): Promise<SketchContainer>;
getSketches({
uri,
exclude,
}: {
uri?: string;
exclude?: string[];
}): Promise<SketchContainer>;
/**
* This is the TS implementation of `SketchLoad` from the CLI and should be replaced with a gRPC call eventually.
@@ -69,7 +74,6 @@ export interface SketchesService {
* Based on https://github.com/arduino/arduino-cli/blob/550179eefd2d2bca299d50a4af9e9bfcfebec649/arduino/builder/builder.go#L30-L38
*/
getIdeTempFolderUri(sketch: Sketch): Promise<string>;
}
export interface Sketch {
@@ -82,23 +86,47 @@ export interface Sketch {
}
export namespace Sketch {
export function is(arg: any): arg is Sketch {
return !!arg && 'name' in arg && 'uri' in arg && typeof arg.name === 'string' && typeof arg.uri === 'string';
return (
!!arg &&
'name' in arg &&
'uri' in arg &&
typeof arg.name === 'string' &&
typeof arg.uri === 'string'
);
}
export namespace Extensions {
export const MAIN = ['.ino', '.pde'];
export const SOURCE = ['.c', '.cpp', '.s'];
export const ADDITIONAL = ['.h', '.c', '.hpp', '.hh', '.cpp', '.S', '.json', '.md', '.adoc'];
export const ALL = Array.from(new Set([...MAIN, ...SOURCE, ...ADDITIONAL]));
export const ADDITIONAL = [
'.h',
'.c',
'.hpp',
'.hh',
'.cpp',
'.S',
'.json',
'.md',
'.adoc',
];
export const ALL = Array.from(
new Set([...MAIN, ...SOURCE, ...ADDITIONAL])
);
}
export function isInSketch(uri: string | URI, sketch: Sketch): boolean {
const { mainFileUri, otherSketchFileUris, additionalFileUris } = sketch;
return [mainFileUri, ...otherSketchFileUris, ...additionalFileUris].indexOf(uri.toString()) !== -1;
return (
[
mainFileUri,
...otherSketchFileUris,
...additionalFileUris,
].indexOf(uri.toString()) !== -1
);
}
export function isSketchFile(arg: string | URI): boolean {
if (arg instanceof URI) {
return isSketchFile(arg.toString());
}
return Extensions.MAIN.some(ext => arg.endsWith(ext));
return Extensions.MAIN.some((ext) => arg.endsWith(ext));
}
}
@@ -108,12 +136,16 @@ export interface SketchContainer {
readonly sketches: Sketch[];
}
export namespace SketchContainer {
export function is(arg: any): arg is SketchContainer {
return !!arg
&& 'label' in arg && typeof arg.label === 'string'
&& 'children' in arg && Array.isArray(arg.children)
&& 'sketches' in arg && Array.isArray(arg.sketches);
return (
!!arg &&
'label' in arg &&
typeof arg.label === 'string' &&
'children' in arg &&
Array.isArray(arg.children) &&
'sketches' in arg &&
Array.isArray(arg.sketches)
);
}
/**
@@ -121,11 +153,14 @@ export namespace SketchContainer {
*/
export function isEmpty(container: SketchContainer): boolean {
const hasSketch = (parent: SketchContainer) => {
if (parent.sketches.length || parent.children.some(child => hasSketch(child))) {
if (
parent.sketches.length ||
parent.children.some((child) => hasSketch(child))
) {
return true;
}
return false;
}
};
return !hasSketch(container);
}
@@ -141,11 +176,10 @@ export namespace SketchContainer {
export function toArray(container: SketchContainer): Sketch[] {
const visit = (parent: SketchContainer, toPushSketch: Sketch[]) => {
toPushSketch.push(...parent.sketches);
parent.children.map(child => visit(child, toPushSketch));
}
parent.children.map((child) => visit(child, toPushSketch));
};
const sketches: Sketch[] = [];
visit(container, sketches);
return sketches;
}
}