kojo 0.2.6 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +26 -4
- data/lib/kojo.rb +5 -1
- data/lib/kojo/cli.rb +1 -0
- data/lib/kojo/collection.rb +1 -1
- data/lib/kojo/commands/command_base.rb +13 -0
- data/lib/kojo/commands/config.rb +4 -3
- data/lib/kojo/commands/dir.rb +4 -11
- data/lib/kojo/commands/file.rb +4 -1
- data/lib/kojo/commands/single.rb +50 -0
- data/lib/kojo/config.rb +1 -1
- data/lib/kojo/extensions/string.rb +25 -3
- data/lib/kojo/front_matter_template.rb +42 -0
- data/lib/kojo/template.rb +5 -26
- data/lib/kojo/version.rb +1 -1
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8eb56880a27b8b6afe976fcd2f4d7b3e46efe647f6892bb616b10cf6a56f592c
|
4
|
+
data.tar.gz: be2b2b5e1a935abf7b049b7960a8d38c4694f0931fd54ae38ed0434e7ffbe58b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d55ba5416cc208750f98f7702fadf4a6e64634fc0a27123b8978b90e9892a7954d8f68efcebd32a5503fad75925ee71632cab9095b4538fe6bd03872efc60655
|
7
|
+
data.tar.gz: 2e9933b0a385a19b39e0c63aa4fc3e76f6d1b80e29eb5c3f1f3973b2a5494311b94ce757140c408d859797172a8233d5f2186ea0bd4a4abec99d3411eaf9b3ba
|
data/README.md
CHANGED
@@ -25,8 +25,9 @@ Table of Contents
|
|
25
25
|
- [Usage](#usage)
|
26
26
|
- [Variables](#variables)
|
27
27
|
- [Import](#import)
|
28
|
-
- [
|
29
|
-
- [One to Many
|
28
|
+
- [Transform an Entire Folder](#transform-an-entire-folder)
|
29
|
+
- [Transform One to Many using Config](#transform-one-to-many-using-config)
|
30
|
+
- [Transform One to Many using Front Matter](#transform-one-to-many-using-front-matter)
|
30
31
|
- [Conditions and Loops with ERB](#conditions-and-loops-with-erb)
|
31
32
|
- [Interactive Mode](#interactive-mode)
|
32
33
|
|
@@ -85,7 +86,7 @@ The space after `filename` is optional.
|
|
85
86
|
|
86
87
|
|
87
88
|
|
88
|
-
###
|
89
|
+
### Transform an Entire Folder
|
89
90
|
|
90
91
|

|
91
92
|
|
@@ -96,7 +97,7 @@ You may use `%{variables}` in filenames.
|
|
96
97
|
|
97
98
|
|
98
99
|
|
99
|
-
### One to Many
|
100
|
+
### Transform One to Many using Config
|
100
101
|
|
101
102
|

|
102
103
|
|
@@ -141,6 +142,27 @@ output:
|
|
141
142
|
argument2: value
|
142
143
|
```
|
143
144
|
|
145
|
+
### Transform One to Many using Front Matter
|
146
|
+
|
147
|
+

|
148
|
+
|
149
|
+
Define a template that contains the instructions on how to transform it as a
|
150
|
+
YAML front matter.
|
151
|
+
|
152
|
+
The YAML front matter should be structured like this:
|
153
|
+
|
154
|
+
```yaml
|
155
|
+
filename2:
|
156
|
+
arg: value
|
157
|
+
another_arg: value
|
158
|
+
|
159
|
+
filename2:
|
160
|
+
arg: value
|
161
|
+
another_arg: value
|
162
|
+
---
|
163
|
+
Your template that uses %{arg} goes here
|
164
|
+
...
|
165
|
+
```
|
144
166
|
|
145
167
|
### Conditions and Loops with ERB
|
146
168
|
|
data/lib/kojo.rb
CHANGED
data/lib/kojo/cli.rb
CHANGED
data/lib/kojo/collection.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'mister_bin'
|
2
|
+
|
3
|
+
module Kojo::Commands
|
4
|
+
class CommandBase < MisterBin::Command
|
5
|
+
def save(file, output)
|
6
|
+
outpath = "#{outdir}/#{file}"
|
7
|
+
dir = File.dirname outpath
|
8
|
+
FileUtils.mkdir_p dir unless Dir.exist? dir
|
9
|
+
File.write outpath, output
|
10
|
+
say "Saved #{outpath}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/kojo/commands/config.rb
CHANGED
@@ -6,7 +6,7 @@ module Kojo::Commands
|
|
6
6
|
class ConfigCmd < MisterBin::Command
|
7
7
|
attr_reader :gen, :outdir, :opts, :import_base, :config_file
|
8
8
|
|
9
|
-
help "
|
9
|
+
help "Transform based on instructions from a config file"
|
10
10
|
|
11
11
|
usage "kojo config CONFIG [--save DIR --imports DIR --args FILE] [ARGS...]"
|
12
12
|
usage "kojo config (-h|--help)"
|
@@ -15,6 +15,7 @@ module Kojo::Commands
|
|
15
15
|
option "-i --imports DIR", "Specify base directory for @import directives"
|
16
16
|
option "-a --args FILE", "Load arguments from YAML file"
|
17
17
|
|
18
|
+
param "CONFIG", "YAML configuration file"
|
18
19
|
param "ARGS", "Optional key=value pairs"
|
19
20
|
|
20
21
|
example "kojo config config.yml"
|
@@ -37,6 +38,8 @@ module Kojo::Commands
|
|
37
38
|
run!
|
38
39
|
end
|
39
40
|
|
41
|
+
private
|
42
|
+
|
40
43
|
def run!
|
41
44
|
config = Kojo::Config.new config_file
|
42
45
|
config.import_base = import_base if import_base
|
@@ -46,8 +49,6 @@ module Kojo::Commands
|
|
46
49
|
end
|
47
50
|
end
|
48
51
|
|
49
|
-
private
|
50
|
-
|
51
52
|
def handle(file, output)
|
52
53
|
if outdir
|
53
54
|
save "#{outdir}/#{file}", output
|
data/lib/kojo/commands/dir.rb
CHANGED
@@ -2,10 +2,10 @@ require 'mister_bin'
|
|
2
2
|
|
3
3
|
module Kojo::Commands
|
4
4
|
# Handle calls to the +kojo dir+ command
|
5
|
-
class DirCmd <
|
5
|
+
class DirCmd < CommandBase
|
6
6
|
attr_reader :opts, :indir, :outdir, :import_base
|
7
7
|
|
8
|
-
help "
|
8
|
+
help "Transform a folder of templates to a similar output folder"
|
9
9
|
|
10
10
|
usage "kojo dir INDIR [--save DIR --imports DIR --args FILE] [ARGS...]"
|
11
11
|
usage "kojo dir (-h|--help)"
|
@@ -14,6 +14,7 @@ module Kojo::Commands
|
|
14
14
|
option "-i --imports DIR", "Specify base directory for @import directives"
|
15
15
|
option "-a --args FILE", "Load arguments from YAML file"
|
16
16
|
|
17
|
+
param "INDIR", "Directory containing templates to transform"
|
17
18
|
param "ARGS", "Optional key=value pairs"
|
18
19
|
|
19
20
|
example "kojo dir indir"
|
@@ -36,7 +37,7 @@ module Kojo::Commands
|
|
36
37
|
run!
|
37
38
|
end
|
38
39
|
|
39
|
-
|
40
|
+
private
|
40
41
|
|
41
42
|
def run!
|
42
43
|
collection = Kojo::Collection.new @indir
|
@@ -61,13 +62,5 @@ module Kojo::Commands
|
|
61
62
|
save file, output
|
62
63
|
end
|
63
64
|
end
|
64
|
-
|
65
|
-
def save(file, output)
|
66
|
-
outpath = "#{outdir}/#{file}"
|
67
|
-
dir = File.dirname outpath
|
68
|
-
FileUtils.mkdir_p dir unless Dir.exist? dir
|
69
|
-
File.write outpath, output
|
70
|
-
say "Saved #{outpath}"
|
71
|
-
end
|
72
65
|
end
|
73
66
|
end
|
data/lib/kojo/commands/file.rb
CHANGED
@@ -5,7 +5,7 @@ module Kojo::Commands
|
|
5
5
|
class FileCmd < MisterBin::Command
|
6
6
|
attr_reader :opts, :outfile, :infile, :import_base
|
7
7
|
|
8
|
-
help "
|
8
|
+
help "Transform a file from a template"
|
9
9
|
|
10
10
|
usage "kojo file INFILE [--save FILE --imports DIR --args FILE] [ARGS...]"
|
11
11
|
usage "kojo file (-h|--help)"
|
@@ -14,6 +14,7 @@ module Kojo::Commands
|
|
14
14
|
option "-i --imports DIR", "Specify base directory for @import directives"
|
15
15
|
option "-a --args FILE", "Load arguments from YAML file"
|
16
16
|
|
17
|
+
param "INFILE", "Template to transform"
|
17
18
|
param "ARGS", "Optional key=value pairs"
|
18
19
|
|
19
20
|
example "kojo file main.yml"
|
@@ -36,6 +37,8 @@ module Kojo::Commands
|
|
36
37
|
run!
|
37
38
|
end
|
38
39
|
|
40
|
+
private
|
41
|
+
|
39
42
|
def run!
|
40
43
|
template = Kojo::Template.new infile
|
41
44
|
template.import_base = import_base if import_base
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'mister_bin'
|
2
|
+
|
3
|
+
module Kojo::Commands
|
4
|
+
# Handle calls to the +kojo single+ command
|
5
|
+
class SingleCmd < CommandBase
|
6
|
+
attr_reader :opts, :infile, :outdir
|
7
|
+
|
8
|
+
help "Transform using a single file that contains the instructions"
|
9
|
+
|
10
|
+
usage "kojo single INFILE [--save DIR] [ARGS...]"
|
11
|
+
usage "kojo single (-h|--help)"
|
12
|
+
|
13
|
+
option "-s --save DIR", "Save output to directory instead of printing"
|
14
|
+
|
15
|
+
param "INFILE", "Template to transform. The template should contain a YAML front matter with transformation instructions"
|
16
|
+
param "ARGS", "Optional key=value pairs"
|
17
|
+
|
18
|
+
example "kojo single Dockerfile"
|
19
|
+
example "kojo single template.Dockerfile --save ."
|
20
|
+
example "kojo single template.Dockerfile --save output"
|
21
|
+
example "kojo single template.Dockerfile scale=2"
|
22
|
+
|
23
|
+
def run
|
24
|
+
@opts = args['ARGS'].args_to_hash
|
25
|
+
@outdir = args['--save']
|
26
|
+
@infile = args['INFILE']
|
27
|
+
run!
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def run!
|
33
|
+
template = Kojo::FrontMatterTemplate.new infile
|
34
|
+
outdir ? write(template) : show(template)
|
35
|
+
end
|
36
|
+
|
37
|
+
def show(template)
|
38
|
+
template.render opts do |file, output|
|
39
|
+
say "\n!txtgrn!# #{file}"
|
40
|
+
say output
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def write(template)
|
45
|
+
template.render opts do |file, output|
|
46
|
+
save file, output
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/kojo/config.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'ostruct'
|
3
|
+
|
1
4
|
class String
|
2
5
|
# Convert a string to the most appropriate type
|
3
6
|
def to_typed
|
@@ -17,7 +20,7 @@ class String
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def resolve(vars)
|
20
|
-
self % vars
|
23
|
+
self % vars.symbolize_keys
|
21
24
|
|
22
25
|
rescue KeyError => e
|
23
26
|
print "> #{e.key}: "
|
@@ -25,7 +28,21 @@ class String
|
|
25
28
|
retry
|
26
29
|
end
|
27
30
|
|
28
|
-
|
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
|
29
46
|
|
30
47
|
def get_user_input
|
31
48
|
response = $stdin.gets
|
@@ -34,4 +51,9 @@ class String
|
|
34
51
|
rescue Interrupt # Ctrl+C
|
35
52
|
raise Kojo::Interrupt
|
36
53
|
end
|
37
|
-
|
54
|
+
|
55
|
+
def erb(template, vars)
|
56
|
+
ERB.new(template, nil, '-').result(OpenStruct.new(vars).instance_eval { binding })
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Kojo
|
4
|
+
# The FrontMatterTemplate class handles a single template file, that
|
5
|
+
# contains a YAML front matter.
|
6
|
+
class FrontMatterTemplate
|
7
|
+
attr_reader :file, :args, :template
|
8
|
+
|
9
|
+
def initialize(file)
|
10
|
+
@file = file
|
11
|
+
end
|
12
|
+
|
13
|
+
def render(additional_args=nil)
|
14
|
+
additional_args ||= {}
|
15
|
+
config, @template = read_file file
|
16
|
+
|
17
|
+
config.each do |outfile, args|
|
18
|
+
content = handle args.merge(additional_args)
|
19
|
+
yield outfile, content
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def handle(args={})
|
26
|
+
content = template
|
27
|
+
content = content.eval_erb args, file
|
28
|
+
content = content.eval_vars args, file
|
29
|
+
content
|
30
|
+
end
|
31
|
+
|
32
|
+
def read_file(file)
|
33
|
+
raise Kojo::NotFoundError, "File not found: #{file}" unless File.exist? file
|
34
|
+
|
35
|
+
config = YAML.load_file file
|
36
|
+
content = File.read(file)[/^---\s*$\n(.*)/m, 1]
|
37
|
+
|
38
|
+
[config, content]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/lib/kojo/template.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
require 'erb'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
1
|
module Kojo
|
5
2
|
# The Template class handles a single template file, and processes it for:
|
6
3
|
# - Variables (using +%{var}+ syntax)
|
@@ -22,12 +19,12 @@ module Kojo
|
|
22
19
|
evaluate file
|
23
20
|
end
|
24
21
|
|
25
|
-
|
22
|
+
protected
|
26
23
|
|
27
24
|
def evaluate(file)
|
28
25
|
content = read_file file
|
29
|
-
content = eval_erb
|
30
|
-
content = eval_vars
|
26
|
+
content = content.eval_erb args, file
|
27
|
+
content = content.eval_vars args, file
|
31
28
|
content = eval_imports content
|
32
29
|
content
|
33
30
|
end
|
@@ -37,20 +34,6 @@ module Kojo
|
|
37
34
|
File.read file
|
38
35
|
end
|
39
36
|
|
40
|
-
def eval_vars(content)
|
41
|
-
content.resolve args
|
42
|
-
rescue ArgumentError => e
|
43
|
-
raise Kojo::TemplateError, "#{e.message}\nin: #{file}"
|
44
|
-
end
|
45
|
-
|
46
|
-
def eval_erb(content)
|
47
|
-
erb content, args
|
48
|
-
rescue RuntimeError => e
|
49
|
-
raise Kojo::TemplateError, "Invalid Ruby code #{e.message}\nin: #{file}"
|
50
|
-
rescue SyntaxError => e
|
51
|
-
raise Kojo::TemplateError, "#{e.message}\nin: #{file}"
|
52
|
-
end
|
53
|
-
|
54
37
|
def eval_imports(content)
|
55
38
|
result = []
|
56
39
|
|
@@ -78,13 +61,9 @@ module Kojo
|
|
78
61
|
self.class.new(filename).render(all_args)
|
79
62
|
end
|
80
63
|
|
81
|
-
def erb(template, vars)
|
82
|
-
ERB.new(template, nil, '-').result(OpenStruct.new(vars).instance_eval { binding })
|
83
|
-
end
|
84
|
-
|
85
64
|
def indent(text, spaces)
|
86
65
|
text.lines.collect { |line| "#{' ' * spaces}#{line}" }.join
|
87
66
|
end
|
88
|
-
end
|
89
67
|
|
90
|
-
end
|
68
|
+
end
|
69
|
+
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.
|
4
|
+
version: 0.3.0
|
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: 2019-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mister_bin
|
@@ -65,14 +65,17 @@ files:
|
|
65
65
|
- lib/kojo.rb
|
66
66
|
- lib/kojo/cli.rb
|
67
67
|
- lib/kojo/collection.rb
|
68
|
+
- lib/kojo/commands/command_base.rb
|
68
69
|
- lib/kojo/commands/config.rb
|
69
70
|
- lib/kojo/commands/dir.rb
|
70
71
|
- lib/kojo/commands/file.rb
|
72
|
+
- lib/kojo/commands/single.rb
|
71
73
|
- lib/kojo/config.rb
|
72
74
|
- lib/kojo/exceptions.rb
|
73
75
|
- lib/kojo/extensions/array.rb
|
74
76
|
- lib/kojo/extensions/hash.rb
|
75
77
|
- lib/kojo/extensions/string.rb
|
78
|
+
- lib/kojo/front_matter_template.rb
|
76
79
|
- lib/kojo/template.rb
|
77
80
|
- lib/kojo/version.rb
|
78
81
|
homepage: https://github.com/dannyben/kojo
|
@@ -95,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
98
|
version: '0'
|
96
99
|
requirements: []
|
97
100
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.7.
|
101
|
+
rubygems_version: 2.7.8
|
99
102
|
signing_key:
|
100
103
|
specification_version: 4
|
101
104
|
summary: Configuration Ninja
|