-
Notifications
You must be signed in to change notification settings - Fork 0
/
classname.py
executable file
·129 lines (111 loc) · 4.47 KB
/
classname.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
import os
import sys
import time
import subprocess
import shutil
def append_line(file_name, line_num, text):
lines = open(file_name, 'r').readlines()
lines.insert(line_num,text)
out = open(file_name, 'w')
out.writelines(lines)
out.close()
def remove_auto(file_name):
lines = open(file_name, 'r').readlines()
newLines = list()
cont = False
for lineNumber,line in enumerate(lines):
if cont is False and "//AutoGeneratedClassName-start" in line:
cont = True
continue
if cont is True and "//AutoGeneratedClassName-end" in line:
cont = False
continue
if cont is True:
continue
newLines.append(line)
out = open(file_name, 'w')
out.writelines(newLines)
out.close()
def findFiles(dir,type,exclude,array):
for fname in os.listdir(dir):
path = os.path.join(dir, fname)
if os.path.isdir(path):
findFiles(path,type,exclude,array)
if len(exclude) > 0:
if hasLastPathComponent(path,type) and not inArray(exclude,path):
array.append(path)
elif type in path :
array.append(path)
class StringBuffer:
baseString = ""
currentPos = 0
def setBaseString(self, string):
self.baseString = string
def setPosFromSubString(self, string):
try:
self.currentPos = self.baseString.index(string) + len(string)
except ValueError:
print string + " not found in base-string : " + self.baseString
raise ValueError()
def readTillChar(self, expectedChar, toofarChar):
substring = self.baseString[self.currentPos:]
resultString = ""
for char in substring:
if char == expectedChar:
break
if char == toofarChar:
print substring + " : went too far. tried to find " + expectedChar + " but found " + toofarChar
resultString = ""
break
resultString = resultString + char
return resultString
def transpileSize():
array = list()
findFiles("./ts",".ts","",array)
for filepath in array:
with open(filepath,'r' ) as f:
in_file = f.readlines()
namespace = ""
classname = ""
hasAlready = False
hasAuto = False
lineNumb = 0
for lineNumber,line in enumerate(in_file):
if "namespace" in line:
buffer = StringBuffer()
buffer.setBaseString(line)
buffer.setPosFromSubString("namespace")
namespace = buffer.readTillChar('{','(').strip()
continue
if "//AutoGeneratedClassName-start" in line:
hasAuto = True
continue
if "static classname" in line:
hasAlready = True
if "@class" in line or "{" not in line:
continue
if "class " in line and "<" in line:
continue
if len(classname) == 0 and "class " in line and "{" in line:
buffer = StringBuffer()
buffer.setBaseString(line)
buffer.setPosFromSubString("class ")
lineNumb = lineNumber
if "extends " in line:
classname = buffer.readTillChar(" ", "{").strip()
elif "implements" in line:
classname = buffer.readTillChar(" ", "{").strip()
else:
classname = buffer.readTillChar("{", "(").strip()
if hasAuto is True:
remove_auto(filepath)
if hasAuto is False and hasAlready is True:
break
if len(namespace) > 0 and len(classname) > 0 and lineNumb != 0:
if "ViewController" in classname:
print classname
append_line(filepath,lineNumb+1,'\t\t//AutoGeneratedClassName-start - do not eidt this line\n')
append_line(filepath,lineNumb+2,'\t\tpublic static classname:string = "'+namespace+'.'+classname+'";\n')
append_line(filepath,lineNumb+3,'\t\tpublic getClassName():string{return '+classname+'.classname;}\n')
append_line(filepath,lineNumb+4,'\t\t//AutoGeneratedClassName-end - do not eidt this line\n')
transpileSize()