Skip to content

Commit

Permalink
clean AssetMgr
Browse files Browse the repository at this point in the history
  • Loading branch information
boydc2014 committed Apr 29, 2020
1 parent bc85531 commit 15c747f
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ const locationRef = {
};
describe('assetManager', () => {
it('getProjectTemplate', async () => {
const assetManager = new AssetManager(mockAssetLibraryPath, mockRuntimeLibraryPath);
const assetManager = new AssetManager();
const result = await assetManager.getProjectTemplates();
expect(result.length).toBeGreaterThan(0);
expect(result[0].name).toBe('Sample Bot');
expect(result[0].id).toBe('SampleBot');
});

it('copyProjectTemplateTo', async () => {
const assetManager = new AssetManager(mockAssetLibraryPath, mockRuntimeLibraryPath);
const assetManager = new AssetManager();
await assetManager.getProjectTemplates();
await assetManager.getProjectRuntime();

await expect(assetManager.copyProjectTemplateTo('SampleBot', locationRef)).resolves.toBe(locationRef);
// remove the saveas files
Expand Down
59 changes: 1 addition & 58 deletions Composer/packages/server/src/models/asset/assetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT License.

import find from 'lodash/find';
import { ProjectTemplate } from '@bfc/shared';
import { UserIdentity, pluginLoader } from '@bfc/plugin-loader';

import log from '../../logger';
Expand All @@ -13,63 +12,17 @@ import { copyDir } from '../../utility/storage';
import StorageService from '../../services/storage';
import { IFileStorage } from '../storage/interface';

interface TemplateData {
[key: string]: {
name: string;
description: string;
order?: number;
icon?: string;
};
}

const runtimes: TemplateData = {
CSharp: {
name: 'CSharp Runtime',
description: 'A Bot Framework runtime using the CSharp/dotnet version of the SDK',
},
};

// set a default runtime template.
// when we have multiple runtimes this will be a parameter.
const DEFAULT_RUNTIME = 'CSharp';

export class AssetManager {
public templateStorage: LocalDiskStorage;
private runtimesPath: string;
private runtimeTemplates: ProjectTemplate[] = [];

constructor(_: string, runtimesPath: string) {
this.runtimesPath = runtimesPath;
constructor() {
this.templateStorage = new LocalDiskStorage();

// initialize the list of runtimes.
this.getProjectRuntime();
}

public async getProjectTemplates() {
return pluginLoader.extensions.botTemplates;
}

public async getProjectRuntime() {
const path = this.runtimesPath;
const output: ProjectTemplate[] = [];

if (await this.templateStorage.exists(path)) {
const folders = await this.templateStorage.readDir(path);
this.runtimeTemplates = [];
for (const name of folders) {
const absPath = Path.join(path, name);
if ((await this.templateStorage.stat(absPath)).isDir) {
const base = { id: name, name: runtimes[name].name, description: runtimes[name].description };
this.runtimeTemplates.push({ ...base, path: absPath });
output.push(base);
}
}
}

return output;
}

public async copyDataFilesTo(templateId: string, dstDir: string, dstStorage: IFileStorage) {
const template = find(pluginLoader.extensions.botTemplates, { id: templateId });
if (template === undefined || template.path === undefined) {
Expand All @@ -79,15 +32,6 @@ export class AssetManager {
await copyDir(template.path, this.templateStorage, dstDir, dstStorage);
}

public async copyRuntimeTo(dstDir: string, dstStorage: IFileStorage) {
const runtime = find(this.runtimeTemplates, { id: DEFAULT_RUNTIME });
if (runtime === undefined || runtime.path === undefined) {
throw new Error(`no such runtime with id ${DEFAULT_RUNTIME}`);
}
// copy runtime code files
await copyDir(runtime.path, this.templateStorage, dstDir, dstStorage);
}

public async copyProjectTemplateTo(templateId: string, ref: LocationRef, user?: UserIdentity): Promise<LocationRef> {
// user storage maybe diff from template storage
const dstStorage = StorageService.getStorageClient(ref.storageId, user);
Expand All @@ -97,7 +41,6 @@ export class AssetManager {
throw new Error('already have this folder, please give another name');
}
await this.copyDataFilesTo(templateId, dstDir, dstStorage);
// await this.copyRuntimeTo(dstDir, dstStorage);
return ref;
}
}
3 changes: 1 addition & 2 deletions Composer/packages/server/src/services/asset.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import settings from '../settings';
import { AssetManager } from '../models/asset/assetManager';

class AssetService {
public manager: AssetManager;

constructor() {
this.manager = new AssetManager(settings.assetsLibray, settings.runtimeFolder);
this.manager = new AssetManager();
}
}

Expand Down

0 comments on commit 15c747f

Please sign in to comment.