Skip to content

Commit 811fa6e

Browse files
committed
[[ Scriptifier ]] Implement scriptifier
This patch adds a scriptifier tool that parses a stackFile and exports non-trivial scripts as script only behavior stacks. It sets the stackFiles property ensuring the scripts can be found when the stack is loaded. The script in the original stack is set to empty. For simplicity any control with an existing behavior is not modified.
1 parent 31fe5aa commit 811fa6e

File tree

1 file changed

+269
-0
lines changed

1 file changed

+269
-0
lines changed

tools/Scriptifier.livecodescript

Lines changed: 269 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
script "Scriptifier"
2+
local sInit = "false"
3+
4+
---------------------------------------------
5+
--- EVENTS
6+
7+
on openStack
8+
__InitUI
9+
end openStack
10+
11+
on resumeStack
12+
__UpdateStackList
13+
end resumeStack
14+
15+
on menuPick pWhich
16+
switch short name of the target
17+
case "mainstacks"
18+
break
19+
end switch
20+
pass menuPick
21+
end menuPick
22+
23+
on mouseUp pButton
24+
if pButton is 1 then
25+
switch the short name of the target
26+
case "Scriptify"
27+
__Scriptify the label of button "mainstacks" of me
28+
if the result is not empty then
29+
answer the result
30+
end if
31+
break
32+
end switch
33+
end if
34+
pass mouseUp
35+
end mouseUp
36+
37+
---------------------------------------------
38+
--- UI GENERATION
39+
40+
private command __InitUI
41+
if sInit then
42+
return empty for value
43+
end if
44+
45+
reset the templateButton
46+
set the style of the templateButton to "menu"
47+
set the menuMode of the templateButton to "option"
48+
create button "mainstacks"
49+
50+
set the rect of button "mainstacks" to 5,5,155,26
51+
set the text of button "mainstacks" to the mainstacks
52+
53+
set the style of the templateButton to "standard"
54+
create button "Scriptify"
55+
set the rect of button "Scriptify" to 160,5,235,26
56+
57+
__UpdateStackList
58+
59+
set the visible of this stack to true
60+
end __InitUI
61+
62+
command ResetUI
63+
__ResetUI
64+
end ResetUI
65+
66+
private command __ResetUI
67+
local tControlID
68+
repeat for each line tControlID in the childControlIds of this card
69+
delete control id tControlID of this card
70+
end repeat
71+
72+
put false into sInit
73+
74+
__InitUI
75+
end __ResetUI
76+
77+
private command __UpdateStackList
78+
local tStacks, tStack
79+
repeat for each line tStack in the mainstacks
80+
if the scriptOnly of stack tStack then
81+
next repeat
82+
end if
83+
84+
put tStack & return after tStacks
85+
end repeat
86+
87+
set the text of button "mainstacks" to tStacks
88+
end __UpdateStackList
89+
90+
---------------------------------------------
91+
--- SCRIPTIFIER
92+
93+
local sUniqueNames
94+
95+
private command __Scriptify pStack
96+
__Clean
97+
98+
__EnsureStack pStack
99+
if the result is not empty then
100+
return the result
101+
end if
102+
103+
local tDirectory
104+
put the filename of stack pStack into tDirectory
105+
106+
set the itemDelimiter to "."
107+
delete the last item of tDirectory
108+
__EnsureDirectory tDirectory
109+
if the result is not empty then
110+
return the result
111+
end if
112+
113+
__RecurseObjects the long id of stack pStack, tDirectory, the long id of stack pStack
114+
115+
save stack pStack
116+
end __Scriptify
117+
118+
private command __Clean
119+
delete variable sUniqueNames
120+
end __Clean
121+
122+
private command __EnsureStack pStack
123+
if there is no stack pStack then
124+
return "no stack" for error
125+
end if
126+
return empty for value
127+
end __EnsureStack
128+
129+
private command __EnsureDirectory @xDirectory
130+
if xDirectory ends with slash then
131+
delete the last char of xDirectory
132+
end if
133+
134+
local tPath
135+
set the itemDelimiter to slash
136+
repeat for each item tFolder in xDirectory
137+
put tFolder & slash after tPath
138+
if there is not a folder tPath then
139+
create folder tPath
140+
if the result is not empty then
141+
return the result for error
142+
end if
143+
end if
144+
end repeat
145+
146+
return empty for value
147+
end __EnsureDirectory
148+
149+
private command __RecurseObjects pObject, pDirectory, pTarget
150+
-- don't mess with objects that already have behaviors or have
151+
-- trivial scripts
152+
if the number of words of the script of pObject > 10 and \
153+
the behavior of pObject is empty then
154+
local tBehaviorName
155+
__UniqueBehaviorNameForObject pObject, tBehaviorName
156+
if the result is not empty then
157+
throw "Could not create a unique behavior name for:" & return & \
158+
the long name of pObject & return & "Please change the object name."
159+
end if
160+
161+
__CreateBehavior pObject, tBehaviorName, pDirectory, pTarget
162+
if the result is not empty then
163+
return the result
164+
end if
165+
end if
166+
167+
local tID
168+
switch word 1 of pObject
169+
case "stack"
170+
repeat for each line tID in the cardIDs of pObject
171+
__RecurseObjects the long id of card id tID of pObject, pDirectory, pTarget
172+
end repeat
173+
repeat for each line tID in the sharedGroupIDs of pObject
174+
__RecurseObjects the long id of control id tID of pObject, pDirectory, pTarget
175+
end repeat
176+
repeat for each line tStack in the substacks of pObject
177+
__RecurseObjects the long id of stack tStack of pObject, pDirectory, pTarget
178+
end repeat
179+
break
180+
case "card"
181+
repeat for each line tID in the childControlIDs of pObject
182+
get the long id of control id tID of pObject
183+
if word 1 of it is in "group bkgnd" and the sharedBehavior of it then
184+
next repeat
185+
end if
186+
__RecurseObjects it, pDirectory, pTarget
187+
end repeat
188+
break
189+
case "group"
190+
case "bkgnd"
191+
repeat for each line tID in the childControlIDs of pObject
192+
__RecurseObjects the long id of control id tID of pObject, pDirectory, pTarget
193+
end repeat
194+
break
195+
end switch
196+
end __RecurseObjects
197+
198+
private command __UniqueBehaviorNameForObject pObject, @rBehaviorName
199+
local tBehaviorName
200+
-- behavior name: <stack name><Object name><Object type>Behavior
201+
local tStack
202+
put ideMainStackOfObject(pObject) into tStack
203+
204+
local tStackName
205+
put the short name of tStack into tStackName
206+
207+
put toUpper(char 1 of pObject) & char 2 to -1 of word 1 of pObject & "Behavior" after tBehaviorName
208+
209+
repeat
210+
get the short name of pObject
211+
if it is not empty then
212+
put toUpper(char 1 of it) & char 2 to -1 of it before tBehaviorName
213+
end if
214+
215+
if the long id of pObject is tStack then
216+
exit repeat
217+
end if
218+
219+
put the long owner of pObject into pObject
220+
end repeat
221+
put replaceText(tBehaviorName,"[^a-zA-Z0-9]",empty) into tBehaviorName
222+
223+
if sUniqueNames[tBehaviorName] then
224+
return "not unique" for error
225+
else
226+
put true into sUniqueNames[tBehaviorName]
227+
put tBehaviorName into rBehaviorName
228+
return empty for value
229+
end if
230+
end __UniqueBehaviorNameForObject
231+
232+
233+
private command __CreateBehavior pObject, pBehaviorName, pDirectory, pTarget
234+
if there is a stack pBehaviorName then
235+
delete stack pBehaviorName
236+
end if
237+
238+
create script only stack pBehaviorName
239+
set the script of stack pBehaviorName to the script of pObject
240+
241+
local tFileName
242+
put tolower(pBehaviorName) & ".livecodescript" into tFileName
243+
244+
local tStackName
245+
put the short name of pTarget into tStackName
246+
put replaceText(tStackName,"[^a-zA-Z0-9]",empty) into tStackName
247+
delete char 1 to the length of tStackName of tFileName
248+
249+
set the filename of stack pBehaviorName to (pDirectory & slash & tFileName)
250+
251+
lock messages
252+
save stack pBehaviorName
253+
unlock messages
254+
255+
set the behavior of pObject to the long id of stack pBehaviorName
256+
set the script of pObject to empty
257+
258+
get the stackFiles of pTarget
259+
if it is not empty then
260+
put return after it
261+
end if
262+
263+
set the itemDelimiter to slash
264+
265+
put pBehaviorName, the last item of pDirectory & slash & tFileName after it
266+
set the stackFiles of pTarget to it
267+
268+
return tFileName for value
269+
end __CreateBehavior

0 commit comments

Comments
 (0)