kojo 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6dcf75263bba96888bad2f8988215d0c20f911ed331a560aa5eb544ee029e0d9
4
+ data.tar.gz: 4a56066e3bead9aed02ce0140d011c7f678dceb6406cda08d81defc517fbe65a
5
+ SHA512:
6
+ metadata.gz: 5124c69508ed6730bf07d280ea0d31db15ceed65cb2fa45965e89c2a759b6aa001327509839ddc9d7cf3ae285230a55a9ad3ed68648caaa87d90711bfaffa556
7
+ data.tar.gz: 30ab1f2f3d98f5e8cf26b78f07e67765671af5d18fcf50c0a923be09be0dd3928e69592ca8843cc28167bd0044999fa63ad61a15e6c690388ec4f6cde1fc7743
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ ![kojo](kojo.png)
2
+
3
+ Kojo - Configuration Ninja
4
+ ==================================================
5
+
6
+ [![Gem](https://img.shields.io/gem/v/kojo.svg?style=flat-square)](https://rubygems.org/gems/kojo)
7
+ [![Build](https://img.shields.io/travis/DannyBen/kojo.svg?style=flat-square)](https://travis-ci.org/DannyBen/kojo)
8
+ [![Maintainability](https://img.shields.io/codeclimate/maintainability/DannyBen/kojo.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/kojo)
9
+ [![Issues](https://img.shields.io/codeclimate/issues/github/DannyBen/kojo.svg?style=flat-square)](https://codeclimate.com/github/DannyBen/kojo)
10
+
11
+ ---
12
+
13
+
14
+
15
+ ---
16
+
17
+ Installation
18
+ --------------------------------------------------
19
+
20
+ $ gem install kojo
21
+
22
+
23
+
24
+ Usage
25
+ --------------------------------------------------
26
+
27
+ TODO
data/bin/kojo ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require 'kojo'
3
+ require 'mister_bin'
4
+ require 'kojo/version'
5
+
6
+ runner = MisterBin::Runner.new 'kojo', basedir: __dir__,
7
+ header: "Kojo #{Kojo::VERSION}"
8
+
9
+ begin
10
+ exit runner.run ARGV
11
+ rescue => e
12
+ puts e.backtrace.reverse if ENV['DEBUG']
13
+ say! "!txtred!#{e.class}: #{e.message}"
14
+ exit 1
15
+ end
@@ -0,0 +1,30 @@
1
+ require 'kojo'
2
+
3
+ help "Generate a file from a template"
4
+
5
+ usage "kojo generate INFILE [--save FILE] [ARGS...]"
6
+ usage "kojo generate (-h|--help)"
7
+
8
+ option "-s --save FILE", "Save to file instead of printing"
9
+
10
+ param "ARGS", "Optional key=value pairs"
11
+
12
+ example "kojo generate main.yml"
13
+ example "kojo generate main.yml --save out.yml"
14
+ example "kojo generate main.yml -s out.yml app=lause"
15
+
16
+ action do |args|
17
+ opts = args['ARGS'].args_to_hash
18
+ outfile = args['--save']
19
+ infile = args['INFILE']
20
+
21
+ output = Kojo::Template.new(infile, opts).render
22
+
23
+ if outfile
24
+ File.write outfile, output
25
+ say "Saved #{outfile}"
26
+ else
27
+ puts output
28
+ end
29
+ end
30
+
data/bin/kojo-run.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'kojo'
2
+ require 'fileutils'
3
+
4
+ help "Run and process a config file"
5
+
6
+ usage "kojo run CONFIG [--save DIR] [ARGS...]"
7
+ usage "kojo run (-h|--help)"
8
+
9
+ option "-s --save DIR", "Save output to directory instead of printing"
10
+
11
+ param "ARGS", "Optional key=value pairs"
12
+
13
+ example "kojo run config.yml"
14
+ example "kojo run config.yml --save output"
15
+ example "kojo run config.yml -s output scale=3"
16
+
17
+ action do |args|
18
+ gen = Kojo::Generator.new args['CONFIG']
19
+ outdir = args['--save']
20
+
21
+ if outdir
22
+ FileUtils.mkdir_p outdir unless Dir.exist? outdir
23
+
24
+ gen.generate do |file, output|
25
+ path = "#{outdir}/#{file}"
26
+ File.write path, output
27
+ say "Saved #{path}"
28
+ end
29
+
30
+ else
31
+ puts gen.generate
32
+
33
+ end
34
+ end
35
+
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def args_to_hash
3
+ collect { |a| k, v = a.split '=' }.to_h.transform_keys(&:to_sym)
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def symbolize_keys
3
+ transform_keys &:to_sym
4
+ end
5
+ end
@@ -0,0 +1,40 @@
1
+ require 'yaml'
2
+
3
+ module Kojo
4
+ class Generator
5
+ attr_reader :config_file, :outdir
6
+
7
+ def initialize(config_file)
8
+ @config_file = config_file
9
+ end
10
+
11
+ def generate(opts={})
12
+ base_dir = File.dirname config_file
13
+ infile = "#{base_dir}/#{config['input']}"
14
+
15
+ result = []
16
+
17
+ config['output'].each do |outfile, opts|
18
+ output = render infile, opts.symbolize_keys
19
+ if block_given?
20
+ yield outfile, output
21
+ else
22
+ result << "\n# #{outfile}\n#{output}"
23
+ end
24
+ end
25
+
26
+ result.empty? ? nil : result.join("\n")
27
+ end
28
+
29
+ private
30
+
31
+ def config
32
+ @config ||= YAML.load_file config_file
33
+ end
34
+
35
+ def render(infile, opts={})
36
+ Template.new(infile, opts).render
37
+ end
38
+ end
39
+
40
+ end
@@ -0,0 +1,48 @@
1
+ module Kojo
2
+ class Template
3
+ attr_reader :file, :extension, :args, :dir
4
+
5
+ def initialize(file, args={})
6
+ @file = file
7
+ @args = args
8
+ @extension = file[/(\..*)$/]
9
+ @dir = File.dirname file
10
+ end
11
+
12
+ def render
13
+ evaluate file, args
14
+ end
15
+
16
+ private
17
+
18
+ def import(file, args={})
19
+ self.class.new("#{dir}/#{file}#{extension}", args).render
20
+ end
21
+
22
+ def evaluate(file, args={})
23
+ content = File.read(file) % args
24
+ result = []
25
+
26
+ content.lines.each do |line|
27
+ line.chomp!
28
+ spaces = line[/^\s*/].size
29
+
30
+ if line =~ /@import ([^(\s]*)\s*(?:\((.*)\))?\s*/
31
+ file = $1
32
+ args = $2 || ''
33
+ args = instance_eval("{#{args}}")
34
+ imported = import file, args
35
+ line = indent imported, spaces
36
+ end
37
+
38
+ result.push line
39
+ end
40
+ result.join "\n"
41
+ end
42
+
43
+ def indent(text, spaces)
44
+ text.lines.collect { |line| "#{' ' * spaces}#{line}" }.join
45
+ end
46
+ end
47
+
48
+ end
@@ -0,0 +1,3 @@
1
+ module Kojo
2
+ VERSION = "0.0.1"
3
+ end
data/lib/kojo.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'kojo/extensions/array'
2
+ require 'kojo/extensions/hash'
3
+
4
+ require 'kojo/template'
5
+ require 'kojo/generator'
6
+
7
+ require 'byebug' if ENV['BYEBUG']
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kojo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Danny Ben Shitrit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mister_bin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec_fixtures
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: runfile
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: runfile-tasks
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.4'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.15'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.15'
111
+ description: Configuration wizard
112
+ email: db@dannyben.com
113
+ executables:
114
+ - kojo
115
+ - kojo-generate.rb
116
+ - kojo-run.rb
117
+ extensions: []
118
+ extra_rdoc_files: []
119
+ files:
120
+ - README.md
121
+ - bin/kojo
122
+ - bin/kojo-generate.rb
123
+ - bin/kojo-run.rb
124
+ - lib/kojo.rb
125
+ - lib/kojo/extensions/array.rb
126
+ - lib/kojo/extensions/hash.rb
127
+ - lib/kojo/generator.rb
128
+ - lib/kojo/template.rb
129
+ - lib/kojo/version.rb
130
+ homepage: https://github.com/dannyben/kojo
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: 2.4.0
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.7.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Configuration wizard
154
+ test_files: []