|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import click |
| 16 | +import logging |
| 17 | + |
| 18 | +from fireci import ci_command |
| 19 | +from fireci import ci_utils |
| 20 | +from fireci import dir_utils |
| 21 | +from typing import Tuple, List, Callable, Union |
| 22 | +from termcolor import colored |
| 23 | + |
| 24 | +log = logging.getLogger('fireci.clean') |
| 25 | + |
| 26 | +@click.argument("projects", |
| 27 | + nargs=-1, |
| 28 | + type=click.Path(), |
| 29 | + required=False |
| 30 | +) |
| 31 | +@click.option('--gradle/--no-gradle', default=False, help="Delete the local .gradle caches.") |
| 32 | +@click.option('--build/--no-build', default=True, help="Delete the local build caches.") |
| 33 | +@click.option('--transforms/--no-transforms', default=False, help="Delete the system-wide transforms cache.") |
| 34 | +@click.option('--build-cache/--no-build-cache', default=False, help="Delete the system-wide build cache.") |
| 35 | + |
| 36 | +@click.option('--deep/--no-deep', default=False, help="Delete all of the system-wide files for gradle.") |
| 37 | +@click.option('--cache/--no-cache', default=False, help="Delete all of the system-wide caches for gradle.") |
| 38 | +@ci_command(epilog=""" |
| 39 | + Clean a subset of projects: |
| 40 | +
|
| 41 | + \b |
| 42 | + $ fireci clean firebase-common |
| 43 | + $ fireci clean firebase-common firebase-vertexai |
| 44 | +
|
| 45 | + Clean all projects: |
| 46 | +
|
| 47 | + $ fireci clean |
| 48 | +""") |
| 49 | +def clean(projects, gradle, build, transforms, build_cache, deep, cache): |
| 50 | + """ |
| 51 | + Delete files cached by gradle. |
| 52 | +
|
| 53 | + Alternative to the standard `gradlew clean`, which runs outside the scope of gradle, |
| 54 | + and provides deeper cache cleaning capabilities. |
| 55 | + """ |
| 56 | + if not projects: |
| 57 | + log.debug("No projects specified, so we're defaulting to all projects.") |
| 58 | + projects = ci_utils.get_projects() |
| 59 | + |
| 60 | + cache = cache or deep |
| 61 | + gradle = gradle or cache |
| 62 | + |
| 63 | + cleaners = [] |
| 64 | + |
| 65 | + if build: |
| 66 | + cleaners.append(delete_build) |
| 67 | + if gradle: |
| 68 | + cleaners.append(delete_gradle) |
| 69 | + |
| 70 | + results = [call_and_sum(projects, cleaner) for cleaner in cleaners] |
| 71 | + local_count = tuple(map(sum, zip(*results))) |
| 72 | + |
| 73 | + cleaners = [] |
| 74 | + |
| 75 | + if deep: |
| 76 | + cleaners.append(delete_deep) |
| 77 | + elif cache: |
| 78 | + cleaners.append(delete_cache) |
| 79 | + else: |
| 80 | + if transforms: |
| 81 | + cleaners.append(delete_transforms) |
| 82 | + if build_cache: |
| 83 | + cleaners.append(delete_build_cache) |
| 84 | + |
| 85 | + results = [cleaner() for cleaner in cleaners] |
| 86 | + system_count = ci_utils.counts(results) |
| 87 | + |
| 88 | + [deleted, skipped] = tuple(a + b for a, b in zip(local_count, system_count)) |
| 89 | + |
| 90 | + log.info(f""" |
| 91 | + Clean results: |
| 92 | +
|
| 93 | + {colored("Deleted:", None, attrs=["bold"])} {colored(deleted, "red")} |
| 94 | + {colored("Already deleted:", None, attrs=["bold"])} {colored(skipped, "grey")} |
| 95 | + """) |
| 96 | + |
| 97 | + |
| 98 | +def call_and_sum(variables: List[str], func: Callable[[str], Union[bool, int]]) -> Tuple[int, int]: |
| 99 | + results = list(map(lambda var: func(var), variables)) |
| 100 | + return ci_utils.counts(results) |
| 101 | + |
| 102 | +def delete_build(dir: str) -> bool: |
| 103 | + return dir_utils.rmdir(f"{dir}/build") |
| 104 | + |
| 105 | +def delete_gradle(dir: str) -> bool: |
| 106 | + return dir_utils.rmdir(f"{dir}/.gradle") |
| 107 | + |
| 108 | +def delete_transforms() -> int: |
| 109 | + return dir_utils.rmglob("~/.gradle/caches/transforms-*") |
| 110 | + |
| 111 | +def delete_build_cache() -> int: |
| 112 | + return dir_utils.rmglob("~/.gradle/caches/build-cache-*") |
| 113 | + |
| 114 | +def delete_deep() -> bool: |
| 115 | + return dir_utils.rmdir("~/.gradle") |
| 116 | + |
| 117 | +def delete_cache() -> bool: |
| 118 | + return dir_utils.rmdir("~/.gradle/caches") |
0 commit comments