Skip to content

Commit

Permalink
Added code generation from TypeDefinition
Browse files Browse the repository at this point in the history
  • Loading branch information
fantoine committed Jul 18, 2016
1 parent f7e1e93 commit 189b183
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/hxextern/command/GenerateCommand.hx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package hxextern.command;

import haxe.io.Path;
import haxe.macro.Printer;
import hxextern.service.Console;
import hxextern.service.Haxelib;
import hxextern.step.IStep;
import hxextern.step.StepContext;
import minject.Injector;
import sys.FileSystem;
import sys.io.File;

using StringTools;

Expand Down Expand Up @@ -48,7 +50,7 @@ class GenerateCommand implements ICommand

// Run steps
this.executeSteps(context, data.steps);
this.generateCode(context);
this.generateCode(context, data.output);
}

private function executeSteps(context : StepContext, steps : Array<HaxelibHxExternStep>) : Void
Expand Down Expand Up @@ -76,8 +78,36 @@ class GenerateCommand implements ICommand
}
}

private function generateCode(context : StepContext) : Void
private function generateCode(context : StepContext, output : String) : Void
{
this.console.info('Generating code');
var basePath = Path.addTrailingSlash(FileSystem.absolutePath(output));
var printer = new Printer();

// Generate all files
var done = false;
for (definition in context.definitions) {
done = true;

// Ensure directory exists
var directory = basePath + definition.pack.join('/');
FileSystem.createDirectory(directory);

var filename = '${directory}/${definition.name}.hx';
var type = (definition.pack.length > 0 ? definition.pack.join('.') + '.' : '') + definition.name;

// Preparing content
this.console.debug('Preparing code for type "${type}"');
var content = printer.printTypeDefinition(definition, true);

// Writing file content
this.console.debug('Writing content to "${filename}"');
File.saveContent(filename, '${content}\n');

this.console.success('Type "${type}" generated');
}
if (!done) {
this.console.success('No types generated');
}
}
}
3 changes: 3 additions & 0 deletions src/hxextern/service/Haxelib.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ typedef HaxelibHxExternStep = {
};

typedef HaxelibHxExtern = {
var output : String;
var steps : Array<HaxelibHxExternStep>;
};

Expand Down Expand Up @@ -59,13 +60,15 @@ class Haxelib
// Get json
var json = Json.parse(content);
var hxextern = this.extractField(json, 'hxextern');
var output = this.extractField(hxextern, 'output');
var steps = this.extractField(hxextern, 'steps');
if (!Std.is(steps, Array)) {
throw 'Field "steps" should be an array';
}

// Return validated object
return {
output: output,
steps: [ for (step in (steps : Array<DynamicAccess<Dynamic>>)) {
var type = this.extractField(step, 'type');
var options = null;
Expand Down

0 comments on commit 189b183

Please sign in to comment.