planter-cli 3.0.4 → 3.0.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Planter::Prompt::Question do
6
+ let(:question) do
7
+ question = {
8
+ key: 'test',
9
+ prompt: 'CLI Prompt',
10
+ type: :string,
11
+ default: 'default',
12
+ value: nil
13
+ }
14
+ end
15
+
16
+ before do
17
+ Planter.accept_defaults = true
18
+ end
19
+
20
+ describe '#initialize' do
21
+ it 'initializes a new question object' do
22
+ q = described_class.new(question)
23
+ expect(q).to be_a described_class
24
+ end
25
+ end
26
+
27
+ describe '#ask' do
28
+ it 'asks a question' do
29
+ q = described_class.new(question)
30
+ expect(q.ask).to eq('default')
31
+ end
32
+ end
33
+
34
+ describe "#ask with date type" do
35
+ it 'asks a date question' do
36
+ question[:type] = :date
37
+ question[:value] = 'today'
38
+ q = described_class.new(question)
39
+ expect(q.ask).to eq(Date.today.strftime('%Y-%m-%d'))
40
+ end
41
+ end
42
+
43
+ describe "#ask with date type and inline date format" do
44
+ it 'asks a date question' do
45
+ question[:type] = :date
46
+ question[:value] = "today '%Y'"
47
+ q = described_class.new(question)
48
+ expect(q.ask).to eq(Date.today.strftime('%Y'))
49
+ end
50
+ end
51
+
52
+ describe "#ask with date type and date format config" do
53
+ it 'asks a date question' do
54
+ question[:type] = :date
55
+ question[:date_format] = '%Y'
56
+ question[:value] = "today"
57
+ q = described_class.new(question)
58
+ expect(q.ask).to eq(Date.today.strftime('%Y'))
59
+ end
60
+ end
61
+
62
+ describe "#ask with choices" do
63
+ it 'asks a question with choices' do
64
+ question[:type] = :string
65
+ question[:choices] = %w[(o)ne (t)wo t(h)ree]
66
+ question[:default] = 'one'
67
+ q = described_class.new(question)
68
+ expect(q.ask).to eq('one')
69
+ end
70
+ end
71
+ end
@@ -12,6 +12,7 @@ describe Planter::Script do
12
12
  let(:base_script_path) { File.join(Planter.base_dir, 'scripts', script_name) }
13
13
 
14
14
  before do
15
+ ENV['PLANTER_RSPEC'] = 'true'
15
16
  Planter.base_dir = File.expand_path('spec')
16
17
  allow(File).to receive(:exist?).and_call_original
17
18
  allow(File).to receive(:directory?).and_call_original
@@ -30,14 +31,14 @@ describe Planter::Script do
30
31
  allow(File).to receive(:exist?).with(script_path).and_return(false)
31
32
  expect do
32
33
  Planter::Script.new(template_dir, output_dir, script_name)
33
- end.to raise_error(ScriptError)
34
+ end.to raise_error(SystemExit)
34
35
  end
35
36
 
36
37
  it 'raises an error if output directory is not found' do
37
38
  allow(File).to receive(:directory?).with(output_dir).and_return(false)
38
39
  expect do
39
40
  Planter::Script.new(template_dir, output_dir, script_name)
40
- end.to raise_error(ScriptError)
41
+ end.to raise_error(SystemExit)
41
42
  end
42
43
  end
43
44
 
@@ -60,7 +61,7 @@ describe Planter::Script do
60
61
  expect do
61
62
  script = Planter::Script.new(template_dir, output_dir, script_name)
62
63
  script.find_script(template_dir, script_name)
63
- end.to raise_error(ScriptError)
64
+ end.to raise_error(SystemExit)
64
65
  end
65
66
  end
66
67
 
@@ -74,7 +75,7 @@ describe Planter::Script do
74
75
  script = Planter::Script.new(template_dir, output_dir, script_name_fail)
75
76
  expect do
76
77
  script.run
77
- end.to raise_error(ScriptError)
78
+ end.to raise_error(SystemExit)
78
79
  end
79
80
  end
80
81
  end
@@ -141,6 +141,32 @@ describe ::String do
141
141
  end
142
142
  end
143
143
 
144
+ describe '.apply_operator_logic' do
145
+ it 'applies logic' do
146
+ template = 'copy if language == ruby'
147
+ operators = { language: 'ruby' }
148
+ expect(template.apply_operator_logic(operators)).to eq :copy
149
+ end
150
+
151
+ it 'applies default' do
152
+ template = 'copy if language == perl'
153
+ operators = { language: 'ruby' }
154
+ expect(template.apply_operator_logic(operators)).to eq :ignore
155
+ end
156
+
157
+ it 'handles no logic' do
158
+ template = 'copy'
159
+ operators = {}
160
+ expect(template.apply_operator_logic(operators)).to eq :copy
161
+ end
162
+
163
+ it 'handles if then' do
164
+ template = 'if language == ruby: copy; else: ignore'
165
+ operators = { language: 'perl' }
166
+ expect(template.apply_operator_logic(operators)).to eq :ignore
167
+ end
168
+ end
169
+
144
170
  describe '.apply_regexes' do
145
171
  it 'applies a single regex replacement' do
146
172
  template = 'Hello, World!'
data/spec/spec_helper.rb CHANGED
@@ -29,10 +29,11 @@ require 'open3'
29
29
  require 'time'
30
30
 
31
31
  module PlanterHelpers
32
- PLANTER_EXEC = File.join(File.dirname(__FILE__), '..', 'exe', 'plant')
32
+ PLANTER_EXEC = File.join(File.dirname(__FILE__), '..', 'bin', 'plant')
33
33
 
34
34
  def planter_with_env(env, *args, stdin: nil)
35
- pread(env, 'bundle', 'exec', PLANTER_EXEC, "--base-dir=#{File.dirname(__FILE__)}", *args, stdin: stdin)
35
+ pread(env, 'bundle', 'exec', PLANTER_EXEC, "--base-dir=#{File.dirname(__FILE__)}", "--defaults", *args,
36
+ stdin: stdin)
36
37
  end
37
38
 
38
39
  def pread(env, *cmd, stdin: nil)
@@ -5,5 +5,7 @@ variables:
5
5
  type: string
6
6
  default: "bollocks and beans"
7
7
  git_init: false
8
+ script:
9
+ - plant.sh
8
10
  files:
9
11
  "*.tmp": ignore
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+
3
+ echo "Testing" > planted_by_script.txt
data/src/_README.md CHANGED
@@ -7,6 +7,33 @@
7
7
 
8
8
  Plant a directory structure and files using templates.
9
9
 
