Skip to content

Commit 1d12ae9

Browse files
author
Tony Junkes
committed
Add create bean command / cleanup
1 parent 21e436e commit 1d12ae9

File tree

6 files changed

+111
-19
lines changed

6 files changed

+111
-19
lines changed

fw1/create/app.cfc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ component displayname="FW/1 Create Application Command"
2929
{
3030
property name="packageService" inject="PackageService";
3131
property name="parser" inject="Parser";
32-
property name="skeletonLocation";
3332

3433
public any function init() {
3534
super.init();
@@ -47,9 +46,9 @@ component displayname="FW/1 Create Application Command"
4746
* @package.hint Generate a box.json to make the current directory a package.
4847
*/
4948
public void function run(
50-
name = "My FW/1 App",
51-
skeleton = "Skeleton",
52-
directory = getCWD(),
49+
string name = "My FW/1 App",
50+
string skeleton = "Skeleton",
51+
string directory = getCWD(),
5352
boolean installFW1 = false,
5453
boolean package = true
5554
) {

fw1/create/bean.cfc

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// BASED FROM THE COLDBOX "CONTROLLER.CFC" COMMAND - https://github.com/Ortus-Solutions/commandbox/tree/master/src/cfml/commands/coldbox/create
2+
/**
3+
* Create a new bean in an existing FW/1 application.
4+
* .
5+
* {code:bash}
6+
* fw1 create bean MyBean
7+
* {code}
8+
* .
9+
* The command can also take a list of bean names.
10+
* .
11+
* {code:bash}
12+
* fw1 create bean MyBean,MyOtherBean
13+
* {code}
14+
* .
15+
* By default, the bean will be created in /model/beans but can be
16+
* overridden.
17+
* .
18+
* {code:bash}
19+
* fw1 create bean MyBean myDirectory
20+
* {code}
21+
* .
22+
* Bean file names are formatted as initial caps.
23+
* This can be altered by setting initialCaps to false.
24+
* .
25+
* {code:bash}
26+
* fw1 create bean MyBean /model/beans false
27+
* {code}
28+
* .
29+
* Once created, the bean can be opened in your default editor by
30+
* setting the "open" param to true.
31+
* .
32+
* {code:bash}
33+
* fw1 create bean MyBean --open
34+
* {code}
35+
*/
36+
component displayname="FW/1 Create Bean Command"
37+
extends="commandbox.system.BaseCommand"
38+
excludeFromHelp=false
39+
{
40+
public any function init() {
41+
// ascii codes
42+
variables.ascii = {
43+
br = Chr(10) & Chr(13), // line break
44+
br2 = Chr(10) & Chr(13) & Chr(10) & Chr(13), // line break 2x
45+
tb = Chr(9), // tab
46+
tb2 = Chr(9) & Chr(9), // tab 2x
47+
};
48+
49+
return this;
50+
}
51+
52+
/**
53+
* @name.hint Name of the bean to create. For packages, specify name as 'myPackage/MyBean'
54+
* @directory.hint The base directory to create your bean in. Defaults to 'beans'.
55+
* @open.hint Open the bean once generated.
56+
*/
57+
public void function run(
58+
required string name,
59+
string directory = "model/beans",
60+
boolean initialCaps = true,
61+
boolean open = false
62+
) {
63+
for ( var bean in listToArray( arguments.name ) ) {
64+
// This will make each directory canonical and absolute
65+
arguments.directory = fileSystemUtil.resolvePath( arguments.directory );
66+
// Validate directory
67+
if ( !directoryExists( arguments.directory ) ) { directoryCreate( arguments.directory ); }
68+
// Allow dot-delimited paths
69+
bean = replace( bean, ".", "/", "all" );
70+
// Make bean name intital caps?
71+
var beanName = arguments.initialCaps
72+
? reReplace( listLast( bean, "/" ), "\b(\w)", "\u\1", "all" )
73+
: listLast( bean, "/" );
74+
// This help readability so the success messages aren't up against the previous command line
75+
print.line();
76+
// Generate bean with init function
77+
savecontent variable="beanContent" {
78+
writeOutput( "component {" & ascii.br );
79+
writeOutput( ascii.tb & "public #beanName# function init() {" & ascii.br );
80+
writeOutput( ascii.tb2 & "return this;" & ascii.br );
81+
writeOutput( ascii.tb & "}" );
82+
writeOutput( ascii.br & "}" );
83+
}
84+
var beanPath = "#arguments.directory#/#beanName#.cfc";
85+
// Create dir if it doesn't exist
86+
directoryCreate( getDirectoryFromPath( beanPath ), true, true );
87+
// Create files
88+
file action="write" file="#beanPath#" mode="777" output="#beanContent#";
89+
print.greenLine( "Created #beanPath#" );
90+
// Open file
91+
if ( arguments.open ){ openPath( beanPath ); }
92+
}
93+
}
94+
}

fw1/create/controller.cfc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ component displayname="FW/1 Create Controller Command"
5656
}
5757

5858
/**
59-
* @name.hint Name of the controller to create without the .cfc. For packages, specify name as 'myPackage/myController'
59+
* @name.hint Name of the controller to create.
6060
* @actions.hint A comma-delimited list of actions to generate
6161
* @directory.hint The base directory to create your controller in. Defaults to 'controllers'.
6262
* @views.hint Generate a view for each action.
63-
* @viewsDirectory.hint The directory where your views are stored. Only used if views is set to true.
63+
* @viewsDirectory.hint The directory where your views are stored.
6464
* @open.hint Open the controller once generated.
6565
*/
6666
public void function run(
67-
required name,
68-
actions = "default",
69-
directory = "controllers",
67+
required string name,
68+
string actions = "default",
69+
string directory = "controllers",
7070
boolean views = true,
71-
viewsDirectory = "views",
71+
string viewsDirectory = "views",
7272
boolean open = false
7373
) {
7474
// This will make each directory canonical and absolute
@@ -87,7 +87,7 @@ component displayname="FW/1 Create Controller Command"
8787
writeOutput( "component {" & ascii.br );
8888
for ( var thisAction in actionArray ) {
8989
index++;
90-
writeOutput( ascii.tb & "public void function #thisAction#() {" & ascii.br );
90+
writeOutput( ascii.tb & "public void function #thisAction#(struct rc = {}) {" & ascii.br );
9191
writeOutput( ascii.tb2 & ascii.br );
9292
writeOutput( ascii.tb & "}" );
9393
if ( index != arrayLen( actionArray ) ) { writeOutput( ascii.br2 ) }
@@ -101,6 +101,6 @@ component displayname="FW/1 Create Controller Command"
101101
file action="write" file="#controllerPath#" mode="777" output="#controllerContent#";
102102
print.greenLine( "Created #controllerPath#" );
103103
// Open file
104-
if ( arguments.open ){ openPath( controllerPath ); }
104+
if ( arguments.open ){ openPath( controllerPath ); }
105105
}
106106
}

fw1/create/layout.cfc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ component displayname="FW/1 Create Layout Command"
2020
* @directory.hint The base directory to create your layout in.
2121
*/
2222
public void function run(
23-
required name,
24-
boolean helper = false,
25-
directory = "layouts"
23+
required string name,
24+
string directory = "layouts"
2625
) {
2726
// This will make each directory canonical and absolute
2827
arguments.directory = fileSystemUtil.resolvePath( arguments.directory );

fw1/create/subsystem.cfc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ component displayname="FW/1 Create Subsystem Command"
2222
* @directory.hint The directory to create the app in. Defaults to the subsystem name passed in.
2323
*/
2424
public void function run(
25-
required name,
26-
directory = getCWD()
25+
required string name,
26+
string directory = getCWD()
2727
) {
2828
var skeletonLocation = getDirectoryFromPath( getCurrentTemplatePath() ) & "/../resources/templates/";
2929
// This will make the directory canonical and absolute

fw1/create/view.cfc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ component displayname="FW/1 Create View Command"
2121
* @directory.hint The base directory to create your view in. Defaults to current working directory.
2222
*/
2323
public void function run(
24-
required name,
25-
directory = "views"
24+
required string name,
25+
string directory = "views"
2626
) {
2727
// This will make each directory canonical and absolute
2828
arguments.directory = fileSystemUtil.resolvePath( arguments.directory );

0 commit comments

Comments
 (0)