-
Notifications
You must be signed in to change notification settings - Fork 9
/
upgrade.py
executable file
·194 lines (155 loc) · 6.13 KB
/
upgrade.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
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
191
192
193
194
#!/usr/bin/env python
from os import *
import sys
import argparse
parser = argparse.ArgumentParser(description="Upgrade Marionette apps from 1.x to 2.x")
parser.add_argument("-a", "--all", required=False, action='store_true', help="Skip y/n/q prompt and search for all changes. You will be able to review changes before committing.")
parser.add_argument("path", help="Directory of your Marionette app to upgrade")
args = parser.parse_args()
print args.path
if path.isdir(args.path) is False:
parser.print_help()
sys.exit("\nDirectory does not exist: " + args.path + "\n")
renameCloseToDestroy = True
renameLayoutToLayoutView = True
renameItemToChild = True
apiCleanup = True
codemod = "%s/bin/codemod.py" % path.dirname(__file__)
def confirm():
if args.all == True:
return True
ch = sys.stdin.readline().rstrip('\n')
if ch == "\n" or ch == "y":
return True
if ch == "n":
return False
elif ch == "q":
sys.exit()
else:
print "sorry, didn\'t understand"
def sub(search, replace):
print "\n\033[94mReplace:\n\n\033[91m %s \033[0m with \033[92m %s \033[0m \033[93m\n\n[y,n,q]\033[0m " % (search, replace),
good = confirm()
if good:
cmd = ("%s -m --extensions js,coffee -d %s %s %s" % (codemod, args.path, search, replace))
print "\n\n"
system(cmd)
def subString(search, replace):
searchTermS = "\"\'%s\'\"" % (search)
replaceTermS = "\"\'%s\'\"" % (replace)
searchTermD = '\'\"%s\"\'' % (search)
replaceTermD = '\'\"%s\"\'' % (replace)
sub(searchTermS, replaceTermS)
sub(searchTermD, replaceTermD)
def subMethod(search, replace):
searchTerm = "'(\.|@)%s\\('" % (search)
replaceTerm = "'\\1%s('" % (replace)
sub(searchTerm, replaceTerm)
def subEvent(search, replace):
searchTermCbk = "\"%s\"" % (eventToCbk(search))
replaceTermCbk = "\"%s\"" % (eventToCbk(replace))
sub(searchTermCbk, replaceTermCbk)
subString(search, replace)
def subTerm(search, replace):
searchTerm = "\"%s\"" % search
replaceTerm = "\"%s\"" % replace
sub(searchTerm, replaceTerm)
def subString(search, replace):
searchTerm = "\"%s\"" % search
replaceTerm = "\"%s\"" % replace
sub(searchTerm, replaceTerm)
def subKey(search, replace):
searchTerm = "\"%s:\"" % search
replaceTerm = "\"%s:\"" % replace
sub(searchTerm, replaceTerm)
def eventToCbk(event):
parts = event.split(':')
parts = [part.capitalize() for part in parts]
cbk = "on"+ ''.join(parts)
return cbk
#
#
#
#
#
# UPGRADER CHANGES
#
#
#
#
#
#
if (renameCloseToDestroy):
subMethod("close", "destroy")
subMethod("closeRegions", "destroyRegions")
subMethod("closeChildren", "destroyChildren")
subMethod("onBeforeClose", "onBeforeDestroy")
subEvent('collection:before:close', 'before:destroy')
subEvent('collection:closed', 'destroy')
subEvent('item:before:close', 'before:destroy')
subEvent('item:closed', 'destroy')
subTerm("closed", "destroy")
subEvent("close", "destroy")
subTerm('isClosed', 'isDestroyed')
subKey('preventClose', 'preventDestroy')
if (renameLayoutToLayoutView):
subTerm("Marionette.Layout(?!View)", "Marionette.LayoutView")
subTerm("Marionette.Layout\.", "Marionette.LayoutView.")
subTerm("Marionette.Layout\(", "Marionette.LayoutView(")
if (renameItemToChild):
subTerm("itemView", "childView")
subTerm("itemViewEventPrefix", "childViewEventPrefix")
subTerm("itemViewOptions", "childViewOptions")
subTerm("itemEvents", "childEvents")
subTerm("itemViewContainer", "childViewContainer")
subMethod("addChildView", "onChildAdd")
subMethod("getItemView", "getChildView")
subTerm("addItemView:", "addChild:")
subMethod("addItemView", "addChild")
subTerm("getItemView:", "getChildView:")
subMethod("removeItemView", "removeChildView")
subMethod("renderItemView", "renderChildView")
subMethod("closeChildren", "destroyChildren")
subMethod("resetItemViewContainer", "resetChildViewContainer")
subMethod("getItemViewContainer", "getChildViewContainer")
subMethod("buildItemView", "buildChildView")
subEvent("before:item:added", "before:add:child")
subEvent("after:item:added", "add:child")
subEvent("before:item:rendered", "before:render")
subEvent("item:rendered", "render")
subEvent("item:before:render", "before:render")
subTerm("itemview", "childview")
subTerm("onItemview", "onChildview")
if (apiCleanup):
subMethod("appendHtml", "attachHtml")
subMethod("appendBuffer", "attachBuffer")
subMethod("renderModel", "_renderRoot")
subMethod("ensureEl", "_ensureElement")
subEvent("region:add", "add:region")
subEvent("region:remove", "remove:region")
subEvent("initialize:before", "before:start")
subEvent("initialize:after", "start")
subTerm("regionType", "regionClass")
subEvent("composite:collection:before:render", "before:render:template")
subEvent("composite:model:rendered", "render:template")
subEvent("composite:rendered", "render")
subEvent("composite:before:render:collection", "before:render:collection")
subEvent("composite:collection:rendered", "render:collection")
subEvent("collection:before:destroy", "before:destroy:collection")
subEvent("collection:destroy", "destroy:collection")
subEvent("collection:before:render", "before:render:collection")
subEvent("collection:rendered", "render")
subEvent("before:item:added", "before:add:child")
subEvent("after:item:added", "add:child")
subEvent("before:item:remove", "before:remove:child")
subEvent("item:removed", "remove:child")
subEvent("region:add", "add:region")
subEvent("region:remove", "remove:region")
# THINGS WE MIGHT NOT BE ABLE TO CHANGE
# this is a todo list of things I found when working on adding regex
# that were not immediately obvious to change. Sorry, you'll have to do these yourself :)
#
#
# 1. Module's initialize function now accepts these params(options, moduleName, app) it used to be these (moduleName, app, options)
# 2. regions need to have an element when they're showing a view. Previously you could show a view in a region and if the region didn't have an element on the page at the time, nothing would happen. Now we through an error so you know immediately that you need to fix something.
# I'm sure there other changes as well, so don't treat this as an exhaustive list.