kojo 0.2.0 → 0.2.1

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: 45f5390450577b2234342140411f0e0f2c0d35922dcea389217797ec2937c4f6
4
- data.tar.gz: 6646baf64c8ca5df34c0d93b4937ed045efe35514b5f07e25abcf73b92392c6d
3
+ metadata.gz: 66b59e59e2a7d874db8c30218544c194798aa8b0e0680dbc1a906f3f75f67e59
4
+ data.tar.gz: 56ff38e17c7cedd6df3373275fdacc65e9bc7fe8f2df5bdf7bdd650edea5eb37
5
5
  SHA512:
6
- metadata.gz: ce8e0883e9c75c615d88c2597554e0b0f927ddef024a36ab62873b4ee67e9193a128f25c70dab4adc70fda927ccdfa1aa0684308c9a82377fb16acf123db6ff9
7
- data.tar.gz: d4e562cc197c81f125a43f81ab5c5a0d625cc607fea2556cb5f9baa4b698027291208ca224d719f87421e57812b305f97f8c5804375ba655eb3e81a76c0cd12f
6
+ metadata.gz: 1202ad70051723bcbeceaf4ef641704f133685177dc42f9479bb7d4391d69e0b1d6b80245b684c68788627933be382843865654a6b7766e4ad6379d5963d4c89
7
+ data.tar.gz: 312a0a592b342cef71bbe08f5c38c3db2007047fa052ae3f34faa65910d4cb41af60deb9b241804290a2ab3d2e42c269e8581ece1c543fae3ddf9c2349d94387
data/README.md CHANGED
@@ -18,6 +18,19 @@ It is a command line utility, and it works on any text file format.
18
18
 
19
19
  ---
20
20
 
