xref: /aosp_15_r20/external/gson/version_generator.py (revision a8de600362638ea28fd6cb3225451dc706d269bb)
1*a8de6003SAndroid Build Coastguard Worker#!/usr/bin/python3
2*a8de6003SAndroid Build Coastguard Worker
3*a8de6003SAndroid Build Coastguard Worker# Copyright (C) 2022 The Android Open Source Project
4*a8de6003SAndroid Build Coastguard Worker#
5*a8de6003SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*a8de6003SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*a8de6003SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*a8de6003SAndroid Build Coastguard Worker#
9*a8de6003SAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*a8de6003SAndroid Build Coastguard Worker#
11*a8de6003SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*a8de6003SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*a8de6003SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*a8de6003SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*a8de6003SAndroid Build Coastguard Worker# limitations under the License.
16*a8de6003SAndroid Build Coastguard Worker
17*a8de6003SAndroid Build Coastguard Worker# Script that generates the GsonBuildConfig.java file based on the version in
18*a8de6003SAndroid Build Coastguard Worker# pom.xml. It expects paths to pom.xml and the GsonBuildConfig.java template as
19*a8de6003SAndroid Build Coastguard Worker# arguments in any order. It emits the finished file to the standard output.
20*a8de6003SAndroid Build Coastguard Worker
21*a8de6003SAndroid Build Coastguard Workerimport sys
22*a8de6003SAndroid Build Coastguard Workerimport xml.etree.ElementTree as ET
23*a8de6003SAndroid Build Coastguard Worker
24*a8de6003SAndroid Build Coastguard Worker
25*a8de6003SAndroid Build Coastguard Workerdef main():
26*a8de6003SAndroid Build Coastguard Worker  pom_path = next(path for path in sys.argv[1:] if path.endswith(".xml"))
27*a8de6003SAndroid Build Coastguard Worker  java_path = next(path for path in sys.argv[1:] if path.endswith(".java"))
28*a8de6003SAndroid Build Coastguard Worker  pom_file = open(pom_path, "r")
29*a8de6003SAndroid Build Coastguard Worker  pom_xml = ET.parse(pom_file)
30*a8de6003SAndroid Build Coastguard Worker  version = pom_xml.getroot().find("{http://maven.apache.org/POM/4.0.0}version").text
31*a8de6003SAndroid Build Coastguard Worker  java_file = open(java_path, "r")
32*a8de6003SAndroid Build Coastguard Worker  print(java_file.read().replace("${project.version}", version))
33*a8de6003SAndroid Build Coastguard Worker
34*a8de6003SAndroid Build Coastguard Workermain()
35