'============================================================== ' generateController.vbs ' ' Usage (runs under the AxonASP CLI in the container): ' scripts\gen.cmd controller ControllerName "Sub1;Sub2(param1,param2);Sub3" ' ' Generates a controller class file with specified action methods '============================================================== Option Explicit ' AxonASP CLI: provide the WScript object (see _cli.vbs) Dim shimFso Set shimFso = CreateObject("Scripting.FileSystemObject") ExecuteGlobal shimFso.OpenTextFile(Server.MapPath("/scripts/_cli.vbs"), 1).ReadAll Dim args, controllerName, subsList, subs, fso, fileName, outputPath ' Parse arguments Set args = WScript.Arguments If args.Count < 2 Then WScript.Echo "Usage: scripts\gen.cmd controller ControllerName ""Sub1;Sub2(param1,param2);...""" WScript.Echo "" WScript.Echo "Example: scripts\gen.cmd controller User ""Index;Show(id);Create;Update(id)""" WScript.Quit 1 End If controllerName = args(0) subsList = args(1) ' Validate controller name format (alphanumeric and underscore only) If Not IsValidIdentifier(controllerName) Then WScript.Echo "Error: Invalid controller name format. Only letters, numbers, and underscores are allowed." WScript.Quit 1 End If ' Parse action methods ' (stray double quotes are dropped: cmd/bash quoting can pass them through literally) subs = Split(Replace(subsList, """", ""), ";") ' Determine output path Set fso = CreateObject("Scripting.FileSystemObject") outputPath = Server.MapPath("/app/controllers") fileName = fso.BuildPath(outputPath, controllerName & "Controller.asp") ' Check if file already exists If fso.FileExists(fileName) Then WScript.Echo "Error: Controller file already exists: " & fileName WScript.Echo "Delete the existing file or choose a different controller name." WScript.Quit 1 End If ' Generate the controller file Dim content content = BuildController(controllerName, subs) ' Write to file Dim file Set file = fso.CreateTextFile(fileName, True, False) file.Write content file.Close Set file = Nothing WScript.Echo "Controller file created: " & fileName ' ---- wire it up: include, whitelist, routes AppendIfMissing "/app/controllers/autoload_controllers.asp", controllerName & "Controller.asp", "" InsertAfterLine "/core/lib.ControllerRegistry.asp", "RegisterController ""errorcontroller""", """" & LCase(controllerName) & "controller""", _ " RegisterController """ & LCase(controllerName) & "controller""" Dim routeText routeText = BuildRoutes(controllerName, subs) InsertBeforeLine "/public/Default.asp", "' Dispatch the request", """" & controllerName & "Controller""", routeText WScript.Echo "Wired: autoload_controllers.asp, ControllerRegistry, and these routes in public/Default.asp:" WScript.Echo routeText WScript.Echo "Next: fill in the actions, add views under app/views/" & controllerName & "/, restart the container (docker restart axonasp)." '============================================================== ' Build controller content using array-based string building '============================================================== Function BuildController(ctrlName, actionsArray) Dim parts() ReDim parts(500) ' Pre-allocate for performance Dim partIdx : partIdx = 0 Dim className : className = ctrlName & "Controller_Class" Dim funcName : funcName = ctrlName & "Controller" ' Header comments parts(partIdx) = "<%" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "' Auto-generated Controller: " & ctrlName & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "' Generated on " & Now() & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "' Generator: generateController.vbs v1.0" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "'" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "' The include, whitelist entry and routes were added automatically (see public/Default.asp)." & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf & vbCrLf : partIdx = partIdx + 1 ' Class definition parts(partIdx) = "Class " & className & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Private m_useLayout" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Private m_title" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 ' Class_Initialize parts(partIdx) = " Private Sub Class_Initialize()" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " m_useLayout = True" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " m_title = """ & ctrlName & """" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 ' useLayout property parts(partIdx) = " Public Property Get useLayout" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " useLayout = m_useLayout" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Public Property Let useLayout(v)" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " m_useLayout = v" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 ' Title property parts(partIdx) = " Public Property Get Title" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Title = m_title" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Public Property Let Title(v)" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " m_title = v" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End Property" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 ' Generate action methods Dim i, actionDef, namePart, paramsPart, paramsArr For i = 0 To UBound(actionsArray) actionDef = Trim(actionsArray(i)) ' Skip empty entries If Len(actionDef) > 0 Then ' Parse action name and parameters If InStr(actionDef, "(") > 0 Then namePart = Trim(Left(actionDef, InStr(actionDef, "(") - 1)) paramsPart = Mid(actionDef, InStr(actionDef, "(") + 1) paramsPart = Left(paramsPart, Len(paramsPart) - 1) ' Strip trailing ')' paramsPart = Trim(paramsPart) ' Validate parameter names If Len(paramsPart) > 0 Then paramsArr = Split(paramsPart, ",") Dim j, paramName For j = 0 To UBound(paramsArr) paramName = Trim(paramsArr(j)) If Not IsValidIdentifier(paramName) Then WScript.Echo "Error: Invalid parameter name '" & paramName & "' in action '" & actionDef & "'" WScript.Quit 1 End If Next End If Else namePart = Trim(actionDef) paramsPart = "" End If ' Validate action name If Not IsValidIdentifier(namePart) Then WScript.Echo "Error: Invalid action name '" & namePart & "'" WScript.Quit 1 End If ' Generate method stub parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " ' Action: " & namePart & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " '---------------------------------------------------------------" & vbCrLf : partIdx = partIdx + 1 If paramsPart = "" Then parts(partIdx) = " Public Sub " & namePart & "()" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Response.Write """ & namePart & " action called""" & vbCrLf : partIdx = partIdx + 1 Else parts(partIdx) = " Public Sub " & namePart & "(" & paramsPart & ")" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " ' TODO: Implement " & namePart & " action" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Response.Write """ & namePart & " called
""" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Response.Write ""Parameters:
""" & vbCrLf : partIdx = partIdx + 1 ' Echo each parameter paramsArr = Split(paramsPart, ",") For j = 0 To UBound(paramsArr) paramName = Trim(paramsArr(j)) parts(partIdx) = " Response.Write """ & paramName & " = "" & Server.HTMLEncode(CStr(" & paramName & ")) & ""
""" & vbCrLf : partIdx = partIdx + 1 Next End If parts(partIdx) = " End Sub" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 End If Next ' End class parts(partIdx) = "End Class" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = vbCrLf : partIdx = partIdx + 1 ' Singleton pattern parts(partIdx) = "' Singleton instance" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "Dim " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "Function " & funcName & "()" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " If IsEmpty(" & className & "__Singleton) Then" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Set " & className & "__Singleton = New " & className & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " End If" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = " Set " & funcName & " = " & className & "__Singleton" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "End Function" & vbCrLf : partIdx = partIdx + 1 parts(partIdx) = "%>" & vbCrLf : partIdx = partIdx + 1 ' Join array for performance ReDim Preserve parts(partIdx - 1) BuildController = Join(parts, "") End Function '============================================================== ' Validate identifier (controller name, action name, parameter name) '============================================================== Function IsValidIdentifier(name) If IsEmpty(name) Or Len(name) = 0 Then IsValidIdentifier = False Exit Function End If Dim i, ch For i = 1 To Len(name) ch = Mid(name, i, 1) ' Allow a-z, A-Z, 0-9, and underscore only If Not ((ch >= "a" And ch <= "z") Or _ (ch >= "A" And ch <= "Z") Or _ (ch >= "0" And ch <= "9") Or _ ch = "_") Then IsValidIdentifier = False Exit Function End If Next IsValidIdentifier = True End Function '============================================================== ' Routes: conventional action names map to REST-style routes, anything else is GET //[/:param] '============================================================== Function BuildRoutes(ctrlName, actionsArray) Dim r, base, i, def, nm, prm, verb, path, line, k, pa base = "/" & LCase(ctrlName) r = vbTab & "' " & ctrlName & " (generated by generateController.vbs)" & vbCrLf For i = 0 To UBound(actionsArray) def = Trim(actionsArray(i)) If Len(def) > 0 Then nm = def prm = "" If InStr(def, "(") > 0 Then nm = Trim(Left(def, InStr(def, "(") - 1)) prm = Trim(Replace(Mid(def, InStr(def, "(") + 1), ")", "")) End If verb = "GET" Select Case LCase(nm) Case "index": path = base Case "newform": path = base & "/new" Case "create": path = base : verb = "POST" Case "show": path = base & "/:id" Case "edit": path = base & "/:id/edit" Case "update": path = base & "/:id" : verb = "POST" Case "delete": path = base & "/:id/delete" : verb = "POST" Case Else path = base & "/" & LCase(nm) If Len(prm) > 0 Then pa = Split(prm, ",") For k = 0 To UBound(pa) path = path & "/:" & Trim(pa(k)) Next End If End Select r = r & vbTab & "router.AddRoute """ & verb & """, """ & path & """, """ & ctrlName & "Controller"", """ & nm & """" & vbCrLf End If Next BuildRoutes = r & vbCrLf End Function '============================================================== ' File helpers (wiring) '============================================================== Function ReadFile(path) Dim ts Set ts = fso.OpenTextFile(Server.MapPath(path), 1) ReadFile = ts.ReadAll ts.Close End Function Sub WriteWhole(path, text) Dim tf Set tf = fso.CreateTextFile(Server.MapPath(path), True) tf.Write text tf.Close End Sub Function EolOf(text) If InStr(text, vbCrLf) > 0 Then EolOf = vbCrLf Else EolOf = vbLf End If End Function Function WithEol(text, eol) WithEol = Replace(Replace(text, vbCrLf, vbLf), vbLf, eol) End Function ' Appends a line unless marker is already in the file Sub AppendIfMissing(path, marker, line) Dim s, eol s = ReadFile(path) If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub eol = EolOf(s) If Right(s, 1) <> vbLf Then s = s & eol WriteWhole path, s & line & eol End Sub ' Inserts newText on a new line after the first line containing anchor (unless marker is already present) Sub InsertAfterLine(path, anchor, marker, newText) Dim s, eol, pos, eolPos s = ReadFile(path) If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub eol = EolOf(s) pos = InStr(1, s, anchor, vbTextCompare) If pos = 0 Then WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add this yourself: " & newText Exit Sub End If eolPos = InStr(pos, s, eol) WriteWhole path, Left(s, eolPos - 1) & eol & newText & Mid(s, eolPos) End Sub ' Inserts newText before the line containing anchor (unless marker is already present) Sub InsertBeforeLine(path, anchor, marker, newText) Dim s, eol, pos, lineStart s = ReadFile(path) If InStr(1, s, marker, vbTextCompare) > 0 Then Exit Sub eol = EolOf(s) pos = InStr(1, s, anchor, vbTextCompare) If pos = 0 Then WScript.Echo "Warning: anchor '" & anchor & "' not found in " & path & "; add the routes yourself:" & vbCrLf & newText Exit Sub End If lineStart = InStrRev(s, eol, pos) If lineStart = 0 Then lineStart = 1 Else lineStart = lineStart + Len(eol) End If WriteWhole path, Left(s, lineStart - 1) & WithEol(newText, eol) & Mid(s, lineStart) End Sub