ferrisup 0.2.5

A versatile Rust project bootstrapping tool - start anywhere, scale anywhere
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# FerrisUp Template Authoring Guide

This guide explains how to create and customize templates for the FerrisUp CLI, including the advanced features like conditional content, variable substitution, and custom next steps.

## Table of Contents

1. [Template Directory Structure]#template-directory-structure
2. [Template Configuration File]#template-configuration-file
3. [Variables and Substitution]#variables-and-substitution
4. [Conditional Logic]#conditional-logic
5. [File Transformations]#file-transformations
6. [Best Practices]#best-practices
7. [Examples]#examples
8. [Working with the Enhanced Template Configuration Framework]#working-with-the-enhanced-template-configuration-framework

## Template Directory Structure

A FerrisUp template consists of:

- A root directory with the template name
- A `template.json` configuration file
- Template files that will be copied to the target project

Example:
```
templates/
└── my-template/
    ├── template.json
    ├── README.md
    ├── src/
    │   └── main.rs
    └── Cargo.toml.template
```

## Template Configuration File

The `template.json` file defines the template's behavior and options. Here's a complete reference:

```json
{
  "name": "template-name",
  "description": "Template description",
  "type": "binary|library",
  "files": [
    {
      "source": "relative/path/in/template",
      "target": "destination/path/in/project"
    }
  ],
  "options": [
    {
      "name": "variable_name",
      "description": "User-friendly description",
      "type": "select|input|boolean",
      "options": ["option1", "option2"],
      "default": "option1"
    }
  ],
  "transformations": [
    {
      "pattern": "path/to/file",
      "replacement": {
        "condition1": "replacement1",
        "condition2": "replacement2"
      }
    }
  ],
  "dependencies": {
    "default": [
      "dependency1 = \"0.1\"",
      "dependency2 = { version = \"0.2\", features = [\"feature1\"] }"
    ],
    "condition1": [
      "extra_dependency = \"0.3\""
    ]
  },
  "dev-dependencies": {
    "default": [
      "dev-dependency1 = \"0.1\""
    ]
  },
  "next_steps": {
    "default": [
      "cd {{project_name}}",
      "cargo build"
    ],
    "conditional": [
      {
        "when": "variable_name == 'option1'",
        "steps": [
          "Additional steps for option1"
        ]
      }
    ]
  },
  "post_setup_info": {
    "conditional": [
      {
        "when": "variable_name == 'option1'",
        "message": "Additional info for option1"
      }
    ]
  }
}
```

## Variables and Substitution

Templates can use variables in two ways:

1. In template files using Handlebars syntax: `{{variable_name}}`
2. In the template.json configuration for conditional logic

### Built-in Variables

- `{{project_name}}`: The name of the project being created

### Custom Variables

Define custom variables in the `options` section of `template.json`. These variables will be:

1. Automatically prompted to the user during project creation
2. Available for substitution in template files
3. Available for conditional logic in `template.json`

## Conditional Logic

### In Template Files

You can use conditional blocks in template files with Handlebars syntax:

```handlebars
{{#if (eq variable_name "value")}}
This content will only appear if variable_name equals "value"
{{else}}
This is the fallback content
{{/if}}
```

### In template.json

Use the `conditional` property in the `next_steps` and `post_setup_info` sections:

```json
"next_steps": {
  "conditional": [
    {
      "when": "variable_name == 'value'",
      "steps": ["These steps will only appear if the condition is met"]
    }
  ]
}
```

## File Transformations

The `transformations` section allows dynamically selecting different source files based on variable values:

```json
"transformations": [
  {
    "pattern": "source/file/path",
    "replacement": {
      "option1": "source/file/for/option1",
      "option2": "source/file/for/option2"
    }
  }
]
```

### Important Note on File Paths

When using transformations, the `pattern` should match the **target** path from the `files` section, not just the filename. For example:

```json
"files": [
  {
    "source": "main.rs",
    "target": "src/main.rs"
  }
],
"transformations": [
  {
    "pattern": "src/main.rs",  // CORRECT: matches the target path
    "replacement": {
      "option1": "main.rs.option1"
    }
  }
]
```

**INCORRECT** transformation pattern that won't work:
```json
"pattern": "main.rs",  // WRONG: doesn't match the target path
```

## Best Practices

1. **Keep templates modular**: Only include what's necessary
2. **Use descriptive variable names**: Make it clear what each option controls
3. **Provide good defaults**: Users should be able to accept defaults and get a working project
4. **Document next steps**: Include clear instructions on how to work with the generated project
5. **Test with and without interaction**: Ensure templates work in both interactive and non-interactive modes
6. **Use consistent formatting**: Follow Rust style guidelines in template code

## Examples

### Basic Template with Options

```json
{
  "name": "web-server",
  "description": "A web server template",
  "type": "binary",
  "files": [
    {
      "source": "src/main.rs",
      "target": "src/main.rs"
    },
    {
      "source": "Cargo.toml.template",
      "target": "Cargo.toml"
    },
    {
      "source": "README.md",
      "target": "README.md"
    }
  ],
  "options": [
    {
      "name": "framework",
      "description": "Web framework to use",
      "type": "select",
      "options": ["axum", "actix", "rocket"],
      "default": "axum"
    }
  ],
  "next_steps": {
    "default": [
      "cd {{project_name}}",
      "cargo run"
    ],
    "conditional": [
      {
        "when": "framework == 'rocket'",
        "steps": [
          "cd {{project_name}}",
          "# Note: Rocket requires nightly Rust",
          "rustup override set nightly",
          "cargo run"
        ]
      }
    ]
  }
}
```

### Complex Template with Multiple Options

See the embedded template for a complete example of a complex template with multiple options and conditional content.

## Working with the Enhanced Template Configuration Framework

FerrisUp now supports a more flexible template configuration framework that allows for conditional content and variable substitution. This guide explains how to create and customize templates using this framework.

### Template Configuration File

Each template has a `template.json` file that defines its structure and behavior. Here's an example of a template with conditional logic:

```json
{
  "name": "example-template",
  "description": "Example template with conditional logic",
  "type": "binary",
  "files": [
    {
      "source": "README.md",
      "target": "README.md"
    },
    {
      "source": "Cargo.toml.template",
      "target": "Cargo.toml"
    }
  ],
  "options": [
    {
      "name": "feature",
      "description": "Which feature do you want to enable?",
      "type": "select",
      "options": ["basic", "advanced", "expert"],
      "default": "basic"
    }
  ],
  "conditional_files": [
    {
      "when": "feature == 'basic'",
      "files": [
        {
          "source": "basic/src/main.rs",
          "target": "src/main.rs"
        }
      ]
    },
    {
      "when": "feature == 'advanced'",
      "files": [
        {
          "source": "advanced/src/main.rs",
          "target": "src/main.rs"
        }
      ]
    },
    {
      "when": "feature == 'expert'",
      "files": [
        {
          "source": "expert/src/main.rs",
          "target": "src/main.rs"
        }
      ]
    }
  ],
  "dependencies": {
    "default": [
      "serde = { version = \"1.0\", features = [\"derive\"] }"
    ],
    "advanced": [
      "tokio = { version = \"1.36\", features = [\"full\"] }"
    ],
    "expert": [
      "axum = \"0.7\"",
      "tower = \"0.4\""
    ]
  },
  "next_steps": {
    "default": [
      "cd {{project_name}}",
      "cargo run"
    ],
    "conditional": [
      {
        "when": "feature == 'basic'",
        "steps": [
          "# This is a basic project with minimal features"
        ]
      },
      {
        "when": "feature == 'advanced'",
        "steps": [
          "# This project includes async support with Tokio"
        ]
      },
      {
        "when": "feature == 'expert'",
        "steps": [
          "# This project includes a web server with Axum"
        ]
      }
    ]
  }
}
```

### Template Structure

- **name**: The name of the template.
- **description**: A brief description of the template.
- **type**: The type of project (`binary`, `library`, etc.).
- **files**: Array of files to be included in all projects.
- **options**: User-selectable options that affect the template content.
- **conditional_files**: Files to include based on user selections.
- **dependencies**: Rust dependencies to add to Cargo.toml.
- **next_steps**: Instructions shown to the user after project creation.

### Template Variables

Template variables are available within template files and can be used with Handlebars syntax:

- **project_name**: The name of the project being created.
- **Any user-selected option**: Values from the `options` section.

### File Templates

Files with a `.template` extension will be processed with Handlebars to substitute variables. For example, in `Cargo.toml.template`:

```toml
[package]
name = "{{project_name}}"
version = "0.1.0"
edition = "2021"

[dependencies]
```

### Conditional Files

The `conditional_files` section allows you to include different files based on user selections. The `when` expression is evaluated against the user's selected options.

### Conditional Next Steps

The `next_steps` section can include both default steps shown to all users and conditional steps shown only when specific conditions are met.

### Template Directory Structure

For templates with conditional content, organize your files in subdirectories that match the condition values. For example:

```
templates/
  └── my-template/
      ├── template.json
      ├── README.md
      ├── Cargo.toml.template
      ├── basic/
      │   └── src/
      │       └── main.rs
      ├── advanced/
      │   └── src/
      │       └── main.rs
      └── expert/
          └── src/
              └── main.rs
```

This structure makes it easy to maintain different versions of files for different user selections.

### Best Practices

1. **Use Descriptive Option Names**: Make sure option names clearly describe what they affect.
2. **Provide Helpful Descriptions**: Option descriptions should help users understand the implications of their choices.
3. **Set Sensible Defaults**: Always specify default values for options.
4. **Organize Files Logically**: Keep related files in subdirectories that match option values.
5. **Include Clear Next Steps**: Provide specific instructions for users to get started with their new project.