21
+ Table of Contents
22
+ --------------------------------------------------
23
+
24
+ - [Installation](#installation)
25
+ - [Usage](#usage)
26
+ - [Variables](#variables)
27
+ - [Import](#import)
28
+ - [One to Many Generation](#one-to-many-generation)
29
+ - [Compile an Entire Folder](#compile-an-entire-folder)
30
+ - [Conditions and Loops with ERB](#conditions-and-loops-with-erb)
31
+
32
+ ---
33
+
21
34
  Installation
22
35
  --------------------------------------------------
23
36
 
@@ -48,6 +61,7 @@ Include variables in your configuration templates using this syntax:
48
61
  with any additional variables that are defined in subsequent `@imports`.
49
62
 
50
63
 
64
+
51
65
  ### Import
52
66
 
53
67
  ![kojo](images/features-import.svg)
@@ -70,6 +84,7 @@ resulting configuration file.
70
84
  The space after `filename` is optional.
71
85
 
72
86
 
87
+
73
88
  ### One to Many Generation
74
89
 
75
90
  ![kojo](images/features-config.svg)
@@ -95,9 +110,28 @@ output:
95
110
  ```
96
111
 
97
112
 
113
+
98
114
  ### Compile an Entire Folder
99
115
 
100
116
  ![kojo](images/features-dir.svg)
101
117
 
102
118
  Process a folder containing templates and `@imports`, and generate a mirror
103
119
  output folder, with all the variables and `@imports` evaluated.
120
+
121
+
122
+
123
+ ### Conditions and Loops with ERB
124
+
125
+ ![kojo](images/features-erb.svg)
126
+
127
+ Template files are evaluated using ERB, so you can use any Ruby code for more
128
+ advanced templates (for conditions, loops etc.).
129
+
130
+ Use this syntax for ruby code:
131
+
132
+ ```erb
133
+ <%- ruby code here -%> # for code that should not be printed
134
+ <%= ruby code here -%> # for code that should be printed
135
+ ```
136
+
137
+
data/lib/kojo/cli.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'mister_bin'
2
- require 'kojo/commands'
3
2
 
4
3
  module Kojo
5
4
  class CLI
@@ -1,52 +1,62 @@
1
1
  require 'fileutils'
2
+ require 'mister_bin'
2
3
 
3
- module Kojo
4
- module Commands
5
- class ConfigCmd < MisterBin::Command
6
- include Colsole
7
-
8
- help "Generate based on instructions from a config file"
9
-
10
- usage "kojo config CONFIG [--save DIR --args FILE] [ARGS...]"
11
- usage "kojo config (-h|--help)"
12
-
13
- option "-s --save DIR", "Save output to directory instead of printing"
14
- option "-a --args FILE", "Load arguments from YAML file"
15
-
16
- param "ARGS", "Optional key=value pairs"
17
-
18
- example "kojo config config.yml"
19
- example "kojo config config.yml --save output"
20
- example "kojo config config.yml -s output scale=3"
21
- example "kojo config config.yml -s output --args args.yml"
22
-
23
- def run(args)
24
- gen = Kojo::Generator.new args['CONFIG']
25
- outdir = args['--save']
26
- opts = args['ARGS'].args_to_hash
27
- argfile = args['--args']
28
-
29
- if argfile
30
- fileopts = YAML.load_file(argfile).symbolize_keys
31
- opts = fileopts.merge opts
32
- end
33
-
34
- gen.generate opts do |file, output|
35
- path = "#{outdir}/#{file}"
36
- if outdir
37
- dir = File.dirname path
38
- FileUtils.mkdir_p dir unless Dir.exist? dir
39
-
40
- File.write path, output
41
- say "Saved #{path}"
42
- else
43
- say "\n!txtgrn!# #{file}"
44
- say output
45
- end
46
- end
4
+ module Kojo::Commands
5
+ class ConfigCmd < MisterBin::Command
6
+ attr_reader :gen, :outdir, :opts
7
+
8
+ help "Generate based on instructions from a config file"
9
+
10
+ usage "kojo config CONFIG [--save DIR --args FILE] [ARGS...]"
11
+ usage "kojo config (-h|--help)"
12
+
13
+ option "-s --save DIR", "Save output to directory instead of printing"
14
+ option "-a --args FILE", "Load arguments from YAML file"
15
+
16
+ param "ARGS", "Optional key=value pairs"
17
+
18
+ example "kojo config config.yml"
19
+ example "kojo config config.yml --save output"
20
+ example "kojo config config.yml -s output scale=3"
21
+ example "kojo config config.yml -s output --args args.yml"
22
+
23
+ def run(args)
24
+ @gen = Kojo::Generator.new args['CONFIG']
25
+ @outdir = args['--save']
26
+ @opts = args['ARGS'].args_to_hash
27
+ argfile = args['--args']
28
+
29
+ if argfile
30
+ fileopts = YAML.load_file(argfile).symbolize_keys
31
+ @opts = fileopts.merge opts
32
+ end
33
+
34
+ run!
35
+ end
36
+
37
+ def run!
38
+ gen.generate opts do |file, output|
39
+ handle file, output
47
40
  end
41
+ end
48
42
 
43
+ private
44
+
45
+ def handle(file, output)
46
+ if outdir
47
+ save "#{outdir}/#{file}", output
48
+ else
49
+ say "\n!txtgrn!# #{file}"
50
+ say output
51
+ end
49
52
  end
53
+
54
+ def save(path, output)
55
+ dir = File.dirname path
56
+ FileUtils.mkdir_p dir unless Dir.exist? dir
57
+ File.write path, output
58
+ say "Saved #{path}"
59
+ end
60
+
50
61
  end
51
62
  end
52
-
@@ -1,61 +1,72 @@
1
- module Kojo
2
- module Commands
3
- class DirCmd < MisterBin::Command
4
- include Colsole
5
-
6
- help "Compile a folder of templates to a similar output folder"
7
-
8
- usage "kojo dir INDIR [--save DIR --import DIR --args FILE] [ARGS...]"
9
- usage "kojo dir (-h|--help)"
10
-
11
- option "-s --save DIR", "Save output to directory instead of printing"
12
- option "-i --import DIR", "Specify base directory for @import directives"
13
- option "-a --args FILE", "Load arguments from YAML file"
14
-
15
- param "ARGS", "Optional key=value pairs"
16
-
17
- example "kojo dir indir"
18
- example "kojo dir in --save out env=production"
19
- example "kojo dir in --save out --import snippets env=production"
20
- example "kojo dir in -s out -i snippets -a args.yml"
21
-
22
- def run(args)
23
- opts = args['ARGS'].args_to_hash
24
- indir = args['INDIR']
25
- outdir = args['--save']
26
- import_base = args['--import']
27
- argfile = args['--args']
28
-
29
- if argfile
30
- fileopts = YAML.load_file(argfile).symbolize_keys
31
- opts = fileopts.merge opts
32
- end
33
-
34
- files = Dir["#{indir}/**/*"].reject { |file| File.directory? file }
35
-
36
- files.each do |file|
37
-
38
- template = Kojo::Template.new(file, opts)
39
- template.import_base = import_base if import_base
40
- output = template.render
41
-
42
- if outdir
43
- outpath = file.sub(/#{indir}/, outdir)
44
-
45
- dir = File.dirname outpath
46
- FileUtils.mkdir_p dir unless Dir.exist? dir
47
-
48
- File.write outpath, output
49
- say "Saved #{outpath}"
50
- else
51
- outpath = file.sub(/#{indir}/, '')
52
- say "\n!txtgrn!# #{outpath}"
53
- say output
54
- end
55
- end
1
+ require 'mister_bin'
2
+
3
+ module Kojo::Commands
4
+ class DirCmd < MisterBin::Command
5
+ attr_reader :opts, :indir, :outdir, :import_base
6
+
7
+ help "Compile a folder of templates to a similar output folder"
8
+
9
+ usage "kojo dir INDIR [--save DIR --import DIR --args FILE] [ARGS...]"
10
+ usage "kojo dir (-h|--help)"
11
+
12
+ option "-s --save DIR", "Save output to directory instead of printing"
13
+ option "-i --import DIR", "Specify base directory for @import directives"
14
+ option "-a --args FILE", "Load arguments from YAML file"
15
+
16
+ param "ARGS", "Optional key=value pairs"
17
+
18
+ example "kojo dir indir"
19
+ example "kojo dir in --save out env=production"
20
+ example "kojo dir in --save out --import snippets env=production"
21
+ example "kojo dir in -s out -i snippets -a args.yml"
22
+
23
+ def run(args)
24
+ @opts = args['ARGS'].args_to_hash
25
+ @indir = args['INDIR']
26
+ @outdir = args['--save']
27
+ @import_base = args['--import']
28
+ argfile = args['--args']
29
+
30
+ if argfile
31
+ fileopts = YAML.load_file(argfile).symbolize_keys
32
+ @opts = fileopts.merge @opts
56
33
  end
57
34
 
35
+ run!
36
+ end
37
+
38
+ private
39
+
40
+ def files
41
+ @files ||= Dir["#{indir}/**/*"].reject { |file| File.directory? file }
42
+ end
43
+
44
+ def run!
45
+ files.each do |file|
46
+ handle file
47
+ end
48
+ end
49
+
50
+ def handle(file)
51
+ template = Kojo::Template.new(file, opts)
52
+ template.import_base = import_base if import_base
53
+ output = template.render
54
+
55
+ if outdir
56
+ save file, output
57
+ else
58
+ outpath = file.sub(/#{indir}/, '')
59
+ say "\n!txtgrn!# #{outpath}"
60
+ say output
61
+ end
62
+ end
63
+
64
+ def save(file, output)
65
+ outpath = file.sub(/#{indir}/, outdir)
66
+ dir = File.dirname outpath
67
+ FileUtils.mkdir_p dir unless Dir.exist? dir
68
+ File.write outpath, output
69
+ say "Saved #{outpath}"
58
70
  end
59
71
  end
60
72
  end
61
-
@@ -1,47 +1,48 @@
1
- require 'kojo'
1
+ require 'mister_bin'
2
2
 
3
- module Kojo
4
- module Commands
5
- class FileCmd < MisterBin::Command
6
- include Colsole
3
+ module Kojo::Commands
4
+ class FileCmd < MisterBin::Command
5
+ attr_reader :opts, :outfile, :infile
7
6
 
8
- help "Compile a file from a template"
7
+ help "Compile a file from a template"
9
8
 
10
- usage "kojo file INFILE [--save FILE --args FILE] [ARGS...]"
11
- usage "kojo file (-h|--help)"
9
+ usage "kojo file INFILE [--save FILE --args FILE] [ARGS...]"
10
+ usage "kojo file (-h|--help)"
12
11
 
13
- option "-s --save FILE", "Save to file instead of printing"
14
- option "-a --args FILE", "Load arguments from YAML file"
12
+ option "-s --save FILE", "Save to file instead of printing"
13
+ option "-a --args FILE", "Load arguments from YAML file"
15
14
 
16
- param "ARGS", "Optional key=value pairs"
15
+ param "ARGS", "Optional key=value pairs"
17
16
 
18
- example "kojo file main.yml"
19
- example "kojo file main.yml --save out.yml"
20
- example "kojo file main.yml -s out.yml app=lause"
21
- example "kojo file main.yml -s out.yml --args args.yml"
17
+ example "kojo file main.yml"
18
+ example "kojo file main.yml --save out.yml"
19
+ example "kojo file main.yml -s out.yml app=lause"
20
+ example "kojo file main.yml -s out.yml --args args.yml"
22
21
 
23
- def run(args)
24
- opts = args['ARGS'].args_to_hash
25
- outfile = args['--save']
26
- infile = args['INFILE']
27
- argfile = args['--args']
22
+ def run(args)
23
+ @opts = args['ARGS'].args_to_hash
24
+ @outfile = args['--save']
25
+ @infile = args['INFILE']
26
+ argfile = args['--args']
28
27
 
29
- if argfile
30
- fileopts = YAML.load_file(argfile).symbolize_keys
31
- opts = fileopts.merge opts
32
- end
28
+ if argfile
29
+ fileopts = YAML.load_file(argfile).symbolize_keys
30
+ @opts = fileopts.merge opts
31
+ end
33
32
 
34
- output = Kojo::Template.new(infile, opts).render
33
+ run!
34
+ end
35
35
 
36
- if outfile
37
- File.write outfile, output
38
- say "Saved #{outfile}"
39
- else
40
- puts output
41
- end
42
- end
36
+ def run!
37
+ output = Kojo::Template.new(infile, opts).render
43
38
 
39
+ if outfile
40
+ File.write outfile, output
41
+ say "Saved #{outfile}"
42
+ else
43
+ puts output
44
+ end
44
45
  end
46
+
45
47
  end
46
48
  end
47
-
@@ -1,5 +1,10 @@
1
+ require 'kojo/extensions/hash'
2
+
1
3
  class Array
2
4
  def args_to_hash
3
- collect { |a| k, v = a.split '=' }.to_h.transform_keys(&:to_sym)
5
+ collect { |a| k, v = a.split '=' }
6
+ .to_h
7
+ .symbolize_keys
8
+ .typecast_values
4
9
  end
5
10
  end
@@ -1,5 +1,11 @@
1
+ require 'kojo/extensions/string'
2
+
1
3
  class Hash
2
4
  def symbolize_keys
3
5
  transform_keys &:to_sym
4
6
  end
7
+
8
+ def typecast_values
9
+ transform_values &:to_typed
10
+ end
5
11
  end
@@ -0,0 +1,17 @@
1
+ class String
2
+ def to_typed
3
+ if self =~ /\A[+-]?\d+\Z/
4
+ self.to_i
5
+
6
+ elsif self =~ /\A[+-]?\d+\.\d+\Z/
7
+ self.to_f
8
+
9
+ elsif %w[yes no true false].include? self.downcase
10
+ %w[yes true].include? self.downcase
11
+
12
+ else
13
+ self
14
+
15
+ end
16
+ end
17
+ end
data/lib/kojo/template.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'erb'
2
+ require 'ostruct'
3
+
1
4
  module Kojo
2
5
  class Template
3
6
  attr_reader :file, :extension, :args, :dir
@@ -17,14 +20,15 @@ module Kojo
17
20
 
18
21
  private
19
22
 
20
- def import(file, import_args={})
21
- filename = File.expand_path "#{file}#{extension}", import_base
22
- all_args = args.merge import_args
23
- self.class.new(filename, all_args).render
23
+ def evaluate(file, args={})
24
+ content = File.read(file)
25
+
26
+ content = erb content, args
27
+ content = content % args
28
+ content = eval_imports content
24
29
  end
25
30
 
26
- def evaluate(file, args={})
27
- content = File.read(file) % args
31
+ def eval_imports(content)
28
32
  result = []
29
33
 
30
34
  content.lines.each do |line|
@@ -41,9 +45,20 @@ module Kojo
41
45
 
42
46
  result.push line
43
47
  end
48
+
44
49
  result.join "\n"
45
50
  end
46
51
 
52
+ def import(file, import_args={})
53
+ filename = File.expand_path "#{file}#{extension}", import_base
54
+ all_args = args.merge import_args
55
+ self.class.new(filename, all_args).render
56
+ end
57
+
58
+ def erb(template, vars)
59
+ ERB.new(template, nil, '-').result(OpenStruct.new(vars).instance_eval { binding })
60
+ end
61
+
47
62
  def indent(text, spaces)
48
63
  text.lines.collect { |line| "#{' ' * spaces}#{line}" }.join
49
64
  end
data/lib/kojo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kojo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/kojo.rb CHANGED
@@ -1,9 +1,8 @@
1
- require 'kojo/extensions/array'
2
- require 'kojo/extensions/hash'
3
-
4
- require 'kojo/cli'
5
- require 'kojo/template'
6
- require 'kojo/generator'
7
- require 'kojo/version'
8
-
1
+ require 'requires'
9
2
  require 'byebug' if ENV['BYEBUG']
3
+ requires 'kojo'
4
+
5
+ module Kojo
6
+ module Commands
7
+ end
8
+ 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.0
4
+ version: 0.2.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: 2018-08-05 00:00:00.000000000 Z
11
+ date: 2018-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mister_bin
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: requires
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: byebug
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -120,12 +134,12 @@ files:
120
134
  - bin/kojo
121
135
  - lib/kojo.rb
122
136
  - lib/kojo/cli.rb
123
- - lib/kojo/commands.rb
124
137
  - lib/kojo/commands/config.rb
125
138
  - lib/kojo/commands/dir.rb
126
139
  - lib/kojo/commands/file.rb
127
140
  - lib/kojo/extensions/array.rb
128
141
  - lib/kojo/extensions/hash.rb
142
+ - lib/kojo/extensions/string.rb
129
143
  - lib/kojo/generator.rb
130
144
  - lib/kojo/template.rb
131
145
  - lib/kojo/version.rb
data/lib/kojo/commands.rb DELETED
@@ -1,3 +0,0 @@
1
- require 'kojo/commands/config'
2
- require 'kojo/commands/dir'
3
- require 'kojo/commands/file'