refactor: rely on etcher-image-stream to fetch bmap contents (#654)

In order to get the bmap file contents to the Etcher CLI, we were
handling extraction, writing to a temporary file, then reading again,
and all sorts of other mumbo-jumbo, without realising that
`etcher-image-stream` already has this information right where we need
it (in the CLI's writer module) and in the way we need it (as plain
text).

Re-using from there hugely simplifies things.

Signed-off-by: Juan Cruz Viotti <jviotti@openmailbox.org>
This commit is contained in:
Juan Cruz Viotti 2016-08-24 10:19:18 -04:00 committed by GitHub
parent 8371757c70
commit da81849d4b
9 changed files with 9 additions and 98 deletions

View File

@ -39,7 +39,6 @@ Options
--check, -c validate write --check, -c validate write
--robot, -r parse-able output without interactivity --robot, -r parse-able output without interactivity
--log, -l output log file --log, -l output log file
--bmap, -b bmap file
--yes, -y confirm non-interactively --yes, -y confirm non-interactively
--unmount, -u unmount on success --unmount, -u unmount on success
``` ```

View File

@ -120,11 +120,6 @@ module.exports = yargs
string: true, string: true,
alias: 'l' alias: 'l'
}, },
bmap: {
describe: 'bmap file',
string: true,
alias: 'b'
},
yes: { yes: {
describe: 'confirm non-interactively', describe: 'confirm non-interactively',
boolean: true, boolean: true,

View File

@ -18,7 +18,6 @@
const _ = require('lodash'); const _ = require('lodash');
const Bluebird = require('bluebird'); const Bluebird = require('bluebird');
const fs = Bluebird.promisifyAll(require('fs'));
const visuals = require('resin-cli-visuals'); const visuals = require('resin-cli-visuals');
const form = require('resin-cli-form'); const form = require('resin-cli-form');
const drivelist = Bluebird.promisifyAll(require('drivelist')); const drivelist = Bluebird.promisifyAll(require('drivelist'));
@ -60,19 +59,8 @@ form.run([
check: new visuals.Progress('Validating') check: new visuals.Progress('Validating')
}; };
return Bluebird.props({ return drivelist.listAsync().then((drives) => {
drives: drivelist.listAsync(), const selectedDrive = _.find(drives, {
bmap: _.attempt(() => {
if (!options.bmap) {
return;
}
return fs.readFileAsync(options.bmap, {
encoding: 'utf8'
});
})
}).then((results) => {
const selectedDrive = _.find(results.drives, {
device: answers.drive device: answers.drive
}); });
@ -82,8 +70,7 @@ form.run([
return writer.writeImage(options._[0], selectedDrive, { return writer.writeImage(options._[0], selectedDrive, {
unmountOnSuccess: options.unmount, unmountOnSuccess: options.unmount,
validateWriteOnSuccess: options.check, validateWriteOnSuccess: options.check
bmapContents: results.bmap
}, (state) => { }, (state) => {
if (options.robot) { if (options.robot) {

View File

@ -38,7 +38,6 @@ const isWindows = os.platform() === 'win32';
* @param {Object} options - options * @param {Object} options - options
* @param {Boolean} [options.unmountOnSuccess=false] - unmount on success * @param {Boolean} [options.unmountOnSuccess=false] - unmount on success
* @param {Boolean} [options.validateWriteOnSuccess=false] - validate write on success * @param {Boolean} [options.validateWriteOnSuccess=false] - validate write on success
* @param {String} [options.bmapContents] - bmap file contents
* @param {Function} onProgress - on progress callback (state) * @param {Function} onProgress - on progress callback (state)
* *
* @fulfil {Boolean} - whether the operation was successful * @fulfil {Boolean} - whether the operation was successful
@ -70,7 +69,7 @@ exports.writeImage = (imagePath, drive, options, onProgress) => {
}, results.image, { }, results.image, {
check: options.validateWriteOnSuccess, check: options.validateWriteOnSuccess,
transform: results.image.transform, transform: results.image.transform,
bmap: options.bmapContents bmap: results.image.bmap
}); });
}).then((writer) => { }).then((writer) => {
return new Bluebird((resolve, reject) => { return new Bluebird((resolve, reject) => {

View File

@ -260,20 +260,6 @@ SelectionStateModel.service('SelectionStateModel', function(DrivesModel) {
return _.get(Store.getState().toJS(), 'selection.image.logo'); return _.get(Store.getState().toJS(), 'selection.image.logo');
}; };
/**
* @summary Get image bmap
* @function
* @public
*
* @returns {String} image bmap
*
* @example
* const imageBmap = SelectionStateModel.getImageBmap();
*/
this.getImageBmap = () => {
return _.get(Store.getState().toJS(), 'selection.image.bmap');
};
/** /**
* @summary Check if there is a selected drive * @summary Check if there is a selected drive
* @function * @function

View File

@ -58,8 +58,7 @@ imageWriter.service('ImageWriterService', function($q, $rootScope, SettingsModel
return $q((resolve, reject) => { return $q((resolve, reject) => {
const child = childWriter.write(image, drive, { const child = childWriter.write(image, drive, {
validateWriteOnSuccess: SettingsModel.get('validateWriteOnSuccess'), validateWriteOnSuccess: SettingsModel.get('validateWriteOnSuccess'),
unmountOnSuccess: SettingsModel.get('unmountOnSuccess'), unmountOnSuccess: SettingsModel.get('unmountOnSuccess')
bmapContents: SelectionStateModel.getImageBmap()
}); });
child.on('error', reject); child.on('error', reject);
child.on('done', resolve); child.on('done', resolve);

View File

@ -17,9 +17,6 @@
'use strict'; 'use strict';
const EventEmitter = require('events').EventEmitter; const EventEmitter = require('events').EventEmitter;
const _ = require('lodash');
const Bluebird = require('bluebird');
const fs = Bluebird.promisifyAll(require('fs'));
const childProcess = require('child_process'); const childProcess = require('child_process');
const rendererUtils = require('./renderer-utils'); const rendererUtils = require('./renderer-utils');
const utils = require('./utils'); const utils = require('./utils');
@ -61,22 +58,10 @@ const EXIT_CODES = require('../exit-codes');
exports.write = (image, drive, options) => { exports.write = (image, drive, options) => {
const emitter = new EventEmitter(); const emitter = new EventEmitter();
Bluebird.props({ utils.getTemporaryLogFilePath().then((logFile) => {
logFile: utils.getTemporaryLogFilePath(),
bmapFile: _.attempt(() => {
if (!options.bmapContents) {
return;
}
return utils.getTemporaryBmapFilePath().tap((bmapFilePath) => {
return fs.writeFileAsync(bmapFilePath, options.bmapContents);
});
})
}).then((results) => {
const argv = utils.getCLIWriterArguments({ const argv = utils.getCLIWriterArguments({
entryPoint: rendererUtils.getApplicationEntryPoint(), entryPoint: rendererUtils.getApplicationEntryPoint(),
logFile: results.logFile, logFile: logFile,
bmap: results.bmapFile,
image: image, image: image,
device: drive.device, device: drive.device,
validateWriteOnSuccess: options.validateWriteOnSuccess, validateWriteOnSuccess: options.validateWriteOnSuccess,
@ -85,7 +70,7 @@ exports.write = (image, drive, options) => {
// Make writer proxy inherit the temporary log file location // Make writer proxy inherit the temporary log file location
// while keeping current environment variables intact. // while keeping current environment variables intact.
process.env[CONSTANTS.TEMPORARY_LOG_FILE_ENVIRONMENT_VARIABLE] = results.logFile; process.env[CONSTANTS.TEMPORARY_LOG_FILE_ENVIRONMENT_VARIABLE] = logFile;
const child = childProcess.fork(CONSTANTS.WRITER_PROXY_SCRIPT, argv, { const child = childProcess.fork(CONSTANTS.WRITER_PROXY_SCRIPT, argv, {
silent: true, silent: true,

View File

@ -60,7 +60,6 @@ exports.getBooleanArgumentForm = (argumentName, value) => {
* @param {String} options.device - device * @param {String} options.device - device
* @param {String} options.entryPoint - entry point * @param {String} options.entryPoint - entry point
* @param {String} [options.logFile] - log file * @param {String} [options.logFile] - log file
* @param {String} [options.bmap] - bmap file
* @param {Boolean} [options.validateWriteOnSuccess] - validate write on success * @param {Boolean} [options.validateWriteOnSuccess] - validate write on success
* @param {Boolean} [options.unmountOnSuccess] - unmount on success * @param {Boolean} [options.unmountOnSuccess] - unmount on success
* @returns {String[]} arguments * @returns {String[]} arguments
@ -108,10 +107,6 @@ exports.getCLIWriterArguments = (options) => {
argv.push('--log', options.logFile); argv.push('--log', options.logFile);
} }
if (options.bmap) {
argv.push('--bmap', options.bmap);
}
return argv; return argv;
}; };
@ -151,23 +146,3 @@ exports.getTemporaryLogFilePath = () => {
postfix: '.log' postfix: '.log'
}); });
}; };
/**
* @summary Get a temporary bmap file path
* @function
* @public
*
* @fulfil {String} - bmap path
* @returns {Promise}
*
* @example
* utils.getTemporaryBmapFilePath().then((bmapFilePath) => {
* console.log(bmapFilePath);
* });
*/
exports.getTemporaryBmapFilePath = () => {
return tmp.fileAsync({
prefix: `${packageJSON.name}-`,
postfix: '.bmap'
});
};

View File

@ -55,10 +55,6 @@ describe('Browser: SelectionState', function() {
m.chai.expect(SelectionStateModel.getImageLogo()).to.be.undefined; m.chai.expect(SelectionStateModel.getImageLogo()).to.be.undefined;
}); });
it('getImageBmap() should return undefined', function() {
m.chai.expect(SelectionStateModel.getImageBmap()).to.be.undefined;
});
it('hasDrive() should return false', function() { it('hasDrive() should return false', function() {
const hasDrive = SelectionStateModel.hasDrive(); const hasDrive = SelectionStateModel.hasDrive();
m.chai.expect(hasDrive).to.be.false; m.chai.expect(hasDrive).to.be.false;
@ -276,8 +272,7 @@ describe('Browser: SelectionState', function() {
size: 999999999, size: 999999999,
url: 'https://www.raspbian.org', url: 'https://www.raspbian.org',
name: 'Raspbian', name: 'Raspbian',
logo: '<svg><text fill="red">Raspbian</text></svg>', logo: '<svg><text fill="red">Raspbian</text></svg>'
bmap: '<Range>Foo Bar</Range>'
}); });
}); });
@ -430,15 +425,6 @@ describe('Browser: SelectionState', function() {
}); });
describe('.getImageBmap()', function() {
it('should return the image bmap', function() {
const imageBmap = SelectionStateModel.getImageBmap();
m.chai.expect(imageBmap).to.equal('<Range>Foo Bar</Range>');
});
});
describe('.hasImage()', function() { describe('.hasImage()', function() {
it('should return true', function() { it('should return true', function() {