Skip to content

Commit

Permalink
Refactoring: Split idls.ts into smaller files.
Browse files Browse the repository at this point in the history
Along with increasing external contribution, the complexity of the file
has increased as well. On the other hand, readability has decreased.
So, we should split the file into smaller files and clear their purpose.

The main goal of this change is spliting the idls.ts file into serveral
files and removing it.

ISSUE=#101
  • Loading branch information
romandev committed Oct 5, 2017
1 parent 75ad619 commit fecf2da
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 202 deletions.
6 changes: 5 additions & 1 deletion generator/generator.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
'variables': {
'generator_files': [
'base/file.ts',
'parser/idl_definition.ts',
'parser/idl_definition_factory.ts',
'parser/idl_identifier.ts',
'parser/idl_interface.ts',
'parser/parser.ts',
'reader/simple_reader.ts',
'idl_parser/idls.ts',
'main.ts',
],
},
Expand Down
163 changes: 0 additions & 163 deletions generator/idl_parser/idls.ts

This file was deleted.

62 changes: 27 additions & 35 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,58 @@
*/

import * as file from './base/file';
import * as idls from './idl_parser/idls';
import * as mkdirp from 'mkdirp';
import * as nunjucks from 'nunjucks';
import * as path from 'path';
import * as reader from './reader/simple_reader';
import * as webidl from 'webidl2';

import snakeCase = require('snake-case');

import IDLDefinition from './parser/idl_definition';
import Parser from './parser/parser';

const TEMPLATE_DIR = path.resolve(__dirname, '../../../template');

async function generateBacardi(
env: nunjucks.Environment, output_path: string,
idl_interface_names: idls.InterfaceNames) {
definitions: IDLDefinition[]) {
const [cpp_tmpl] = await Promise.all(
[file.read(path.resolve(TEMPLATE_DIR, 'bacardi_cpp.njk'))]);
const cpp_file_path = path.resolve(output_path, 'bacardi.cc');

return Promise.all([file.write(
cpp_file_path, env.renderString(cpp_tmpl, idl_interface_names))]);
let idl_interface_names: string[] = [];
definitions.forEach(async (definition) => {
if (definition.isIDLInterface()) {
idl_interface_names.push(definition.name);
}
});

return file.write(
cpp_file_path, env.renderString(cpp_tmpl, {names: idl_interface_names}));
}

async function generateInterface(
env: nunjucks.Environment, output_path: string,
idl_fragments: idls.Fragments) {
definitions: IDLDefinition[]) {
const [header_tmpl, cpp_tmpl] = await Promise.all([
file.read(path.resolve(TEMPLATE_DIR, 'interface_header.njk')),
file.read(path.resolve(TEMPLATE_DIR, 'interface_cpp.njk'))
]);

for (const definition of idl_fragments.definitions) {
if (definition instanceof idls.InterfaceImpl) {
const interfaceImpl: idls.InterfaceImpl =
definition as idls.InterfaceImpl;

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(interfaceImpl.name) + '_bridge.h');
output_path, 'examples/' + snakeCase(definition.name) + '_bridge.h');
const cpp_file_path = path.resolve(
output_path,
'examples/' + snakeCase(interfaceImpl.name) + '_bridge.cc');
output_path, 'examples/' + snakeCase(definition.name) + '_bridge.cc');

await file.write(
header_file_path, env.renderString(header_tmpl, interfaceImpl));
await file.write(
cpp_file_path, env.renderString(cpp_tmpl, interfaceImpl));
header_file_path, env.renderString(header_tmpl, definition));
await file.write(cpp_file_path, env.renderString(cpp_tmpl, definition));
}
};

return;
});
}

async function main([out_dir, ...idl_files]) {
Expand All @@ -79,20 +80,11 @@ async function main([out_dir, ...idl_files]) {
return snakeCase(str);
});

let interface_names = new Array<String>();
const parsedData = webidl.parse(await reader.readAll(idl_files));
const idl_fragments: idls.Fragments = new idls.Fragments(parsedData);
await generateInterface(env, out_dir, idl_fragments);
for (const definition of idl_fragments.definitions) {
// FIXME: definition type is only for Interface, so need to modify below
// condition after definition type changed.
if (definition instanceof idls.InterfaceImpl) {
interface_names.push(new String(definition.name));
}
}
const interfaceNames: idls.InterfaceNames =
new idls.InterfaceNames(interface_names);
await generateBacardi(env, out_dir, interfaceNames);
let definitions: IDLDefinition[] =
await Parser.parse(await reader.readAll(idl_files));
await generateInterface(env, out_dir, definitions);
await generateBacardi(env, out_dir, definitions);

return 0;
}

Expand Down
31 changes: 31 additions & 0 deletions generator/parser/idl_definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2017 The Bacardi Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default abstract class IDLDefinition {
public readonly name: string;
protected readonly raw_idl_definition_info: {};

protected constructor(name: string, raw_idl_definition_info: {}) {
this.name = name;
this.raw_idl_definition_info = raw_idl_definition_info;
}

public abstract render(): void;

public isIDLInterface(): boolean {
return this.raw_idl_definition_info['type'] == 'interface';
}
}
32 changes: 32 additions & 0 deletions generator/parser/idl_definition_factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2017 The Bacardi Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import IDLDefinition from './idl_definition';
import IDLInterface from './idl_interface';

export default class IDLDefinitionFactory {
static create(raw_idl_definition_info: {}): IDLDefinition {
if (this.isIDLInterface(raw_idl_definition_info)) {
return new IDLInterface(raw_idl_definition_info);
}

return null;
}

private static isIDLInterface(raw_idl_definition_info: {}): boolean {
return raw_idl_definition_info['type'] == 'interface';
}
}
20 changes: 20 additions & 0 deletions generator/parser/idl_identifier.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2017 The Bacardi Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default interface IDLIdentifier {
readonly name: string;
readonly type: string;
}
Loading

0 comments on commit fecf2da

Please sign in to comment.