Last active
August 28, 2019 13:37
-
-
Save eugene-krivobokov/d848775e2dbbb3ad16f32422b25a7b8e to your computer and use it in GitHub Desktop.
Android project cleaner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# | |
# THIS IS AN EXAMPLE. USE IT ON YOUR OWN RISK ! | |
# | |
import argparse | |
import glob | |
import os | |
import shutil | |
import subprocess | |
from pathlib import Path | |
android_studio_gradle_config_filename = '.idea/gradle.xml' | |
android_studio_modules_config_filename = '.idea/modules.xml' | |
android_studio_caches_path = '.idea/caches/' | |
project_gradle_path = '.gradle/' | |
global_build_cache_path = '.gradle/build-cache/' | |
mirakle_host = '___INSERT HERE YOUR MAINFRAMER HOST___' | |
mirakle_folder_path_relative = '/.mirakle' | |
# Gradle default build cache directory: https://guides.gradle.org/using-build-cache/#fully_cached_builds | |
shared_gradle_cache_path_relative = '/.gradle/caches/build-cache-1' | |
# Android default build cache directory: https://developer.android.com/studio/build/build-cache.html#change_cache_location | |
shared_android_build_cache_path_relative = '/.android/build-cache' | |
shared_gradle_cache_path = str(Path.home()) + shared_gradle_cache_path_relative | |
shared_build_cache_path = str(Path.home()) + shared_android_build_cache_path_relative | |
def system_call(command): | |
open = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True) | |
out = open.stdout.read() | |
print(out.decode('utf8')) | |
def main(remove_local=False, remove_remote=False, username=None): | |
if remove_local: | |
if os.path.exists(android_studio_caches_path): | |
shutil.rmtree(android_studio_caches_path) | |
if os.path.exists(android_studio_gradle_config_filename): | |
os.remove(android_studio_gradle_config_filename) | |
if os.path.exists(android_studio_modules_config_filename): | |
os.remove(android_studio_modules_config_filename) | |
for filename in glob.iglob('**/*.iml', recursive=True): | |
os.remove(filename) | |
for build_path in glob.iglob('**/build', recursive=True): | |
shutil.rmtree(build_path) | |
if os.path.exists(project_gradle_path): | |
shutil.rmtree(project_gradle_path) | |
if os.path.exists(global_build_cache_path): | |
shutil.rmtree(global_build_cache_path) | |
if os.path.exists(shared_gradle_cache_path): | |
shutil.rmtree(shared_gradle_cache_path) | |
if os.path.exists(shared_build_cache_path): | |
shutil.rmtree(shared_build_cache_path) | |
system_call("./gradlew --stop") | |
if remove_remote: | |
ssh_command_pattern = "ssh {}".format(username + "@" if username else "") + mirakle_host | |
system_call("{} 'rm -rf {}'".format(ssh_command_pattern, "~" + shared_gradle_cache_path_relative)) | |
system_call("{} 'rm -rf {}'".format(ssh_command_pattern, "~" + shared_android_build_cache_path_relative)) | |
system_call("{} 'rm -rf {}'".format(ssh_command_pattern, "~" + mirakle_folder_path_relative)) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Clears Android project') | |
parser.add_argument('-l', '--local', action="store_true", help="Clean local") | |
parser.add_argument('-r', '--remote', action="store_true", | |
help="Clean remote. Specify '-u' argument, if your local machine username differs from " | |
"username") | |
parser.add_argument('-u', '--username', action="store", help="Username for ssh connection. 'iiivanov' for example") | |
parser.add_argument('-a', '--all', action="store_true", | |
help="Clean project(local and mainframer). Contains '-l' and '-r' options") | |
arguments = parser.parse_args() | |
need_to_remove_local = arguments.all or arguments.local | |
need_to_remove_remote = arguments.all or arguments.remote | |
username = arguments.username | |
if not need_to_remove_local and not need_to_remove_remote: | |
print(parser.parse_args(['-h'])) | |
else: | |
main(need_to_remove_local, | |
need_to_remove_remote, | |
username) | |
print("Project is cleaned. Now sync project with Android Studio") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment