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+ }
0 commit comments