Skip to content

Commit

Permalink
Add original IDL information (filename and location) to IDL definition
Browse files Browse the repository at this point in the history
This is removing hard-coded value(examples/) in generateInterface() in
generator/main.ts. This is also resolving a problem that the CWD path is
differnt in Windows during generating.

ISSUE=#101
  • Loading branch information
romandev committed Oct 7, 2017
1 parent fba16d2 commit 6677111
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 18 deletions.
2 changes: 2 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
'action': [
'<@(PRODUCT_DIR)/../../bootstrap/command/node',
'<@(PRODUCT_DIR)/generator/main.js',
'<(module_root_dir)',
'<@(SHARED_INTERMEDIATE_DIR)',
'<@(_inputs)',
],
Expand All @@ -123,6 +124,7 @@
'action': [
'<@(PRODUCT_DIR)/../../third_party/node/node.exe',
'<@(PRODUCT_DIR)/generator/main.js',
'<(module_root_dir)',
'<@(SHARED_INTERMEDIATE_DIR)',
'<@(_inputs)',
],
Expand Down
2 changes: 1 addition & 1 deletion examples/examples.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],

'examples_idl_files': [
'calculator.idl',
'<(module_root_dir)/examples/calculator.idl',
],

'examples_idl_output_files': [
Expand Down
23 changes: 17 additions & 6 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ async function generateInterface(

definitions.forEach(async (definition) => {
if (definition.isIDLInterface()) {
// FIXME(zino): The examples/ directory should be changed to better fixed
// path.
const header_file_path = path.resolve(
output_path, 'examples/' + snakeCase(definition.name) + '_bridge.h');
output_path,
definition.idlDirName() + '/' + snakeCase(definition.name) +
'_bridge.h');
const cpp_file_path = path.resolve(
output_path, 'examples/' + snakeCase(definition.name) + '_bridge.cc');
output_path,
definition.idlDirName() + '/' + snakeCase(definition.name) +
'_bridge.cc');

await file.write(
header_file_path, env.renderString(header_tmpl, definition));
Expand All @@ -70,7 +72,16 @@ async function generateInterface(
});
}

async function main([out_dir, ...idl_files]) {
async function main([root_dir, out_dir, ...idl_files]) {
// We expect that current working directory will be $BACARDI_PATH. But it
// might not be in Windows platform. So, we should resolve the path here.
process.chdir(root_dir);

let relative_idl_files: string[] = [];
idl_files.forEach((idl_file) => {
relative_idl_files.push(path.relative(root_dir, idl_file));
});

var env = new nunjucks.Environment();
env.addFilter('camelcase', function(str, count) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
Expand All @@ -82,7 +93,7 @@ async function main([out_dir, ...idl_files]) {
});

let definitions: IDLDefinition[] =
await Parser.parse(await reader.readAll(idl_files));
await Parser.parse(await reader.readAll(relative_idl_files));
await generateInterface(env, out_dir, definitions);
await generateBacardi(env, out_dir, definitions);

Expand Down
8 changes: 8 additions & 0 deletions generator/parser/idl_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@ export default abstract class IDLDefinition {
public isIDLInterface(): boolean {
return this.raw_idl_definition_info['type'] == 'interface';
}

public idlBaseName(): string {
return this.raw_idl_definition_info['idlBaseName'];
}

public idlDirName(): string {
return this.raw_idl_definition_info['idlDirName'];
}
}
29 changes: 24 additions & 5 deletions generator/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,39 @@
* limitations under the License.
*/

import * as path from 'path';
import * as webidl from 'webidl2';

import IDLDefinition from './idl_definition';
import IDLDefinitionFactory from './idl_definition_factory';

export default class Parser {
static async parse(idl_fragment: string): Promise<IDLDefinition[]> {
const parsed_data: {}[] = webidl.parse(idl_fragment);

static async parse(idl_fragments: [string, string][]):
Promise<IDLDefinition[]> {
let definitions: IDLDefinition[] = [];
parsed_data.forEach((definition_info) => {
definitions.push(IDLDefinitionFactory.create(definition_info));

idl_fragments.forEach((idl_fragment) => {
const parsed_data: {}[] = this.parseIDLFragment(idl_fragment);

parsed_data.forEach((definition_info) => {
definitions.push(IDLDefinitionFactory.create(definition_info));
});
});

return definitions;
}

private static parseIDLFragment(idl_fragment: [string, string]) {
const idl_fragment_path = idl_fragment[0];
const idl_fragment_contents = idl_fragment[1];

let parsed_data: {}[] = webidl.parse(idl_fragment_contents);

parsed_data.forEach((definition_info) => {
definition_info['idlBaseName'] = path.basename(idl_fragment_path);
definition_info['idlDirName'] = path.dirname(idl_fragment_path);
});

return parsed_data;
}
}
17 changes: 11 additions & 6 deletions generator/reader/simple_reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@

import * as file from '../base/file';

export async function readAll(idl_files: string[]): Promise<string> {
export async function readAll(idl_files: string[]):
Promise<[string, string][]> {
let read_tasks: Promise<string>[] = [];
idl_files.forEach((idl_file: string) => {
read_tasks.push(file.read(idl_file));
});

let result: string = '';
(await Promise.all(read_tasks)).forEach((idl_contents: string) => {
result += idl_contents;
});
return result;
let idl_contents: string[] = await Promise.all(read_tasks);
// assert idl_files.length == idl_contents.length;

let idl_fragments: [string, string][] = [];
for (let i: number = 0; i < idl_files.length; i++) {
idl_fragments.push([idl_files[i], idl_contents[i]]);
}

return idl_fragments;
}

0 comments on commit 6677111

Please sign in to comment.