Skip to content

Commit

Permalink
Merge pull request #39 from cryptoeng/enter-data-helper-upgrades
Browse files Browse the repository at this point in the history
Adds new features to enter-data-helper
  • Loading branch information
franziskuskiefer authored Apr 3, 2020
2 parents ca6017c + cce2f85 commit 8d65706
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 52 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/enter-data-helper.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: Enter Data Helper

on: [push]
on:
push:
branches:
- master

jobs:
build-and-package:
Expand Down
3 changes: 2 additions & 1 deletion tools/enter-data-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "enter-data-helper",
"author": "CryptoEng",
"version": "0.1.0",
"version": "0.1.1",
"private": true,
"homepage": "./",
"build": {
Expand All @@ -27,6 +27,7 @@
"@jsonforms/react": "^2.4.0-alpha.0",
"@material-ui/core": "^4.9.5",
"@material-ui/icons": "^4.9.1",
"@material-ui/lab": "^4.0.0-alpha.47",
"@material-ui/pickers": "^3.2.10",
"electron-is-dev": "^1.1.0",
"react": "^16.12.0",
Expand Down
53 changes: 50 additions & 3 deletions tools/enter-data-helper/src/App.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,86 @@
import React from 'react';
import { HashRouter as Router, Switch, Route, withRouter } from 'react-router-dom';
import { Container } from '@material-ui/core';
import { Container, Snackbar, CssBaseline } from '@material-ui/core';
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles';
import { Alert } from '@material-ui/lab';
import SelectScheme from './components/SelectScheme';
import SchemeOverview from './components/SchemeOverview';
import { FlavorOverview, SubtypeOverview } from './components/FlavorOverview';
import { NavBar } from './components/BaseComponents';
import { checkRootDir } from './components/Tools'
import { checkRootDir, registerApp } from './components/Tools'
import './App.css';

const NavBarRouter = withRouter(props => <NavBar {...props} />);

function getTheme(type) {
return createMuiTheme({
palette: {
type: type
},
});
}

const themes = {
'light': getTheme('light'),
'dark': getTheme('dark')
}

class App extends React.Component {
constructor(props) {
super(props)
this.foundRootDir = checkRootDir();
registerApp(this);
this.state = {
alertOpen: false,
alertMsg: "",
alertSeverity: "info",
themeId: "light"
};
}

setTheme(id) {
this.setState({ themeId: id });
}

openAlert(msg, severity) {
this.setState({ alertOpen: true, alertMsg: msg, alertSeverity: severity });
}

closeAlert(event, reason) {
if (reason === 'clickaway') {
return;
}

this.setState({ alertOpen: false, alertMsg: "" });
}

render() {
if (!this.foundRootDir) {
return <h1>Required directories not found. Please specify the correct path to the repository as a command line argument.</h1>;
}
return (
<ThemeProvider key={this.state.themeId} theme={themes[this.state.themeId]}>
<CssBaseline />
<Container disableGutters maxWidth={false}>
<Router forceRefresh={false}>
<NavBarRouter />
<NavBarRouter setTheme={this.setTheme.bind(this)} theme={this.state.themeId} />
<Container>
<Switch>
<Route path='/:type/:schemeName/:name/:subType/:subName' component={SubtypeOverview} />
<Route path='/:type/:schemeName/:name/' component={FlavorOverview} />
<Route path='/:type/:name/' component={SchemeOverview} />
<Route exact path='/' component={SelectScheme} />
</Switch>
<Snackbar key={this.state.alertMsg} autoHideDuration={6000}
open={this.state.alertOpen} onClose={this.closeAlert.bind(this)}>
<Alert onClose={this.closeAlert.bind(this)} severity={this.state.alertSeverity}>
{this.state.alertMsg}
</Alert>
</Snackbar>
</Container>
</Router>
</Container>
</ThemeProvider>
)
}
}
Expand Down
60 changes: 52 additions & 8 deletions tools/enter-data-helper/src/components/BaseComponents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import React from 'react';
import { JsonForms, JsonFormsDispatch } from '@jsonforms/react';
import { Generate, schemaMatches, rankWith, resolveSchema } from '@jsonforms/core';
import { materialRenderers, materialCells } from '@jsonforms/material-renderers';
import { Tooltip, Grid, Button, AppBar, Toolbar, IconButton, Link, Breadcrumbs, Typography } from '@material-ui/core';
import { Info as InfoIcon, ArrowBack as BackIcon, Home as HomeIcon } from '@material-ui/icons';
import {
Tooltip, Grid, Button, AppBar, Toolbar, IconButton, Link, Breadcrumbs, Typography, ButtonGroup, Box,
List, ListItem, ListItemIcon, ListItemText, Paper
} from '@material-ui/core';
import {
Info as InfoIcon, ArrowBack as BackIcon, Home as HomeIcon, ArrowForward as ForwardIcon
} from '@material-ui/icons';
import { Link as RouterLink } from 'react-router-dom';

class TooltipWrapper extends React.Component {
Expand Down Expand Up @@ -39,7 +44,11 @@ class NavBar extends React.Component {
constructor(props) {
super(props);
this.history = props.history;
this.location = '/';
this.location = this.history.location.pathname;
}

selectTheme(theme) {
this.props.setTheme(theme);
}

componentDidMount() {
Expand Down Expand Up @@ -91,6 +100,15 @@ class NavBar extends React.Component {
{pathLinks.slice(0, last).map(x => <LinkRouter key={x[0]} to={x[0]}>{x[1]}</LinkRouter>)}
{<Typography color="textPrimary">{pathLinks[last][1]}</Typography>}
</Breadcrumbs>
<Box flex={1} />
<ButtonGroup>
<Button
variant={this.props.theme === 'light' ? "contained" : "outlined"}
onClick={() => this.selectTheme('light')}>Light</Button>
<Button
variant={this.props.theme === 'dark' ? "contained" : "outlined"}
onClick={() => this.selectTheme('dark')}>Dark</Button>
</ButtonGroup>
</Toolbar>
</AppBar>
);
Expand Down Expand Up @@ -128,6 +146,32 @@ class JsonFormsContainer extends React.Component {
}
}

class SelectList extends React.Component {
constructor(props) {
super(props);
this.entries = props.entries;
}

render() {
return (
<Paper>
<List component="ul">
{
this.entries.map(entry => (
<ListItem button key={"item-" + entry} onClick={() => this.props.action(entry)}>
<ListItemText primary={entry} />
<ListItemIcon>
<ForwardIcon />
</ListItemIcon>
</ListItem>
))
}
</List>
</Paper>
);
}
}

class SelectOrCreate extends JsonFormsContainer {
constructor(props) {
super(props);
Expand Down Expand Up @@ -155,16 +199,16 @@ class SelectOrCreate extends JsonFormsContainer {
generateSchema() {
var schema = {
type: "object", properties: {
name: {
identifier: {
type: "string",
pattern: this.regex
}
}, required: ["name"]
}, required: ["identifier"]
};
if (this.addNew && this.props.schemes.length > 0)
schema.properties.name.not = { type: "string", enum: this.props.schemes };
schema.properties.identifier.not = { type: "string", enum: this.props.schemes };
else if (!this.addNew)
schema.properties.name.enum = this.props.schemes;
schema.properties.identifier.enum = this.props.schemes;

return schema;
}
Expand All @@ -190,4 +234,4 @@ class SelectOrCreate extends JsonFormsContainer {
}

export default JsonFormsContainer;
export { JsonFormsContainer, SelectOrCreate, NavBar };
export { JsonFormsContainer, SelectOrCreate, SelectList, NavBar };
39 changes: 20 additions & 19 deletions tools/enter-data-helper/src/components/FlavorOverview.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { Generate } from '@jsonforms/core';
import { JsonFormsContainer, SelectOrCreate } from './BaseComponents';
import { JsonFormsContainer, SelectOrCreate, SelectList } from './BaseComponents';
import { Grid, Button, Paper, Box } from '@material-ui/core';
import { listFiles, ROOT_DIR, disableUIElements } from './Tools';
import { listFiles, ROOT_DIR, disableUIElements, showAlert } from './Tools';
const fs = window.require('fs');
const path = require('path');
const yaml = require('js-yaml')
Expand Down Expand Up @@ -41,9 +41,9 @@ class EditFlavor extends JsonFormsContainer {
try {
var data = yaml.dump(this.state.data);
fs.writeFileSync(this.flavorFile, data);
alert("Saved to " + this.flavorFile);
showAlert("Saved to " + this.flavorFile, "success");
} catch {
alert("Error while saving file.");
showAlert("Error while saving file.", "error");
}
}
}
Expand Down Expand Up @@ -78,9 +78,9 @@ class SubtypeOverview extends JsonFormsContainer {
}
try {
fs.writeFileSync(this.targetFile, yaml.dump(data));
alert("Saved to " + this.targetFile);
showAlert("Saved to " + this.targetFile, "success");
} catch {
alert("Error while saving file.");
showAlert("Error while saving file.", "error");
}
}

Expand Down Expand Up @@ -126,7 +126,7 @@ class FlavorOverview extends React.Component {

submitForm(name, type, create) {
if (create === fs.existsSync(path.join(this.baseDir, type, name + ".yaml"))) {
alert("Error. Unexpected existance or non-existance of flavor file.");
showAlert('Error. Unexpected existance or non-existance of flavor file.', 'error');
return;
}

Expand All @@ -136,9 +136,9 @@ class FlavorOverview extends React.Component {
if (!fs.existsSync(dir)) fs.mkdirSync(dir);
var data = (type === 'bench') ? { platform: name.split('_')[2] } : { name: name };
fs.writeFileSync(path.join(dir, name + ".yaml"), yaml.dump(data));
alert('"' + name + '" was successfully created.');
showAlert('"' + name + '" was successfully created.', 'success');
} catch {
alert('"' + name + '" could not be created.');
showAlert('"' + name + '" could not be created.', 'error');
}

var newState = {};
Expand Down Expand Up @@ -167,36 +167,37 @@ class FlavorOverview extends React.Component {
<Paper>
<Box px={2} pt={1} pb={2}>
<h2>Parameter Sets</h2>
<SelectOrCreate schemes={this.state.param} addNew={false}
action={(data) => this.submitForm(data.name, "param", false)} />
<SelectList entries={this.state.param}
action={(identifier) => this.submitForm(identifier, "param", false)} />
<SelectOrCreate schemes={this.state.param} addNew={true}
action={(data) => this.submitForm(data.name, "param", true)} />
action={(data) => this.submitForm(data.identifier, "param", true)} />
</Box>
</Paper>
</Grid>
<Grid item>
<Paper>
<Box px={2} pt={1} pb={2}>
<h2>Implementations</h2>
<SelectOrCreate schemes={this.state.impl} addNew={false}
action={(data) => this.submitForm(data.name, "impl", false)} />
<SelectList entries={this.state.impl}
action={(identifier) => this.submitForm(identifier, "impl", false)} />
<SelectOrCreate schemes={this.state.impl} addNew={true}
action={(data) => this.submitForm(data.name, "impl", true)} />
action={(data) => this.submitForm(data.identifier, "impl", true)} />
</Box>
</Paper>
</Grid>
<Grid item>
<Paper>
<Box px={2} pt={1} pb={2}>
<h2>Benchmarks</h2>
<SelectOrCreate schemes={this.state.bench} regex={this.benchRegex} addNew={false}
action={(data) => this.submitForm(data.name, "bench", false)} />
<SelectList entries={this.state.bench}
action={(identifier) => this.submitForm(identifier, "bench", false)} />
<SelectOrCreate schemes={this.state.bench} regex={this.benchRegex} addNew={true}
action={(data) => this.submitForm(data.name, "bench", true)} />
action={(data) => this.submitForm(data.identifier, "bench", true)} />
</Box>
</Paper>
</Grid>
</Grid>

</Grid >
);
}
}
Expand Down
20 changes: 11 additions & 9 deletions tools/enter-data-helper/src/components/SchemeOverview.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from 'react';
import { Generate } from '@jsonforms/core';
import { JsonFormsContainer, SelectOrCreate } from './BaseComponents';
import { JsonFormsContainer, SelectOrCreate, SelectList } from './BaseComponents';
import { Grid, Button, Paper, Box } from '@material-ui/core';
import { listDirs, ROOT_DIR, disableUIElements } from './Tools';
import { listDirs, ROOT_DIR, disableUIElements, showAlert } from './Tools';
const fs = window.require('fs');
const path = require('path');
const yaml = require('js-yaml')
Expand Down Expand Up @@ -46,9 +46,9 @@ class EditScheme extends JsonFormsContainer {
delete data.type;
data = yaml.dump(data);
fs.writeFileSync(this.schemeFile, data);
alert("Saved to " + this.schemeFile);
showAlert("Saved to " + this.schemeFile, "success");
} catch {
alert("Error while saving file.");
showAlert("Error while saving file.", "error");
}
}
}
Expand All @@ -65,7 +65,7 @@ class SchemeOverview extends React.Component {

submitForm(name, create) {
if (create === fs.existsSync(path.join(this.baseDir, name, name + ".yaml"))) {
alert("Error. Unexpected existance or non-existance of flavor file.");
showAlert("Error. Unexpected existance or non-existance of flavor file.", "error");
return;
}

Expand All @@ -77,9 +77,9 @@ class SchemeOverview extends React.Component {
.filter(x => !fs.existsSync(x)).forEach(x => fs.mkdirSync(x));
var data = { name: name };
fs.writeFileSync(path.join(dir, name + ".yaml"), yaml.dump(data));
alert('Flavor "' + name + '" was successfully created.');
showAlert('Flavor "' + name + '" was successfully created.', 'success');
} catch {
alert('Flavor "' + name + '" could not be created.');
showAlert('Flavor "' + name + '" could not be created.', 'error');
}

this.setState({ flavors: listDirs(this.baseDir) });
Expand All @@ -105,8 +105,10 @@ class SchemeOverview extends React.Component {
<Paper>
<Box px={2} pt={1} pb={2}>
<h2>Flavours</h2>
<SelectOrCreate schemes={this.state.flavors} addNew={false} action={(data) => this.submitForm(data.name, false)} />
<SelectOrCreate schemes={this.state.flavors} addNew={true} action={(data) => this.submitForm(data.name, true)} />
<SelectList entries={this.state.flavors}
action={(identifier) => this.submitForm(identifier, false)} />
<SelectOrCreate schemes={this.state.flavors} addNew={true}
action={(data) => this.submitForm(data.identifier, true)} />
</Box>
</Paper>
</Grid>
Expand Down
Loading

0 comments on commit 8d65706

Please sign in to comment.