|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import logging |
| 5 | +from pathlib import Path |
| 6 | +from typing import Set |
| 7 | + |
| 8 | +DEFAULT_VERSION = "1.0.0" |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | +logging.basicConfig( |
| 12 | + level=logging.INFO, |
| 13 | + format="%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s", |
| 14 | + datefmt="%Y-%m-%dT%H:%M:%S", |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def main(properties_file: Path, projects: Set[str]): |
| 19 | + if not properties_file.is_file(): |
| 20 | + raise ValueError(f"Properties file {properties_file} does not exist") |
| 21 | + |
| 22 | + properties = read_properties(properties_file) |
| 23 | + |
| 24 | + for project in sorted(projects): |
| 25 | + project_key = f"model.{project}.version" |
| 26 | + if project_key not in properties: |
| 27 | + logger.info( |
| 28 | + f"Project ({project_key}) not found in properties file, setting to ({DEFAULT_VERSION})" |
| 29 | + ) |
| 30 | + properties[project_key] = DEFAULT_VERSION |
| 31 | + else: |
| 32 | + version = properties[project_key] |
| 33 | + major, minor, patch = version.split(".") |
| 34 | + patch = int(patch) + 1 |
| 35 | + properties[project_key] = f"{major}.{minor}.{patch}" |
| 36 | + logger.info( |
| 37 | + f"Updated version for ({project_key}) to ({properties[project_key]})" |
| 38 | + ) |
| 39 | + properties[project_key] = properties[project_key] |
| 40 | + write_properties(properties_file, properties) |
| 41 | + |
| 42 | + |
| 43 | +def read_properties(properties_file: Path) -> dict: |
| 44 | + lines = properties_file.read_text().split() |
| 45 | + # read each line, if it is empty, skip it |
| 46 | + properties = {} |
| 47 | + for line in lines: |
| 48 | + if line.isspace(): |
| 49 | + continue |
| 50 | + # split the line on the first '=' |
| 51 | + key, value = line.split("=", 1) |
| 52 | + properties[key.strip()] = value.strip() |
| 53 | + return properties |
| 54 | + |
| 55 | + |
| 56 | +def write_properties(properties_file: Path, properties: dict): |
| 57 | + properties_text = "\n".join([f"{key}={value}" for key, value in properties.items()]) |
| 58 | + properties_file.write_text(properties_text) |
| 59 | + |
| 60 | + |
| 61 | +if __name__ == "__main__": |
| 62 | + parser = argparse.ArgumentParser( |
| 63 | + description="Update versions of service model packages controlled via gradle.properties" |
| 64 | + ) |
| 65 | + |
| 66 | + parser.add_argument( |
| 67 | + "--properties", |
| 68 | + "-p", |
| 69 | + dest="properties", |
| 70 | + type=Path, |
| 71 | + required=True, |
| 72 | + help="The gradle.properties file that contains the versions.", |
| 73 | + ) |
| 74 | + |
| 75 | + parser.add_argument( |
| 76 | + "--services", |
| 77 | + "-s", |
| 78 | + dest="services", |
| 79 | + type=str, |
| 80 | + nargs="+", |
| 81 | + required=True, |
| 82 | + help="A list of projects to update the versions of.", |
| 83 | + ) |
| 84 | + |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + main(args.properties, set(args.services)) |
0 commit comments