kojo 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +65 -1
- data/bin/kojo +1 -0
- data/lib/kojo.rb +17 -5
- data/lib/kojo/cli.rb +4 -0
- data/lib/kojo/collection.rb +2 -0
- data/lib/kojo/commands/command_base.rb +11 -9
- data/lib/kojo/commands/config.rb +3 -1
- data/lib/kojo/commands/dir.rb +50 -46
- data/lib/kojo/commands/file.rb +51 -47
- data/lib/kojo/commands/single.rb +38 -34
- data/lib/kojo/config.rb +2 -0
- data/lib/kojo/front_matter_template.rb +2 -0
- data/lib/kojo/refinements/array.rb +15 -0
- data/lib/kojo/refinements/hash.rb +15 -0
- data/lib/kojo/refinements/string.rb +66 -0
- data/lib/kojo/template.rb +2 -0
- data/lib/kojo/version.rb +1 -1
- metadata +6 -7
- data/lib/kojo/extensions/array.rb +0 -11
- data/lib/kojo/extensions/hash.rb +0 -11
- data/lib/kojo/extensions/string.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b8608c2acf1ded769eac7140256f0cd9d7b172cc38c6ccf4c8b0cb5a3ef15fd7
|
4
|
+
data.tar.gz: 0b94e504f27c2504ce3801b94c81f6b8f94d583189d529ffe6c6f73a00b19216
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9086487049e3ddd2a0f4e2ce912ca451a1b67c1812301a967893e9bd701b762598343d7aef04a2d0a90dbfddb46e2da7eabfec48400a5a96ab152033d1860d73
|
7
|
+
data.tar.gz: e9c040bd31fd36cca5ec7be5285a1098d42a26d216a4edb26bfb41dc9ea0b227f49e3ca435bd19c8427b89a313cf6fd1df631e332e519fb62924d968d95856c2
|
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Kojo Configuration Ninja
|
|
8
8
|
[![Gem Version](https://badge.fury.io/rb/kojo.svg)](https://badge.fury.io/rb/kojo)
|
9
9
|
[![Build Status](https://travis-ci.com/DannyBen/kojo.svg?branch=master)](https://travis-ci.com/DannyBen/kojo)
|
10
10
|
[![Maintainability](https://api.codeclimate.com/v1/badges/f24566ad04b5054a2251/maintainability)](https://codeclimate.com/github/DannyBen/kojo/maintainability)
|
11
|
-
|
11
|
+
|
12
12
|
|
13
13
|
Kojo helps you generate configuration files from templates, using variables
|
14
14
|
and definition files.
|
@@ -30,6 +30,7 @@ Table of Contents
|
|
30
30
|
- [Transform One to Many using Front Matter](#transform-one-to-many-using-front-matter)
|
31
31
|
- [Conditions and Loops with ERB](#conditions-and-loops-with-erb)
|
32
32
|
- [Interactive Mode](#interactive-mode)
|
33
|
+
- [Using from Ruby Code](#using-from-ruby-code)
|
33
34
|
|
34
35
|
---
|
35
36
|
|
@@ -164,6 +165,9 @@ Your template that uses %{arg} goes here
|
|
164
165
|
...
|
165
166
|
```
|
166
167
|
|
168
|
+
Additional arguments provided to the command line, will also be transferred
|
169
|
+
to the template.
|
170
|
+
|
167
171
|
### Conditions and Loops with ERB
|
168
172
|
|
169
173
|
![kojo](images/features-erb.svg)
|
@@ -187,3 +191,63 @@ line or through a configuration file), it will prompt for a value.
|
|
187
191
|
|
188
192
|
![kojo](images/interactive-mode.gif)
|
189
193
|
|
194
|
+
You can enable or disable interactive mode by setting the environment
|
195
|
+
variable `KOJO_INTERACTIVE` to `yes` or `no`.
|
196
|
+
|
197
|
+
By default, interactivity is enabled when running the CLI, and disabled when
|
198
|
+
running from within Ruby code.
|
199
|
+
|
200
|
+
When running from within Ruby code, you can also use `Kojo.interactive = true`
|
201
|
+
and `Kojo.interactive?` to get the current state.
|
202
|
+
|
203
|
+
|
204
|
+
Using from Ruby Code
|
205
|
+
--------------------------------------------------
|
206
|
+
|
207
|
+
Although Kojo was primarily designed as a command line utility, you can also
|
208
|
+
use it as a library from your Ruby code.
|
209
|
+
|
210
|
+
These are the primary classes:
|
211
|
+
|
212
|
+
| Class | Description | CLI equivalent
|
213
|
+
|-----------------------------|----------------------------------------------|---------------
|
214
|
+
| `Kojo::Template` | generate from a single template | `kojo file`
|
215
|
+
| `Kojo::FrontMatterTemplate` | generate from a template with a front matter | `kojo single`
|
216
|
+
| `Kojo::Config` | generate from a config file | `kojo config`
|
217
|
+
| `Kojo::Collection` | generate from a directory | `kojo dir`
|
218
|
+
|
219
|
+
### Examples
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
# Template
|
223
|
+
template = Kojo::Template.new 'examples/variables/main.yml'
|
224
|
+
result = template.render domain: 'example.com', scale: 2
|
225
|
+
puts result
|
226
|
+
|
227
|
+
# Collection
|
228
|
+
collection = Kojo::Collection.new 'examples/dir'
|
229
|
+
collection.import_base = 'examples/dir/imports'
|
230
|
+
|
231
|
+
params = { env: 'env', app: 'app' }
|
232
|
+
result = collection.render params do |path, content|
|
233
|
+
# code to handle results here
|
234
|
+
end
|
235
|
+
|
236
|
+
# Config
|
237
|
+
config = Kojo::Config.new 'examples/config-from-file/config.yml'
|
238
|
+
config.import_base = "examples/config-from-file/imports"
|
239
|
+
|
240
|
+
config.generate do |path, content|
|
241
|
+
# code to handle results here
|
242
|
+
end
|
243
|
+
|
244
|
+
# FrontMatterTemplate
|
245
|
+
template = Kojo::FrontMatterTemplate.new 'examples/single/Dockerfile'
|
246
|
+
params = { version: '0.1.1' }
|
247
|
+
|
248
|
+
result = template.render params do |path, content|
|
249
|
+
# code to handle results here
|
250
|
+
end
|
251
|
+
|
252
|
+
```
|
253
|
+
|
data/bin/kojo
CHANGED
data/lib/kojo.rb
CHANGED
@@ -1,12 +1,24 @@
|
|
1
1
|
require 'requires'
|
2
2
|
require 'byebug' if ENV['BYEBUG']
|
3
3
|
|
4
|
-
requires 'kojo/
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
requires 'kojo/refinements'
|
5
|
+
|
6
|
+
require 'kojo/exceptions'
|
7
|
+
require 'kojo/template'
|
8
|
+
require 'kojo/collection'
|
9
|
+
require 'kojo/config'
|
10
|
+
require 'kojo/front_matter_template'
|
8
11
|
|
9
12
|
module Kojo
|
10
|
-
|
13
|
+
class << self
|
14
|
+
def interactive?
|
15
|
+
return @interactive unless @interactive.nil?
|
16
|
+
@interactive = ENV['KOJO_INTERACTIVE'] == 'yes'
|
17
|
+
end
|
18
|
+
|
19
|
+
def interactive=(value)
|
20
|
+
@interactive = value
|
21
|
+
end
|
11
22
|
end
|
23
|
+
|
12
24
|
end
|
data/lib/kojo/cli.rb
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
require 'mister_bin'
|
2
|
+
require 'kojo/version'
|
3
|
+
requires 'commands'
|
2
4
|
|
3
5
|
module Kojo
|
4
6
|
# The CLI class is used by the kojo binary and forwards incoming CLI
|
5
7
|
# commands to the relevant Kojo::Commands class
|
6
8
|
class CLI
|
7
9
|
def self.runner
|
10
|
+
Kojo.interactive = ENV['KOJO_INTERACTIVE'] != 'no'
|
11
|
+
|
8
12
|
runner = MisterBin::Runner.new version: Kojo::VERSION
|
9
13
|
|
10
14
|
runner.route 'file', to: Kojo::Commands::FileCmd
|
data/lib/kojo/collection.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'mister_bin'
|
2
2
|
|
3
|
-
module Kojo
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module Kojo
|
4
|
+
module Commands
|
5
|
+
class CommandBase < MisterBin::Command
|
6
|
+
def save(file, output)
|
7
|
+
outpath = "#{outdir}/#{file}"
|
8
|
+
dir = File.dirname outpath
|
9
|
+
FileUtils.mkdir_p dir unless Dir.exist? dir
|
10
|
+
File.write outpath, output
|
11
|
+
say "Saved #{outpath}"
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
13
|
-
end
|
15
|
+
end
|
data/lib/kojo/commands/config.rb
CHANGED
@@ -4,6 +4,8 @@ require 'mister_bin'
|
|
4
4
|
module Kojo::Commands
|
5
5
|
# Handle calls to the +kojo config+ command
|
6
6
|
class ConfigCmd < MisterBin::Command
|
7
|
+
using Kojo::Refinements
|
8
|
+
|
7
9
|
attr_reader :gen, :outdir, :opts, :import_base, :config_file
|
8
10
|
|
9
11
|
help "Transform based on instructions from a config file"
|
@@ -41,7 +43,7 @@ module Kojo::Commands
|
|
41
43
|
private
|
42
44
|
|
43
45
|
def run!
|
44
|
-
config =
|
46
|
+
config = Config.new config_file
|
45
47
|
config.import_base = import_base if import_base
|
46
48
|
|
47
49
|
config.generate opts do |file, output|
|
data/lib/kojo/commands/dir.rb
CHANGED
@@ -1,66 +1,70 @@
|
|
1
1
|
require 'mister_bin'
|
2
2
|
|
3
|
-
module Kojo
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Kojo
|
4
|
+
module Commands
|
5
|
+
# Handle calls to the +kojo dir+ command
|
6
|
+
class DirCmd < CommandBase
|
7
|
+
using Kojo::Refinements
|
7
8
|
|
8
|
-
|
9
|
+
attr_reader :opts, :indir, :outdir, :import_base
|
9
10
|
|
10
|
-
|
11
|
-
usage "kojo dir (-h|--help)"
|
11
|
+
help "Transform a folder of templates to a similar output folder"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
option "-a --args FILE", "Load arguments from YAML file"
|
13
|
+
usage "kojo dir INDIR [--save DIR --imports DIR --args FILE] [ARGS...]"
|
14
|
+
usage "kojo dir (-h|--help)"
|
16
15
|
|
17
|
-
|
18
|
-
|
16
|
+
option "-s --save DIR", "Save output to directory instead of printing"
|
17
|
+
option "-i --imports DIR", "Specify base directory for @import directives"
|
18
|
+
option "-a --args FILE", "Load arguments from YAML file"
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
example "kojo dir in --save out --imports snippets env=production"
|
23
|
-
example "kojo dir in -s out -i snippets -a args.yml"
|
20
|
+
param "INDIR", "Directory containing templates to transform"
|
21
|
+
param "ARGS", "Optional key=value pairs"
|
24
22
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
@import_base = args['--imports']
|
30
|
-
argfile = args['--args']
|
23
|
+
example "kojo dir indir"
|
24
|
+
example "kojo dir in --save out env=production"
|
25
|
+
example "kojo dir in --save out --imports snippets env=production"
|
26
|
+
example "kojo dir in -s out -i snippets -a args.yml"
|
31
27
|
|
32
|
-
|
33
|
-
|
34
|
-
@
|
35
|
-
|
28
|
+
def run
|
29
|
+
@opts = args['ARGS'].args_to_hash
|
30
|
+
@indir = args['INDIR']
|
31
|
+
@outdir = args['--save']
|
32
|
+
@import_base = args['--imports']
|
33
|
+
argfile = args['--args']
|
36
34
|
|
37
|
-
|
38
|
-
|
35
|
+
if argfile
|
36
|
+
fileopts = YAML.load_file(argfile).symbolize_keys
|
37
|
+
@opts = fileopts.merge @opts
|
38
|
+
end
|
39
|
+
|
40
|
+
run!
|
41
|
+
end
|
39
42
|
|
40
|
-
|
43
|
+
private
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
+
def run!
|
46
|
+
collection = Collection.new @indir
|
47
|
+
collection.import_base = import_base if import_base
|
45
48
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
49
|
+
if outdir
|
50
|
+
write collection
|
51
|
+
else
|
52
|
+
show collection
|
53
|
+
end
|
50
54
|
end
|
51
|
-
end
|
52
55
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
def show(collection)
|
57
|
+
collection.render @opts do |file, output|
|
58
|
+
say "\n!txtgrn!# #{file}"
|
59
|
+
say output
|
60
|
+
end
|
57
61
|
end
|
58
|
-
end
|
59
62
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
def write(collection)
|
64
|
+
collection.render @opts do |file, output|
|
65
|
+
save file, output
|
66
|
+
end
|
63
67
|
end
|
64
68
|
end
|
65
69
|
end
|
66
|
-
end
|
70
|
+
end
|
data/lib/kojo/commands/file.rb
CHANGED
@@ -1,56 +1,60 @@
|
|
1
1
|
require 'mister_bin'
|
2
2
|
|
3
|
-
module Kojo
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
help "Transform a file from a template"
|
9
|
-
|
10
|
-
usage "kojo file INFILE [--save FILE --imports DIR --args FILE] [ARGS...]"
|
11
|
-
usage "kojo file (-h|--help)"
|
12
|
-
|
13
|
-
option "-s --save FILE", "Save to file instead of printing"
|
14
|
-
option "-i --imports DIR", "Specify base directory for @import directives"
|
15
|
-
option "-a --args FILE", "Load arguments from YAML file"
|
16
|
-
|
17
|
-
param "INFILE", "Template to transform"
|
18
|
-
param "ARGS", "Optional key=value pairs"
|
19
|
-
|
20
|
-
example "kojo file main.yml"
|
21
|
-
example "kojo file main.yml --save out.yml"
|
22
|
-
example "kojo file main.yml -s out.yml app=lause"
|
23
|
-
example "kojo file main.yml -s out.yml --args args.yml"
|
24
|
-
|
25
|
-
def run
|
26
|
-
@opts = args['ARGS'].args_to_hash
|
27
|
-
@outfile = args['--save']
|
28
|
-
@infile = args['INFILE']
|
29
|
-
@import_base = args['--imports']
|
30
|
-
argfile = args['--args']
|
31
|
-
|
32
|
-
if argfile
|
33
|
-
fileopts = YAML.load_file(argfile).symbolize_keys
|
34
|
-
@opts = fileopts.merge opts
|
35
|
-
end
|
3
|
+
module Kojo
|
4
|
+
module Commands
|
5
|
+
# Handle calls to the +kojo file+ command
|
6
|
+
class FileCmd < MisterBin::Command
|
7
|
+
using Kojo::Refinements
|
36
8
|
|
37
|
-
|
38
|
-
|
9
|
+
attr_reader :opts, :outfile, :infile, :import_base
|
10
|
+
|
11
|
+
help "Transform a file from a template"
|
12
|
+
|
13
|
+
usage "kojo file INFILE [--save FILE --imports DIR --args FILE] [ARGS...]"
|
14
|
+
usage "kojo file (-h|--help)"
|
15
|
+
|
16
|
+
option "-s --save FILE", "Save to file instead of printing"
|
17
|
+
option "-i --imports DIR", "Specify base directory for @import directives"
|
18
|
+
option "-a --args FILE", "Load arguments from YAML file"
|
19
|
+
|
20
|
+
param "INFILE", "Template to transform"
|
21
|
+
param "ARGS", "Optional key=value pairs"
|
22
|
+
|
23
|
+
example "kojo file main.yml"
|
24
|
+
example "kojo file main.yml --save out.yml"
|
25
|
+
example "kojo file main.yml -s out.yml app=lause"
|
26
|
+
example "kojo file main.yml -s out.yml --args args.yml"
|
39
27
|
|
40
|
-
|
28
|
+
def run
|
29
|
+
@opts = args['ARGS'].args_to_hash
|
30
|
+
@outfile = args['--save']
|
31
|
+
@infile = args['INFILE']
|
32
|
+
@import_base = args['--imports']
|
33
|
+
argfile = args['--args']
|
41
34
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
35
|
+
if argfile
|
36
|
+
fileopts = YAML.load_file(argfile).symbolize_keys
|
37
|
+
@opts = fileopts.merge opts
|
38
|
+
end
|
46
39
|
|
47
|
-
|
48
|
-
File.write outfile, output
|
49
|
-
say "Saved #{outfile}"
|
50
|
-
else
|
51
|
-
puts output
|
40
|
+
run!
|
52
41
|
end
|
53
|
-
end
|
54
42
|
|
43
|
+
private
|
44
|
+
|
45
|
+
def run!
|
46
|
+
template = Template.new infile
|
47
|
+
template.import_base = import_base if import_base
|
48
|
+
output = template.render(opts)
|
49
|
+
|
50
|
+
if outfile
|
51
|
+
File.write outfile, output
|
52
|
+
say "Saved #{outfile}"
|
53
|
+
else
|
54
|
+
puts output
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
55
59
|
end
|
56
|
-
end
|
60
|
+
end
|
data/lib/kojo/commands/single.rb
CHANGED
@@ -1,50 +1,54 @@
|
|
1
1
|
require 'mister_bin'
|
2
2
|
|
3
|
-
module Kojo
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module Kojo
|
4
|
+
module Commands
|
5
|
+
# Handle calls to the +kojo single+ command
|
6
|
+
class SingleCmd < CommandBase
|
7
|
+
using Kojo::Refinements
|
7
8
|
|
8
|
-
|
9
|
+
attr_reader :opts, :infile, :outdir
|
9
10
|
|
10
|
-
|
11
|
-
usage "kojo single (-h|--help)"
|
11
|
+
help "Transform using a single file that contains the instructions"
|
12
12
|
|
13
|
-
|
13
|
+
usage "kojo single INFILE [--save DIR] [ARGS...]"
|
14
|
+
usage "kojo single (-h|--help)"
|
14
15
|
|
15
|
-
|
16
|
-
param "ARGS", "Optional key=value pairs"
|
16
|
+
option "-s --save DIR", "Save output to directory instead of printing"
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
example "kojo single template.Dockerfile --save output"
|
21
|
-
example "kojo single template.Dockerfile scale=2"
|
18
|
+
param "INFILE", "Template to transform. The template should contain a YAML front matter with transformation instructions"
|
19
|
+
param "ARGS", "Optional key=value pairs"
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
run!
|
28
|
-
end
|
21
|
+
example "kojo single Dockerfile"
|
22
|
+
example "kojo single template.Dockerfile --save ."
|
23
|
+
example "kojo single template.Dockerfile --save output"
|
24
|
+
example "kojo single template.Dockerfile scale=2"
|
29
25
|
|
30
|
-
|
26
|
+
def run
|
27
|
+
@opts = args['ARGS'].args_to_hash
|
28
|
+
@outdir = args['--save']
|
29
|
+
@infile = args['INFILE']
|
30
|
+
run!
|
31
|
+
end
|
31
32
|
|
32
|
-
|
33
|
-
template = Kojo::FrontMatterTemplate.new infile
|
34
|
-
outdir ? write(template) : show(template)
|
35
|
-
end
|
33
|
+
private
|
36
34
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
def run!
|
36
|
+
template = FrontMatterTemplate.new infile
|
37
|
+
outdir ? write(template) : show(template)
|
38
|
+
end
|
39
|
+
|
40
|
+
def show(template)
|
41
|
+
template.render opts do |file, output|
|
42
|
+
say "\n!txtgrn!# #{file}"
|
43
|
+
say output
|
44
|
+
end
|
41
45
|
end
|
42
|
-
end
|
43
46
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
+
def write(template)
|
48
|
+
template.render opts do |file, output|
|
49
|
+
save file, output
|
50
|
+
end
|
47
51
|
end
|
48
52
|
end
|
49
53
|
end
|
50
|
-
end
|
54
|
+
end
|
data/lib/kojo/config.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'kojo/refinements/hash'
|
2
|
+
|
3
|
+
module Kojo
|
4
|
+
module Refinements
|
5
|
+
refine Array do
|
6
|
+
# Convert an array of +["key=value", "key=value"]+ pairs to a hash
|
7
|
+
def args_to_hash
|
8
|
+
collect { |a| k, v = a.split '=' }
|
9
|
+
.to_h
|
10
|
+
.symbolize_keys
|
11
|
+
.typecast_values
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'ostruct'
|
3
|
+
require 'kojo/refinements/hash'
|
4
|
+
|
5
|
+
module Kojo
|
6
|
+
module Refinements
|
7
|
+
refine String do
|
8
|
+
# Convert a string to the most appropriate type
|
9
|
+
def to_typed
|
10
|
+
if self =~ /\A[+-]?\d+\Z/
|
11
|
+
self.to_i
|
12
|
+
|
13
|
+
elsif self =~ /\A[+-]?\d+\.\d+\Z/
|
14
|
+
self.to_f
|
15
|
+
|
16
|
+
elsif %w[yes no true false].include? downcase
|
17
|
+
%w[yes true].include? downcase
|
18
|
+
|
19
|
+
else
|
20
|
+
self
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def resolve(vars)
|
26
|
+
self % vars.symbolize_keys
|
27
|
+
|
28
|
+
rescue KeyError => e
|
29
|
+
raise unless Kojo.interactive?
|
30
|
+
|
31
|
+
print "> #{e.key}: "
|
32
|
+
vars[e.key] = get_user_input
|
33
|
+
retry
|
34
|
+
end
|
35
|
+
|
36
|
+
def eval_vars(args, filename)
|
37
|
+
resolve args.symbolize_keys
|
38
|
+
rescue ArgumentError => e
|
39
|
+
raise Kojo::TemplateError, "#{e.message}\nin: #{filename}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def eval_erb(args, filename)
|
43
|
+
erb self, args
|
44
|
+
rescue RuntimeError => e
|
45
|
+
raise Kojo::TemplateError, "Invalid Ruby code #{e.message}\nin: #{filename}"
|
46
|
+
rescue SyntaxError => e
|
47
|
+
raise Kojo::TemplateError, "#{e.message}\nin: #{filename}"
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def get_user_input
|
53
|
+
response = $stdin.gets
|
54
|
+
raise Kojo::Interrupt unless response # Ctrl+D
|
55
|
+
response.chomp
|
56
|
+
rescue ::Interrupt # Ctrl+C
|
57
|
+
raise Kojo::Interrupt
|
58
|
+
end
|
59
|
+
|
60
|
+
def erb(template, vars)
|
61
|
+
ERB.new(template, nil, '-').result(OpenStruct.new(vars).instance_eval { binding })
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/kojo/template.rb
CHANGED
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.1
|
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:
|
11
|
+
date: 2020-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mister_bin
|
@@ -72,10 +72,10 @@ files:
|
|
72
72
|
- lib/kojo/commands/single.rb
|
73
73
|
- lib/kojo/config.rb
|
74
74
|
- lib/kojo/exceptions.rb
|
75
|
-
- lib/kojo/extensions/array.rb
|
76
|
-
- lib/kojo/extensions/hash.rb
|
77
|
-
- lib/kojo/extensions/string.rb
|
78
75
|
- lib/kojo/front_matter_template.rb
|
76
|
+
- lib/kojo/refinements/array.rb
|
77
|
+
- lib/kojo/refinements/hash.rb
|
78
|
+
- lib/kojo/refinements/string.rb
|
79
79
|
- lib/kojo/template.rb
|
80
80
|
- lib/kojo/version.rb
|
81
81
|
homepage: https://github.com/dannyben/kojo
|
@@ -97,8 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
99
|
requirements: []
|
100
|
-
|
101
|
-
rubygems_version: 2.7.8
|
100
|
+
rubygems_version: 3.0.3
|
102
101
|
signing_key:
|
103
102
|
specification_version: 4
|
104
103
|
summary: Configuration Ninja
|
data/lib/kojo/extensions/hash.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
require 'erb'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
class String
|
5
|
-
# Convert a string to the most appropriate type
|
6
|
-
def to_typed
|
7
|
-
if self =~ /\A[+-]?\d+\Z/
|
8
|
-
self.to_i
|
9
|
-
|
10
|
-
elsif self =~ /\A[+-]?\d+\.\d+\Z/
|
11
|
-
self.to_f
|
12
|
-
|
13
|
-
elsif %w[yes no true false].include? downcase
|
14
|
-
%w[yes true].include? downcase
|
15
|
-
|
16
|
-
else
|
17
|
-
self
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def resolve(vars)
|
23
|
-
self % vars.symbolize_keys
|
24
|
-
|
25
|
-
rescue KeyError => e
|
26
|
-
print "> #{e.key}: "
|
27
|
-
vars[e.key] = get_user_input
|
28
|
-
retry
|
29
|
-
end
|
30
|
-
|
31
|
-
def eval_vars(args, filename)
|
32
|
-
resolve args.symbolize_keys
|
33
|
-
rescue ArgumentError => e
|
34
|
-
raise Kojo::TemplateError, "#{e.message}\nin: #{filename}"
|
35
|
-
end
|
36
|
-
|
37
|
-
def eval_erb(args, filename)
|
38
|
-
erb self, args
|
39
|
-
rescue RuntimeError => e
|
40
|
-
raise Kojo::TemplateError, "Invalid Ruby code #{e.message}\nin: #{filename}"
|
41
|
-
rescue SyntaxError => e
|
42
|
-
raise Kojo::TemplateError, "#{e.message}\nin: #{filename}"
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def get_user_input
|
48
|
-
response = $stdin.gets
|
49
|
-
raise Kojo::Interrupt unless response # Ctrl+D
|
50
|
-
response.chomp
|
51
|
-
rescue Interrupt # Ctrl+C
|
52
|
-
raise Kojo::Interrupt
|
53
|
-
end
|
54
|
-
|
55
|
-
def erb(template, vars)
|
56
|
-
ERB.new(template, nil, '-').result(OpenStruct.new(vars).instance_eval { binding })
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|