You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

379 lines
17KB

  1. '==============================================================
  2. ' generateController.vbs
  3. '
  4. ' Usage (runs under the AxonASP CLI in the container):
  5. ' scripts\gen.cmd controller ControllerName "Sub1;Sub2(param1,param2);Sub3"
  6. '
  7. ' Generates a controller class file with specified action methods
  8. '==============================================================
  9. Option Explicit
  10. ' AxonASP CLI: provide the WScript object (see _cli.vbs)
  11. Dim shimFso
  12. Set shimFso = CreateObject("Scripting.FileSystemObject")
  13. ExecuteGlobal shimFso.OpenTextFile(Server.MapPath("/scripts/_cli.vbs"), 1).ReadAll
  14. Dim args, controllerName, subsList, subs, fso, fileName, outputPath
  15. ' Parse arguments
  16. Set args = WScript.Arguments
  17. If args.Count < 2 Then
  18. WScript.Echo "Usage: scripts\gen.cmd controller ControllerName ""Sub1;Sub2(param1,param2);..."""
  19. WScript.Echo ""
  20. WScript.Echo "Example: scripts\gen.cmd controller User ""Index;Show(id);Create;Update(id)"""
  21. WScript.Quit 1
  22. End If
  23. controllerName = args(0)
  24. subsList = args(1)
  25. ' Validate controller name format (alphanumeric and underscore only)
  26. If Not IsValidIdentifier(controllerName) Then
  27. WScript.Echo "Error: Invalid controller name format. Only letters, numbers, and underscores are allowed."
  28. WScript.Quit 1
  29. End If
  30. ' Parse action methods
  31. ' (stray double quotes are dropped: cmd/bash quoting can pass them through literally)
  32. subs = Split(Replace(subsList, """", ""), ";")
  33. ' Determine output path
  34. Set fso = CreateObject("Scripting.FileSystemObject")
  35. outputPath = Server.MapPath("/app/controllers")
  36. fileName = fso.BuildPath(outputPath, controllerName & "Controller.asp")
  37. ' Check if file already exists
  38. If fso.FileExists(fileName) Then
  39. WScript.Echo "Error: Controller file already exists: " & fileName
  40. WScript.Echo "Delete the existing file or choose a different controller name."
  41. WScript.Quit 1
  42. End If
  43. ' Generate the controller file
  44. Dim content
  45. content = BuildController(controllerName, subs)
  46. ' Write to file
  47. Dim file
  48. Set file = fso.CreateTextFile(fileName, True, False)
  49. file.Write content
  50. file.Close
  51. Set file = Nothing
  52. WScript.Echo "Controller file created: " & fileName
  53. ' ---- wire it up: include, whitelist, routes
  54. AppendIfMissing "/app/controllers/autoload_controllers.asp", controllerName & "Controller.asp", "<!--#include file=""" & controllerName & "Controller.asp"" -->"
  55. InsertAfterLine "/core/lib.ControllerRegistry.asp", "RegisterController ""errorcontroller""", """" & LCase(controllerName) & "controller""", _
  56. " RegisterController """ & LCase(controllerName) & "controller"""
  57. Dim routeText
  58. routeText = BuildRoutes(controllerName, subs)
  59. InsertBeforeLine "/public/Default.asp", "' Dispatch the request", """" & controllerName & "Controller""", routeText
  60. WScript.Echo "Wired: autoload_controllers.asp, ControllerRegistry, and these routes in public/Default.asp:"
  61. WScript.Echo routeText
  62. WScript.Echo "Next: fill in the actions, add views under app/views/" & controllerName & "/, restart the container (docker restart axonasp)."
  63. '==============================================================
  64. ' Build controller content using array-based string building
  65. '==============================================================
  66. Function BuildController(ctrlName, actionsArray)
  67. Dim parts()
  68. ReDim parts(500) ' Pre-allocate for performance
  69. Dim partIdx : partIdx = 0
  70. Dim className : className = ctrlName & "Controller_Class"
  71. Dim funcName : funcName = ctrlName & "Controller"
  72. ' Header comments
  73. parts(partIdx) = "<%" & vbCrLf : partIdx = partIdx + 1
  74. parts(partIdx) = "' Auto-generated Controller: " & ctrlName & vbCrLf : partIdx = partIdx + 1
  75. parts(partIdx) = "' Generated on " & Now() & vbCrLf : partIdx = partIdx + 1
  76. parts(partIdx) = "' Generator: generateController.vbs v1.0" & vbCrLf : partIdx = partIdx + 1
  77. parts(partIdx) = "'" & vbCrLf : partIdx = partIdx + 1
  78. parts(partIdx) = "' The include, whitelist entry and routes were added automatically (see public/Default.asp)." & vbCrLf : partIdx = partIdx + 1
  79. parts(partIdx) = vbCrLf & vbCrLf : partIdx = partIdx + 1
  80. ' Class definition
  81. parts(partIdx) = "Class " & className & vbCrLf : partIdx = partIdx + 1
  82. parts(partIdx) = " Private m_useLayout" & vbCrLf : partIdx = partIdx + 1
  83. parts(partIdx) = " Private m_title" & vbCrLf : partIdx = partIdx + 1
  84. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  85. ' Class_Initialize
  86. parts(partIdx) = " Private Sub Class_Initialize()" & vbCrLf : partIdx = partIdx + 1
  87. parts(partIdx) = " m_useLayout = True" & vbCrLf : partIdx = partIdx + 1
  88. parts(partIdx) = " m_title = """ & ctrlName & """" & vbCrLf : partIdx = partIdx + 1
  89. parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1
  90. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  91. ' useLayout property
  92. parts(partIdx) = " Public Property Get useLayout" & vbCrLf : partIdx = partIdx + 1
  93. parts(partIdx) = " useLayout = m_useLayout" & vbCrLf : partIdx = partIdx + 1
  94. parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1
  95. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  96. parts(partIdx) = " Public Property Let useLayout(v)" & vbCrLf : partIdx = partIdx + 1
  97. parts(partIdx) = " m_useLayout = v" & vbCrLf : partIdx = partIdx + 1
  98. parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1
  99. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  100. ' Title property
  101. parts(partIdx) = " Public Property Get Title" & vbCrLf : partIdx = partIdx + 1
  102. parts(partIdx) = " Title = m_title" & vbCrLf : partIdx = partIdx + 1
  103. parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1
  104. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  105. parts(partIdx) = " Public Property Let Title(v)" & vbCrLf : partIdx = partIdx + 1
  106. parts(partIdx) = " m_title = v" & vbCrLf : partIdx = partIdx + 1
  107. parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1
  108. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  109. ' Generate action methods
  110. Dim i, actionDef, namePart, paramsPart, paramsArr, invokeCases, callArgs
  111. invokeCases = ""
  112. For i = 0 To UBound(actionsArray)
  113. actionDef = Trim(actionsArray(i))
  114. ' Skip empty entries
  115. If Len(actionDef) > 0 Then
  116. ' Parse action name and parameters
  117. If InStr(actionDef, "(") > 0 Then
  118. namePart = Trim(Left(actionDef, InStr(actionDef, "(") - 1))
  119. paramsPart = Mid(actionDef, InStr(actionDef, "(") + 1)
  120. paramsPart = Left(paramsPart, Len(paramsPart) - 1) ' Strip trailing ')'
  121. paramsPart = Trim(paramsPart)
  122. ' Validate parameter names
  123. If Len(paramsPart) > 0 Then
  124. paramsArr = Split(paramsPart, ",")
  125. Dim j, paramName
  126. For j = 0 To UBound(paramsArr)
  127. paramName = Trim(paramsArr(j))
  128. If Not IsValidIdentifier(paramName) Then
  129. WScript.Echo "Error: Invalid parameter name '" & paramName & "' in action '" & actionDef & "'"
  130. WScript.Quit 1
  131. End If
  132. Next
  133. End If
  134. Else
  135. namePart = Trim(actionDef)
  136. paramsPart = ""
  137. End If
  138. ' Validate action name
  139. If Not IsValidIdentifier(namePart) Then
  140. WScript.Echo "Error: Invalid action name '" & namePart & "'"
  141. WScript.Quit 1
  142. End If
  143. ' Generate method stub
  144. parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1
  145. parts(partIdx) = " ' Action: " & namePart & vbCrLf : partIdx = partIdx + 1
  146. parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1
  147. If paramsPart = "" Then
  148. parts(partIdx) = " Public Sub " & namePart & "()" & vbCrLf : partIdx = partIdx + 1
  149. parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1
  150. parts(partIdx) = " Response.Write """ & namePart & " action called""" & vbCrLf : partIdx = partIdx + 1
  151. Else
  152. parts(partIdx) = " Public Sub " & namePart & "(" & paramsPart & ")" & vbCrLf : partIdx = partIdx + 1
  153. parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1
  154. parts(partIdx) = " Response.Write ""<strong>" & namePart & " called</strong><br>""" & vbCrLf : partIdx = partIdx + 1
  155. parts(partIdx) = " Response.Write ""Parameters:<br>""" & vbCrLf : partIdx = partIdx + 1
  156. ' Echo each parameter
  157. paramsArr = Split(paramsPart, ",")
  158. For j = 0 To UBound(paramsArr)
  159. paramName = Trim(paramsArr(j))
  160. parts(partIdx) = " Response.Write """ & paramName & " = "" & Server.HTMLEncode(CStr(" & paramName & ")) & ""<br>""" & vbCrLf : partIdx = partIdx + 1
  161. Next
  162. End If
  163. parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1
  164. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  165. ' Record a static dispatch case for Invoke
  166. callArgs = ""
  167. If paramsPart <> "" Then
  168. paramsArr = Split(paramsPart, ",")
  169. For j = 0 To UBound(paramsArr)
  170. If j > 0 Then callArgs = callArgs & ", "
  171. callArgs = callArgs & "args(" & j & ")"
  172. Next
  173. End If
  174. invokeCases = invokeCases & " Case """ & LCase(namePart) & """ : " & namePart & " " & callArgs & vbCrLf
  175. End If
  176. Next
  177. ' Static action dispatch (AxonASP mishandles runtime-built Eval/Execute calls and shared call sites)
  178. parts(partIdx) = " Public Sub Invoke(actionName, args)" & vbCrLf : partIdx = partIdx + 1
  179. parts(partIdx) = " Select Case LCase(actionName)" & vbCrLf : partIdx = partIdx + 1
  180. parts(partIdx) = invokeCases : partIdx = partIdx + 1
  181. parts(partIdx) = " Case Else" & vbCrLf : partIdx = partIdx + 1
  182. parts(partIdx) = " Err.Raise 438, """ & ctrlName & """, ""Action '"" & actionName & ""' not found""" & vbCrLf : partIdx = partIdx + 1
  183. parts(partIdx) = " End Select" & vbCrLf : partIdx = partIdx + 1
  184. parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1
  185. ' End class
  186. parts(partIdx) = "End Class" & vbCrLf : partIdx = partIdx + 1
  187. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  188. ' Singleton pattern
  189. parts(partIdx) = "' Singleton instance" & vbCrLf : partIdx = partIdx + 1
  190. parts(partIdx) = "Dim " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1
  191. parts(partIdx) = "Function " & funcName & "()" & vbCrLf : partIdx = partIdx + 1
  192. parts(partIdx) = " If IsEmpty(" & className & "__Singleton) Then" & vbCrLf : partIdx = partIdx + 1
  193. parts(partIdx) = " Set " & className & "__Singleton = New " & className & vbCrLf : partIdx = partIdx + 1
  194. parts(partIdx) = " End If" & vbCrLf : partIdx = partIdx + 1
  195. parts(partIdx) = " Set " & funcName & " = " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1
  196. parts(partIdx) = "End Function" & vbCrLf : partIdx = partIdx + 1
  197. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  198. parts(partIdx) = "' Global entry point used by MVC_InvokeAction via GetRef (one call site per controller)" & vbCrLf : partIdx = partIdx + 1
  199. parts(partIdx) = "Sub " & funcName & "_Invoke(actionName, args)" & vbCrLf : partIdx = partIdx + 1
  200. parts(partIdx) = " " & funcName & "().Invoke actionName, args" & vbCrLf : partIdx = partIdx + 1
  201. parts(partIdx) = "End Sub" & vbCrLf : partIdx = partIdx + 1
  202. parts(partIdx) = "%>" & vbCrLf : partIdx = partIdx + 1
  203. ' Join array for performance
  204. ReDim Preserve parts(partIdx - 1)
  205. BuildController = Join(parts, "")
  206. End Function
  207. '==============================================================
  208. ' Validate identifier (controller name, action name, parameter name)
  209. '==============================================================
  210. Function IsValidIdentifier(name)
  211. If IsEmpty(name) Or Len(name) = 0 Then
  212. IsValidIdentifier = False
  213. Exit Function
  214. End If
  215. Dim i, ch
  216. For i = 1 To Len(name)
  217. ch = Mid(name, i, 1)
  218. ' Allow a-z, A-Z, 0-9, and underscore only
  219. If Not ((ch >= "a" And ch <= "z") Or _
  220. (ch >= "A" And ch <= "Z") Or _
  221. (ch >= "0" And ch <= "9") Or _
  222. ch = "_") Then
  223. IsValidIdentifier = False
  224. Exit Function
  225. End If
  226. Next
  227. IsValidIdentifier = True
  228. End Function
  229. '==============================================================
  230. ' Routes: conventional action names map to REST-style routes, anything else is GET /<controller>/<action>[/:param]
  231. '==============================================================
  232. Function BuildRoutes(ctrlName, actionsArray)
  233. Dim r, base, i, def, nm, prm, verb, path, line, k, pa
  234. base = "/" & LCase(ctrlName)
  235. r = vbTab & "' " & ctrlName & " (generated by generateController.vbs)" & vbCrLf
  236. For i = 0 To UBound(actionsArray)
  237. def = Trim(actionsArray(i))
  238. If Len(def) > 0 Then
  239. nm = def
  240. prm = ""
  241. If InStr(def, "(") > 0 Then
  242. nm = Trim(Left(def, InStr(def, "(") - 1))
  243. prm = Trim(Replace(Mid(def, InStr(def, "(") + 1), ")", ""))
  244. End If
  245. verb = "GET"
  246. Select Case LCase(nm)
  247. Case "index": path = base
  248. Case "newform": path = base & "/new"
  249. Case "create": path = base : verb = "POST"
  250. Case "show": path = base & "/:id"
  251. Case "edit": path = base & "/:id/edit"
  252. Case "update": path = base & "/:id" : verb = "POST"
  253. Case "delete": path = base & "/:id/delete" : verb = "POST"
  254. Case Else
  255. path = base & "/" & LCase(nm)
  256. If Len(prm) > 0 Then
  257. pa = Split(prm, ",")
  258. For k = 0 To UBound(pa)
  259. path = path & "/:" & Trim(pa(k))
  260. Next
  261. End If
  262. End Select
  263. r = r & vbTab & "router.AddRoute """ & verb & """, """ & path & """, """ & ctrlName & "Controller"", """ & nm & """" & vbCrLf
  264. End If
  265. Next
  266. BuildRoutes = r & vbCrLf
  267. End Function
  268. '==============================================================
  269. ' File helpers (wiring)
  270. '==============================================================
  271. Function ReadFile(path)
  272. Dim ts
  273. Set ts = fso.OpenTextFile(Server.MapPath(path), 1)
  274. ReadFile = ts.ReadAll
  275. ts.Close
  276. End Function
  277. Sub WriteWhole(path, text)
  278. Dim tf
  279. Set tf = fso.CreateTextFile(Server.MapPath(path), True)
  280. tf.Write text
  281. tf.Close
  282. End Sub
  283. Function EolOf(text)
  284. If InStr(text, vbCrLf) > 0 Then
  285. EolOf = vbCrLf
  286. Else
  287. EolOf = vbLf
  288. End If
  289. End Function
  290. Function WithEol(text, eol)
  291. WithEol = Replace(Replace(text, vbCrLf, vbLf), vbLf, eol)
  292. End Function
  293. ' Appends a line unless marker is already in the file
  294. Sub AppendIfMissing(path, marker, line)
  295. Dim s, eol
  296. s = ReadFile(path)
  297. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  298. eol = EolOf(s)
  299. If Right(s, 1) <> vbLf Then s = s & eol
  300. WriteWhole path, s & line & eol
  301. End Sub
  302. ' Inserts newText on a new line after the first line containing anchor (unless marker is already present)
  303. Sub InsertAfterLine(path, anchor, marker, newText)
  304. Dim s, eol, pos, eolPos
  305. s = ReadFile(path)
  306. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  307. eol = EolOf(s)
  308. pos = InStr(1, s, anchor, vbTextCompare)
  309. If pos = 0 Then
  310. WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add this yourself: " & newText
  311. Exit Sub
  312. End If
  313. eolPos = InStr(pos, s, eol)
  314. WriteWhole path, Left(s, eolPos - 1) & eol & newText & Mid(s, eolPos)
  315. End Sub
  316. ' Inserts newText before the line containing anchor (unless marker is already present)
  317. Sub InsertBeforeLine(path, anchor, marker, newText)
  318. Dim s, eol, pos, lineStart
  319. s = ReadFile(path)
  320. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  321. eol = EolOf(s)
  322. pos = InStr(1, s, anchor, vbTextCompare)
  323. If pos = 0 Then
  324. WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add the routes yourself:" & vbCrLf & newText
  325. Exit Sub
  326. End If
  327. lineStart = InStrRev(s, eol, pos)
  328. If lineStart = 0 Then
  329. lineStart = 1
  330. Else
  331. lineStart = lineStart + Len(eol)
  332. End If
  333. WriteWhole path, Left(s, lineStart - 1) & WithEol(newText, eol) & Mid(s, lineStart)
  334. End Sub

Powered by TurnKey Linux.