Wiki source code of Custom Project Templates

Version 39.1 by David Holt on 2009/03/31 19:03

Show last authors
1 {{toc}}{{/toc}}
2
3 = Introduction =
4
5 WOLips 3.3 supports the definition of custom project templates. These templates support the declaration of custom configuration parameters and the files within the template are processed using the [[Velocity Template Engine>>http://velocity.apache.org/engine/devel/vtl-reference-guide.html]].
6
7 To use a custom project template, select the "WO Project from Template" option under the WOLips category of the New Project Wizard.
8
9 {{info}}
10
11 !NewProjectFromTemplate.png!
12
13 {{/info}}
14
15 = Creating a Template =
16
17 The declaration of templates is very simple. The template system looks for folders in the following locations (in the following order):
18
19 * The ProjectTemplates folder inside of the WOLips templateengine plugin jar
20 * /Library/Application Support/WOLips/Project Templates
21 * YourHomeDirocuments and Settingspplication DataOLipsroject Templates
22 * YourHomeDirocuments and SettingsppDataocalOLipsroject Templates
23 * /Library/Application Support/WOLips/Project Templates
24
25 If you want to look at the current templates that ships with WOLips, the best way to analyse them is by [[importing the source>>Building WOLips]] of WOLips from Subversion.  The templates are located in //woproject/wolips/core/plugins/org.objectstyle.wolips.templateengine/ProjectTemplates//.
26
27 If you copy a stock template to test it out, renaming the folder is not sufficient for WOLips to show the new Template in the "WO Project From Template" list. You must make sure that the template name is also changed in the template.xml file.
28
29 Without any additional metadata, the template system derives the name of the template from the name of the folder in one of the above locations. For instance, if you create a folder named ",,/Library/Application Support/WOLips/Project Templates/Wonder Application," the template system will offer a template named "Wonder Application" in the template selection dialog. Project templates found later in the list of locations above will override those of the same name from earlier in the list. For instance, a "Wonder Application" template found in /Library will overrode a "Wonder Application" template from /Library.,,
30
31 After creating a template folder, you can create a hierarchy of files and folders within that folder. When you create a project using this template, a copy of all of the files and folders in your template will be used to create the new project. It is up to you to declare //all// of the files within a project, including Eclipse project metadata files like .classpath. You can refer to the built-in project templates as a starting point for creating your own custom templates.
32
33 That's it For creating static boilerplate templates, you're done.
34
35 = Template Metadata and Template Inputs =
36
37 For a static template, the simple process above is enough. However, it's a common requirement to have configuration options for project templates. The WOLips project template engine provides an easy way to declare these options.
38
39 After creating a template using the directions in the above section, you can additionally create a file named "template.xml" inside your project template folder. For instance, in the example above, you would create the file ",,/Library/Application Support/WOLips/Project Templates/Wonder Application/template.xml".,,
40
41 An example template.xml is below:
42
43 {{code value="xml"}}
44
45 <?xml version="1.0" encoding="UTF-8"?>
46 <template name = "Wonder Application">
47 <inputs>
48 <input name = "linkToWonderProjects" type = "Boolean">
49 <question>Link to Wonder Projects?</question>
50 <default>false</default>
51 </input>
52 <input name = "linkToWonderFrameworks" type = "Boolean">
53 <question>Link to Wonder Frameworks?</question>
54 <default>true</default>
55 </input>
56 <input name = "YourFavoriteColor" type = "String">
57 <question>Your Favorite Color?</question>
58 <options>
59 <option name = "Red" value = "#FF0000"/>
60 <option name = "Green" value = "#00FF00"/>
61 <option name = "Blue" value = "#0000FF"/>
62 </options>
63 <default>#FF0000</default>
64 </input>
65 </inputs>
66 </template>
67
68 {{/code}}
69
70 The "name" attribute of the template node overrides the name of the folder the templates are in. For instance, you could have the above template.xml inside a folder named "Template 1" and the template system would consider the name of the template to be "Wonder Application."
71
72 Within a template, you can declare a single "inputs" node that can contain multiple "input" nodes. Each input node corresponds to a variable that will be presented to the user on the second page of the wizard. Each input specifies a "name" attribute, which will become the variable name of the input for later reference in the Velocity templates; and a "type" attribute which can be one of Boolean, String, Package, or Integer. The type value determines the control that will be used to display the input to the user (String = text field, Boolean = checkbox, Integer = spinner, Package = text field, etc). Each input also contains a "question" node, whose value corresponds to the label of the control when displayed to the user. In the above example, the "linkToWonderFrameworks" will display a checkbox to the user with the label "Link to Wonder Frameworks?". Additionally, you can provide a "default" node that defines the default value of the variable. If a default is not specified, the default value will be null for all input types.
73
74 The package type is slight extension to the String type. For a variable declared as type Package, in addition to having your variable bound, you will also have a variable named "yourvariablename//folder" with replaces dots for slashes. For instance, if your variable is named "basePackage," you will also get a variable named "basePackage//folder." This is useful because you can use template variables in folder names on the filesystem.
75
76 Finally, the input system supports the declaration of enumerated types. By declaring an "options" node that contains an ordered set of "option" nodes, you can define the possible values that the user can provide. In the above example, the "YourFavoriteColor" input defines three options: Red, Green, and Blue. Each option node has a "name" attribute, which will be the value displayed to the user, and a "value" attribute, which will be the actual backing value of the selection. The value of the option should be of the type specified in the "type" attribute of the input. For instance, if you declare the input type to be "Integer," your option values should be integer values (in quotes).
77
78 = Using Template Inputs =
79
80 So now that you have template input defined, you will want to be able to use them. The name use used in the "name" attribute of your input declaration will be the name of the variable in your Velocity context. For instance, in the example above, the Velocity variable "linkToWonderProjects" will be bound to the boolean value corresponding to the user's selection, and can be used just like any other velocity variable. The Apache project provides a [[Velocity reference guide>>http://velocity.apache.org/engine/devel/vtl-reference-guide.html]].
81
82 As an example, the Wonder Application template's .classpath file is defined as:
83
84 {{code value="xml"}}
85
86 <?xml version="1.0" encoding="UTF-8"?>
87 <classpath>
88 <classpathentry kind="src" path="Sources"/>
89 #if ($linkToWonderProjects)
90 <classpathentry combineaccessrules="false" kind="src" path="/ERJars"/>
91 <classpathentry combineaccessrules="false" kind="src" path="/ERExtensions"/>
92 <classpathentry combineaccessrules="false" kind="src" path="/ERPrototypes"/>
93 <classpathentry combineaccessrules="false" kind="src" path="/JavaWOExtensions"/>
94 #end
95 #if ($linkToWonderFrameworks)
96 <classpathentry kind="con" path="org.objectstyle.wolips.WO_CLASSPATH/ERExtensions/ERJars/ERPrototypes/JavaWOExtensions/JavaEOAccess/JavaEOControl/JavaFoundation/JavaJDBCAdaptor/JavaWebObjects/JavaXML"/>
97 #else
98 <classpathentry kind="con" path="org.objectstyle.wolips.WO_CLASSPATH/JavaEOAccess/JavaEOControl/JavaFoundation/JavaJDBCAdaptor/JavaWebObjects/JavaXML"/>
99 #end
100 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
101 <classpathentry kind="output" path="bin"/>
102 </classpath>
103
104 {{/code}}
105
106 In addition to variables inside of Velocity templates, you can also use template inputs in folder names. However, because $ is not allowed on some filesystems, we instead surround the variable names with "//" (for instance $someVariable would be //someVariable in the filename or path). As an example, the Wonder Application template has an input named "basePackage" (of type Package), which creates a magic variable named "basePackage//folder" (where the dots are turned into slashes), and the Source folder on the filesystem is named "Wonder Application/Sources///basePackage//folder//".
107
108 Happy templating