Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

353 рядки
15KB

  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
  111. For i = 0 To UBound(actionsArray)
  112. actionDef = Trim(actionsArray(i))
  113. ' Skip empty entries
  114. If Len(actionDef) > 0 Then
  115. ' Parse action name and parameters
  116. If InStr(actionDef, "(") > 0 Then
  117. namePart = Trim(Left(actionDef, InStr(actionDef, "(") - 1))
  118. paramsPart = Mid(actionDef, InStr(actionDef, "(") + 1)
  119. paramsPart = Left(paramsPart, Len(paramsPart) - 1) ' Strip trailing ')'
  120. paramsPart = Trim(paramsPart)
  121. ' Validate parameter names
  122. If Len(paramsPart) > 0 Then
  123. paramsArr = Split(paramsPart, ",")
  124. Dim j, paramName
  125. For j = 0 To UBound(paramsArr)
  126. paramName = Trim(paramsArr(j))
  127. If Not IsValidIdentifier(paramName) Then
  128. WScript.Echo "Error: Invalid parameter name '" & paramName & "' in action '" & actionDef & "'"
  129. WScript.Quit 1
  130. End If
  131. Next
  132. End If
  133. Else
  134. namePart = Trim(actionDef)
  135. paramsPart = ""
  136. End If
  137. ' Validate action name
  138. If Not IsValidIdentifier(namePart) Then
  139. WScript.Echo "Error: Invalid action name '" & namePart & "'"
  140. WScript.Quit 1
  141. End If
  142. ' Generate method stub
  143. parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1
  144. parts(partIdx) = " ' Action: " & namePart & vbCrLf : partIdx = partIdx + 1
  145. parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1
  146. If paramsPart = "" Then
  147. parts(partIdx) = " Public Sub " & namePart & "()" & vbCrLf : partIdx = partIdx + 1
  148. parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1
  149. parts(partIdx) = " Response.Write """ & namePart & " action called""" & vbCrLf : partIdx = partIdx + 1
  150. Else
  151. parts(partIdx) = " Public Sub " & namePart & "(" & paramsPart & ")" & vbCrLf : partIdx = partIdx + 1
  152. parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1
  153. parts(partIdx) = " Response.Write ""<strong>" & namePart & " called</strong><br>""" & vbCrLf : partIdx = partIdx + 1
  154. parts(partIdx) = " Response.Write ""Parameters:<br>""" & vbCrLf : partIdx = partIdx + 1
  155. ' Echo each parameter
  156. paramsArr = Split(paramsPart, ",")
  157. For j = 0 To UBound(paramsArr)
  158. paramName = Trim(paramsArr(j))
  159. parts(partIdx) = " Response.Write """ & paramName & " = "" & Server.HTMLEncode(CStr(" & paramName & ")) & ""<br>""" & vbCrLf : partIdx = partIdx + 1
  160. Next
  161. End If
  162. parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1
  163. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  164. End If
  165. Next
  166. ' End class
  167. parts(partIdx) = "End Class" & vbCrLf : partIdx = partIdx + 1
  168. parts(partIdx) = vbCrLf : partIdx = partIdx + 1
  169. ' Singleton pattern
  170. parts(partIdx) = "' Singleton instance" & vbCrLf : partIdx = partIdx + 1
  171. parts(partIdx) = "Dim " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1
  172. parts(partIdx) = "Function " & funcName & "()" & vbCrLf : partIdx = partIdx + 1
  173. parts(partIdx) = " If IsEmpty(" & className & "__Singleton) Then" & vbCrLf : partIdx = partIdx + 1
  174. parts(partIdx) = " Set " & className & "__Singleton = New " & className & vbCrLf : partIdx = partIdx + 1
  175. parts(partIdx) = " End If" & vbCrLf : partIdx = partIdx + 1
  176. parts(partIdx) = " Set " & funcName & " = " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1
  177. parts(partIdx) = "End Function" & vbCrLf : partIdx = partIdx + 1
  178. parts(partIdx) = "%>" & vbCrLf : partIdx = partIdx + 1
  179. ' Join array for performance
  180. ReDim Preserve parts(partIdx - 1)
  181. BuildController = Join(parts, "")
  182. End Function
  183. '==============================================================
  184. ' Validate identifier (controller name, action name, parameter name)
  185. '==============================================================
  186. Function IsValidIdentifier(name)
  187. If IsEmpty(name) Or Len(name) = 0 Then
  188. IsValidIdentifier = False
  189. Exit Function
  190. End If
  191. Dim i, ch
  192. For i = 1 To Len(name)
  193. ch = Mid(name, i, 1)
  194. ' Allow a-z, A-Z, 0-9, and underscore only
  195. If Not ((ch >= "a" And ch <= "z") Or _
  196. (ch >= "A" And ch <= "Z") Or _
  197. (ch >= "0" And ch <= "9") Or _
  198. ch = "_") Then
  199. IsValidIdentifier = False
  200. Exit Function
  201. End If
  202. Next
  203. IsValidIdentifier = True
  204. End Function
  205. '==============================================================
  206. ' Routes: conventional action names map to REST-style routes, anything else is GET /<controller>/<action>[/:param]
  207. '==============================================================
  208. Function BuildRoutes(ctrlName, actionsArray)
  209. Dim r, base, i, def, nm, prm, verb, path, line, k, pa
  210. base = "/" & LCase(ctrlName)
  211. r = vbTab & "' " & ctrlName & " (generated by generateController.vbs)" & vbCrLf
  212. For i = 0 To UBound(actionsArray)
  213. def = Trim(actionsArray(i))
  214. If Len(def) > 0 Then
  215. nm = def
  216. prm = ""
  217. If InStr(def, "(") > 0 Then
  218. nm = Trim(Left(def, InStr(def, "(") - 1))
  219. prm = Trim(Replace(Mid(def, InStr(def, "(") + 1), ")", ""))
  220. End If
  221. verb = "GET"
  222. Select Case LCase(nm)
  223. Case "index": path = base
  224. Case "newform": path = base & "/new"
  225. Case "create": path = base : verb = "POST"
  226. Case "show": path = base & "/:id"
  227. Case "edit": path = base & "/:id/edit"
  228. Case "update": path = base & "/:id" : verb = "POST"
  229. Case "delete": path = base & "/:id/delete" : verb = "POST"
  230. Case Else
  231. path = base & "/" & LCase(nm)
  232. If Len(prm) > 0 Then
  233. pa = Split(prm, ",")
  234. For k = 0 To UBound(pa)
  235. path = path & "/:" & Trim(pa(k))
  236. Next
  237. End If
  238. End Select
  239. r = r & vbTab & "router.AddRoute """ & verb & """, """ & path & """, """ & ctrlName & "Controller"", """ & nm & """" & vbCrLf
  240. End If
  241. Next
  242. BuildRoutes = r & vbCrLf
  243. End Function
  244. '==============================================================
  245. ' File helpers (wiring)
  246. '==============================================================
  247. Function ReadFile(path)
  248. Dim ts
  249. Set ts = fso.OpenTextFile(Server.MapPath(path), 1)
  250. ReadFile = ts.ReadAll
  251. ts.Close
  252. End Function
  253. Sub WriteWhole(path, text)
  254. Dim tf
  255. Set tf = fso.CreateTextFile(Server.MapPath(path), True)
  256. tf.Write text
  257. tf.Close
  258. End Sub
  259. Function EolOf(text)
  260. If InStr(text, vbCrLf) > 0 Then
  261. EolOf = vbCrLf
  262. Else
  263. EolOf = vbLf
  264. End If
  265. End Function
  266. Function WithEol(text, eol)
  267. WithEol = Replace(Replace(text, vbCrLf, vbLf), vbLf, eol)
  268. End Function
  269. ' Appends a line unless marker is already in the file
  270. Sub AppendIfMissing(path, marker, line)
  271. Dim s, eol
  272. s = ReadFile(path)
  273. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  274. eol = EolOf(s)
  275. If Right(s, 1) <> vbLf Then s = s & eol
  276. WriteWhole path, s & line & eol
  277. End Sub
  278. ' Inserts newText on a new line after the first line containing anchor (unless marker is already present)
  279. Sub InsertAfterLine(path, anchor, marker, newText)
  280. Dim s, eol, pos, eolPos
  281. s = ReadFile(path)
  282. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  283. eol = EolOf(s)
  284. pos = InStr(1, s, anchor, vbTextCompare)
  285. If pos = 0 Then
  286. WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add this yourself: " & newText
  287. Exit Sub
  288. End If
  289. eolPos = InStr(pos, s, eol)
  290. WriteWhole path, Left(s, eolPos - 1) & eol & newText & Mid(s, eolPos)
  291. End Sub
  292. ' Inserts newText before the line containing anchor (unless marker is already present)
  293. Sub InsertBeforeLine(path, anchor, marker, newText)
  294. Dim s, eol, pos, lineStart
  295. s = ReadFile(path)
  296. If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub
  297. eol = EolOf(s)
  298. pos = InStr(1, s, anchor, vbTextCompare)
  299. If pos = 0 Then
  300. WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add the routes yourself:" & vbCrLf & newText
  301. Exit Sub
  302. End If
  303. lineStart = InStrRev(s, eol, pos)
  304. If lineStart = 0 Then
  305. lineStart = 1
  306. Else
  307. lineStart = lineStart + Len(eol)
  308. End If
  309. WriteWhole path, Left(s, lineStart - 1) & WithEol(newText, eol) & Mid(s, lineStart)
  310. End Sub

Powered by TurnKey Linux.