Skip to content

Commit

Permalink
refactor: move config file stuff from swingset-runner into vat contro…
Browse files Browse the repository at this point in the history
…ller
  • Loading branch information
FUDCo committed Aug 12, 2020
1 parent 07765e1 commit a9200b4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
39 changes: 39 additions & 0 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,45 @@ export function loadBasedir(basedir) {
return config;
}

function normalizeConfigDescriptor(desc, dirname, expectParameters) {
if (desc) {
for (const name of Object.keys(desc)) {
const entry = desc[name];
if (entry.sourcePath) {
entry.sourcePath = path.resolve(dirname, entry.sourcePath);
}
if (entry.bundlePath) {
entry.bundlePath = path.resolve(dirname, entry.bundlePath);
}
if (expectParameters && !entry.parameters) {
entry.parameters = {};
}
}
}
}

export function loadSwingsetConfigFile(configPath) {
try {
const config = JSON.parse(fs.readFileSync(configPath));
const dirname = path.dirname(configPath);
normalizeConfigDescriptor(config.vats, dirname, true);
normalizeConfigDescriptor(config.bundles, dirname, false);
// normalizeConfigDescriptor(config.devices, dirname, true); // TODO: represent devices
if (!config.bootstrap) {
throw Error(`no designated bootstrap vat in ${configPath}`);
} else if (!config.vats[config.bootstrap]) {
throw Error(`bootstrap vat ${config.bootstrap} not found in ${configPath}`);
}
return config;
} catch (e) {
if (e.code === 'ENOENT') {
return null;
} else {
throw e;
}
}
}

export async function buildVatController(
config,
argv = [],
Expand Down
6 changes: 5 additions & 1 deletion packages/SwingSet/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export { loadBasedir, buildVatController } from './controller';
export {
loadBasedir,
loadSwingsetConfigFile,
buildVatController,
} from './controller';

export { buildMailboxStateMap, buildMailbox } from './devices/mailbox';
export { buildTimer } from './devices/timer';
Expand Down
49 changes: 10 additions & 39 deletions packages/swingset-runner/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import repl from 'repl';
import util from 'util';

import { makeStatLogger } from '@agoric/stat-logger';
import { buildVatController, loadBasedir } from '@agoric/swingset-vat';
import {
buildVatController,
loadSwingsetConfigFile,
loadBasedir,
} from '@agoric/swingset-vat';
import {
initSwingStore as initSimpleSwingStore,
openSwingStore as openSimpleSwingStore,
Expand Down Expand Up @@ -85,42 +89,6 @@ function fail(message, printUsage) {
process.exit(1);
}

function normalizeConfigDescriptor(desc, dirname, expectParameters) {
if (desc) {
for (const name of Object.keys(desc)) {
const entry = desc[name];
if (entry.sourcePath) {
entry.sourcePath = path.resolve(dirname, entry.sourcePath);
}
if (entry.bundlePath) {
entry.bundlePath = path.resolve(dirname, entry.bundlePath);
}
if (expectParameters && !entry.parameters) {
entry.parameters = {};
}
}
}
}

function readConfig(configPath) {
try {
const config = JSON.parse(fs.readFileSync(configPath));
const dirname = path.dirname(configPath);
normalizeConfigDescriptor(config.vats, dirname, true);
normalizeConfigDescriptor(config.bundles, dirname, false);
// normalizeConfigDescriptor(config.devices, dirname, true); // TODO: represent devices
if (!config.bootstrap) {
fail(`no designated bootstrap vat in ${configPath}`);
} else if (!config.vats[config.bootstrap]) {
fail(`bootstrap vat ${config.bootstrap} not found in ${configPath}`);
}
return config;
} catch (e) {
fail(`bad config file: ${e}`);
return null; // just to make eslint shut up
}
}

function generateIndirectConfig(baseConfig) {
const config = {
bootstrap: 'launcher',
Expand Down Expand Up @@ -328,10 +296,13 @@ export async function main() {

let config;
if (configPath) {
config = readConfig(configPath);
config = loadSwingsetConfigFile(configPath);
if (config === null) {
fail(`config file ${configPath} not found`);
}
basedir = path.dirname(configPath);
} else {
config = await loadBasedir(basedir);
config = loadBasedir(basedir);
}
if (launchIndirectly) {
config = generateIndirectConfig(config);
Expand Down

0 comments on commit a9200b4

Please sign in to comment.