Skip to content

Instantly share code, notes, and snippets.

@JeroenVinke
Last active July 13, 2017 14:57
Show Gist options
  • Save JeroenVinke/7121ebee181da3ff20c936c0a12a621b to your computer and use it in GitHub Desktop.
Save JeroenVinke/7121ebee181da3ff20c936c0a12a621b to your computer and use it in GitHub Desktop.
workflow
require('aurelia-polyfills');
const gulp = require('gulp');
const Container = require('aurelia-dependency-injection').Container;
const definition = require('../../lib/commands/new/new-application.json');
const WorkflowEngine = require('../../lib/workflow/workflow-engine').WorkflowEngine;
const ProjectCreate = require('../../lib/workflow/activities/project-create');
const UI = require('../../lib/ui').UI;
const ConsoleUI = require('../../lib/ui').ConsoleUI;
const fs = require('fs');
const path = require('path');
const LogManager = require('aurelia-logging');
const Logger = require('../../lib/logger').Logger;
gulp.task('generate-projects', function(done) {
LogManager.addAppender(new Logger(new ConsoleUI()));
// new ConsoleUI()
// .question('Where would you like to create the projects?')
// .then(dir => {
// if (!fs.existsSync(dir)) {
// fs.mkdirSync(dir);
// }
generateInstructions(100, { state: {}, answers: {} });
console.log(`Generated ${allInstructions.length} different flows`);
for (let instruction of allInstructions) {
let s = instruction.state;
instruction.answers['100'] = `${s.bundler.id}_${s.loader.id}_${s.cssProcessor.id}_${s.markupProcessor.id}_${s.unitTestRunner.id}`;
}
let dir = 'C:\\Development\\Aurelia\\Apps\\webpack-tests';
createProjectsInSeries(dir, allInstructions)
.then(() => {
console.log('Created all projects');
done();
})
.catch(e => {
console.log(e);
done();
});
// });
});
function createProjectsInSeries(dir, instructions) {
let i = 0;
function createP(instruction) {
return createProject(path.join(dir, instruction.answers['100']), instruction)
.then(() => {
i++;
return createP(instructions[i]);
});
}
return createP(instructions[i]);
}
function createProject(targetDir, answers) {
let container = new Container();
let engine = new WorkflowEngine(
definition,
container
);
container.registerInstance(UI, new ConsoleUI());
container.unregister('project-install');
container.registerInstance('project-install', new AutoProjectInstall());
container.unregister('project-create');
container.registerSingleton('project-create', AutoProjectCreate);
container.unregister('input-text');
container.registerInstance('input-text', new AutoInputText(answers));
container.unregister('input-select');
container.registerInstance('input-select', new AutoInputText(answers));
return engine.start({
root: targetDir
})
.then(() => console.log(`Finished creating project at ${targetDir}`))
.catch(e => {
console.log('error while creating project');
console.log(e);
throw e;
});
}
let allInstructions = [];
function generateInstructions(activityId, instructions) {
if (activityId === 1100) {
allInstructions.push(instructions);
return;
}
let activity = getActivity(activityId);
if (activity.type === 'input-text') {
instructions.state[activity.stateProperty] = 'some answer';
instructions.answers[activityId] = 'some answer';
} else if (activity.type === 'input-select') {
for (let option of activity.options.filter(x => !x.flag)) {
let clone = JSON.parse(JSON.stringify(instructions));
clone.state[activity.stateProperty] = option.value;
clone.answers[activityId] = option.value;
generateInstructions(activity.nextActivity, clone);
}
return;
} else if (activity.type === 'state-assign') {
Object.assign(instructions.state, activity.state);
} else if (activity.type === 'branch-switch') {
let stateProp = instructions.state[activity.stateProperty];
let branch = activity.branches.find(x => x.case === (stateProp.id ? stateProp.id : stateProp));
if (!branch) {
branch = activity.branches.find(x => x.case === 'default');
if (!branch) {
throw new Error('branch not found');
}
}
return generateInstructions(branch.nextActivity, instructions);
}
return generateInstructions(activity.nextActivity, JSON.parse(JSON.stringify(instructions)));
}
function getActivity(id) {
return definition.activities.find(activity => activity.id === id);
}
class AutoInputSelect {
constructor(answers) {
this._answers = answers;
}
execute(context) {
let answer = this._answers[this.id];
if (!answer) {
throw new Error('AutoInputSelect has no answer for question ' + this.id);
}
context.state[this.stateProperty] = answer;
context.next(this.nextActivity);
}
}
class AutoInputText {
constructor(answers) {
this._answers = answers;
}
execute(context) {
let answer = this._answers[this.id];
if (!answer) {
throw new Error('AutoInputText has no answer for question ' + this.id);
}
context.state[this.stateProperty] = answer;
context.next(this.nextActivity);
}
}
class AutoProjectCreate extends ProjectCreate {
projectConfirmation() {
return Promise.resolve({ value: 'yes' });
}
}
class AutoProjectInstall {
execute(context) {
context.next(this.nextActivity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment