kojo 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +44 -33
- data/lib/kojo.rb +1 -0
- data/lib/kojo/cli.rb +1 -0
- data/lib/kojo/commands/form.rb +36 -0
- data/lib/kojo/form.rb +61 -0
- data/lib/kojo/version.rb +1 -1
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 39cdf289cbe0c64d4344cbe494922c1b377f940da377b74367227de926679c62
|
4
|
+
data.tar.gz: 062efc5aaa4e6bae3dbc3bc9adefd3e8b8a2f6b76e088a8b6ca5ba4c536a131e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c88d0578808aff72c649bdcfc9a0919fcfd29179793d08ae617d10f44b61d82df8dff86d2745defe6b079ebce958210158d89aa4d62a27ff495e42c7ef2c834
|
7
|
+
data.tar.gz: 39392b24f84d4e62df6e2b4278b2363e41eb61fb453a651b15ecbc23af96a72e78feef478b628e184ffc3068374db5ed64917a1c47f2560b784e9404dda72b0c
|
data/README.md
CHANGED
@@ -2,8 +2,7 @@
|
|
2
2
|
|
3
3
|
![kojo](images/kojo.png)
|
4
4
|
|
5
|
-
Kojo Configuration Ninja
|
6
|
-
==================================================
|
5
|
+
# Kojo Configuration Ninja
|
7
6
|
|
8
7
|
[![Gem Version](https://badge.fury.io/rb/kojo.svg)](https://badge.fury.io/rb/kojo)
|
9
8
|
[![Build Status](https://github.com/DannyBen/kojo/workflows/Test/badge.svg)](https://github.com/DannyBen/kojo/actions?query=workflow%3ATest)
|
@@ -18,8 +17,7 @@ It is a command line utility, and it works on any text file format.
|
|
18
17
|
|
19
18
|
---
|
20
19
|
|
21
|
-
Table of Contents
|
22
|
-
--------------------------------------------------
|
20
|
+
## Table of Contents
|
23
21
|
|
24
22
|
- [Installation](#installation)
|
25
23
|
- [Usage](#usage)
|
@@ -28,28 +26,32 @@ Table of Contents
|
|
28
26
|
- [Transform an Entire Folder](#transform-an-entire-folder)
|
29
27
|
- [Transform One to Many using Config](#transform-one-to-many-using-config)
|
30
28
|
- [Transform One to Many using Front Matter](#transform-one-to-many-using-front-matter)
|
29
|
+
- [Interactive Form Templates](#interactive-form-templates)
|
31
30
|
- [Conditions and Loops with ERB](#conditions-and-loops-with-erb)
|
32
|
-
- [Interactive
|
31
|
+
- [Interactive Fallback](#interactive-fallback)
|
33
32
|
- [Using from Ruby Code](#using-from-ruby-code)
|
34
33
|
- [Contributing / Support](#contributing--support)
|
35
34
|
|
36
35
|
---
|
37
36
|
|
38
|
-
Installation
|
39
|
-
--------------------------------------------------
|
37
|
+
## Installation
|
40
38
|
|
41
|
-
|
39
|
+
```shell
|
40
|
+
$ gem install kojo
|
41
|
+
```
|
42
42
|
|
43
|
+
Or with Docker:
|
43
44
|
|
45
|
+
```shell
|
46
|
+
$ alias kojo='docker run --rm -it -v $PWD:/app dannyben/kojo'
|
47
|
+
```
|
44
48
|
|
45
|
-
Usage
|
46
|
-
--------------------------------------------------
|
49
|
+
## Usage
|
47
50
|
|
48
51
|
If you prefer to learn by example, see the [examples](examples#examples) folder for
|
49
52
|
several use cases. Each example subfolder contains the command to run, the
|
50
53
|
relevant files, and the expected output.
|
51
54
|
|
52
|
-
|
53
55
|
### Variables
|
54
56
|
|
55
57
|
![kojo](images/features-vars.svg)
|
@@ -63,8 +65,6 @@ Include variables in your configuration templates by using this syntax:
|
|
63
65
|
- Variables from the top level will be forwarded downstream, and aggregated
|
64
66
|
with any additional variables that are defined in subsequent `@imports`.
|
65
67
|
|
66
|
-
|
67
|
-
|
68
68
|
### Import
|
69
69
|
|
70
70
|
![kojo](images/features-import.svg)
|
@@ -86,8 +86,6 @@ resulting configuration file.
|
|
86
86
|
|
87
87
|
The space after `filename` is optional.
|
88
88
|
|
89
|
-
|
90
|
-
|
91
89
|
### Transform an Entire Folder
|
92
90
|
|
93
91
|
![kojo](images/features-dir.svg)
|
@@ -97,8 +95,6 @@ output folder, with all the variables and `@imports` evaluated.
|
|
97
95
|
|
98
96
|
You may use `%{variables}` in filenames.
|
99
97
|
|
100
|
-
|
101
|
-
|
102
98
|
### Transform One to Many using Config
|
103
99
|
|
104
100
|
![kojo](images/features-config.svg)
|
@@ -169,6 +165,20 @@ Your template that uses %{arg} goes here
|
|
169
165
|
Additional arguments provided to the command line, will also be transferred
|
170
166
|
to the template.
|
171
167
|
|
168
|
+
### Interactive Form Templates
|
169
|
+
|
170
|
+
![kojo](images/features-form.svg)
|
171
|
+
|
172
|
+
Using the `kojo form` command lets you define an ERB or [ERBX][erbx] template, and include interactive prompts to enter the input.
|
173
|
+
|
174
|
+
1. Use either ERB tags (`<%= %>`, `<%- -%>`) or ERBX tags (`{{ }}`, `(( ))`).
|
175
|
+
2. Use the built in `prompt` object, which is a [TTY::Prompt](tty-prompt) instance, to prompt for input when running the command (for example: `{{ prompt.ask? "Your Name?" }}`)
|
176
|
+
3. Any unidentified ruby command will be forwarded to the `prompt` object, so `prompt.ask` is the same as just using `ask`.
|
177
|
+
4. If there is a file with the same name as the template, and with an `.rb` extension (for example `form.md` and `form.md.rb`), then the ruby file will be loaded into the ERB template as if it was written inside it.
|
178
|
+
5. If you prefer using a single template file (without the ruby appendix), you can simply use regular ERB/ERBX tags, like demonstrated below.
|
179
|
+
|
180
|
+
![kojo](images/features-form-inline.svg)
|
181
|
+
|
172
182
|
### Conditions and Loops with ERB
|
173
183
|
|
174
184
|
![kojo](images/features-erb.svg)
|
@@ -182,10 +192,7 @@ Use this syntax for ruby code:
|
|
182
192
|
<%- ruby code here -%> # for code that should not be printed
|
183
193
|
<%= ruby code here -%> # for code that should be printed
|
184
194
|
```
|
185
|
-
|
186
|
-
|
187
|
-
Interactive Mode
|
188
|
-
--------------------------------------------------
|
195
|
+
## Interactive Fallback
|
189
196
|
|
190
197
|
When Kojo encounters a variable that was not supplied (either through the command
|
191
198
|
line or through a configuration file), it will prompt for a value.
|
@@ -201,21 +208,20 @@ running from within Ruby code.
|
|
201
208
|
When running from within Ruby code, you can also use `Kojo.interactive = true`
|
202
209
|
and `Kojo.interactive?` to get the current state.
|
203
210
|
|
204
|
-
|
205
|
-
Using from Ruby Code
|
206
|
-
--------------------------------------------------
|
211
|
+
## Using from Ruby Code
|
207
212
|
|
208
213
|
Although Kojo was primarily designed as a command line utility, you can also
|
209
214
|
use it as a library from your Ruby code.
|
210
215
|
|
211
216
|
These are the primary classes:
|
212
217
|
|
213
|
-
| Class | Description | CLI equivalent
|
214
|
-
|
215
|
-
| `Kojo::Template` | generate from a single template | `kojo file`
|
216
|
-
| `Kojo::FrontMatterTemplate` | generate from a template with a front matter | `kojo single`
|
217
|
-
| `Kojo::Config` | generate from a config file | `kojo config`
|
218
|
-
| `Kojo::Collection` | generate from a directory | `kojo dir`
|
218
|
+
| Class | Description | CLI equivalent |
|
219
|
+
| --------------------------- | -------------------------------------------- | -------------- |
|
220
|
+
| `Kojo::Template` | generate from a single template | `kojo file` |
|
221
|
+
| `Kojo::FrontMatterTemplate` | generate from a template with a front matter | `kojo single` |
|
222
|
+
| `Kojo::Config` | generate from a config file | `kojo config` |
|
223
|
+
| `Kojo::Collection` | generate from a directory | `kojo dir` |
|
224
|
+
| `Kojo::Form` | generate interactively | `kojo form` |
|
219
225
|
|
220
226
|
### Examples
|
221
227
|
|
@@ -249,6 +255,10 @@ params = { version: '0.1.1' }
|
|
249
255
|
template.render params do |path, content|
|
250
256
|
# code to handle results here
|
251
257
|
end
|
258
|
+
|
259
|
+
# Form
|
260
|
+
template = Kojo::Form.new 'examples/form/movie.md'
|
261
|
+
puts template.render
|
252
262
|
```
|
253
263
|
|
254
264
|
In addition, Kojo extends Ruby's `File` class with the `File.deep_write`
|
@@ -265,12 +275,13 @@ config.generate do |path, content|
|
|
265
275
|
end
|
266
276
|
```
|
267
277
|
|
268
|
-
Contributing / Support
|
269
|
-
--------------------------------------------------
|
278
|
+
## Contributing / Support
|
270
279
|
|
271
280
|
If you experience any issue, have a question or a suggestion, or if you wish
|
272
281
|
to contribute, feel free to [open an issue][issues].
|
273
282
|
|
274
283
|
---
|
275
284
|
|
276
|
-
[issues]: https://github.com/DannyBen/kojo/issues
|
285
|
+
[issues]: https://github.com/DannyBen/kojo/issues
|
286
|
+
[erbx]: https://github.com/DannyBen/erbx
|
287
|
+
[tty-prompt]: https://github.com/piotrmurach/tty-prompt#contents
|
data/lib/kojo.rb
CHANGED
data/lib/kojo/cli.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'mister_bin'
|
2
|
+
|
3
|
+
module Kojo
|
4
|
+
module Commands
|
5
|
+
# Handle calls to the +kojo form+ command
|
6
|
+
class FormCmd < CommandBase
|
7
|
+
attr_reader :infile, :outdir
|
8
|
+
|
9
|
+
help "Fill a template form interactively"
|
10
|
+
|
11
|
+
usage "kojo form INFILE [--save FILE]"
|
12
|
+
usage "kojo form (-h|--help)"
|
13
|
+
|
14
|
+
option "-s --save FILE", "Save to file instead of printing"
|
15
|
+
|
16
|
+
param "INFILE", "ERBX template to transform"
|
17
|
+
|
18
|
+
example "kojo form report.md"
|
19
|
+
example "kojo form report.md --save output.md"
|
20
|
+
|
21
|
+
def run
|
22
|
+
infile = args['INFILE']
|
23
|
+
outfile = args['--save']
|
24
|
+
template = Kojo::Form.new infile
|
25
|
+
|
26
|
+
if outfile
|
27
|
+
File.deep_write outfile, template.render
|
28
|
+
say "Saved #{outfile}"
|
29
|
+
else
|
30
|
+
puts template.render
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/kojo/form.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'erbx'
|
2
|
+
require 'tty-prompt'
|
3
|
+
|
4
|
+
module Kojo
|
5
|
+
# The Form class handles interactive ERBX templates
|
6
|
+
class Form
|
7
|
+
attr_reader :file
|
8
|
+
|
9
|
+
def initialize(file)
|
10
|
+
@file = file
|
11
|
+
end
|
12
|
+
|
13
|
+
def content
|
14
|
+
@content ||= content!
|
15
|
+
end
|
16
|
+
|
17
|
+
def content!
|
18
|
+
content = File.read file
|
19
|
+
ruby_code = File.exist?("#{file}.rb") ? File.read("#{file}.rb") : nil
|
20
|
+
content = "<%-\n#{ruby_code}\n-%>\n#{content}" if ruby_code
|
21
|
+
content
|
22
|
+
end
|
23
|
+
|
24
|
+
def render
|
25
|
+
ERBX.new(content).result(binding)
|
26
|
+
rescue TTY::Reader::InputInterrupt
|
27
|
+
# :nocov:
|
28
|
+
raise Kojo::Interrupt
|
29
|
+
# :nocov:
|
30
|
+
end
|
31
|
+
|
32
|
+
def prompt
|
33
|
+
@prompt ||= TTY::Prompt.new
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(method_name, *args, &block)
|
37
|
+
if respond_to? method_name
|
38
|
+
prompt.send method_name, *args, *block
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def respond_to?(method_name, include_private = false)
|
45
|
+
prompt.respond_to?(method_name) || super
|
46
|
+
end
|
47
|
+
|
48
|
+
# Below are TTY::Prompt functions that are not captured by the
|
49
|
+
# `mthod_missing`, so we specify them explicitly
|
50
|
+
|
51
|
+
def select(*args, &block)
|
52
|
+
prompt.select *args, &block
|
53
|
+
end
|
54
|
+
|
55
|
+
def warn(*args, &block)
|
56
|
+
prompt.warn *args, &block
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
data/lib/kojo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kojo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mister_bin
|
@@ -52,6 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: tty-prompt
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.21'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.21'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: erbx
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.1.1
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.1.1
|
55
83
|
description: Generate configuration files from templates, using variables and definition
|
56
84
|
files.
|
57
85
|
email: db@dannyben.com
|
@@ -69,10 +97,12 @@ files:
|
|
69
97
|
- lib/kojo/commands/config.rb
|
70
98
|
- lib/kojo/commands/dir.rb
|
71
99
|
- lib/kojo/commands/file.rb
|
100
|
+
- lib/kojo/commands/form.rb
|
72
101
|
- lib/kojo/commands/single.rb
|
73
102
|
- lib/kojo/config.rb
|
74
103
|
- lib/kojo/exceptions.rb
|
75
104
|
- lib/kojo/extensions/file.rb
|
105
|
+
- lib/kojo/form.rb
|
76
106
|
- lib/kojo/front_matter_template.rb
|
77
107
|
- lib/kojo/refinements/array.rb
|
78
108
|
- lib/kojo/refinements/hash.rb
|