Skip to content

Commit

Permalink
Merge pull request #283 from alibaba/feat/codeSnippetPath
Browse files Browse the repository at this point in the history
feat(pont-engine vscode-pont): 修改codeSnippet位置
  • Loading branch information
z979054461 authored Feb 18, 2022
2 parents 2bc782f + f47af8c commit d63b2d8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 40 deletions.
5 changes: 5 additions & 0 deletions packages/pont-engine/src/generators/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ export class CodeGenerator {
return;
}
}

codeSnippet(inter: Interface): string {
const mod = inter.getContext().mod as Mod;
return `API.${mod.name}.${inter.name}`;
}
}

export class FilesManager {
Expand Down
15 changes: 7 additions & 8 deletions packages/pont-engine/src/manage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Interface, Mod, StandardDataSource } from './standard';
import { StandardDataSource } from './standard';
import { Config, getTemplate, DataSourceConfig, hasChinese, diffDses, getRelatedBos } from './utils';
import * as fs from 'fs-extra';
import * as path from 'path';
Expand All @@ -23,8 +23,6 @@ export class Manager {

fileManager: FilesManager;

codeSnippet: (inter: Interface) => string;

diffs = {
modDiffs: [] as Model[],
boDiffs: [] as Model[]
Expand All @@ -47,8 +45,6 @@ export class Manager {
async selectDataSource(name: string) {
this.currConfig = this.allConfigs.find((conf) => conf.name === name);

this.setCodeSnippet();

await this.readLocalDataSource();
await this.readRemoteDataSource();

Expand Down Expand Up @@ -150,7 +146,6 @@ export class Manager {
constructor(private projectRoot: string, config: Config, configDir = process.cwd()) {
this.allConfigs = config.getDataSourcesConfig(configDir);
this.currConfig = this.allConfigs[0];
this.setCodeSnippet();
}
pollingId = null;

Expand Down Expand Up @@ -479,7 +474,11 @@ export class Manager {
DsManager.openReport(currProj);
}

setCodeSnippet(config = this.currConfig) {
this.codeSnippet = Config.getCodeSnippetConfig(config) || function () {};
getCodeSnippet() {
const generator = this.fileManager.fileStructures.generators.find((g) => {
return g.dataSource.name === this.currLocalDataSource.name;
});

return generator.codeSnippet;
}
}
28 changes: 1 addition & 27 deletions packages/pont-engine/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as prettier from 'prettier';
import * as ts from 'typescript';
import { ResolveConfigOptions } from 'prettier';
import { error } from './debugLog';
import { Interface, Mod, StandardDataSource, StandardDataType } from './standard';
import { Mod, StandardDataSource, StandardDataType } from './standard';
import { Manager } from './manage';
import { OriginType } from './scripts';
import { diff } from './diff';
Expand Down Expand Up @@ -38,14 +38,6 @@ export default function (url: string): string {
}
`;

const defaultCodeSnippetCode = `
import { Mod, Interface } from "pont-engine";
export default function(mod: Mod,inter: Interface): StandardDataSource {
return ['API',mod.name,inter.name].join(".");
}
`;

export class Mocks {
enable = false;
port = 8080;
Expand Down Expand Up @@ -81,7 +73,6 @@ export class DataSourceConfig {
outDir = 'src/service';
scannedRange = [];
transformPath = '';
codeSnippetPath = '';
fetchMethodPath = '';
prettierConfig: ResolveConfigOptions = {};
/** 单位为秒,默认 20 分钟 */
Expand Down Expand Up @@ -109,7 +100,6 @@ export class Config extends DataSourceConfig {
name: string;
usingOperationId: boolean;
transformPath?: string;
codeSnippetPath?: string;
fetchMethodPath?: string;
outDir?: string;
}>;
Expand All @@ -119,21 +109,6 @@ export class Config extends DataSourceConfig {
this.origins = config.origins || [];
}

static getCodeSnippetConfig(config: Config | DataSourceConfig) {
if (config.codeSnippetPath) {
const moduleResult = getTemplate(config.codeSnippetPath, '', defaultCodeSnippetCode) as any;

if (moduleResult) {
return moduleResult.default;
}
}

return (inter: Interface) => {
const context = inter.getContext();
return `API${config.usingMultipleOrigins ? `.${context.dataSource.name}` : ''}.${context.mod.name}.${inter.name}`;
};
}

static getTransformFromConfig(config: Config | DataSourceConfig) {
if (config.transformPath) {
const moduleResult = getTemplate(config.transformPath, '', defaultTransformCode) as any;
Expand Down Expand Up @@ -200,7 +175,6 @@ export class Config extends DataSourceConfig {
scannedRange: Array.isArray(this.scannedRange) ? this.scannedRange.map((dir) => path.join(configDir, dir)) : [],
templatePath: this.templatePath ? path.join(configDir, this.templatePath) : undefined,
transformPath: this.transformPath ? path.join(configDir, this.transformPath) : undefined,
codeSnippetPath: this.codeSnippetPath ? path.join(configDir, this.codeSnippetPath) : undefined,
fetchMethodPath: this.fetchMethodPath ? path.join(configDir, this.fetchMethodPath) : undefined
};

Expand Down
10 changes: 5 additions & 5 deletions packages/vscode-pont/src/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class Control {
}

findInterface(ignoreEdit = false) {
const codeTemplate = this.manager.codeSnippet;
const codeTemplate = this.manager.getCodeSnippet();

const items = this.manager.currLocalDataSource.mods
.map((mod) => {
Expand Down Expand Up @@ -211,7 +211,7 @@ export class Control {
return;
}

const code = codeTemplate(item.inter);
const code = codeTemplate?.(item.inter) || '';

const editor = vscode.window.activeTextEditor;

Expand Down Expand Up @@ -305,7 +305,7 @@ export class Control {

p.report({ message: '更新成功!' });
vscode.window.showInformationMessage(modName + '更新成功!');
resolve();
resolve(true);
} catch (e) {
reject(e);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ export class Control {

p.report({ message: '更新成功!' });
vscode.window.showInformationMessage(boName + '更新成功!');
resolve();
resolve(true);
} catch (e) {
reject(e);
}
Expand Down Expand Up @@ -394,7 +394,7 @@ export class Control {
p.report({ message: '更新成功!' });
vscode.window.showInformationMessage('更新成功!');

resolve();
resolve(true);
} catch (e) {
reject(e);
}
Expand Down

0 comments on commit d63b2d8

Please sign in to comment.