forked from albertlauncher/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
190 lines (165 loc) · 7.26 KB
/
Copy path__init__.py
File metadata and controls
190 lines (165 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Supported IDEs:
Android Studio, CLion, DataGrip, DataSpell, GoLand, IntelliJ IDEA, PhpStorm, PyCharm, Rider, RubyMine, WebStorm.
Note: To open projects the command-line launcher is required. If your IDE has no \
command-line launcher in $PATH, use `Tools` > `Create Command-line Launcher`.
"""
from dataclasses import dataclass
from pathlib import Path
from typing import Union
from shutil import which
from sys import platform
from xml.etree import ElementTree
from albert import *
md_iid = '2.0'
md_version = "1.5"
md_name = "Jetbrains projects"
md_description = "Open your JetBrains projects"
md_license = "GPL-3"
md_url = "https://github.com/albertlauncher/python/"
md_maintainers = ["@mqus", "@tomsquest"]
@dataclass
class Project:
name: str
path: str
last_opened: int
@dataclass
class Editor:
name: str
icon: Path
config_dir_prefix: str
binary: str
def __init__(self, name: str, icon: Path, config_dir_prefix: str, binaries: list[str]):
self.name = name
self.icon = icon
self.config_dir_prefix = config_dir_prefix
self.binary = self._find_binary(binaries)
def _find_binary(self, binaries: list[str]) -> Union[str, None]:
for binary in binaries:
if which(binary):
return binary
return None
def list_projects(self) -> list[Project]:
config_dir = Path.home() / ".config"
if platform == "darwin":
config_dir = Path.home() / "Library" / "Application Support"
dirs = list(config_dir.glob(f"{self.config_dir_prefix}*/"))
if not dirs:
return []
latest = sorted(dirs)[-1]
return self._parse_recent_projects(Path(latest) / "options" / "recentProjects.xml")
def _parse_recent_projects(self, recent_projects_file: Path) -> list[Project]:
try:
root = ElementTree.parse(recent_projects_file).getroot()
entries = root.findall(".//component[@name='RecentProjectsManager']//entry[@key]")
projects = []
for entry in entries:
project_path = entry.attrib["key"].replace("$USER_HOME$", str(Path.home()))
tag_opened = entry.find(".//option[@name='projectOpenTimestamp']")
last_opened = tag_opened.attrib["value"] if tag_opened is not None and "value" in tag_opened.attrib else None
if project_path and last_opened:
projects.append(
Project(name=Path(project_path).name, path=project_path, last_opened=int(last_opened))
)
return projects
except ElementTree.ParseError:
return []
class Plugin(PluginInstance, TriggerQueryHandler):
executables = []
def __init__(self):
TriggerQueryHandler.__init__(self,
id=md_id,
name=md_name,
description=md_description,
synopsis='project name',
defaultTrigger='jb ')
PluginInstance.__init__(self, extensions=[self])
plugin_dir = Path(__file__).parent
editors = [
Editor(
name="Android Studio",
icon=plugin_dir / "androidstudio.svg",
config_dir_prefix="Google/AndroidStudio",
binaries=["studio", "androidstudio", "android-studio", "android-studio-canary", "jdk-android-studio",
"android-studio-system-jdk"]),
Editor(
name="CLion",
icon=plugin_dir / "clion.svg",
config_dir_prefix="JetBrains/CLion",
binaries=["clion", "clion-eap"]),
Editor(
name="DataGrip",
icon=plugin_dir / "datagrip.svg",
config_dir_prefix="JetBrains/DataGrip",
binaries=["datagrip", "datagrip-eap"]),
Editor(
name="DataSpell",
icon=plugin_dir / "dataspell.svg",
config_dir_prefix="JetBrains/DataSpell",
binaries=["dataspell", "dataspell-eap"]),
Editor(
name="GoLand",
icon=plugin_dir / "goland.svg",
config_dir_prefix="JetBrains/GoLand",
binaries=["goland", "goland-eap"]),
Editor(
name="IntelliJ IDEA",
icon=plugin_dir / "idea.svg",
config_dir_prefix="JetBrains/IntelliJIdea",
binaries=["idea", "idea.sh", "idea-ultimate", "idea-ce-eap", "idea-ue-eap", "intellij-idea-ce",
"intellij-idea-ce-eap", "intellij-idea-ue-bundled-jre", "intellij-idea-ultimate-edition",
"intellij-idea-community-edition-jre", "intellij-idea-community-edition-no-jre"]),
Editor(
name="PhpStorm",
icon=plugin_dir / "phpstorm.svg",
config_dir_prefix="JetBrains/PhpStorm",
binaries=["phpstorm", "phpstorm-eap"]),
Editor(
name="PyCharm",
icon=plugin_dir / "pycharm.svg",
config_dir_prefix="JetBrains/PyCharm",
binaries=["charm", "pycharm", "pycharm-eap"]),
Editor(
name="Rider",
icon=plugin_dir / "rider.svg",
config_dir_prefix="JetBrains/Rider",
binaries=["rider", "rider-eap"]),
Editor(
name="RubyMine",
icon=plugin_dir / "rubymine.svg",
config_dir_prefix="JetBrains/RubyMine",
binaries=["rubymine", "rubymine-eap", "jetbrains-rubymine", "jetbrains-rubymine-eap"]),
Editor(
name="WebStorm",
icon=plugin_dir / "webstorm.svg",
config_dir_prefix="JetBrains/WebStorm",
binaries=["webstorm", "webstorm-eap"]),
]
self.editors = [e for e in editors if e.binary is not None]
def handleTriggerQuery(self, query: TriggerQuery):
editor_project_pairs = []
for editor in self.editors:
projects = editor.list_projects()
projects = [p for p in projects if Path(p.path).exists()]
projects = [p for p in projects if query.string.lower() in p.name.lower()]
editor_project_pairs.extend([(editor, p) for p in projects])
# sort by last opened
editor_project_pairs.sort(key=lambda pair: pair[1].last_opened, reverse=True)
query.add([self._make_item(editor, project, query) for editor, project in editor_project_pairs])
def _make_item(self, editor: Editor, project: Project, query: TriggerQuery) -> Item:
return StandardItem(
id="%s-%s-%s" % (editor.binary, project.path, project.last_opened),
text=project.name,
subtext=project.path,
inputActionText=query.trigger + project.name,
iconUrls=["file:" + str(editor.icon)],
actions=[
Action(
"Open",
"Open in %s" % editor.name,
lambda selected_project=project.path: runDetachedProcess(
[editor.binary, selected_project]
),
)
],
)