22// Licensed under the MIT License.
33import path from 'path' ;
44import os from 'os' ;
5- import { findRecursive , findDirRecursive , executeCmd , rmdirRecursive , lowerCaseCompare , lowerCaseCompareLists , lowerCaseStartsWith , readJsonFile , writeJsonFile , safeMkdir , safeUnlink , fileExists , lowerCaseEquals , lowerCaseContains } from './utils' ;
5+ import { findRecursive , findDirRecursive , executeCmd , rmdirRecursive , lowerCaseCompare , lowerCaseCompareLists , lowerCaseStartsWith , readJsonFile , writeJsonFile , safeMkdir , safeUnlink , fileExists , lowerCaseEquals } from './utils' ;
66import * as constants from './constants' ;
77import { prepareReadme } from './specs' ;
88import colors from 'colors' ;
99import { ScopeType , AutoGenConfig } from './models' ;
10- import { get , set , flatten , uniq , concat , Dictionary , groupBy , keys , difference , pickBy } from 'lodash' ;
10+ import { get , set , flatten , uniq , concat , Dictionary , groupBy , keys , difference } from 'lodash' ;
1111
1212const autorestBinary = os . platform ( ) === 'win32' ? 'autorest.cmd' : 'autorest' ;
13- const apiVersionRegex = / ^ \d { 4 } - \d { 2 } - \d { 2 } ( | - p r e v i e w ) $ / ;
13+ export const apiVersionRegex = / ^ \d { 4 } - \d { 2 } - \d { 2 } ( | - p r e v i e w ) $ / ;
1414
1515export interface SchemaConfiguration {
1616 references : SchemaReference [ ] ;
@@ -35,27 +35,17 @@ const RootSchemaConfigs: Map<ScopeType, RootSchemaConfiguration> = new Map([
3535 [ ScopeType . ManagementGroup , constants . managementGroupRootSchema ]
3636] ) ;
3737
38- export async function getApiVersionsByNamespace ( readme : string ) : Promise < Dictionary < string [ ] > > {
38+ export async function detectProviderNamespaces ( readme : string ) {
3939 const searchPath = path . resolve ( `${ readme } /..` ) ;
40- const apiVersionPaths = await findDirRecursive ( searchPath , p => path . basename ( p ) . match ( apiVersionRegex ) !== null ) ;
41-
42- const output : Dictionary < string [ ] > = { } ;
43- for ( const [ namespace , , apiVersion ] of apiVersionPaths . map ( p => path . relative ( searchPath , p ) . split ( path . sep ) ) ) {
44- output [ namespace ] = [ ...( output [ namespace ] ?? [ ] ) , apiVersion ] ;
45- }
4640
47- return output ;
41+ // To try and detect possible provider namespaces, assume a folder structure of <provider>/preview|stable/<api-version>/..., based on convention
42+ const apiVersionPaths = await findDirRecursive ( searchPath , p => path . basename ( p ) . match ( apiVersionRegex ) !== null ) ;
43+ return uniq ( apiVersionPaths . map ( p => path . relative ( searchPath , p ) . split ( path . sep ) [ 0 ] ) ) ;
4844}
4945
50- export async function generateSchemas ( readme : string , autoGenConfig ? : AutoGenConfig ) : Promise < SchemaConfiguration [ ] > {
46+ export async function generateSchemas ( readme : string , autoGenConfig : AutoGenConfig ) : Promise < SchemaConfiguration [ ] > {
5147 await prepareReadme ( readme , autoGenConfig ) ;
5248
53- const apiVersionsByNamespace = pickBy (
54- await getApiVersionsByNamespace ( readme ) ,
55- ( _ , key ) => ! autoGenConfig || lowerCaseEquals ( key , autoGenConfig . namespace ) ) ;
56-
57- const namespaces = keys ( apiVersionsByNamespace ) ;
58-
5949 const schemaConfigs : SchemaConfiguration [ ] = [ ] ;
6050 const tmpFolder = path . join ( os . tmpdir ( ) , Math . random ( ) . toString ( 36 ) . substr ( 2 ) ) ;
6151
@@ -64,7 +54,7 @@ export async function generateSchemas(readme: string, autoGenConfig?: AutoGenCon
6454
6555 for ( const schemaPath of generatedSchemas ) {
6656 const namespace = path . basename ( schemaPath . substring ( 0 , schemaPath . lastIndexOf ( path . extname ( schemaPath ) ) ) ) ;
67- if ( ! lowerCaseContains ( namespaces , namespace ) ) {
57+ if ( ! lowerCaseEquals ( autoGenConfig . namespace , namespace ) ) {
6858 continue ;
6959 }
7060
0 commit comments