import { Channel , AutorestExtensionHost } from "@autorest/extension-base" ; import { AutorestOptions , getHost , getSession } from "../autorestSession" ; import { TracingInfo } from "../models/clientDetails" ; import { PackageDetails } from "../models/packageDetails" ; import { NameType , normalizeName } from "./nameUtils" ; /**
 * Extracts common autorest options
 */ export async function extractAutorestOptions ( ) { const host = getHost ( ) ; const useCoreV2 = await getUseCoreV2 ( host ) ; const restLevelClient = await getRestLevelClient ( host ) ; const rlcShortcut = await getHasShortcutMethods ( host ) ; const azureArm = await getIsAzureArm ( host ) ; const addCredentials = await getAddCredentials ( host ) ; const credentialKeyHeaderName = await getKeyCredentialHeaderName ( host ) ; const srcPath = await getSrcPath ( host ) ; const outputPath = await getOutputPath ( host ) ; const credentialScopes = await getCredentialScopes ( host ) ; const packageDetails = await getPackageDetails ( host ) ; const licenseHeader = await getLicenseHeader ( host ) ; const generateMetadata = await getGenerateMetadata ( host ) ; const hideClients = await getHideClients ( host ) ; const title = await getTitle ( host ) ; const tracingInfo = await getTracingInfo ( host ) ; const disablePagingAsyncIterators = await getDisableAsyncOperators ( host ) ; const ignoreNullableOnOptional = await getIgnoreNullableOnOptional ( host ) ; const allowInsecureConnection = await getAllowInsecureConnection ( host ) ; const skipEnumValidation = await getSkipEnumValidation ( host ) ; const azureOutputDirectory = await getAzureOutputDirectoryPath ( host ) ; const headAsBoolean = await getHeadAsBoolean ( host ) ; const isTestPackage = await getIsTestPackage ( host ) ; const generateTest = await getGenerateTest ( host ) ; const batch = await getBatch ( host ) ; const generateSample = await getGenerateSample ( host ) ; return { azureArm , addCredentials , credentialKeyHeaderName , credentialScopes , restLevelClient , rlcShortcut , srcPath , outputPath , packageDetails , licenseHeader , tracingInfo , generateMetadata , useCoreV2 , hideClients , ignoreNullableOnOptional , allowInsecureConnection , disablePagingAsyncIterators , skipEnumValidation , title , azureOutputDirectory , headAsBoolean , isTestPackage , generateTest , batch , generateSample } ; } async function getHasShortcutMethods ( host ) { const headAsBoolean = await host . getValue ( "rlc-shortcut" ) ; return Boolean ( headAsBoolean ) ; } async function getHeadAsBoolean ( host ) { const headAsBoolean = await host . getValue ( "head-as-boolean" ) ; return Boolean ( headAsBoolean ) ; } async function getIsTestPackage ( host ) { const isTestPackage = await host . getValue ( "is-test-package" ) ; return isTestPackage === null ? false : Boolean ( isTestPackage ) ; } async function getGenerateTest ( host ) { const generateTest = await host . getValue ( "generate-test" ) ; return generateTest === null ? false : Boolean ( generateTest ) ; } async function getGenerateSample ( host ) { const generateSample = await host . getValue ( "generate-sample" ) ; return generateSample === null ? false : Boolean ( generateSample ) ; } async function getSkipEnumValidation ( host ) { const skipEnumValidation = await host . getValue ( "skip-enum-validation" ) ; return skipEnumValidation === true ; } async function getAllowInsecureConnection ( host ) { return ( await host . getValue ( "allow-insecure-connection" ) ) || false ; } async function getIgnoreNullableOnOptional ( host ) { const isAzureArm = await getIsAzureArm ( host ) ; return ( await host . getValue ( "ignore-nullable-on-optional" ) ) ? true : isAzureArm ; } async function getDisableAsyncOperators ( host ) { return ( await host . getValue ( "disable-async-iterators" ) ) === true ; } async function getHideClients ( host ) { return ( await host . getValue ( "hide-clients" ) ) || false ; } async function getGenerateMetadata ( host ) { return ( await host . getValue ( "generate-metadata" ) ) !== false ; } async function getLicenseHeader ( host ) { return ( await host . getValue ( "license-header" ) ) || false ; } async function getTitle ( host ) { return ( await host . getValue ( "title" ) ) || undefined ; } async function getSrcPath ( host ) { return ( ( await host . getValue ( "source-code-folder-path" ) ) as string ) || "src" ; } async function getOutputPath ( host ) { return ( await host . getValue ( "output-folder" ) ) || undefined ; } async function getKeyCredentialHeaderName ( host ) { return ( await host . getValue ( "credential-key-header-name" ) ) || undefined ; } async function getAddCredentials ( host ) { const addCredentials = await host . getValue ( "add-credentials" ) ; if ( addCredentials === false ) { return false ; } else { return true ; } } async function getIsAzureArm ( host ) { const flag = ( await host . getValue ( "azure-arm" ) ) === true ; const openapi = ( await host . getValue ( "openapi-type" ) ) === "arm" ; return flag || openapi ; } async function getRestLevelClient ( host ) { return ( await host . getValue ( "rest-level-client" ) ) === true ; } async function getUseCoreV2 ( host ) { const useCoreV2Option = await host . getValue ( "use-core-v2" ) ; return useCoreV2Option === null ? true : Boolean ( useCoreV2Option ) ; } async function getTracingInfo ( host ) { const tracing = ( await host . getValue ( "tracing-info" ) ) || undefined ; if ( tracing && tracing . namespace && tracing . packagePrefix ) { return tracing ; } const namespace = ( await host . getValue < string > ( "tracing-info.namespace" ) ) || undefined ; const packagePrefix = ( await host . getValue ( "tracing-info.packagePrefix" ) ) || undefined ; if ( packagePrefix && namespace ) { return { namespace , packagePrefix } ; } if ( ! tracing && ! packagePrefix && ! namespace ) { return undefined ; } throw new Error ( "Invalid tracing-info. Make sure that namespace and packagePrefix are defined" ) ; } async function getPackageDetails ( host ) { const { model } = getSession ( ) ; const name = normalizeName ( model . language . default . name , NameType . File ) ; const packageName = ( await host . getValue ( "package-name" ) ) || name ; const packageNameParts = packageName . match ( /(^@(.*)\/)?(.*)/ ) ?? [ ] ; const version = ( await host . getValue ( "package-version" ) ) || "1.0.0-beta.1" ; return { name : packageName , scopeName : packageNameParts [ 2 ] , nameWithoutScope : packageNameParts [ 3 ] , description : model . language . default . description , version } ; } export async function getCredentialScopes ( host ) { const addCredentials = await host . getValue ( "add-credentials" ) ; const credentialScopes = await host . getValue ( "credential-scopes" ) ; const azureArm = await host . getValue ( "azure-arm" ) ; if ( credentialScopes && ! addCredentials ) { throw new Error ( "--credential-scopes must be used with the --add-credentials flag" ) ; } if ( ! credentialScopes ) { if ( azureArm ) { return [ "https://management.azure.com/.default" ] ; } else if ( addCredentials ) { host . message ( { Channel : Channel . Warning , Text : `You have default credential policy BearerTokenCredentialPolicy
        but not the --credential-scopes flag set while generating non-management plane code.
        This is not recommended because it forces the customer to pass credential scopes
        through kwargs if they want to authenticate.` } ) ; } } if ( typeof credentialScopes === "string" ) { return credentialScopes . split ( "," ) ; } return undefined ; } async function getAzureOutputDirectoryPath ( host ) { const outputDirectoryPath = await host . getValue < string > ( "outputFolderUri" ) ; const outputDirectoryRelativePath = outputDirectoryPath ?. replace ( /\/$/ , "" ) . split ( "/" ) . slice ( - 3 ) . join ( "/" ) ; return outputDirectoryRelativePath ?. substr ( 0 , 3 ) === "sdk" ? outputDirectoryRelativePath : undefined ; } async function getBatch ( host ) { const batch = await host . getValue < [ string , any ] [ ] > ( 'batch' ) ; return batch ; }