Last active
August 22, 2024 22:21
-
-
Save barrownicholas/4532cc8187ed47319c96ccf61e901f72 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Patch a `@openapitools/openapi-generator-cli` client for java (for publishing to GitHub packages) | |
# Example Usage: | |
# python3 -m patch.py --org=github-org-or-username --repo=my-cool-repo --pom=./generated/pom.xml | |
import argparse | |
import os.path | |
from xml.etree.ElementTree import parse, register_namespace, SubElement, Element, tostring | |
from xml.dom import minidom | |
from dataclasses import dataclass | |
@dataclass | |
class RepoInfo: | |
pom: str | |
org: str | |
repo: str | |
def patch_pom(repo: RepoInfo): | |
tree = parse(repo.pom) | |
root = tree.getroot() | |
# Define namespaces (if any) and register them | |
namespaces = {'': 'http://maven.apache.org/POM/4.0.0'} | |
register_namespace('', namespaces['']) | |
# Create the new XML structure | |
distribution_management = Element('distributionManagement') | |
repository = SubElement(distribution_management, 'repository') | |
repo_id = SubElement(repository, 'id') | |
repo_id.text = 'github' | |
name = SubElement(repository, 'name') | |
name.text = 'GitHub Apache Maven Packages' | |
url = SubElement(repository, 'url') | |
url.text = f'https://maven.pkg.github.com/{repo.org}/{repo.repo}' | |
# Find the project element and add the new XML structure | |
project = root.find('{http://maven.apache.org/POM/4.0.0}project') | |
if project is None: | |
project = root | |
project.append(distribution_management) | |
# Convert the tree to a string and write it back without XML declaration | |
xml_str = tostring(root, encoding='utf-8').decode('utf-8') | |
xml_pretty_str = minidom.parseString(xml_str).toprettyxml(indent=" ", encoding='utf-8').decode('utf-8') | |
with open(repo.pom, 'w', encoding='utf-8') as f: | |
f.write(xml_pretty_str) | |
def main(args: argparse.Namespace) -> None: | |
# validate path exists | |
if not os.path.exists(args.pom) or not os.path.isfile(args.pom): | |
raise RuntimeError(f"'{args.pom}' pom.xml file does not exist") | |
patch_pom(RepoInfo(pom=args.pom, org=args.org, repo=args.repo)) | |
if __name__ == '__main__': | |
# Create the parser | |
parser = argparse.ArgumentParser( | |
description='Patch a @openapitools/openapi-generator-cli generated pom.xml for publishing') | |
# Add arguments | |
parser.add_argument('--pom', type=str, required=True, help='Path to the POM file.') | |
parser.add_argument('--org', type=str, required=True, help='Organization name.') | |
parser.add_argument('--repo', type=str, required=True, help='Repository name.') | |
# Parse the arguments | |
main(parser.parse_args()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment