@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
109 lines • 8.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const access_modifier_1 = require("../csharp/code-dom/access-modifier");
const class_1 = require("../csharp/code-dom/class");
const constructor_1 = require("../csharp/code-dom/constructor");
const expression_1 = require("../csharp/code-dom/expression");
const field_1 = require("../csharp/code-dom/field");
const import_1 = require("../csharp/code-dom/import");
const method_1 = require("../csharp/code-dom/method");
const dotnet = require("../csharp/code-dom/mscorlib");
const parameter_1 = require("../csharp/code-dom/parameter");
const property_1 = require("../csharp/code-dom/property");
const return_1 = require("../csharp/code-dom/statements/return");
const clientruntime_1 = require("../csharp/lowlevel-generator/clientruntime");
class ModuleClass extends class_1.Class {
constructor(namespace, state, objectInitializer) {
super(namespace, 'Module');
this.state = state;
this.apply(objectInitializer);
this.partial = true;
// static instance property
this.add(new property_1.LazyProperty('Instance', this, new expression_1.LiteralExpression(`new ${this.declaration}()`), {
instanceAccess: this.declaration,
static: access_modifier_1.Modifier.Static,
}));
const isAzure = this.state.project.azure;
// get the name of the client API class
const TaskOfHttpResponseMessage = dotnet.System.Threading.Tasks.Task(dotnet.System.Net.Http.HttpResponseMessage);
const BoundParameterDictionary = dotnet.System.Collections.Generic.Dictionary(dotnet.String, dotnet.Object);
const clientAPI = new dotnet.LibraryType(this.state.model.details.csharp.namespace, this.state.model.details.csharp.name);
const clientProperty = this.add(new property_1.Property('ClientAPI', clientAPI));
// lets the common code call the signal again (recursive! careful!)
const signalDelegateI = dotnet.System.Func(dotnet.String, dotnet.System.Threading.CancellationToken, dotnet.System.Func(dotnet.System.EventArgs),
/* returns */ dotnet.System.Threading.Tasks.Task());
const signalDelegate = dotnet.System.Func(dotnet.String, dotnet.System.Threading.CancellationToken, dotnet.System.Func(dotnet.System.EventArgs), signalDelegateI,
/* returns */ dotnet.System.Threading.Tasks.Task());
const IEventListenerExpanded = [
dotnet.System.Threading.CancellationToken,
dotnet.System.Action(),
signalDelegateI,
];
const nextStep = dotnet.System.Func(dotnet.System.Net.Http.HttpRequestMessage, ...IEventListenerExpanded,
/* returns */ TaskOfHttpResponseMessage);
const signalDelegateIAlias = namespace.add(new import_1.Alias('SignalDelegateI', signalDelegateI));
const signalDelegateAlias = namespace.add(new import_1.Alias('SignalDelegate', signalDelegate));
const getParameterDelegate = namespace.add(new import_1.Alias('GetParameterDelegate', dotnet.System.Func(dotnet.String, dotnet.String, BoundParameterDictionary, dotnet.String,
/* returns */ dotnet.Object)));
const sendAsyncStep = namespace.add(new import_1.Alias('SendAsyncStepDelegate', dotnet.System.Func(dotnet.System.Net.Http.HttpRequestMessage, ...IEventListenerExpanded, nextStep, /* Next( ...) */
/* returns */ TaskOfHttpResponseMessage)));
const boundParams = new parameter_1.Parameter('boundParameters', dotnet.System.Collections.Generic.Dictionary(dotnet.String, dotnet.Object));
const pipelineField = this.add(new field_1.Field('_pipeline', clientruntime_1.ClientRuntime.HttpPipeline, { access: access_modifier_1.Access.Private }));
const createPipeline = this.add(new method_1.Method('CreatePipeline', clientruntime_1.ClientRuntime.HttpPipeline, {
parameters: [boundParams]
}));
const init = this.add(new method_1.Method('Init', dotnet.Void));
if (isAzure) {
const pipelineChangeDelegate = namespace.add(new import_1.Alias('PipelineChangeDelegate', dotnet.System.Action(sendAsyncStep.fullDefinition)));
const nextDelegate = namespace.add(new import_1.Alias('NextDelegate', nextStep));
const moduleLoadPipelineAction = namespace.add(new import_1.Alias('ModuleLoadPipelineDelegate', dotnet.System.Action(dotnet.String, dotnet.String, pipelineChangeDelegate.fullDefinition, pipelineChangeDelegate.fullDefinition)));
const newRequestPipelineAction = namespace.add(new import_1.Alias('NewRequestPipelineDelegate', dotnet.System.Action(dotnet.System.Collections.Generic.Dictionary(dotnet.String, dotnet.Object), pipelineChangeDelegate.fullDefinition, pipelineChangeDelegate.fullDefinition)));
const OnModuleLoad = this.add(new property_1.Property('OnModuleLoad', moduleLoadPipelineAction));
const OnNewRequest = this.add(new property_1.Property('OnNewRequest', newRequestPipelineAction));
const moduleIdentity = this.add(new property_1.LambdaProperty('Name', dotnet.String, new expression_1.StringExpression(state.project.moduleName)));
const moduleResourceId = this.add(new property_1.LambdaProperty('ResourceId', dotnet.String, new expression_1.StringExpression(state.project.moduleName)));
init.add(function* () {
yield `${OnModuleLoad.value}?.Invoke( ${moduleResourceId.value}, ${moduleIdentity.value} ,(step)=> { ${pipelineField.value}.Prepend(step); } , (step)=> { ${pipelineField.value}.Append(step); } );`;
;
});
const GetParameterValue = this.add(new property_1.Property('GetParameterValue', getParameterDelegate));
const pBoundParameters = new parameter_1.Parameter('boundParameters', BoundParameterDictionary);
const pKey = new parameter_1.Parameter('parameterName', dotnet.String);
const GetParameter = this.add(new method_1.LambdaMethod('GetParameter', dotnet.Object, new expression_1.LiteralExpression(`${GetParameterValue.value}?.Invoke( ${moduleResourceId.value}, ${moduleIdentity.value}, ${pBoundParameters.value}, ${pKey.value} )`), { parameters: [pBoundParameters, pKey] }));
const EventListener = this.add(new property_1.Property('EventListener', signalDelegateAlias));
const pId = new parameter_1.Parameter('id', dotnet.String);
const pToken = new parameter_1.Parameter('token', dotnet.System.Threading.CancellationToken);
const pGetEvent = new parameter_1.Parameter('getEventData', dotnet.System.Func(dotnet.System.EventArgs));
const pSignal = new parameter_1.Parameter('signal', signalDelegateIAlias);
this.add(new method_1.Method('Signal', dotnet.System.Threading.Tasks.Task(), { parameters: [pId, pToken, pGetEvent, pSignal], async: access_modifier_1.Modifier.Async })).add(function* () {
yield `await ${EventListener.value}?.Invoke(${pId.value},${pToken.value},${pGetEvent.value}, ${pSignal.value});`;
});
createPipeline.add(function* () {
yield `var result = this._pipeline.Clone();`;
if (isAzure) {
yield ` ${OnNewRequest.value}?.Invoke( ${boundParams.use}, (step)=> { result.Prepend(step); } , (step)=> { result.Append(step); } );`;
}
yield return_1.Return(new expression_1.LiteralExpression(`result`));
});
}
else {
// non-azure init method
init.add(function* () {
yield `// called at module init time...`;
});
// non-azure createPipeline method
createPipeline.add(function* () {
yield return_1.Return(new expression_1.LiteralExpression(`this._pipeline.Clone()`));
});
}
this.add(new constructor_1.Constructor(this, {
access: access_modifier_1.Access.Private,
})).add(function* () {
yield `/// constructor`;
yield clientProperty.assignPrivate(clientAPI.newInstance());
yield pipelineField.assignPrivate(clientruntime_1.ClientRuntime.HttpPipeline.newInstance());
});
}
}
exports.ModuleClass = ModuleClass;
//# sourceMappingURL=module-class.js.map