-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Ryan Nowak <[email protected]>
- Loading branch information
Showing
19 changed files
with
341 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: 'gitsubmodule' | ||
directory: '/' | ||
- package-ecosystem: 'npm' | ||
directory: '/' | ||
schedule: | ||
interval: 'monthly' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "bicep-types"] | ||
path = bicep-types | ||
url = https://github.com/Azure/bicep-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bicep-types/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"tabWidth": 2, | ||
"semi": false, | ||
"singleQuote": true | ||
} | ||
} |
Submodule bicep-types
added at
3676a8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
module.exports = { | ||
projects: ["<rootDir>/packages/*"], | ||
testEnvironment: "node", | ||
projects: ['<rootDir>/packages/*'], | ||
testEnvironment: 'node', | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest", {}], | ||
'^.+.tsx?$': ['ts-jest', {}], | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
module.exports = { | ||
displayName: "manifest-to-bicep-extension", | ||
roots: ["<rootDir>"], | ||
displayName: 'manifest-to-bicep-extension', | ||
roots: ['<rootDir>'], | ||
transform: { | ||
"^.+\\.tsx?$": "ts-jest", | ||
'^.+\\.tsx?$': 'ts-jest', | ||
}, | ||
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$", | ||
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], | ||
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', | ||
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], | ||
moduleNameMapper: { | ||
"^src/(.*)": "<rootDir>/src/$1", | ||
'^src/(.*)': '<rootDir>/src/$1', | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { | ||
buildIndex, | ||
ObjectTypeProperty, | ||
ObjectTypePropertyFlags, | ||
ResourceFlags, | ||
ScopeType, | ||
TypeFactory, | ||
TypeFile, | ||
TypeReference, | ||
writeIndexJson, | ||
writeIndexMarkdown, | ||
writeTypesJson, | ||
} from 'bicep-types' | ||
import { ResourceProvider, Schema } from './manifest' | ||
|
||
export function convert(manifest: ResourceProvider): { | ||
typesContent: string | ||
indexContent: string | ||
documentationContent: string | ||
} { | ||
const factory = new TypeFactory() | ||
|
||
for (const resourceType of manifest.types) { | ||
for (const apiVersion of resourceType.apiVersions) { | ||
const qualifiedName = `${manifest.name}/${resourceType.name}@${apiVersion.name}` | ||
|
||
const propertyType = factory.addObjectType( | ||
`${resourceType.name}Properties`, | ||
schemaProperties(apiVersion.schema, factory) | ||
) | ||
|
||
const bodyType = factory.addObjectType(qualifiedName, { | ||
name: { | ||
type: factory.addStringType(), | ||
flags: | ||
ObjectTypePropertyFlags.Required | | ||
ObjectTypePropertyFlags.Identifier, | ||
description: 'The resource name.', | ||
}, | ||
location: { | ||
type: factory.addStringType(), | ||
flags: ObjectTypePropertyFlags.None, | ||
description: 'The resource location.', | ||
}, | ||
properties: { | ||
type: propertyType, | ||
flags: ObjectTypePropertyFlags.Required, | ||
description: 'The resource properties.', | ||
}, | ||
apiVersion: { | ||
type: factory.addStringLiteralType(apiVersion.name), | ||
flags: | ||
ObjectTypePropertyFlags.ReadOnly | | ||
ObjectTypePropertyFlags.DeployTimeConstant, | ||
description: 'The API version.', | ||
}, | ||
type: { | ||
type: factory.addStringLiteralType( | ||
`${manifest.name}/${resourceType.name}` | ||
), | ||
flags: | ||
ObjectTypePropertyFlags.ReadOnly | | ||
ObjectTypePropertyFlags.DeployTimeConstant, | ||
description: 'The resource type.', | ||
}, | ||
id: { | ||
type: factory.addStringType(), | ||
flags: ObjectTypePropertyFlags.ReadOnly, | ||
description: 'The resource id.', | ||
}, | ||
}) | ||
factory.addResourceType( | ||
qualifiedName, | ||
ScopeType.Unknown, | ||
undefined, | ||
bodyType, | ||
ResourceFlags.None, | ||
{} | ||
) | ||
} | ||
} | ||
|
||
const typeFiles: TypeFile[] = [] | ||
typeFiles.push({ | ||
relativePath: 'types.json', | ||
types: factory.types, | ||
}) | ||
|
||
const indexContent = buildIndex(typeFiles, (log) => console.log(log), { | ||
name: manifest.name.toLowerCase().replace('.', ''), | ||
version: '0.0.1', | ||
isSingleton: false, | ||
}) | ||
|
||
return { | ||
typesContent: writeTypesJson(factory.types), | ||
indexContent: writeIndexJson(indexContent), | ||
documentationContent: writeIndexMarkdown(indexContent), | ||
} | ||
} | ||
|
||
function schemaProperties( | ||
parent: Schema, | ||
factory: TypeFactory | ||
): Record<string, ObjectTypeProperty> { | ||
const results: Record<string, ObjectTypeProperty> = {} | ||
for (const [key, value] of Object.entries(parent.properties ?? {})) { | ||
results[key] = addSchemaProperty(parent, key, value, factory) | ||
} | ||
return results | ||
} | ||
|
||
function addSchemaProperty( | ||
parent: Schema, | ||
key: string, | ||
property: Schema, | ||
factory: TypeFactory | ||
): ObjectTypeProperty { | ||
const propertyType = addSchema(property, key, factory) | ||
|
||
let flags = ObjectTypePropertyFlags.None | ||
if (parent.required?.includes(key)) { | ||
flags |= ObjectTypePropertyFlags.Required | ||
} | ||
if (property.readOnly === true) { | ||
flags |= ObjectTypePropertyFlags.ReadOnly | ||
} | ||
|
||
return { | ||
description: property.description, | ||
type: propertyType, | ||
flags: flags, | ||
} | ||
} | ||
|
||
function addSchema( | ||
schema: Schema, | ||
name: string, | ||
factory: TypeFactory | ||
): TypeReference { | ||
if (schema.type === 'string') { | ||
return factory.addStringType() | ||
} else if (schema.type === 'object') { | ||
return factory.addObjectType(name, schemaProperties(schema, factory)) | ||
} else { | ||
throw new Error(`Unsupported schema type: ${schema.type}`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import { add } from './math'; | ||
add(9, 3); | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
const program = require('./program') | ||
program.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export interface ResourceProvider { | ||
name: string | ||
types: ResourceType[] | ||
} | ||
|
||
export interface ResourceType { | ||
name: string | ||
defaultApiVersion?: string | ||
apiVersions: APIVersion[] | ||
} | ||
|
||
export interface APIVersion { | ||
name: string | ||
schema: Schema | ||
capabilities?: string[] | ||
} | ||
|
||
export interface Schema { | ||
type: 'string' | 'object' | ||
description?: string | ||
properties?: Schema[] | ||
required?: string[] | ||
readOnly?: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export function add(a: number, b: number): number { | ||
return a + b; | ||
return a + b | ||
} |
Oops, something went wrong.