2023-03-03 09:38:23 -05:00
|
|
|
#!/usr/bin/python3
|
2023-02-19 18:10:23 -05:00
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
home = os.environ['HOME']
|
2023-03-07 11:58:11 -05:00
|
|
|
git_dirs = [
|
|
|
|
home + "/projects",
|
|
|
|
home + "/git",
|
2023-09-27 12:21:12 -04:00
|
|
|
home + "/git/worktrees",
|
2023-03-07 11:58:11 -05:00
|
|
|
home + "/WebStormProjects",
|
|
|
|
home + "/RiderProjects"
|
|
|
|
]
|
2023-02-19 18:10:23 -05:00
|
|
|
|
|
|
|
|
2023-03-07 11:58:11 -05:00
|
|
|
icons = {}
|
2023-02-19 18:10:23 -05:00
|
|
|
def get_application_desktop_file_info(application: str, prefix: str) -> str:
|
2023-03-07 11:58:11 -05:00
|
|
|
existing_key = prefix + application
|
|
|
|
existing = icons.get(existing_key)
|
|
|
|
if existing is not None:
|
|
|
|
return existing
|
2023-02-19 18:10:23 -05:00
|
|
|
path = f"{home}/.local/share/applications/jetbrains-{application}.desktop"
|
|
|
|
with open(path, 'r') as file:
|
|
|
|
for line in file.readlines():
|
|
|
|
if line.startswith(prefix):
|
2023-03-07 11:58:11 -05:00
|
|
|
icons[existing_key] = line
|
2023-02-19 18:10:23 -05:00
|
|
|
return line
|
|
|
|
|
|
|
|
|
2023-03-07 11:35:19 -05:00
|
|
|
def get_project_type(project) -> (str, str):
|
|
|
|
ret = ("idea", None)
|
2023-02-19 18:10:23 -05:00
|
|
|
for file in os.listdir(project):
|
|
|
|
if file.endswith("pom.xml"):
|
2023-03-07 11:35:19 -05:00
|
|
|
return ("idea", None)
|
2023-02-19 18:10:23 -05:00
|
|
|
if file.endswith("package.json"):
|
2023-03-07 11:35:19 -05:00
|
|
|
return ("webstorm", None)
|
2023-02-19 18:10:23 -05:00
|
|
|
if file.endswith("cargo.toml") or file.endswith(".c"):
|
2023-03-07 11:35:19 -05:00
|
|
|
return ("clion", None)
|
|
|
|
if file.endswith(".csproj"):
|
|
|
|
return ("rider", file)
|
|
|
|
if file.endswith("DotSettings"):
|
|
|
|
# Allow .csproj to override this value
|
|
|
|
ret = ("rider", None)
|
2023-02-19 18:10:23 -05:00
|
|
|
if file.endswith(".py"):
|
2023-03-07 11:35:19 -05:00
|
|
|
return ("pycharm", None)
|
2023-02-19 18:10:23 -05:00
|
|
|
if file.endswith(".go"):
|
2023-03-07 11:35:19 -05:00
|
|
|
return ("goland", None)
|
|
|
|
return ret
|
2023-02-19 18:10:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def get_icon(project: str) -> str | None:
|
2023-03-07 11:35:19 -05:00
|
|
|
(project_type, _) = get_project_type(project)
|
2023-02-19 18:10:23 -05:00
|
|
|
if project_type is None:
|
|
|
|
return None
|
|
|
|
icon_line = get_application_desktop_file_info(project_type, 'Icon')
|
|
|
|
return icon_line[5:].rstrip()
|
|
|
|
|
|
|
|
|
|
|
|
def open_project(project: str) -> ():
|
2023-09-27 12:21:12 -04:00
|
|
|
project = project.replace("~", home)
|
2023-03-07 11:35:19 -05:00
|
|
|
(project_type, file) = get_project_type(project)
|
2023-02-19 18:10:23 -05:00
|
|
|
if project_type is None:
|
|
|
|
return
|
2023-03-07 11:35:19 -05:00
|
|
|
if file is not None:
|
|
|
|
project = f'{project}/{file}'
|
|
|
|
|
2023-02-19 18:10:23 -05:00
|
|
|
command = get_application_desktop_file_info(project_type, 'Exec')[6:-5]
|
2023-03-03 09:38:23 -05:00
|
|
|
subprocess.Popen([command + " " + project], shell=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
2023-02-19 18:10:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
def print_project_message(project: str) -> ():
|
|
|
|
icon = get_icon(project)
|
|
|
|
if icon is None:
|
|
|
|
return
|
|
|
|
|
2023-09-27 12:21:12 -04:00
|
|
|
project = project.replace(home, "~")
|
|
|
|
print(f'git {project}')
|
|
|
|
# print(f'{project}\0icon\x1f{icon}')
|
2023-02-19 18:10:23 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-12-13 13:42:44 -05:00
|
|
|
include_icon = sys.argv[1] != "--no-icon" if len(sys.argv) > 1 else True
|
|
|
|
if len(sys.argv) == 1 or not include_icon:
|
2023-02-19 18:10:23 -05:00
|
|
|
for git_dir in git_dirs:
|
|
|
|
if not os.path.isdir(git_dir):
|
|
|
|
continue
|
|
|
|
for project in os.listdir(git_dir):
|
|
|
|
project = f'{git_dir}/{project}'
|
|
|
|
if os.path.isdir(project):
|
2023-12-13 13:42:44 -05:00
|
|
|
if include_icon:
|
|
|
|
print_project_message(project)
|
|
|
|
else:
|
|
|
|
print(f'git {project}')
|
2023-02-19 18:10:23 -05:00
|
|
|
else:
|
2023-09-27 12:21:12 -04:00
|
|
|
open_project(sys.argv[1][4:])
|