10
+ <!--GITHUB-->
11
+ - [Planter](#planter)
12
+ - [Installation](#installation)
13
+ - [Configuration](#configuration)
14
+ - [Scripts](#scripts)
15
+ - [Templates](#templates)
16
+ - [Multiple choice type](#multiple-choice-type)
17
+ - [File-specific handling](#file-specific-handling)
18
+ - [If/then logic for file handling](#ifthen-logic-for-file-handling)
19
+ - [Regex replacements](#regex-replacements)
20
+ - [Finder Tags](#finder-tags)
21
+ - [Placeholders](#placeholders)
22
+ - [Default values in template strings](#default-values-in-template-strings)
23
+ - [Modifiers](#modifiers)
24
+ - [If/then logic](#ifthen-logic)
25
+ - [Usage](#usage)
26
+ - [Documentation](#documentation)
27
+ - [Development and Testing](#development-and-testing)
28
+ - [Source Code](#source-code)
29
+ - [Requirements](#requirements)
30
+ - [Rake](#rake)
31
+ - [Guard](#guard)
32
+ - [Contributing](#contributing)
33
+ - [License](#license)
34
+ - [Warranty](#warranty)
35
+ <!--END GITHUB-->
36
+
10
37
  ## Installation
11
38
 
12
39
  gem install planter-cli
@@ -38,7 +65,7 @@ Scripts can be executable files in any language, and receive the template direct
38
65
 
39
66
  Templates are directories found in `~/.config/planter/templates/[TEMPLATE_NAME]`. All files and directories inside of these template directories are copied when that template is called. Filenames, directory names, and file contents can all use template placeholders.
40
67
 
41
- Template placeholders are defined with `%%KEY%%`, where key is the key defined in the `variables` section of the configuration. `%%KEY%%` placeholders can be used in directory/file names, and in the file contents. These work in any plain text or RTF format document, including XML, so they can be used in things like Scrivener templates and MindNode files as well.
68
+ Template placeholders are defined with `%%KEY%%`, where key is the key defined in the `variables` section of the configuration. `%%KEY%%` placeholders can be used in directory/file names, and in the file contents. These work in any plain text or RTF format document, including XML, so they can be used in things like Scrivener templates and MindNode files as well. See the subsections for info on default values and modifiers for template placeholders.
42
69
 
43
70
  Each template contains a `_planter.yml` file that defines variables and other configuration options. The file format for all configuration files is [YAML](https://yaml.org/spec/1.2.2/).
44
71
 
@@ -49,12 +76,15 @@ variables:
49
76
  - key: var_key
50
77
  prompt: Prompt text
51
78
  type: string # [string,paragraph,float,integer,number,date,choice] defaults to string
52
- # value: (force value, string can include %%variables%% and regexes will be replaced. For date type can be today, time, now, etc.)
79
+ value: # (force value, string can include %%variables%% and regexes will be replaced. For date type can be today, time, now, etc.)
53
80
  default: Untitled
54
81
  min: 1
55
82
  max: 5
83
+ date_format: "%Y-%m-%d" # can be any strftime format, will be applied to any date type
56
84
  ```
57
85
 
86
+ For the date type, value can be `today`, `now`, `tomorrow`, `last thursday`, etc. and natural language will be converted to a time. The formatting will be determined automatically based on the type of the value, e.g. "now" gets a time string, but "today" just gets a YYYY-mm-dd string. This can be modified using the `date_format` key, which accepts any [strftime](https://www.fabriziomusacchio.com/blog/2021-08-15-strftime_Cheat_Sheet/) value. You can also include a date format string in single parenthesis after a natural language date value, e.g. `value: "now 'This year: %Y'"`.
87
+
58
88
  A configuration can include additional keys:
59
89
 
60
90
  ```yaml
@@ -66,9 +96,6 @@ replacements: # Dictionary of pattern/replacments for regex substitution, see [R
66
96
  repo: # If a repository URL is provided, it will be pulled and duplicated instead of copying an existing file structure
67
97
  ```
68
98
 
69
- #### Default values in template strings
70
-
71
- In a template you can add a default value for a placholder by adding `%default value` to it. For example, `%%project%Default Project%%` will set the placeholder to `Default Project` if the variable value matches the default value in the configuration (or doesn't exist). This allows you to accept the default on the command line but have a different value inserted in the template. To use another variable in its place, use `$KEY` in the placeholder, e.g. `%%project%$title%%` will replace the `project` key with the value of `title` if the default is selected. Modifiers can be used on either side of the `%`, e.g. `%%project%$title:snake%%`.
72
99
 
73
100
  #### Multiple choice type
74
101
 
@@ -108,48 +135,35 @@ variables:
108
135
  - 1. zsh
109
136
  ```
110
137
 
111
- If the choice starts with a number (as above), then a numeric list will be generated and typing the associated index number will accept that choice. Numeric lists are automatically numbered, so the preceding digit doesn't matter, as long as it's a digit. In this case a default can be defined with an integer (in the `defaults:` key) for its placement in the list (starting with 1), and parenthesis aren't required.
112
-
113
- #### If/then logic
114
-
115
- A template can use if/then logic, which is useful with multiple choice types. It can be applied to any type, though.
116
-
117
- The format for if/then logic is:
118
-
119
- ```
120
- %%if KEY OPERATOR VALUE%%
121
- content
122
- %%else if KEY OPERATOR VALUE2%%
123
- content 2
124
- %%else%%
125
- content 3
126
- %%endif%%
127
- ```
128
-
129
- There should be no spaces around the comparison, e.g. `%% if language == javascript %%` won't work. The block must start with an `if` statement and end with `%%endif%%` or `%%end%%`. The `%%else%%` statement is optional -- if it doesn't exist then the entire block will be removed if no conditions are met.
138
+ If the choice starts with a number (as above), then a numeric list will be generated and typing the associated index number will accept that choice. Numeric lists are automatically numbered, so the preceding digit doesn't matter, as long as it's a digit, and only the first item needs a digit to trigger numbering. In this case a default can be defined with an integer (in the `defaults:` key) for its placement in the list (starting with 1), and parenthesis aren't required.
130
139
 
131
- The key should be an existing key defined in `variables`. The operator can be any of:
140
+ `value:` and `default:` can include previous variables, with modifiers:
132
141
 
133
- - `==` or `=` (equals)
134
- - `=~` (matches regex)
135
- - `*=` (contains)
136
- - `^=` (starts with)
137
- - `$=` (ends with)
138
- - `>` (greater than)
139
- - `>=` (greater than or equal)
140
- - `<` (less than)
141
- - `<=` (less than or equal)
142
-
143
- The value after the operator doesn't need to be quoted, anything after the operator will be compared to the value of the key.
144
-
145
- Logic can be used on multiple lines like the example above, or on a single line (useful for filenames):
146
-
147
- ```
148
- %%project%%.%%if language == javascript%%js%%else if language == ruby%%rb%%else%%sh%%endif%%
142
+ ```yaml
143
+ - variables:
144
+ - key: language
145
+ prompt: Programming language
146
+ type: choice
147
+ default: 2
148
+ choices:
149
+ - 1. ruby
150
+ - javascript
151
+ - python
152
+ - bash
153
+ - zsh
154
+ - key: shebang
155
+ prompt: Shebang line
156
+ type: choice
157
+ default: "%%language:first_letter:lowercase%%"
158
+ choices:
159
+ (r)uby: "#! /usr/bin/env ruby"
160
+ (j)avascript: "#! /usr/bin/env node"
161
+ (p)ython: "#! /usr/bin/env python"
162
+ (b)ash: "#! /bin/bash"
163
+ (z)sh: "#! /bin/zsh"
149
164
  ```
150
165
 
151
- Content within if/else blocks can contain variables.
152
-
166
+ The above would define the `language` variable first, accepting `javascript` as default, then when displaying the menu for `shebang`, the language variable would have its first letter lowercased and set as the default option for the menu.
153
167
 
154
168
  ### File-specific handling
155
169
 
@@ -181,7 +195,17 @@ Merged content
181
195
 
182
196
  By default files that already exist in the destination directory are not overwritten, and merging allows you to add missing parts to a Rakefile or Makefile, for example.
183
197
 
184
- If `ask` is specified, a memu will be provided on the command line asking how to handle a file. If the file doesn't already exist, you will be asked only whether to copy the file or not. If it does exist, `overwrite` and `merge` options will be added.
198
+ If `ask` is specified, a menu will be provided on the command line asking how to handle a file. If the file doesn't already exist, you will be asked only whether to copy the file or not. If it does exist, `overwrite` and `merge` options will be added.
199
+
200
+ #### If/then logic for file handling
201
+
202
+ The operation for a file match can be an if/then statement. There are two formats for this.
203
+
204
+ First, you can simply write `OPERATION if VARIABLE COMP VALUE`, e.g. `copy if language == ruby`. This can have an `else` statement: `overwrite if language == ruby else copy`.
205
+
206
+ You can also format it as `if VARIABLE COMP VALUE: OPERATION; else: OPERATION`. This format allows `else if` statements, e.g. `if language == perl: copy;else if language == ruby: overwrite; else: ignore`.
207
+
208
+ Planter's if/then parsing does not handle parenthetical or boolean operations.
185
209
 
186
210
  ### Regex replacements
187
211
 
@@ -199,6 +223,69 @@ Replacements are performed on both file/directory names and file contents. This
199
223
 
200
224
  If `preserve_tags` is set to `true` in the config (either base or template), then existing Finder tags on the file or folder will be copied to the new file when a template is planted.
201
225
 
226
+ ### Placeholders
227
+
228
+ Placeholders are `%%VARIABLE%%` strings used in filenames and within the content of the files. At their most basic, this would just look like `%%project_name%%`. But you can supply default values, string modifiers, and even if/then logic.
229
+
230
+ #### Default values in template strings
231
+
232
+ In a template you can add a default value for a placholder by adding `%default value` to it. For example, `%%project%Default Project%%` will set the placeholder to `Default Project` if the variable value matches the default value in the configuration (or doesn't exist). This allows you to accept the default on the command line but have a different value inserted in the template. To use another variable in its place, use `$KEY` in the placeholder, e.g. `%%project%$title%%` will replace the `project` key with the value of `title` if the default is selected. Modifiers can be used on either side of the `%`, e.g. `%%project%$title:snake%%`.
233
+
234
+ #### Modifiers
235
+
236
+ A `%%variable%%` in a template can include modifiers that affect the output of the variable. These are added as `%%variable:MODIFIER%%` and multiple modifiers can be strung together, e.g. `%%language:lowercase:first_letter%%`. The available modifiers are (listed with available abbreviations):
237
+
238
+ - `snake` (`s`): snake_case the value
239
+ - `camel` (`cam`): camelCase the value
240
+ - `upper` (`u`): UPPERCASE the value
241
+ - `lower` (`l`, `d`): lowercase the value
242
+ - `title` (`t`): Title Case The Value
243
+ - `slug` or `file` (`sl`): slug-format-the-value
244
+ - `first_letter` (`fl`): Extract the first letter from the value
245
+ - `first_word` (`fw`): Extract the first word from the value
246
+
247
+ #### If/then logic
248
+
249
+ A template can use if/then logic, which is useful with multiple choice types. It can be applied to any type, though.
250
+
251
+ The format for if/then logic is:
252
+
253
+ ```
254
+ %%if KEY OPERATOR VALUE%%
255
+ content
256
+ %%else if KEY OPERATOR VALUE2%%
257
+ content 2
258
+ %%else%%
259
+ content 3
260
+ %%endif%%
261
+ ```
262
+
263
+ There should be no spaces around the comparison, e.g. `%% if language == javascript %%` won't work. The block must start with an `if` statement and end with `%%endif%%` or `%%end%%`. The `%%else%%` statement is optional -- if it doesn't exist then the entire block will be removed if no conditions are met.
264
+
265
+ The key should be an existing key defined in `variables`. The operator can be any of:
266
+
267
+ - `==` or `=` (equals)
268
+ - `=~` (matches regex)
269
+ - `*=` (contains)
270
+ - `^=` (starts with)
271
+ - `$=` (ends with)
272
+ - `>` (greater than)
273
+ - `>=` (greater than or equal)
274
+ - `<` (less than)
275
+ - `<=` (less than or equal)
276
+
277
+ Any of these operators can be inverted (negated) by preceding with an exclamation point, e.g. `!=` means `not equal` and `!*=` means `does not contain`.
278
+
279
+ The value after the operator doesn't need to be quoted, anything after the operator will be compared to the value of the key.
280
+
281
+ Logic can be used on multiple lines like the example above, or on a single line (useful for filenames):
282
+
283
+
284
+ %%project%%.%%if language == javascript%%js%%else if language == ruby%%rb%%else%%sh%%endif%%
285
+
286
+
287
+ Content within if/else blocks can contain variables. Planter's if/then parsing does not handle parenthetical or boolean operations.
288
+
202
289
  ## Usage
203
290
 
204
291
  The executable for Planter is `plant`. You can run `plant TEMPLATE` in any directory and TEMPLATE will be planted in the current directory. You can also use `--in PATH` to plant in another directory.
@@ -220,14 +307,13 @@ Some directories like `.git` and files like `_planter.yml` are automatically ign
220
307
 
221
308
  When `plant` is run, any defined variables will be requested on the command line using the defined prompt. If a `default` key is specified, hitting return at the prompt will accept the default value.
222
309
 
223
- Variables can be passed on the command line with `--var KEY:VALUE`. This flag can contain a comma-separated list, e.g. `--var KEY:VALUE,KEY:VALUE` or used multiple times in the same command. Variables passed on the command line will not be prompted for when processing variables.
310
+ Variables can be passed on the command line with `--var KEY:VALUE`. This flag can contain a comma-separated list, e.g. `--var KEY:VALUE,KEY:VALUE` or be used multiple times in the same command. Variables passed on the command line will not be prompted for when processing variables.
224
311
 
225
312
  <!--GITHUB-->
226
313
 
227
314
  ## Documentation
228
315
 
229
316
  - [YARD documentation][RubyDoc] is hosted by RubyDoc.info.
230
- - [Interactive documentation][Omniref] is hosted by Omniref.
231
317
 
232
318
  [RubyDoc]: http://www.rubydoc.info/gems/planter-cli
233
319
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: planter-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.4
4
+ version: 3.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brett Terpstra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-02 00:00:00.000000000 Z
11
+ date: 2024-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '2.11'
61
+ version: '2.16'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '2.11'
68
+ version: '2.16'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -346,6 +346,7 @@ files:
346
346
  - ".irbrc"
347
347
  - ".rspec"
348
348
  - ".rubocop.yml"
349
+ - ".rubocop_todo.yml"
349
350
  - ".travis.yml"
350
351
  - ".yardopts"
351
352
  - CHANGELOG.md
@@ -368,11 +369,13 @@ files:
368
369
  - lib/planter.rb
369
370
  - lib/planter/array.rb
370
371
  - lib/planter/color.rb
372
+ - lib/planter/config.rb
371
373
  - lib/planter/errors.rb
372
374
  - lib/planter/file.rb
373
375
  - lib/planter/fileentry.rb
374
376
  - lib/planter/filelist.rb
375
377
  - lib/planter/hash.rb
378
+ - lib/planter/numeric.rb
376
379
  - lib/planter/plant.rb
377
380
  - lib/planter/prompt.rb
378
381
  - lib/planter/script.rb
@@ -472,6 +475,7 @@ files:
472
475
  - spec/planter/filelist_spec.rb
473
476
  - spec/planter/hash_spec.rb
474
477
  - spec/planter/plant_spec.rb
478
+ - spec/planter/prompt_spec.rb
475
479
  - spec/planter/script_spec.rb
476
480
  - spec/planter/string_spec.rb
477
481
  - spec/planter/symbol_spec.rb
@@ -482,6 +486,7 @@ files:
482
486
  - spec/templates/test/%%project:snake%%.rtf
483
487
  - spec/templates/test/Rakefile
484
488
  - spec/templates/test/_planter.yml
489
+ - spec/templates/test/_scripts/plant.sh
485
490
  - spec/templates/test/_scripts/test.sh
486
491
  - spec/templates/test/_scripts/test_fail.sh
487
492
  - spec/templates/test/test.rb
@@ -522,6 +527,7 @@ test_files:
522
527
  - spec/planter/filelist_spec.rb
523
528
  - spec/planter/hash_spec.rb
524
529
  - spec/planter/plant_spec.rb
530
+ - spec/planter/prompt_spec.rb
525
531
  - spec/planter/script_spec.rb
526
532
  - spec/planter/string_spec.rb
527
533
  - spec/planter/symbol_spec.rb
@@ -532,6 +538,7 @@ test_files:
532
538
  - spec/templates/test/%%project:snake%%.rtf
533
539
  - spec/templates/test/Rakefile
534
540
  - spec/templates/test/_planter.yml
541
+ - spec/templates/test/_scripts/plant.sh
535
542
  - spec/templates/test/_scripts/test.sh
536
543
  - spec/templates/test/_scripts/test_fail.sh
537
544
  - spec/templates/test/test.rb