当我需要从xsd schema生成一些C#代码,例如DTO类或excel表格时,我使用了一些Roslyn API。
是否有类似于typescript的东西?
[编辑]: 我最终使用了ts-morph
当我需要从xsd schema生成一些C#代码,例如DTO类或excel表格时,我使用了一些Roslyn API。
是否有类似于typescript的东西?
[编辑]: 我最终使用了ts-morph
尝试使用ts-morph。虽然我只使用了一个小时,但它似乎非常有能力。
import {Project, Scope, SourceFile} from "ts-morph";
const project = new Project();
const sourceFile = project.createSourceFile(`./target/file.ts`);
const classDeclaration = sourceFile.addClass({
name: 'SomeClass'
});
const constr = classDeclaration.addConstructor({});
constr.setBodyText('this.myProp = myProp');
classDeclaration.addProperty({
name: 'myProp',
type: 'string',
initializer: 'hello world!',
scope: Scope.Public
});
sourceFile.formatText();
console.log(sourceFile.getText());
import ts = require("typescript");
function makeFactorialFunction() {
const functionName = ts.factory.createIdentifier("factorial");
const paramName = ts.factory.createIdentifier("n");
const parameter = ts.factory.createParameterDeclaration(
/*decorators*/ undefined,
/*modifiers*/ undefined,
/*dotDotDotToken*/ undefined,
paramName
);
const condition = ts.factory.createBinaryExpression(
paramName,
ts.SyntaxKind.LessThanEqualsToken,
ts.factory.createNumericLiteral(1)
);
const ifBody = ts.factory.createBlock(
[ts.factory.createReturnStatement(ts.factory.createNumericLiteral(1))],
/*multiline*/ true
);
const decrementedArg = ts.factory.createBinaryExpression(
paramName,
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(1)
);
const recurse = ts.factory.createBinaryExpression(
paramName,
ts.SyntaxKind.AsteriskToken,
ts.factory.createCallExpression(functionName, /*typeArgs*/ undefined, [decrementedArg])
);
const statements = [ts.factory.createIfStatement(condition, ifBody), ts.factory.createReturnStatement(recurse)];
return ts.factory.createFunctionDeclaration(
/*decorators*/ undefined,
/*modifiers*/ [ts.factory.createToken(ts.SyntaxKind.ExportKeyword)],
/*asteriskToken*/ undefined,
functionName,
/*typeParameters*/ undefined,
[parameter],
/*returnType*/ ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
ts.factory.createBlock(statements, /*multiline*/ true)
);
}
const resultFile = ts.createSourceFile(
"someFileName.ts",
"",
ts.ScriptTarget.Latest,
/*setParentNodes*/ false,
ts.ScriptKind.TS
);
const printer = ts.createPrinter({
newLine: ts.NewLineKind.LineFeed
});
const result = printer.printNode(
ts.EmitHint.Unspecified,
makeFactorialFunction(),
resultFile
);
console.log(result);
是否有类似的TypeScript插件?
目前还没有,但TypeScript团队正在开放发射器(这是什么?)以支持插件来实现这个场景:https://github.com/Microsoft/TypeScript/issues/5595
您可以使用此工具使用Swagger在TypeScript中生成API。 https://github.com/unchase/Unchase.OpenAPI.Connectedservice/
相关: * NSwagStudio
当我们需要在使用Angular 4和TypeScript的MEAN堆栈中添加对RESTful API的支持时,我们使用http://editor.swagger.io并传递Swagger 2.0 API定义的JSON版本,然后选择TypeScript的客户端生成器。
当然,我们有点作弊,因为我们首先使用SZ Architech (http://www.solution.zone)创建了RESTful API,它使用SwaggerUi来记录生成的API,并允许我们只需复制Swagger 2.0定义即可使用Swagger的代码生成器生成客户端代码。