|  | #!/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 urllib.request | 
|  |  | 
|  | import gradle | 
|  | import utils | 
|  | import create_maven_release | 
|  | import archive_desugar_jdk_libs | 
|  |  | 
|  |  | 
|  | class Variant(Enum): | 
|  | jdk8 = 'jdk8' | 
|  | jdk11_legacy = 'jdk11_legacy' | 
|  | jdk11_minimal = 'jdk11_minimal' | 
|  | jdk11 = 'jdk11' | 
|  | jdk11_nio = 'jdk11_nio' | 
|  |  | 
|  | 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('--variant', type=Variant, choices=list(Variant)) | 
|  | 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.') | 
|  | parser.add_argument( | 
|  | '--desugar-jdk-libs-revision', | 
|  | '--desugar_jdk_libs_revision', | 
|  | default=None, | 
|  | metavar=('<revision>'), | 
|  | help='Revision of github.com/google/desugar_jdk_libs to use.') | 
|  | parser.add_argument( | 
|  | '--release-version', | 
|  | '--release_version', | 
|  | metavar=('<version>'), | 
|  | help= | 
|  | 'The desugared library release version to use. This will pull from the archived releases' | 
|  | ) | 
|  | 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 run(args): | 
|  | artifact = None | 
|  | configuration_artifact = None | 
|  | configuration = None | 
|  | conversions = None | 
|  | implementation = None | 
|  | version_file_name = None | 
|  | implementation_build_target = None | 
|  | implementation_maven_zip = None | 
|  | release_archive_location = None | 
|  | match args.variant: | 
|  | case Variant.jdk8: | 
|  | artifact = 'desugar_jdk_libs' | 
|  | configuration_artifact = 'desugar_jdk_libs_configuration' | 
|  | configuration = utils.DESUGAR_CONFIGURATION | 
|  | conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP | 
|  | implementation = utils.DESUGAR_IMPLEMENTATION | 
|  | version_file_name = 'VERSION.txt' | 
|  | implementation_build_target = ':maven_release' | 
|  | implementation_maven_zip = 'desugar_jdk_libs.zip' | 
|  | release_archive_location = 'desugar_jdk_libs' | 
|  | case Variant.jdk11_legacy: | 
|  | artifact = 'desugar_jdk_libs' | 
|  | configuration_artifact = 'desugar_jdk_libs_configuration' | 
|  | configuration = utils.DESUGAR_CONFIGURATION_JDK11_LEGACY | 
|  | conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_LEGACY_ZIP | 
|  | implementation = utils.DESUGAR_IMPLEMENTATION_JDK11 | 
|  | version_file_name = 'VERSION_JDK11_LEGACY.txt' | 
|  | implementation_build_target = ':maven_release_jdk11_legacy' | 
|  | implementation_maven_zip = 'desugar_jdk_libs_jdk11_legacy.zip' | 
|  | release_archive_location = 'desugar_jdk_libs' | 
|  | case Variant.jdk11_minimal: | 
|  | artifact = 'desugar_jdk_libs_minimal' | 
|  | configuration_artifact = 'desugar_jdk_libs_configuration_minimal' | 
|  | configuration = utils.DESUGAR_CONFIGURATION_JDK11_MINIMAL | 
|  | conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP | 
|  | implementation = utils.DESUGAR_IMPLEMENTATION_JDK11 | 
|  | version_file_name = 'VERSION_JDK11_MINIMAL.txt' | 
|  | implementation_build_target = ':maven_release_jdk11_minimal' | 
|  | implementation_maven_zip = 'desugar_jdk_libs_jdk11_minimal.zip' | 
|  | release_archive_location = 'desugar_jdk_libs_minimal' | 
|  | case Variant.jdk11: | 
|  | artifact = 'desugar_jdk_libs' | 
|  | configuration_artifact = 'desugar_jdk_libs_configuration' | 
|  | configuration = utils.DESUGAR_CONFIGURATION_JDK11 | 
|  | conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP | 
|  | implementation = utils.DESUGAR_IMPLEMENTATION_JDK11 | 
|  | version_file_name = 'VERSION_JDK11.txt' | 
|  | implementation_build_target = ':maven_release_jdk11' | 
|  | implementation_maven_zip = 'desugar_jdk_libs_jdk11.zip' | 
|  | release_archive_location = 'desugar_jdk_libs' | 
|  | case Variant.jdk11_nio: | 
|  | artifact = 'desugar_jdk_libs_nio' | 
|  | configuration_artifact = 'desugar_jdk_libs_configuration_nio' | 
|  | configuration = utils.DESUGAR_CONFIGURATION_JDK11_NIO | 
|  | conversions = utils.LIBRARY_DESUGAR_CONVERSIONS_ZIP | 
|  | implementation = utils.DESUGAR_IMPLEMENTATION_JDK11 | 
|  | version_file_name = 'VERSION_JDK11_NIO.txt' | 
|  | implementation_build_target = ':maven_release_jdk11_nio' | 
|  | implementation_maven_zip = 'desugar_jdk_libs_jdk11_nio.zip' | 
|  | release_archive_location = 'desugar_jdk_libs_nio' | 
|  | implementation_build_output = join('bazel-bin', implementation_maven_zip) | 
|  | gradle.RunGradle([utils.GRADLE_TASK_R8]) | 
|  | with utils.TempDir() as tmp_dir: | 
|  | (name, | 
|  | configuration_version) = utils.desugar_configuration_name_and_version( | 
|  | configuration, False) | 
|  | if (args.release_version != None and | 
|  | args.release_version != configuration_version): | 
|  | raise Exception( | 
|  | 'Configuration version %s is different for specified version %s' | 
|  | % (configuration_version, version)) | 
|  | version = configuration_version | 
|  | print("Name: %s" % name) | 
|  | print("Version: %s" % version) | 
|  | # 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 args.release_version and not use_existing_checkout): | 
|  | subprocess.check_call([ | 
|  | 'git', 'clone', | 
|  | 'https://github.com/google/desugar_jdk_libs.git', checkout_dir | 
|  | ]) | 
|  | if (args.desugar_jdk_libs_revision): | 
|  | subprocess.check_call([ | 
|  | 'git', '-C', checkout_dir, 'checkout', | 
|  | args.desugar_jdk_libs_revision | 
|  | ]) | 
|  | if not args.release_version: | 
|  | with utils.ChangedWorkingDirectory(checkout_dir): | 
|  | with open(version_file_name) 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 | 
|  | + " in '" + version_file_name | 
|  | + "'. If testing a new version use " | 
|  | + "--desugar-jdk-libs-checkout with updated " | 
|  | + "VERSION_*.txt and DEPENDENCIES_*.txt files.") | 
|  |  | 
|  | # 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, conversions) | 
|  | unzip_dir = join(tmp_dir, 'desugar_jdk_libs_configuration_unzipped') | 
|  | cmd = ['unzip', '-q', maven_zip, '-d', unzip_dir] | 
|  | subprocess.check_call(cmd) | 
|  | cmd = [ | 
|  | 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root, | 
|  | '-DrepositoryId=someName', | 
|  | '-Dfile=' + jar_file(unzip_dir, configuration_artifact, version), | 
|  | '-DpomFile=' + pom_file(unzip_dir, configuration_artifact, version) | 
|  | ] | 
|  | subprocess.check_call(cmd) | 
|  |  | 
|  | undesugared_if_needed = None | 
|  | if not args.release_version: | 
|  | # Build desugared library. | 
|  | print("Building desugared library " + version) | 
|  | with utils.ChangedWorkingDirectory(checkout_dir): | 
|  | subprocess.check_call([ | 
|  | 'bazel', '--bazelrc=/dev/null', 'build', | 
|  | '--spawn_strategy=local', '--verbose_failures', | 
|  | implementation_build_target | 
|  | ]) | 
|  |  | 
|  | # Undesugar desugared library if needed. | 
|  | undesugared_if_needed = join(checkout_dir, | 
|  | implementation_build_output) | 
|  | if (args.variant == Variant.jdk11_minimal or | 
|  | args.variant == Variant.jdk11 or | 
|  | args.variant == Variant.jdk11_nio): | 
|  | undesugared_if_needed = join(tmp_dir, 'undesugared.zip') | 
|  | archive_desugar_jdk_libs.Undesugar( | 
|  | str(args.variant), | 
|  | join(checkout_dir, implementation_build_output), version, | 
|  | undesugared_if_needed) | 
|  | else: | 
|  | # Download the already built and undesugared library from release archive. | 
|  | undesugared_if_needed = join(tmp_dir, implementation_maven_zip) | 
|  | urllib.request.urlretrieve( | 
|  | ('https://storage.googleapis.com/r8-releases/raw/%s/%s/%s' % | 
|  | (release_archive_location, version, implementation_maven_zip)), | 
|  | undesugared_if_needed) | 
|  |  | 
|  | unzip_dir = join(tmp_dir, 'desugar_jdk_libs_unzipped') | 
|  | cmd = ['unzip', '-q', undesugared_if_needed, '-d', unzip_dir] | 
|  | subprocess.check_call(cmd) | 
|  | cmd = [ | 
|  | 'mvn', 'deploy:deploy-file', '-Durl=file:' + args.repo_root, | 
|  | '-DrepositoryId=someName', | 
|  | '-Dfile=' + jar_file(unzip_dir, artifact, version), | 
|  | '-DpomFile=' + pom_file(unzip_dir, artifact, version) | 
|  | ] | 
|  | subprocess.check_call(cmd) | 
|  |  | 
|  | print() | 
|  | print("Artifacts:") | 
|  | print("  com.android.tools:%s:%s" % (configuration_artifact, version)) | 
|  | print("  com.android.tools:%s:%s" % (artifact, version)) | 
|  | print() | 
|  | print("deployed to Maven repository at " + args.repo_root + ".") | 
|  | print() | 
|  | print("For Kotlin Script add") | 
|  | print() | 
|  | print("  maven(url = \"file:///tmp/repo\")") | 
|  | print() | 
|  | print( | 
|  | "to dependencyResolutionManagement.repositories in settings.gradle.kts, and use" | 
|  | ) | 
|  | print( | 
|  | 'the "changing" property of the coreLibraryDesugaring dependency:') | 
|  | print() | 
|  | print("  coreLibraryDesugaring('com.android.tools:%s:%s') {" % | 
|  | (artifact, version)) | 
|  | print("    isChanging = true") | 
|  | print("  }") | 
|  | print() | 
|  |  | 
|  | print("For Groovy 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:%s:%s') {" % | 
|  | (artifact, version)) | 
|  | print("    changing = true") | 
|  | print("  }") | 
|  | print() | 
|  | print( | 
|  | 'If not using the "changing" property remember to run gradle with ' + | 
|  | "--refresh-dependencies (./gradlew --refresh-dependencies ...) " + | 
|  | "to ensure the cache is not used when the same version is published." | 
|  | + "multiple times.") | 
|  |  | 
|  |  | 
|  | def main(): | 
|  | args = parse_options() | 
|  | if args.desugar_jdk_libs_checkout and args.release_version: | 
|  | raise Exception( | 
|  | 'Options --desugar-jdk-libs-checkout and --release-version are mutually exclusive' | 
|  | ) | 
|  | if args.desugar_jdk_libs_revision and args.release_version: | 
|  | raise Exception( | 
|  | 'Options --desugar-jdk-libs-revision and --release-version are mutually exclusive' | 
|  | ) | 
|  | if args.desugar_jdk_libs_checkout and args.desugar_jdk_libs_revision: | 
|  | raise Exception( | 
|  | 'Options --desugar-jdk-libs-checkout and --desugar-jdk-libs-revision are mutually exclusive' | 
|  | ) | 
|  | if args.clear_repo: | 
|  | shutil.rmtree(args.repo_root, ignore_errors=True) | 
|  | utils.makedirs_if_needed(args.repo_root) | 
|  | if (args.variant): | 
|  | run(args) | 
|  | else: | 
|  | for v in Variant: | 
|  | args.variant = v | 
|  | run(args) | 
|  |  | 
|  |  | 
|  | if __name__ == '__main__': | 
|  | sys.exit(main()) |