pommer new

This commit is contained in:
rattatwinko
2025-05-09 16:56:02 +02:00
parent 274a63aa14
commit 160c25c7cc
3 changed files with 95 additions and 92 deletions

131
pommer.py
View File

@@ -1,47 +1,7 @@
import xml.etree.ElementTree as ET
import os
import subprocess
import sys
import argparse
from pathlib import Path
def install_maven(maven_version="3.9.6"):
"""Install Maven dynamically if not found"""
try:
subprocess.run(["mvn", "--version"], check=True, capture_output=True)
print("✓ Maven already installed")
except (subprocess.CalledProcessError, FileNotFoundError):
print("⏳ Installing Maven...")
maven_url = f"https://archive.apache.org/dist/maven/maven-3/{maven_version}/binaries/apache-maven-{maven_version}-bin.tar.gz"
install_cmds = [
f"curl -sL {maven_url} | tar xz -C /opt",
f"ln -s /opt/apache-maven-{maven_version}/bin/mvn /usr/local/bin/mvn",
"rm -rf /tmp/*"
]
for cmd in install_cmds:
subprocess.run(cmd, shell=True, check=True)
print("✓ Maven installed")
def install_java(java_version="21"):
"""Install JDK dynamically if not found"""
try:
jdk_check = subprocess.run(["java", "--version"], capture_output=True, text=True)
if f" {java_version}" in jdk_check.stdout:
print(f"✓ JDK {java_version} already installed")
return
except FileNotFoundError:
pass
print(f"⏳ Installing JDK {java_version}...")
subprocess.run([
"apt-get", "update", "-qq"
], check=True)
subprocess.run([
"apt-get", "install", "-qq", "-y",
f"temurin-{java_version}-jdk"
], check=True)
print(f"✓ JDK {java_version} installed")
import argparse
import os
def analyze_pom(pom_path):
"""Detect required tools from pom.xml"""
@@ -63,41 +23,76 @@ def analyze_pom(pom_path):
return requirements
def optimize_build(pom_path):
"""Apply build optimizations based on project type"""
optimizations = [
"-T 1C", # Parallel builds (1 thread per core)
"-Ddependency.cache=true",
"-Dmaven.artifact.threads=8"
]
def generate_gitea_actions(pom_path, requirements):
"""Generate Gitea Actions workflow file"""
workflow_dir = Path(".gitea/workflows")
workflow_dir.mkdir(parents=True, exist_ok=True)
if "kotlin" in pom_path.read_text():
optimizations.append("-Dkotlin.compiler.incremental=true")
workflow_file = workflow_dir / "build.yml"
return " ".join(optimizations)
java_version = requirements['java_version']
kotlin_version = requirements['kotlin_version']
needs_docker = requirements['needs_docker']
# Base Maven command with optimizations
maven_cmd = "mvn -B -T 1C -Ddependency.cache=true -Dmaven.artifact.threads=8"
if kotlin_version:
maven_cmd += " -Dkotlin.compiler.incremental=true"
content = f"""name: Java CI with Maven
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK {java_version}
uses: actions/setup-java@v3
with:
java-version: '{java_version}'
distribution: 'temurin'
cache: 'maven'
- name: Install Maven
run: |
sudo apt-get update -qq
sudo apt-get install -y maven
mvn --version
"""
if needs_docker:
content += """
- name: Set up Docker
run: |
sudo apt-get install -y docker.io
sudo systemctl start docker
"""
content += f"""
- name: Build with Maven
run: {maven_cmd} clean package
"""
with open(workflow_file, "w") as f:
f.write(content)
print(f"✓ Generated Gitea Actions workflow at {workflow_file}")
def main():
parser = argparse.ArgumentParser(description='Smart Maven project builder')
parser = argparse.ArgumentParser(description='Generate Gitea Actions workflow for Maven project')
parser.add_argument('pom_path', type=str, help='Path to pom.xml')
parser.add_argument('--skip-install', action='store_true', help='Skip dependency installation')
args = parser.parse_args()
pom_path = Path(args.pom_path)
if not pom_path.exists():
print(f"❌ Error: {pom_path} does not exist")
return
requirements = analyze_pom(pom_path)
if not args.skip_install:
install_java(requirements['java_version'])
install_maven()
build_cmd = f"mvn -B {optimize_build(pom_path)} clean package"
print(f"🚀 Build command: {build_cmd}")
try:
subprocess.run(build_cmd, shell=True, check=True)
print("✅ Build succeeded!")
except subprocess.CalledProcessError as e:
print(f"❌ Build failed: {e}")
sys.exit(1)
generate_gitea_actions(pom_path, requirements)
if __name__ == "__main__":
main()