-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy patharduino-context-mapper.ts
126 lines (116 loc) · 2.82 KB
/
arduino-context-mapper.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type {
Port as APIPort,
BoardDetails as ApiBoardDetails,
BuildProperties as ApiBuildProperties,
CompileSummary as ApiCompileSummary,
ConfigOption as ApiConfigOption,
ConfigValue as ApiConfigValue,
Tool as ApiTool,
} from 'vscode-arduino-api';
import type {
BoardDetails,
CompileSummary,
ConfigOption,
ConfigValue,
Port,
Tool,
} from '../protocol';
export function toApiCompileSummary(
compileSummary: CompileSummary
): ApiCompileSummary {
const {
buildPath,
buildProperties,
boardPlatform,
buildPlatform,
executableSectionsSize,
usedLibraries,
} = compileSummary;
return {
buildPath,
buildProperties: toApiBuildProperties(buildProperties),
executableSectionsSize: executableSectionsSize,
boardPlatform,
buildPlatform,
usedLibraries,
};
}
export function toApiPort(port: Port): APIPort | undefined {
const {
hardwareId = '',
properties = {},
address,
protocol,
protocolLabel,
addressLabel: label,
} = port;
return {
label,
address,
hardwareId,
properties,
protocol,
protocolLabel,
};
}
export function toApiBoardDetails(boardDetails: BoardDetails): ApiBoardDetails {
const { fqbn, programmers, configOptions, requiredTools } = boardDetails;
return {
buildProperties: toApiBuildProperties(boardDetails.buildProperties),
configOptions: configOptions.map(toApiConfigOption),
fqbn,
programmers,
toolsDependencies: requiredTools.map(toApiTool),
};
}
function toApiConfigOption(configOption: ConfigOption): ApiConfigOption {
const { label, values, option } = configOption;
return {
optionLabel: label,
option,
values: values.map(toApiConfigValue),
};
}
function toApiConfigValue(configValue: ConfigValue): ApiConfigValue {
const { label, selected, value } = configValue;
return {
selected,
value,
valueLabel: label,
};
}
function toApiTool(toolDependency: Tool): ApiTool {
const { name, packager, version } = toolDependency;
return {
name,
packager,
version,
};
}
const propertySep = '=';
function parseProperty(
property: string
): [key: string, value: string] | undefined {
const segments = property.split(propertySep);
if (segments.length < 2) {
console.warn(`Could not parse build property: ${property}.`);
return undefined;
}
const [key, ...rest] = segments;
if (!key) {
console.warn(`Could not determine property key from raw: ${property}.`);
return undefined;
}
const value = rest.join(propertySep);
return [key, value];
}
export function toApiBuildProperties(properties: string[]): ApiBuildProperties {
return properties.reduce((acc, curr) => {
const entry = parseProperty(curr);
if (entry) {
const [key, value] = entry;
acc[key] = value;
}
return acc;
}, <Record<string, string>>{});
}