| #!/usr/bin/env python3 |
| # Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file |
| # for details. All rights reserved. Use of this source code is governed by a |
| # BSD-style license that can be found in the LICENSE file. |
| |
| import argparse |
| from enum import Enum |
| import os |
| from os.path import join |
| import shutil |
| import subprocess |
| import sys |
| |
| import utils |
| import create_maven_release |
| |
| class Configuration(Enum): |
| jdk8 = 'jdk8' |
| jdk11_legacy = 'jdk11-legacy' |
| |
| def __str__(self): |
| return self.value |
| |
| def parse_options(): |
| parser = argparse.ArgumentParser( |
| description='Local desugared library repository for desugared library configurations') |
| parser.add_argument('--repo-root', '--repo_root', |
| default='/tmp/repo', |
| metavar=('<path>'), |
| help='Location for Maven repository.') |
| parser.add_argument('--clear-repo', '--clear_repo', |
| default=False, |
| action='store_true', |
| help='Clear the Maven repository so it only has one version present') |
| parser.add_argument('--configuration', default='jdk8', type=Configuration, choices=list(Configuration)) |
| parser.add_argument('--desugar-jdk-libs-checkout', '--desugar_jdk_libs_checkout', |
| default=None, |
| metavar=('<path>'), |
| help='Use existing checkout of github.com/google/desugar_jdk_libs.') |
| args = parser.parse_args() |
| return args |
| |
| def jar_or_pom_file(unzip_dir, artifact, version, extension): |
| return join( |
| unzip_dir, |
| 'com', |
| 'android', |
| 'tools', |
| artifact, |
| version, |
| artifact + '-' + version + '.' + extension) |
| |
| def jar_file(unzip_dir, artifact, version): |
| return jar_or_pom_file(unzip_dir, artifact, version, 'jar') |
| |
| def pom_file(unzip_dir, artifact, version): |
| return jar_or_pom_file(unzip_dir, artifact, version, 'pom') |
| |
| def main(): |
| args = parse_options() |
| if args.clear_repo: |
| shutil.rmtree(args.repo_root, ignore_errors=True) |
| utils.makedirs_if_needed(args.repo_root) |
| configuration = (utils.DESUGAR_CONFIGURATION |
| if args.configuration is Configuration.jdk8 |
| else utils.DESUGAR_CONFIGURATION_JDK11_LEGACY) |
| implementation = (utils.DESUGAR_IMPLEMENTATION |
| if args.configuration is Configuration.jdk8 |
| else utils.DESUGAR_IMPLEMENTATION_JDK11) |
| version_file = ('VERSION.txt' |
| if args.configuration is Configuration.jdk8 else |
| 'VERSION_JDK11.txt') |
| with utils.TempDir() as tmp_dir: |
| version = utils.desugar_configuration_version(configuration) |
| # Checkout desugar_jdk_libs from GitHub |
| use_existing_checkout = args.desugar_jdk_libs_checkout != None |
| checkout_dir = (args.desugar_jdk_libs_checkout |
| if use_existing_checkout |
| else join(tmp_dir, 'desugar_jdk_libs')) |
| if (not use_existing_checkout): |
| utils.RunCmd(['git', 'clone', 'https://github.com/google/desugar_jdk_libs.git', checkout_dir]) |
| with utils.ChangedWorkingDirectory(checkout_dir): |
| with open(version_file) as version_file: |
| version_file_lines = version_file.readlines() |
| for line in version_file_lines: |
| if not line.startswith('#'): |
| desugar_jdk_libs_version = line.strip() |
| if (version != desugar_jdk_libs_version): |
| raise Exception( |
| "Version mismatch. Configuration has version '" |
| + version |
| + "', and desugar_jdk_libs has version '" |
| + desugar_jdk_libs_version |
| + "'") |
| |
| # Build desugared library configuration. |
| print("Building desugared library configuration " + version) |
| maven_zip = join(tmp_dir, 'desugar_configuration.zip') |
| create_maven_release.generate_desugar_configuration_maven_zip( |
| maven_zip, |
| configuration, |
| implementation) |
| unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped') |
| cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir] |
| utils.RunCmd(cmd) |
| cmd = [ |
| 'mvn', |
| 'deploy:deploy-file', |
| '-Durl=file:' + args.repo_root, |
| '-DrepositoryId=someName', |
| '-Dfile=' + jar_file(unzip_dir, 'desugar_jdk_libs_configuration', version), |
| '-DpomFile=' + pom_file(unzip_dir, 'desugar_jdk_libs_configuration', version)] |
| utils.RunCmd(cmd) |
| |
| # Build desugared library. |
| print("Building desugared library " + version) |
| with utils.ChangedWorkingDirectory(checkout_dir): |
| utils.RunCmd([ |
| 'bazel', |
| '--bazelrc=/dev/null', |
| 'build', |
| '--spawn_strategy=local', |
| '--verbose_failures', |
| (':maven_release' |
| if args.configuration is Configuration.jdk8 |
| else ':maven_release_jdk11')]) |
| unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped') |
| cmd = [ |
| 'unzip', |
| '-q', |
| join(checkout_dir, |
| 'bazel-bin', |
| ('desugar_jdk_libs.zip' |
| if args.configuration is Configuration.jdk8 |
| else 'desugar_jdk_libs_jdk11.zip')), |
| '-d', |
| unzip_dir] |
| utils.RunCmd(cmd) |
| cmd = [ |
| 'mvn', |
| 'deploy:deploy-file', |
| '-Durl=file:' + args.repo_root, |
| '-DrepositoryId=someName', |
| '-Dfile=' + jar_file(unzip_dir, 'desugar_jdk_libs', version), |
| '-DpomFile=' + pom_file(unzip_dir, 'desugar_jdk_libs', version)] |
| utils.RunCmd(cmd) |
| |
| print() |
| print("Artifacts:") |
| print(" com.android.tools:desugar_jdk_libs_configuration:" + version) |
| print(" com.android.tools:desugar_jdk_libs:" + version) |
| print() |
| print("deployed to Maven repository at " + args.repo_root + ".") |
| print() |
| print("Add") |
| print() |
| print(" maven {") |
| print(" url uri('file://" + args.repo_root + "')") |
| print(" }") |
| print() |
| print("to dependencyResolutionManagement.repositories in settings.gradle, and use") |
| print('the "changing" property of the coreLibraryDesugaring dependency:') |
| print() |
| print(" coreLibraryDesugaring('com.android.tools:desugar_jdk_libs:" + version + "') {") |
| print(" changing = true") |
| print(" }") |
| print() |
| print('If not using the !changing" propertyRemember to run gradle with ' |
| + " --refresh-dependencies (./gradlew --refresh-dependencies ...) " |
| + "to ensure the cache is not used when the same version is published." |
| + "multiple times.") |
| |
| if __name__ == '__main__': |
| sys.exit(main()) |