Last active
October 2, 2023 05:57
-
-
Save hucsmn/a7be090d97820292b6230c2382343dce to your computer and use it in GitHub Desktop.
batch configuring JDK for JetBrains productions
This file contains hidden or 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 | |
import os | |
import argparse | |
profile_jdk_mapping = { | |
'IntelliJIdea': 'idea.jdk', | |
'WebStorm': 'webstorm.jdk', | |
'PyCharm': 'pycharm.jdk', | |
'CLion': 'clion.jdk', | |
'GoLand': 'goland.jdk', | |
'Rider': 'rider.jdk', | |
'RustRover': 'rustrover.jdk', | |
} | |
def search_jdk_config_files(): | |
jdk_config_files = [] | |
config_root = os.path.expanduser('~/.config/JetBrains') | |
for profile_dir in os.listdir(config_root): | |
jdk_config_file = None | |
for profile_prefix, jdk_config in profile_jdk_mapping.items(): | |
if profile_dir.startswith(profile_prefix): | |
jdk_config_file = config_root + '/'+ profile_dir + '/' + jdk_config | |
break | |
if jdk_config_file: | |
jdk_config_files.append(jdk_config_file) | |
return jdk_config_files | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='batch configuring JDK for JetBrains productions') | |
parser.add_argument('-c', '--clean', dest='clean', action='store_true', default=False, | |
help='clean all the JDK configuration') | |
parser.add_argument('-s', '--set', type=str, dest='jdk_path', action='store', default=None, | |
help='set all the JDK configuration') | |
parser.add_argument('--dry-run', dest='dry_run', action='store_true', default=False, | |
help='dry run without any actual operations') | |
args = parser.parse_args() | |
jdk_config_files = search_jdk_config_files() | |
if args.clean: | |
for jdk_config_file in jdk_config_files: | |
if not os.path.exists(jdk_config_file) or os.path.isdir(jdk_config_file): | |
continue | |
print('remove:', jdk_config_file) | |
if not args.dry_run: | |
os.remove(jdk_config_file) | |
elif args.jdk_path: | |
for jdk_config_file in jdk_config_files: | |
print('modify:', jdk_config_file) | |
if not args.dry_run: | |
with open(jdk_config_file, 'w') as fd: | |
fd.write(args.jdk_path) | |
else: | |
parser.print_help() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment