@@ -43,18 +43,6 @@ component displayname="FW/1 Create Controller Command"
4343 extends = " commandbox.system.BaseCommand"
4444 excludeFromHelp = false
4545{
46- public any function init () {
47- // ascii codes
48- variables .ascii = {
49- br = Chr (10 ) & Chr (13 ), // line break
50- br2 = Chr (10 ) & Chr (13 ) & Chr (10 ) & Chr (13 ), // line break 2x
51- tb = Chr (9 ), // tab
52- tb2 = Chr (9 ) & Chr (9 ), // tab 2x
53- };
54-
55- return this ;
56- }
57-
5846 /**
5947 * @name.hint Name of the controller to create.
6048 * @actions.hint A comma-delimited list of actions to generate
@@ -81,26 +69,45 @@ component displayname="FW/1 Create Controller Command"
8169 // This help readability so the success messages aren't up against the previous command line
8270 print .line ();
8371 // Generate controller with actions passed
84- savecontent variable = " controllerContent" {
85- var index = 0 ;
86- var actionArray = listToArray ( arguments .actions );
87- writeOutput ( " component {" & ascii .br );
88- for ( var this Action in actionArray ) {
89- index ++ ;
90- writeOutput ( ascii .tb & " public void function #this Action #(struct rc = {}) {" & ascii .br );
91- writeOutput ( ascii .tb2 & ascii .br );
92- writeOutput ( ascii .tb & " }" );
93- if ( index ! = arrayLen ( actionArray ) ) { writeOutput ( ascii .br2 ) }
94- }
95- writeOutput ( ascii .br & " }" );
96- }
72+ var controllerContent = scaffoldController ( arguments .name , listToArray ( arguments .actions ) );
9773 var controllerPath = " #arguments .directory #/#arguments .name #.cfc" ;
9874 // Create dir if it doesn't exist
9975 directoryCreate ( getDirectoryFromPath ( controllerPath ), true , true );
10076 // Create files
10177 file action = " write" file = " #controllerPath #" mode = " 777" output = " #controllerContent #" ;
10278 print .greenLine ( " Created #controllerPath #" );
79+ // Create views?
80+ if ( arguments .views ) {
81+ for ( var action in listToArray ( arguments .actions ) ) {
82+ var viewPath = arguments .viewsDirectory & " /" & arguments .name & " /" & action & " .cfm" ;
83+ // Create dir if it doesn't exist
84+ directoryCreate ( getDirectoryFromPath ( viewPath ), true , true );
85+ // Create view
86+ fileWrite ( viewPath , " <cfoutput>#cr ##chr (9 ) #<h1>#arguments .name #.#action #</h1>#cr #</cfoutput>" );
87+ print .greenLine ( " Created " & arguments .viewsDirectory & " /" & arguments .name & " /" & action & " .cfm" );
88+ }
89+ }
10390 // Open file
10491 if ( arguments .open ){ openPath ( controllerPath ); }
10592 }
93+
94+ private string function scaffoldController (
95+ required string name ,
96+ array actions = [" default" ]
97+ ) {
98+ savecontent variable = " controller" {
99+ var index = 0 ;
100+ writeOutput ( " component {" & cr );
101+ for ( var action in arguments .actions ) {
102+ index ++ ;
103+ writeOutput ( chr (9 ) & " public void function #action #(struct rc = {}) {" & cr );
104+ writeOutput ( chr (9 ) & chr (9 ) & cr );
105+ writeOutput ( chr (9 ) & " }" );
106+ if ( index ! = arrayLen ( arguments .actions ) ) { writeOutput ( cr & cr ) }
107+ }
108+ writeOutput ( cr & " }" );
109+ }
110+
111+ return controller ;
112+ }
106113}
0 commit comments