diff --git a/src/hxextern/command/GenerateCommand.hx b/src/hxextern/command/GenerateCommand.hx index 2db2807..09d834b 100644 --- a/src/hxextern/command/GenerateCommand.hx +++ b/src/hxextern/command/GenerateCommand.hx @@ -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; @@ -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) : Void @@ -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'); + } } } diff --git a/src/hxextern/service/Haxelib.hx b/src/hxextern/service/Haxelib.hx index dc78237..6108cbb 100644 --- a/src/hxextern/service/Haxelib.hx +++ b/src/hxextern/service/Haxelib.hx @@ -12,6 +12,7 @@ typedef HaxelibHxExternStep = { }; typedef HaxelibHxExtern = { + var output : String; var steps : Array; }; @@ -59,6 +60,7 @@ 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'; @@ -66,6 +68,7 @@ class Haxelib // Return validated object return { + output: output, steps: [ for (step in (steps : Array>)) { var type = this.extractField(step, 'type'); var options = null;