kojo 0.2.6 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ebe60ba380017dc65cf0261fd73ece4ab7f9b70138145ea20c2735ec114f39f
4
- data.tar.gz: cb704fc398df5df582f5b821295c7681f0879e7e9eaf96280e6a1af2bf43d261
3
+ metadata.gz: 8eb56880a27b8b6afe976fcd2f4d7b3e46efe647f6892bb616b10cf6a56f592c
4
+ data.tar.gz: be2b2b5e1a935abf7b049b7960a8d38c4694f0931fd54ae38ed0434e7ffbe58b
5
5
  SHA512:
6
- metadata.gz: ea47a7425bffbe605b8f96537b04ee0b56ff8ec437c44f6dcdac6044a7705c7c06d449361796ca66b0a2b2fac3baf0512bd8e8dbe909a89f120d6995ff0b10af
7
- data.tar.gz: 9f9be3030a47599758014e81ae915f617624873aacd326c20ff44d27e295fb1da04d6938e9a909ec53e17c5c527500c230048004fe4241578ca568812e16df98
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
- - [Compile an Entire Folder](#compile-an-entire-folder)
29
- - [One to Many Generation](#one-to-many-generation)
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
- ### Compile an Entire Folder
89
+ ### Transform an Entire Folder
89
90
 
90
91
  ![kojo](images/features-dir.svg)
91
92
 
@@ -96,7 +97,7 @@ You may use `%{variables}` in filenames.
96
97
 
97
98
 
98
99
 
99
- ### One to Many Generation
100
+ ### Transform One to Many using Config
100
101
 
101
102
  ![kojo](images/features-config.svg)
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
+ ![kojo](images/features-single.svg)
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
 
@@ -1,6 +1,10 @@
1
1
  require 'requires'
2
2
  require 'byebug' if ENV['BYEBUG']
3
- requires 'kojo/exceptions', 'kojo'
3
+
4
+ requires 'kojo/exceptions'
5
+ requires 'kojo/template'
6
+ requires 'kojo/commands/command_base'
7
+ requires 'kojo'
4
8
 
5
9
  module Kojo
6
10
  module Commands
@@ -10,6 +10,7 @@ module Kojo
10
10
  runner.route 'file', to: Kojo::Commands::FileCmd
11
11
  runner.route 'dir', to: Kojo::Commands::DirCmd
12
12
  runner.route 'config', to: Kojo::Commands::ConfigCmd
13
+ runner.route 'single', to: Kojo::Commands::SingleCmd
13
14
 
14
15
  runner
15
16
  end
@@ -16,7 +16,7 @@ module Kojo
16
16
  end
17
17
  end
18
18
 
19
- private
19
+ private
20
20
 
21
21
  def handle(file, args={})
22
22
  template = Template.new file
@@ -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
@@ -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 "Generate based on instructions from a config file"
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
@@ -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 < MisterBin::Command
5
+ class DirCmd < CommandBase
6
6
  attr_reader :opts, :indir, :outdir, :import_base
7
7
 
8
- help "Compile a folder of templates to a similar output folder"
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
- private
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
@@ -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 "Compile a file from a template"
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
@@ -20,7 +20,7 @@ module Kojo
20
20
  end
21
21
  end
22
22
 
23
- private
23
+ private
24
24
 
25
25
  def generate_from_file(opts)
26
26
  config['output'].each do |target, config_opts|
@@ -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
- private
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
- end
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
@@ -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
- private
22
+ protected
26
23
 
27
24
  def evaluate(file)
28
25
  content = read_file file
29
- content = eval_erb content
30
- content = eval_vars content
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
@@ -1,3 +1,3 @@
1
1
  module Kojo
2
- VERSION = "0.2.6"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.6
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: 2018-12-13 00:00:00.000000000 Z
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.6
101
+ rubygems_version: 2.7.8
99
102
  signing_key:
100
103
  specification_version: 4
101
104
  summary: Configuration Ninja