|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | + |
| 16 | +import click as click |
| 17 | +from library_generation.generate_pr_description import generate_pr_descriptions |
| 18 | +from library_generation.generate_repo import generate_from_yaml |
| 19 | +from library_generation.model.generation_config import from_yaml |
| 20 | +from library_generation.utils.generation_config_comparator import compare_config |
| 21 | + |
| 22 | + |
| 23 | +@click.group(invoke_without_command=False) |
| 24 | +@click.pass_context |
| 25 | +@click.version_option(message="%(version)s") |
| 26 | +def main(ctx): |
| 27 | + pass |
| 28 | + |
| 29 | + |
| 30 | +@main.command() |
| 31 | +@click.option( |
| 32 | + "--baseline-generation-config", |
| 33 | + required=False, |
| 34 | + default=None, |
| 35 | + type=str, |
| 36 | + help=""" |
| 37 | + Absolute or relative path to a generation_config.yaml. |
| 38 | + This config file is used for commit history generation, not library |
| 39 | + generation. |
| 40 | + """, |
| 41 | +) |
| 42 | +@click.option( |
| 43 | + "--current-generation-config", |
| 44 | + required=False, |
| 45 | + default=None, |
| 46 | + type=str, |
| 47 | + help=""" |
| 48 | + Absolute or relative path to a generation_config.yaml that contains the |
| 49 | + metadata about library generation. |
| 50 | + """, |
| 51 | +) |
| 52 | +@click.option( |
| 53 | + "--repository-path", |
| 54 | + type=str, |
| 55 | + default=".", |
| 56 | + show_default=True, |
| 57 | + help=""" |
| 58 | + The repository path to which the generated files |
| 59 | + will be sent. |
| 60 | + If not specified, the repository will be generated to the current working |
| 61 | + directory. |
| 62 | + """, |
| 63 | +) |
| 64 | +def generate( |
| 65 | + baseline_generation_config: str, |
| 66 | + current_generation_config: str, |
| 67 | + repository_path: str, |
| 68 | +): |
| 69 | + """ |
| 70 | + Compare baseline generation config and current generation config and |
| 71 | + generate changed libraries based on current generation config with commit |
| 72 | + history. |
| 73 | +
|
| 74 | + If baseline generation config is not specified but current generation |
| 75 | + config is specified, generate all libraries based on current generation |
| 76 | + config without commit history. |
| 77 | +
|
| 78 | + If current generation config is not specified but baseline generation |
| 79 | + config is specified, raise FileNotFoundError because current generation |
| 80 | + config should be the source of truth of library generation. |
| 81 | +
|
| 82 | + If both baseline generation config and current generation config are not |
| 83 | + specified, generate all libraries based on the default generation config, |
| 84 | + which is generation_config.yaml in the current working directory. Raise |
| 85 | + FileNotFoundError if the default config does not exist. |
| 86 | +
|
| 87 | + The commit history, if generated, will be available in |
| 88 | + repository_path/pr_description.txt. |
| 89 | + """ |
| 90 | + default_generation_config = f"{os.getcwd()}/generation_config.yaml" |
| 91 | + |
| 92 | + if baseline_generation_config is None and current_generation_config is None: |
| 93 | + if not os.path.isfile(default_generation_config): |
| 94 | + raise FileNotFoundError( |
| 95 | + f"{default_generation_config} does not exist. " |
| 96 | + "A valid generation config has to be passed in as " |
| 97 | + "current_generation_config or exist in the current working " |
| 98 | + "directory." |
| 99 | + ) |
| 100 | + current_generation_config = default_generation_config |
| 101 | + elif current_generation_config is None: |
| 102 | + raise FileNotFoundError( |
| 103 | + "current_generation_config is not specified when " |
| 104 | + "baseline_generation_config is specified. " |
| 105 | + "current_generation_config should be the source of truth of " |
| 106 | + "library generation." |
| 107 | + ) |
| 108 | + |
| 109 | + current_generation_config = os.path.abspath(current_generation_config) |
| 110 | + repository_path = os.path.abspath(repository_path) |
| 111 | + if not baseline_generation_config: |
| 112 | + # Execute full generation based on current_generation_config if |
| 113 | + # baseline_generation_config is not specified. |
| 114 | + # Do not generate pull request description. |
| 115 | + generate_from_yaml( |
| 116 | + config=from_yaml(current_generation_config), |
| 117 | + repository_path=repository_path, |
| 118 | + ) |
| 119 | + return |
| 120 | + |
| 121 | + # Compare two generation configs and only generate changed libraries. |
| 122 | + # Generate pull request description. |
| 123 | + baseline_generation_config = os.path.abspath(baseline_generation_config) |
| 124 | + config_change = compare_config( |
| 125 | + baseline_config=from_yaml(baseline_generation_config), |
| 126 | + current_config=from_yaml(current_generation_config), |
| 127 | + ) |
| 128 | + generate_from_yaml( |
| 129 | + config=config_change.current_config, |
| 130 | + repository_path=repository_path, |
| 131 | + target_library_names=config_change.get_changed_libraries(), |
| 132 | + ) |
| 133 | + generate_pr_descriptions( |
| 134 | + config=config_change.current_config, |
| 135 | + baseline_commit=config_change.baseline_config.googleapis_commitish, |
| 136 | + description_path=repository_path, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + main() |
0 commit comments