Skip to content

Commit

Permalink
fix(xod-client-electron): reject saved previously board options that …
Browse files Browse the repository at this point in the history
…not relate to the current selected board
  • Loading branch information
brusherru committed Oct 30, 2018
1 parent abe88aa commit 9901830
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions packages/xod-client-electron/src/app/arduinoCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,18 @@ const patchFqbnWithOptions = board => {
const options = board.options || [];

const defaultBoardOptions = R.compose(
R.mergeAll,
R.reject(R.isNil),
R.mergeAll,
R.map(opt => ({
[opt.optionId]: R.pathOr(null, ['values', 0, 'value'], opt),
}))
)(options);
const defaultBoardOptionKeys = R.keys(defaultBoardOptions);

// :: StrMap OptionId [OptionValue]
const boardPossibleOptionValuesById = R.compose(
R.map(R.compose(R.pluck('value'), R.prop('values'))),
R.indexBy(R.prop('optionId'))
)(options);

// Find out selected board options that equal to default board options.
//
Expand All @@ -179,10 +184,24 @@ const patchFqbnWithOptions = board => {
R.toPairs
)(selectedOptions);

// Find out board option keys that does not fit the selected board
// Find out board option keys that does not fit the selected board:
// a. no optionId for this board
// E.G. arduino:avr:mega has no options `debugLevel` and it will be ommited
// b. no optionValue for this board
// E.G. previously user uploaded on Arduino Nano with `cpu=atmega328old`,
// but now he tries to upload onto Arduino Mega, which has optionId
// `cpu`, but does not have `atmega328old` option
// :: [OptionId]
const staleBoardOptionKeys = R.compose(
R.reject(isAmong(defaultBoardOptionKeys)),
R.keys
R.reduce(
(acc, [optionId, optionValue]) =>
boardPossibleOptionValuesById[optionId] &&
R.contains(optionValue, boardPossibleOptionValuesById[optionId])
? acc
: R.append(optionId, acc),
[]
),
R.toPairs
)(selectedOptions);

const keysToOmit = R.concat(
Expand All @@ -197,8 +216,9 @@ const patchFqbnWithOptions = board => {
const oneOfDefaultOptions = R.compose(
R.pick(R.__, defaultBoardOptions),
R.of,
R.head
)(defaultBoardOptionKeys);
R.head,
R.keys
)(defaultBoardOptions);

const selectedBoardOptions = R.omit(keysToOmit, selectedOptions);

Expand Down

0 comments on commit 9901830

Please sign in to comment.