"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.powershellV2 = void 0;
//import { codemodel } from '@azure-tools/codemodel-v3';
const codegen_1 = require("@azure-tools/codegen");
const path_1 = require("path");
const project_1 = require("../internal/project");
const psm1_1 = require("../generators/psm1");
const csproj_1 = require("../generators/csproj");
const psm1_custom_1 = require("../generators/psm1.custom");
const inputhandler_custom_1 = require("../generators/inputhandler.custom");
const psm1_internal_1 = require("../generators/psm1.internal");
const nuspec_1 = require("../generators/nuspec");
const gitignore_1 = require("../generators/gitignore");
const gitattributes_1 = require("../generators/gitattributes");
const readme_1 = require("../generators/readme");
const script_cmdlet_1 = require("../generators/script-cmdlet");
const AssemblyInfo_1 = require("../generators/AssemblyInfo");
const sourceFileCSharp = 'source-file-csharp';
const resources = `${__dirname}/../../resources`;
async function copyRequiredFiles(project) {
    const transformOutput = async (input) => {
        return await project.state.resolveVariables(input);
    };
    // Project assets
    await (0, codegen_1.copyResources)((0, path_1.join)(resources, 'assets'), async (fname, content) => project.state.writeFile((0, path_1.join)(project.baseFolder, fname), content, undefined, 'source-file-other'), undefined, transformOutput);
    // Runtime files
    await (0, codegen_1.copyResources)((0, path_1.join)(resources, 'psruntime'), async (fname, content) => project.state.writeFile((0, path_1.join)(project.runtimeFolder, fname), content, undefined, 'source-file-other'), project.overrides, transformOutput);
    // utils cmdlets
    await (0, codegen_1.copyResources)((0, path_1.join)(resources, 'utils'), async (fname, content) => project.state.writeFile((0, path_1.join)(project.utilsFolder, fname), content, undefined, 'source-file-other'), project.overrides, transformOutput);
    // Modules files
    if (project.azure) {
        await (0, codegen_1.copyBinaryResources)((0, path_1.join)(resources, 'modules'), async (fname, content) => project.state.writeFile((0, path_1.join)(project.dependencyModuleFolder, fname), content, undefined, 'binary-file'));
    }
    if (project.azure) {
        // Signing key file
        await (0, codegen_1.copyBinaryResources)((0, path_1.join)(resources, 'signing'), async (fname, content) => project.state.writeFile((0, path_1.join)(project.baseFolder, fname), content, undefined, 'binary-file'));
    }
}
async function powershellV2(service, state) {
    let debug = false;
    try {
        const project = await new project_1.Project(service).init(state);
        await project.writeFiles(async (filename, content) => project.state.writeFile(filename, (0, codegen_1.applyOverrides)(content, project.overrides), undefined, sourceFileCSharp));
        debug = (await project.state.service.getValue('debug')) || false;
        await project.state.protectFiles(project.psd1);
        await project.state.protectFiles(project.readme);
        await project.state.protectFiles(project.customFolder);
        await project.state.protectFiles(project.testFolder);
        await project.state.protectFiles(project.docsFolder);
        await project.state.protectFiles(project.examplesFolder);
        await project.state.protectFiles(project.resourcesFolder);
        await project.state.protectFiles(project.uxFolder);
        // wait for all the generation to be done
        await copyRequiredFiles(project);
        await (0, csproj_1.generateCsproj)(project);
        await (0, psm1_1.generatePsm1)(project);
        await (0, inputhandler_custom_1.generateInputHandlersCustom)(project);
        await (0, psm1_custom_1.generatePsm1Custom)(project);
        await (0, psm1_internal_1.generatePsm1Internal)(project);
        await (0, nuspec_1.generateNuspec)(project);
        await (0, gitignore_1.generateGitIgnore)(project);
        await (0, gitattributes_1.generateGitAttributes)(project);
        await (0, readme_1.generateReadme)(project);
        await (0, AssemblyInfo_1.generateAssemblyInfo)(project);
        await (0, script_cmdlet_1.generateScriptCmdlets)(project);
    }
    catch (E) {
        if (debug && E instanceof Error) {
            console.error(`${__filename} - FAILURE  ${JSON.stringify(E)} ${E.stack}`);
        }
        throw E;
    }
}
exports.powershellV2 = powershellV2;
//# sourceMappingURL=powershell-v2.js.map