@microsoft.azure/autorest.incubator
Version:
AutoRest incubator project
149 lines (139 loc) • 6.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const file_generator_1 = require("../common/file-generator");
const locations_1 = require("../common/locations");
const utility_1 = require("../common/utility");
const yaml_1 = require("../common/yaml");
const path_1 = require("path");
const project_1 = require("./project");
const state_1 = require("./state");
async function processRequest(service) {
try {
// Get the list of files
const files = await service.ListInputs('code-model-v2');
if (files.length === 0) {
throw new Error('Inputs missing.');
}
const codemodel = files[0];
// get the openapi document
const codeModelText = await service.ReadFile(codemodel);
const model = await yaml_1.deserialize(codeModelText, codemodel);
// generate some files
const modelState = new state_1.State(service, model, codemodel);
const project = await new project_1.Project(modelState).init();
await project.writeFiles(async (filename, content) => service.WriteFile(filename, content, undefined, 'source-file-csharp'));
await service.ProtectFiles(project.csproj);
await service.ProtectFiles(project.customFolder);
// wait for all the generation to be done
await generateCsproj(service, project);
await copyRuntime(service, project);
await generateCsproj(service, project);
await generateModule(service, project);
// await generateProxies(service);
// debug data
service.WriteFile('code-model-v2.powershell.yaml', yaml_1.serialize(model), undefined, 'source-file-other');
}
catch (E) {
console.error(E);
}
}
exports.processRequest = processRequest;
/*
async function generateProxies(service: Host, project: Project) {
const of = await service.GetValue('output-folder');
// find the pwsh executable.
const pwsh = await resolveFullPath('pwsh', process.platform === 'win32' ? ['c:/Program Files/PowerShell', 'c:/Program Files (x86)/PowerShell'] : []);
if (!pwsh) {
// no powershell core found.
throw new Error('PowerShell Core (pwsh) not found in path. Please ensure that pwsh is available.');
}
console.error(`${pwsh} -command "${of}/generate_proxies.ps1"`);
}
*/
async function copyRuntime(service, project) {
await utility_1.copyResources(path_1.join(locations_1.resources, 'scripts', 'powershell'), async (fname, content) => service.WriteFile(fname, content, undefined, 'source-file-csharp'));
await utility_1.copyResources(path_1.join(locations_1.resources, 'runtime', 'powershell'), async (fname, content) => service.WriteFile(path_1.join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'));
if (project.azure) {
await utility_1.copyResources(path_1.join(locations_1.resources, 'runtime', 'powershell.azure'), async (fname, content) => service.WriteFile(path_1.join(project.runtimefolder, fname), content, undefined, 'source-file-csharp'));
}
}
async function generateCsproj(service, project) {
// write out the csproj file if it's not there.
if (!await service.ReadFile(project.csproj)) {
service.WriteFile(project.csproj, `<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>7.1</LangVersion>
<OutputType>Library</OutputType>
<TargetFramework>netstandard2.0</TargetFramework>
<nowarn>1998</nowarn> <!-- some methods are marked async that don't have an await in them.-->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Management.Automation.dll" Version="10.0.10586" />
<PackageReference Include="Microsoft.CSharp" Version="4.4.1" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.3.0" />
</ItemGroup>
</Project>
`, undefined, 'source-file-csharp');
}
}
async function generateModule(service, project) {
// write out the psd1 file if it's not there.
// if (!await service.ReadFile(cfg.psd1)) {
// todo: change this to *update* the psd1?
service.WriteFile(project.psd1, new file_generator_1.Text(function* () {
yield `@{`;
yield `ModuleVersion="1.0"`;
yield `NestedModules = @(`;
yield ` "./bin/${project.moduleName}.private.dll"`;
yield ` "${project.psm1}"`;
yield `)`;
yield `# don't export any actual cmdlets by default`;
yield `CmdletsToExport = ''`;
yield `# export the functions that we loaded(these are the proxy cmdlets)`;
yield `FunctionsToExport = '*-*'`;
yield `}`;
}).text, undefined, 'source-file-powershell');
// write out the psm1 file if it's not there.
const psm1 = new file_generator_1.Text(await service.ReadFile(project.psm1) || '');
// clear regions first
psm1.removeRegion('Initialization');
psm1.removeRegion('AzureInitialization');
psm1.removeRegion('Finalization');
if (project.azure) {
psm1.setRegion('AzureInitialization', `
# GS Testing
$module = ipmo -passthru -ea 0 "C:\\work\\2018\\mark-powershell\\src\\Package\\Debug\\ResourceManager\\AzureResourceManager\\AzureRM.Profile.Netcore\\AzureRM.Profile.Netcore.psd1"
# from PSModulePath
# $module = ipmo -passthru -ea 0 "AzureRM.Profile.Netcore"
Write-Host "Loaded Common Module '$($module.Name)'"
# ask for the table of functions we can call in the common module.
$VTable = Register-AzureModule
# delegate responsibility to the common module for tweaking the pipeline at module load
$instance.OnModuleLoad = $VTable.OnModuleLoad
# and a chance to tweak the pipeline when we are about to make a call.
$instance.OnNewRequest = $VTable.OnNewRequest
# Need to get parameter values back from the common module
$instance.GetParameterValue = $VTable.GetParameterValue
# need to let the common module listen to events from this module
$instance.EventListener = $VTable.EventListener
`);
}
psm1.setRegion('Initialization', `
# this module instance.
$instance = [${project.serviceNamespace.moduleClass.declaration}]::Instance
$privatemodule = ipmo -passthru "$PSScriptRoot/bin/${project.moduleName}.private.dll"
# export the 'exported' cmdlets
Get-ChildItem "$PSScriptRoot/exported" -Recurse -Filter "*.ps1" -File | Sort-Object Name | Foreach {
Write-Verbose "Dot sourcing private script file: $($_.Name)"
. $_.FullName
# Explicity export the member
Export-ModuleMember -Function $_.BaseName
}`);
psm1.setRegion('Finalization', `
# finish initialization of this module
$instance.Init();
`, false);
psm1.trim();
service.WriteFile(project.psm1, `${psm1}`, undefined, 'source-file-powershell');
}
//# sourceMappingURL=powershell-generator.js.map