Skip to content

Commit

Permalink
Merge pull request #11 from IgniteUI/refleciton-shell-strings
Browse files Browse the repository at this point in the history
Refleciton shell strings
  • Loading branch information
zdrawku authored Oct 9, 2018
2 parents e4e7071 + ef35202 commit cedbdda
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 53 deletions.
7 changes: 4 additions & 3 deletions components/convert-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ConvertComponent extends ConverterComponent {
[Converter.EVENT_RESOLVE]: this.resolve,
[Converter.EVENT_RESOLVE_BEGIN]: this.onResolveBegin,
[Converter.EVENT_RESOLVE_END]: this.onResolveEnd,
[Converter.EVENT_END]: this.onEnd,
[Converter.EVENT_BEGIN]: this.onBegin
});

Expand All @@ -43,9 +44,9 @@ export class ConvertComponent extends ConverterComponent {
}
}

// private onEnd(...rest) {
// // process.exit(0);
// }
private onEnd() {
process.exit(0);
}

private onResolveBegin(context) {
const files = context.project.files;
Expand Down
45 changes: 25 additions & 20 deletions components/render-component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from 'path';
import { name as projName} from '../package.json';

import { Component, RendererComponent } from 'typedoc/dist/lib/output/components';
import { ReflectionKind } from 'typedoc/dist/lib/models';
Expand All @@ -8,6 +7,8 @@ import { AttributeType } from '../utils/enums/json-keys';
import { Constants } from '../utils/constants';
import { RendererEvent } from 'typedoc/dist/lib/output/events';
import { Parser } from '../utils/parser';
import { GlobalFuncs } from '../utils/global-funcs';
import { HardcodedStrings } from '../utils/template-strings';

@Component({ name: 'render-component'})
export class RenderComponenet extends RendererComponent {
Expand All @@ -27,8 +28,6 @@ export class RenderComponenet extends RendererComponent {
}

private onRenderBegin(event) {
this.registerHelpers();

const reflections = event.project.reflections;
const options = this.application.options.getRawValues();
const localizeOpt = options[Constants.RENDER_OPTION];
Expand Down Expand Up @@ -62,6 +61,12 @@ export class RenderComponenet extends RendererComponent {
if (this.data) {
this.updateComment(reflection, this.data[reflection.name]);
}

if (reflection.groups) {
this.replaceGroupsTitle(reflection.groups);
}

this.updateReflectionAbbreviation(reflection);
break;
case ReflectionKind.Property:
case ReflectionKind.CallSignature:
Expand Down Expand Up @@ -101,6 +106,23 @@ export class RenderComponenet extends RendererComponent {
}
}

private updateReflectionAbbreviation(reflection) {
reflection.kindString = this.getLocaleValue(reflection.kindString);
}

private replaceGroupsTitle(groups) {
groups.forEach(element => {
element.title = this.getLocaleValue(element.title);
});
}

private getLocaleValue(value) {
return GlobalFuncs.getKeyValuePairRes(
HardcodedStrings.getTemplateStrings(),
HardcodedStrings.getLocal(),
value);
}

private getAttribute(parentName, attribute) {
if (this.data && this.data[parentName]) {
return this.data[parentName][attribute];
Expand Down Expand Up @@ -147,21 +169,4 @@ export class RenderComponenet extends RendererComponent {

return reflection.parent;
}

private registerHelpers() {
let module;
try {
module = require.resolve(projName);
} catch(e) {
this.application.logger.error(e.message);
return;
}

const pluginDist = path.dirname(require.resolve(module));
if (pluginDist) {
this.owner.theme.resources.deactivate();
this.owner.theme.resources.helpers.addOrigin('custom-helpers', `${pluginDist}\\utils\\helpers`);
this.owner.theme.resources.activate();
}
}
}
36 changes: 36 additions & 0 deletions components/theme-component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { name as projName} from '../package.json';
import * as path from 'path';

import { RendererEvent } from "typedoc/dist/lib/output/events";
import { Component } from 'typedoc/dist/lib/utils';
import { RendererComponent } from 'typedoc/dist/lib/output/components';

@Component({ name: 'theme-component' })
export class ThemeComponent extends RendererComponent {
initialize() {
this.listenTo(this.owner, {
[RendererEvent.BEGIN]: this.onRenderBegin
});
}

private onRenderBegin() {
this.registerHelpers();
}

private registerHelpers() {
let module;
try {
module = require.resolve(projName);
} catch(e) {
this.application.logger.error(e.message);
return;
}

const pluginDist = path.dirname(require.resolve(module));
if (pluginDist) {
this.owner.theme.resources.deactivate();
this.owner.theme.resources.helpers.addOrigin('custom-helpers', `${pluginDist}\\utils\\helpers`);
this.owner.theme.resources.activate();
}
}
}
22 changes: 21 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as process from 'process';
import * as fs from 'fs-extra';

import { Application } from 'typedoc/dist/lib/application'
import { ConvertComponent } from './components/convert-component';
import { RenderComponenet } from './components/render-component';
import { OptComponent } from './components/options-component';
import { Constants } from './utils/constants';
import { GlobalFuncs } from './utils/global-funcs';
import { HardcodedStrings } from './utils/template-strings';
import { ThemeComponent } from './components/theme-component';

module.exports = (PluginHost: Application) => {
const app = PluginHost.owner;
Expand Down Expand Up @@ -36,8 +40,24 @@ module.exports = (PluginHost: Application) => {
app.converter.addComponent('convert-component', ConvertComponent);
}

if (startRenderer) {
if (startRenderer) {
app.renderer.addComponent('render-component', RenderComponenet);
}

app.renderer.addComponent('theme-component', ThemeComponent);
registerHardcodedTemplateStrings(processArgs);
}

function registerHardcodedTemplateStrings(options) {
const shellStringsFilePath = GlobalFuncs.getOptionValue(options, Constants.TEMPLATE_STRINGS_OPTION);
const local = GlobalFuncs.getOptionValue(options, Constants.LOCALIZE_OPTION);

if (!shellStringsFilePath || !local) {
return;
}

const templateStrings = fs.readJsonSync(shellStringsFilePath);

HardcodedStrings.setLocal(local);
HardcodedStrings.setTemplateStrings(templateStrings);
}
23 changes: 23 additions & 0 deletions utils/global-funcs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export class GlobalFuncs {
public static getOptionValue(options, key) {
const indx = options.findIndex((e) => e === `--${key}`);
if (indx >= options.length - 1 || indx < 0) {
return;
}

return options[indx + 1];
}

public static getKeyValuePairRes(data, key, value) {
if (!data) {
return value;
}

if (!data[key]) {
return value;
}

const res = data[key][value];
return res ? res : value;
}
}
38 changes: 9 additions & 29 deletions utils/helpers/localize.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import * as process from 'process';
import * as fs from 'fs-extra';
import { Constants } from '../constants';

export function localize(str) {
const value = getLocalizedValue(str);
if (value) {
return value;
}

return str;
}

function getLocalizedValue(str) {
const local = getOptionValue(Constants.LOCALIZE_OPTION);
const jsonFilePath = getOptionValue(Constants.TEMPLATE_STRINGS_OPTION);
const fileContent = fs.readJsonSync(jsonFilePath);

if (fileContent && fileContent[local]) {
return fileContent[local][str];
}

return str;
}

function getOptionValue(key) {
const indx = process.argv.findIndex((e) => e === `--${key}`);
if (indx >= process.argv.length - 1) {
return;
}

return process.argv[indx + 1];
import { GlobalFuncs } from '../global-funcs';
import { HardcodedStrings } from '../template-strings';

export function localize(options: any) {
const value = options.fn(this).trim();
return GlobalFuncs.getKeyValuePairRes(
HardcodedStrings.getTemplateStrings(),
HardcodedStrings.getLocal(),
value);
}
24 changes: 24 additions & 0 deletions utils/template-strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export class HardcodedStrings {
private static local: string;
private static templateStrings: string;

public static setLocal(local: string) {
if (this.local === undefined) {
this.local = local;
}
}

public static getLocal(): string {
return this.local;
}

public static setTemplateStrings(templateStrings: string) {
if (this.templateStrings === undefined) {
this.templateStrings = templateStrings;
}
}

public static getTemplateStrings(): string {
return this.templateStrings;
}
}

0 comments on commit cedbdda

Please sign in to comment.