-
Notifications
You must be signed in to change notification settings - Fork 0
/
ideaFile.py
142 lines (109 loc) · 3.4 KB
/
ideaFile.py
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
#!/usr/bin/python
# encoding: utf-8
import functools
import os
import re
from xml.etree import ElementTree
from Feedback import Feedback
import sys
class Project:
def __init__(self, name, path, time=0):
self.name = name
self.path = path
self.time = time
def searchWorkspaces():
workSpaceStr = os.getenv("WORK_SPACE")
workSpaces = workSpaceStr.split(";")
for workSpace in workSpaces:
search(workSpace)
def search(workspace):
"""
搜索工作空间里的所有Java文件夹
@param workspace: 工作空间路径 举例:/users/admin/workspace
"""
files = os.listdir(workspace)
for file in files:
fullpath = workspace + "/" + file
if isJavaDir(fullpath) and re.search(arg, file.lower()):
projects.append(Project(file, fullpath))
def isJavaDir(path):
"""
判断当前目录是否是Java文件夹。
判断依据:如果文件夹里含有pom.xml文件,判断是,否则不是
@param path:
@return:
"""
if os.path.isdir(path):
tempFiles = os.listdir(path)
for tempFile in tempFiles:
if tempFile == "pom.xml":
return True
return False
def recentProject():
prefix = "~/Library/Application Support/JetBrains/"
suffix = "/options/recentProjects.xml"
prefix = os.path.expanduser(prefix)
files2 = os.listdir(prefix)
path = prefix + findExactPath(files2) + suffix
tree = ElementTree.parse(path)
eles = tree.findall("./component/")
# lastOpenProject(eles)
otherProjects(eles)
def addProject(project):
fb.add_item(project.name, project.path, project.path)
def lastOpenProject(options):
for o in options:
if o.get("name") == 'lastOpenedProject':
path = os.path.expanduser(o.get('value').replace("$USER_HOME$", "~"))
name = path.split("/")[-1]
fb.add_item(name, path)
def otherProjects(options):
for o in options:
if o.get("name") == 'additionalInfo':
entries = o.findall("map/entry")
for entry in entries:
parseEntry(entry)
def parseEntry(entry):
path = os.path.expanduser(entry.get('key').replace("$USER_HOME$", "~"))
name = path.split("/")[-1]
time = 0
options = entry.findall("value/RecentProjectMetaInfo/option")
for o in options:
if o.get("name") == "activationTimestamp":
time = o.get("value")
projects.append(Project(name, path, int(time)))
def findExactPath(files):
opFiles = []
for file in files:
if file.startswith("IntelliJIdea"):
opFiles.append(file)
opFiles.sort(reverse=True)
return opFiles[0]
def sortFun(p1, p2):
if p1.time < p2.time:
return -1
elif p1.time > p2.time:
return 1
else:
if p1.name < p2.name:
return 1
elif p1.name > p2.name:
return -1
else:
return 0
if __name__ == '__main__':
arg = ''
fb = Feedback()
projects = []
if len(sys.argv) < 2:
recentProject()
else:
arg = sys.argv[1].lower()
searchWorkspaces()
projects.sort(key=functools.cmp_to_key(sortFun), reverse=True)
projects = projects[0:9]
for p in projects:
addProject(p)
if fb.isEmpty():
fb.add_item("找不到项目:" + arg, "找不到这个项目,请确认输入没有错🙅♂️!", valid="no")
print(fb)