Skip to content

Commit

Permalink
feat: Update LG library (#4368)
Browse files Browse the repository at this point in the history
* init

* update LG library

* migrate LgFile to LGResource

* fix issue

* update package entry

* app lint:fix command

* remove untracked files

* update yarn.lock

* fix comments

* revert schema file

Co-authored-by: zhixzhan <[email protected]>
Co-authored-by: Dong Lei <[email protected]>
Co-authored-by: Chris Whitten <[email protected]>
  • Loading branch information
4 people authored Oct 20, 2020
1 parent c98037f commit 7701b01
Show file tree
Hide file tree
Showing 16 changed files with 114 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Composer/packages/adaptive-flow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
"@bfc/ui-shared": "*",
"@emotion/core": "^10.0.27",
"@emotion/styled": "^10.0.27",
"adaptive-expressions": "4.10.0-preview-147186",
"botbuilder-lg": "^4.10.0-preview-150886",
"adaptive-expressions": "^4.11.0-dev.20201013.d5458bf",
"botbuilder-lg": "4.11.0-dev.20201010.6e4a99e",
"create-react-class": "^15.6.3",
"d3": "^5.9.1",
"dagre": "^0.8.4",
Expand Down
4 changes: 2 additions & 2 deletions Composer/packages/client/src/hooks/useResolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { useRef } from 'react';
import { importResolverGenerator } from '@bfc/shared';
import { lgImportResolverGenerator } from '@bfc/shared';
import { useRecoilValue } from 'recoil';

import { dialogsState, luFilesState, lgFilesState, localeState, qnaFilesState } from '../recoilModel';
Expand All @@ -28,7 +28,7 @@ export const useResolvers = (projectId: string) => {
const dialogsRef = useRef(dialogs);
dialogsRef.current = dialogs;

const lgImportresolver = () => importResolverGenerator(lgFilesRef.current, '.lg');
const lgImportresolver = () => lgImportResolverGenerator(lgFilesRef.current, '.lg');

const lgFileResolver = (id: string) => {
const fileId = id.includes('.') ? id : `${id}.${localeRef.current}`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { lgUtil } from '@bfc/indexers';
import { importResolverGenerator, LgFile } from '@bfc/shared';
import { lgImportResolverGenerator, LgFile } from '@bfc/shared';

import {
LgActionType,
Expand Down Expand Up @@ -92,7 +92,7 @@ type LgMessageEvent =
type LgResources = Map<string, LgFile>;

const lgFileResolver = (lgFiles) => {
return importResolverGenerator(lgFiles, '.lg');
return lgImportResolverGenerator(lgFiles, '.lg');
};

export class LgCache {
Expand Down
4 changes: 2 additions & 2 deletions Composer/packages/lib/indexers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
},
"dependencies": {
"@microsoft/bf-lu": "^4.11.0-dev.20201013.7ccb128",
"adaptive-expressions": "4.10.0-preview-147186",
"botbuilder-lg": "^4.10.0-preview-150886",
"adaptive-expressions": "^4.11.0-dev.20201013.d5458bf",
"botbuilder-lg": "4.11.0-dev.20201010.6e4a99e",
"lodash": "^4.17.19"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions Composer/packages/lib/indexers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { DialogSetting, FileInfo, importResolverGenerator } from '@bfc/shared';
import { DialogSetting, FileInfo, lgImportResolverGenerator } from '@bfc/shared';

import { dialogIndexer } from './dialogIndexer';
import { dialogSchemaIndexer } from './dialogSchemaIndexer';
Expand Down Expand Up @@ -46,7 +46,7 @@ class Indexer {
};
});

return importResolverGenerator(lgFiles, '.lg', locale);
return lgImportResolverGenerator(lgFiles, '.lg', locale);
};

public index(files: FileInfo[], botName: string, locale: string, skillContent: any, settings: DialogSetting) {
Expand Down
4 changes: 2 additions & 2 deletions Composer/packages/lib/indexers/src/utils/lgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

import { Templates, Template, Diagnostic as LGDiagnostic, ImportResolverDelegate } from 'botbuilder-lg';
import { LgTemplate, importResolverGenerator, TextFile, Diagnostic, Position, Range, LgFile } from '@bfc/shared';
import { LgTemplate, lgImportResolverGenerator, TextFile, Diagnostic, Position, Range, LgFile } from '@bfc/shared';
import formatMessage from 'format-message';
import isEmpty from 'lodash/isEmpty';
import { SourceRange } from 'botbuilder-lg/lib/sourceRange';
Expand Down Expand Up @@ -260,7 +260,7 @@ export function extractOptionByKey(nameOfKey: string, options: string[]): string
}

export function parse(id: string, content: string, lgFiles: TextFile[]): LgFile {
const lgImportResolver = importResolverGenerator(lgFiles, '.lg');
const lgImportResolver = lgImportResolverGenerator(lgFiles, '.lg');

return lgIndexer.parse(content, id, lgImportResolver);
}
46 changes: 43 additions & 3 deletions Composer/packages/lib/shared/src/resolverFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the MIT License.

import formatMessage from 'format-message';
import { LGResource, ImportResolverDelegate } from 'botbuilder-lg';

export declare type ResolverResource = { content: string; id: string };
export declare type ImportResolverDelegate = (source: string, resourceId: string) => ResolverResource;
export declare type LUImportResolverDelegate = (source: string, resourceId: string) => ResolverResource;

function getFileName(path: string): string {
return path.split('/').pop() || path;
Expand All @@ -16,11 +17,11 @@ function getFileName(path: string): string {
* @param ext resource extension, e.g. .lg, .lu
* @param defaultLocale complete resource id = [id].[locale][ext]
*/
export function importResolverGenerator(
export function luImportResolverGenerator(
resources: ResolverResource[],
ext = '',
defaultLocale = 'en-us'
): ImportResolverDelegate {
): LUImportResolverDelegate {
/**
* @param source current file id
* @param resourceId imported file id
Expand Down Expand Up @@ -51,3 +52,42 @@ export function importResolverGenerator(
};
};
}

/**
*
* @param resources resources feed to resolver
* @param ext resource extension, e.g. .lg, .lu
* @param defaultLocale complete resource id = [id].[locale][ext]
*/
export function lgImportResolverGenerator(
resources: ResolverResource[],
ext = '',
defaultLocale = 'en-us'
): ImportResolverDelegate {
/**
* @param source current file id
* @param resourceId imported file id
* for example:
* in todosample.en-us.lg:
* [import](../common/common.lg)
*
* would resolve to common.en-us.lg || common.lg
*
* lgResource = todosample || todosample.en-us || todosample.en-us.lg || todosample.lg || .
* resourceId = common || common.lg || ../common/common.lg
*
*/
return (lgResource: LGResource, resourceId: string) => {
// eslint-disable-next-line security/detect-non-literal-regexp
const extReg = new RegExp(ext + '$');
const sourceId = getFileName(lgResource.id).replace(extReg, '');
const locale = /\w\.\w/.test(sourceId) ? sourceId.split('.').pop() : defaultLocale;
const targetId = getFileName(resourceId).replace(extReg, '');

const targetFile =
resources.find(({ id }) => id === `${targetId}.${locale}`) || resources.find(({ id }) => id === targetId);

if (!targetFile) throw new Error(formatMessage(`File not found`));
return new LGResource(resourceId, resourceId, targetFile.content);
};
}
4 changes: 2 additions & 2 deletions Composer/packages/server/src/services/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import merge from 'lodash/merge';
import find from 'lodash/find';
import flatten from 'lodash/flatten';
import { importResolverGenerator, ResolverResource } from '@bfc/shared';
import { luImportResolverGenerator, ResolverResource } from '@bfc/shared';
import extractMemoryPaths from '@bfc/indexers/lib/dialogUtils/extractMemoryPaths';
import { UserIdentity } from '@bfc/extension';

Expand Down Expand Up @@ -54,7 +54,7 @@ export class BotProjectService {
const { name, content } = file;
return { id: Path.basename(name, '.lu'), content };
});
const resolver = importResolverGenerator(resource, '.lu');
const resolver = luImportResolverGenerator(resource, '.lu');
return resolver(source, id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
"lint:typecheck": "tsc --noEmit"
},
"dependencies": {
"@bfc/built-in-functions": "*",
"@bfc/indexers": "*",
"botbuilder-lg": "^4.10.0-preview-150886",
"vscode-languageserver": "^5.3.0-next",
"@bfc/built-in-functions": "*"
"botbuilder-lg": "4.11.0-dev.20201010.6e4a99e",
"vscode-languageserver": "^5.3.0-next"
},
"devDependencies": {
"@botframework-composer/test-utils": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { TextDocumentPositionParams, DocumentOnTypeFormattingParams } from 'vscode-languageserver-protocol';
import get from 'lodash/get';
import { filterTemplateDiagnostics, isValid, lgUtil } from '@bfc/indexers';
import { MemoryResolver, ResolverResource, LgFile, importResolverGenerator } from '@bfc/shared';
import { MemoryResolver, ResolverResource, LgFile, lgImportResolverGenerator } from '@bfc/shared';
import { buildInFunctionsMap } from '@bfc/built-in-functions';

import { LgParser } from './lgParser';
Expand Down Expand Up @@ -166,7 +166,7 @@ export class LGServer {
const lgTextFile = lgTextFiles.find((item) => item.id === fileId);
if (lgTextFile) {
const lgFile = lgUtil.parse(lgTextFile.id, lgTextFile.content, lgTextFiles);
const lgResolver = importResolverGenerator(lgTextFiles, '.lg');
const lgResolver = lgImportResolverGenerator(lgTextFiles, '.lg');
return lgUtil.updateTemplate(lgFile, templateId, { body: content }, lgResolver);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { fork, ChildProcess } from 'child_process';
import path from 'path';

import { importResolverGenerator } from '@bfc/shared';
import { lgImportResolverGenerator } from '@bfc/shared';
import { ResolverResource } from '@bfc/shared';
import uniqueId from 'lodash/uniqueId';
import { lgIndexer } from '@bfc/indexers';
Expand All @@ -18,7 +18,7 @@ export interface WorkerMsg {

class LgParserWithoutWorker {
public async parseText(content: string, id: string, resources: ResolverResource[]) {
const lgImportResolver = importResolverGenerator(resources, '.lg');
const lgImportResolver = lgImportResolverGenerator(resources, '.lg');
return lgIndexer.parse(content, id, lgImportResolver);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { importResolverGenerator } from '@bfc/shared';
import { lgImportResolverGenerator } from '@bfc/shared';
import { lgIndexer } from '@bfc/indexers';

import { WorkerMsg } from './lgParser';
Expand All @@ -10,7 +10,7 @@ process.on('message', (message: WorkerMsg) => {
const { content, id, resources } = message.payload;

try {
const lgImportResolver = importResolverGenerator(resources, '.lg');
const lgImportResolver = lgImportResolverGenerator(resources, '.lg');
const { templates, allTemplates, diagnostics } = lgIndexer.parse(content, id, lgImportResolver);
process.send?.({ id: message.id, payload: { id, content, templates, allTemplates, diagnostics } });
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { parser } from '@microsoft/bf-lu/lib/parser';

import { EntityTypesObj, LineState } from './entityEnum';
import * as util from './matchingPattern';
import { ImportResolverDelegate, LUOption, LUDocument, generageDiagnostic, convertDiagnostics } from './utils';
import { LUImportResolverDelegate, LUOption, LUDocument, generageDiagnostic, convertDiagnostics } from './utils';

// define init methods call from client
const LABELEXPERIENCEREQUEST = 'labelingExperienceRequest';
Expand Down Expand Up @@ -162,7 +162,7 @@ export class LUServer {
const { uri } = document;
const { fileId, sectionId, projectId, luFeatures = {} } = luOption || {};
const index = () => {
const importResolver: ImportResolverDelegate = this.getImportResolver(document);
const importResolver: LUImportResolverDelegate = this.getImportResolver(document);
let content: string = document.getText();
// if inline mode, composite local with server resolved file.
if (this.importResolver && fileId && sectionId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface LUDocument {
index: () => any;
}

export declare type ImportResolverDelegate = (
export declare type LUImportResolverDelegate = (
source: string,
resourceId: string
) => {
Expand Down
Loading

0 comments on commit 7701b01

Please sign in to comment.