|
| 1 | +# |
| 2 | +# Copyright (c) 2021 Red Hat, Inc. |
| 3 | +# This program and the accompanying materials are made |
| 4 | +# available under the terms of the Eclipse Public License 2.0 |
| 5 | +# which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | +# |
| 7 | +# SPDX-License-Identifier: EPL-2.0 |
| 8 | +# |
| 9 | +# Contributors: |
| 10 | +# Red Hat, Inc. - initial API and implementation |
| 11 | +# |
| 12 | + |
| 13 | +import os |
| 14 | +import argparse |
| 15 | +import yaml |
| 16 | + |
| 17 | + |
| 18 | +def write_contents(filename: str, mode: str, contents: str) -> None: |
| 19 | + """ |
| 20 | + Write the string to the specified filename |
| 21 | + """ |
| 22 | + with open(filename, mode) as out: |
| 23 | + out.write(contents) |
| 24 | + |
| 25 | + |
| 26 | +def get_crd_metadata(output_path: str) -> None: |
| 27 | + """ |
| 28 | + Read in the devworkspace and devworkspace template crds and generate metadata into api/apis.ts |
| 29 | + """ |
| 30 | + crd_path = "crds" |
| 31 | + typescript_contents = "" |
| 32 | + devworkspace_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspaces.yaml') |
| 33 | + with open(devworkspace_crd_path, 'r') as devfile_file: |
| 34 | + yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader) |
| 35 | + spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data) |
| 36 | + typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions, |
| 37 | + latest_version) |
| 38 | + |
| 39 | + devworkspacetemplate_crd_path = os.path.join(crd_path, 'workspace.devfile.io_devworkspacetemplates.yaml') |
| 40 | + with open(devworkspacetemplate_crd_path, 'r') as devfile_file: |
| 41 | + yaml_data = yaml.load(devfile_file, Loader=yaml.FullLoader) |
| 42 | + spec, group, kind, plural, singular, versions, latest_version, latest_api_version = extract_fields(yaml_data) |
| 43 | + typescript_contents += generate_typescript(latest_api_version, group, kind, plural, singular, versions, |
| 44 | + latest_version) |
| 45 | + |
| 46 | + write_contents(os.path.join(output_path, "constants", "constants.ts"), "w", typescript_contents) |
| 47 | + |
| 48 | + |
| 49 | +def extract_fields(yaml_data: {}) -> (str, str, str, str, str, [], str, str): |
| 50 | + """ |
| 51 | + Extract metadata from the crds |
| 52 | + """ |
| 53 | + spec = yaml_data['spec'] |
| 54 | + group = spec['group'] |
| 55 | + kind = spec['names']['kind'] |
| 56 | + plural = spec['names']['plural'] |
| 57 | + singular = spec['names']['singular'] |
| 58 | + versions = [version['name'] for version in spec['versions']] |
| 59 | + latest_version = versions[len(versions) - 1] |
| 60 | + latest_api_version = "{}/{}".format(group, latest_version) |
| 61 | + return spec, group, kind, plural, singular, versions, latest_version, latest_api_version |
| 62 | + |
| 63 | + |
| 64 | +def generate_typescript(api_version: str, group: str, kind: str, plural: str, singular: str, versions: [], |
| 65 | + latest_version: str) -> str: |
| 66 | + """ |
| 67 | + Export a string representation of the typescript |
| 68 | + """ |
| 69 | + return f""" |
| 70 | +export const {singular + "ApiVersion"} = '{api_version}'; |
| 71 | +export const {singular + "Group"} = '{group}'; |
| 72 | +export const {singular + "Kind"} = '{kind}'; |
| 73 | +export const {singular + "Plural"} = '{plural}'; |
| 74 | +export const {singular + "Singular"} = '{singular}'; |
| 75 | +export const {singular + "Versions"} = {versions}; |
| 76 | +export const {singular + "LatestVersion"} = '{latest_version}'; |
| 77 | + """ |
| 78 | + |
| 79 | + |
| 80 | +def export_typescript_api(output_path: str) -> None: |
| 81 | + """ |
| 82 | + Export constants into api.ts |
| 83 | + """ |
| 84 | + export_contents = """ |
| 85 | +export * from './constants/constants'; |
| 86 | + """ |
| 87 | + write_contents(os.path.join(output_path, "api.ts"), "a", export_contents) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + # Get any additional metadata we can from the crds |
| 92 | + parser = argparse.ArgumentParser(description='Generate metadata from crds') |
| 93 | + parser.add_argument('-p', '--path', action='store', type=str, help='The path to the constants directory') |
| 94 | + |
| 95 | + args = parser.parse_args() |
| 96 | + if not args.path: |
| 97 | + parser.print_help() |
| 98 | + parser.exit() |
| 99 | + |
| 100 | + path = args.path |
| 101 | + |
| 102 | + # Grab the metadata from the crds and put it into constants/constant.ts in typescript-model |
| 103 | + get_crd_metadata(path) |
| 104 | + |
| 105 | + # Export constants/constant.ts so that you can import constants from the package |
| 106 | + export_typescript_api(path) |
0 commit comments