forked from aduros/flambe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-haxe3
executable file
·122 lines (97 loc) · 4.44 KB
/
convert-haxe3
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
#!/usr/bin/env python
#
# Tries to convert a project to Haxe 3. It will do most of the heavy lifting, but some manual
# editing will still be necessary.
#
# **** WARNING WARNING WARNING ****
# Create a backup of your project before running this script!
# **** WARNING WARNING WARNING ****
#
# Performs these replacements:
# * var foo (getFoo, setFoo) --> var foo (get, set)
# * callback(foo, bar) --> foo.bind(bar)
# * Hash<Foo> --> Map<String,Foo>
# * IntHash<Foo> --> Map<Int,Foo>
# * class Foo extends Bar, implements Baz --> class Foo extends Bar implements Baz
# * @:macro --> macro
# * ExprRequire --> ExprOf
# * Handles a few moved types in import statements
import os
import re
import sys
property_keywords = ["get", "default", "never", "null"]
# Note: Only import/using statements are replaced, so you may have to go in and manually fix
# whatever was renamed
renamed_types = {
"flambe.util.Hashes": "flambe.util.Maps",
"haxe.BaseCode": "haxe.crypto.BaseCode",
"haxe.Md5": "haxe.crypto.Md5",
"haxe.SHA1": "haxe.crypto.Sha1",
"haxe.Stack": "haxe.CallStack",
"haxe.FastList": "haxe.ds.GenericStack",
# Flambe-specific renames
"flambe.external.External": "flambe.subsystem.ExternalSystem",
"flambe.display.Stage": "flambe.subsystem.StageSystem",
"flambe.input.Keyboard": "flambe.subsystem.KeyboardSystem",
"flambe.input.Motion": "flambe.subsystem.MotionSystem",
"flambe.input.Mouse": "flambe.subsystem.MouseSystem",
"flambe.input.Pointer": "flambe.subsystem.PointerSystem",
"flambe.input.Touch": "flambe.subsystem.TouchSystem",
"flambe.storage.Storage": "flambe.subsystem.StorageSystem",
"flambe.web.Web": "flambe.subsystem.WebSystem",
# This list is not complete
"neko.zip.Reader": "haxe.zip.Reader",
"neko.zip.Writer": "haxe.zip.Writer",
"neko.FileSystem": "sys.FileSystem",
"neko.io.File": "sys.io.File",
}
for root, dirs, files in os.walk(".", followlinks=True):
# Process unhidden .hx files only
files = [path for path in files if path[0] != "." and path.endswith(".hx")]
# Don't go into hidden directories
dirs[:] = [path for path in dirs if path[0] != "."]
for path in files:
path = os.path.join(root, path)
renamed_methods = {}
with open(path) as f:
content = f.read()
def replace_imports (m):
import_statement = m.group(1)
type = m.group(2)
renamed_type = renamed_types.get(type, type)
return "%s %s;" % (import_statement, renamed_type)
content = re.sub(r"\b(import|using)\s+(.+?);", replace_imports, content)
def replace_properties (m):
prop = m.group(1)
getter = m.group(2)
if getter not in property_keywords:
renamed_methods[getter] = "get_"+prop
getter = "get"
setter = m.group(3)
if setter not in property_keywords:
renamed_methods[setter] = "set_"+prop
setter = "set"
return "var %s (%s, %s)" % (prop, getter, setter)
content = re.sub(r"var\s+(\w+)\s*\(\s*(\w+),\s*(\w+)\)", replace_properties, content)
def replace_callbacks (m):
function = m.group(1)
params = m.group(2)
return "%s.bind(%s)" % (function, params)
content = re.sub(r"\bcallback\((.*?)\s*,\s*(.*)\)", replace_callbacks, content)
def replace_hashes (m):
key_type = "Int" if m.group(1) == "Int" else "String"
value_type = m.group(3)
return "Map" if value_type is None else "Map<%s,%s>" % (key_type, value_type)
content = re.sub(r"\b(Int)?Hash(<(.*?)>)?", replace_hashes, content)
def replace_inheritance (m):
return m.group(0)[1:] # Trim the leading comma
content = re.sub(r",\s*(extends|implements)(.*)", replace_inheritance, content)
content = re.sub(r"@:macro(\s+)?", "macro ", content)
content = re.sub(r"ExprRequire<", "ExprOf<", content)
with open(path, "w") as f:
def replace_methods (m):
if m.group(1) in renamed_methods:
return "function " + renamed_methods[m.group(1)]
return m.group()
content = re.sub(r"function\s+(\w+)", replace_methods, content)
f.write(content)