thot 1.0.0 → 1.0.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 +142 -1
- data/VERSION +1 -1
- data/exe/thot +33 -0
- data/lib/thot/cli.rb +62 -0
- data/lib/thot.rb +15 -7
- data/samples/.env.prod +3 -0
- data/samples/template.txt +1 -0
- metadata +12 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66b39148b6f0d21ab8174c953757815943eda80b28000caba1dbdd7821d12ef2
|
4
|
+
data.tar.gz: 5a904474b751660d49e77f1c835490b3c945dda97dc49f9d16c427db9664ecdf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 42cb44934894d02699bcd17c3ba22c7d32b2b1effe5d4e19acdc1e6c9662c4fb623103742de2f54d375cbfbcac47086d17ffb4ca2b2771ea34cc13159be020f4
|
7
|
+
data.tar.gz: 77c8256484294a9e089a2b7320bdf02382600ffd62e26b4e8967843d91710ad3a23dca2d4cce7b127e49b2126ce0150544604455aaaea727cc975a3d85dc52dd
|
data/README.md
CHANGED
@@ -18,9 +18,150 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
$ gem install thot
|
20
20
|
|
21
|
+
|
22
|
+
## Principe
|
23
|
+
|
24
|
+
Thot is a simple templating tool, with :
|
25
|
+
- a template including token, like : %%TOKEN_NAME%% => Token MUST be in uppercase
|
26
|
+
- a hash of data (symbols as keys) corresponding, like : <pre>{token_name: 'value'}</pre>
|
27
|
+
It could generate an output.
|
28
|
+
|
29
|
+
### Usecase
|
30
|
+
|
31
|
+
- with data : <pre>{name: 'Romain'}</pre>
|
32
|
+
- and template content : "Hello %%NAME%% !"
|
33
|
+
|
34
|
+
Thot simply generate :
|
35
|
+
'Hello Romain !'
|
36
|
+
|
37
|
+
### Advanced usecase
|
38
|
+
|
39
|
+
- with data : <pre>{firstname: 'romain', name: 'georges', nickname: 'zaidyur'}</pre>
|
40
|
+
- and template content : "Hello %%FIRSTNAME.capitalize%% %%NAME.upcase%% your nickname is : %%NICKNAME.reverse.capitalize%% !"
|
41
|
+
|
42
|
+
Thot generate :
|
43
|
+
"Hello Romain GEORGES your nickname is : Ruydiaz !"
|
44
|
+
|
45
|
+
|
46
|
+
Thot actually supports String to String piped filters :
|
47
|
+
- filters must be stacked seperated by '.'
|
48
|
+
- filters must be in lowercase
|
49
|
+
- filters must be String instance methods returning a String (Modifier)
|
50
|
+
|
51
|
+
Note : Your could monkey patch String or use Refinment for implementing our own filters.
|
52
|
+
|
53
|
+
|
21
54
|
## Usage
|
22
55
|
|
23
|
-
|
56
|
+
Thot is already a library for you usage and a CLI.
|
57
|
+
|
58
|
+
### Ruby Library usage
|
59
|
+
|
60
|
+
you could use Thot in your Ruby code :
|
61
|
+
|
62
|
+
#### Strict mode and accessor input
|
63
|
+
|
64
|
+
Note : Considering 'template.txt' with : 'Hello %%NAME !!'
|
65
|
+
Note : in strict mode if the Tokens in template file don't match exactly the given token list, Thot raise an exception.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
require 'thot'
|
69
|
+
include Thot
|
70
|
+
template = Template::new list_token: [:name] , template_file: './template.txt'
|
71
|
+
template.name = 'Romain'
|
72
|
+
puts template.output
|
73
|
+
````
|
74
|
+
|
75
|
+
return
|
76
|
+
|
77
|
+
Hello Romain !!
|
78
|
+
|
79
|
+
|
80
|
+
#### Strict mode false with accesor input and template_content
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
require 'thot'
|
84
|
+
include Thot
|
85
|
+
template = Template::new list_token: [:name, :surname] , template_content: 'Hello %%NAME !!'
|
86
|
+
template.name = 'Romain'
|
87
|
+
puts template.output
|
88
|
+
````
|
89
|
+
|
90
|
+
return
|
91
|
+
|
92
|
+
Hello Romain !!
|
93
|
+
|
94
|
+
#### Strict mode false with map input and template_content
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
require 'thot'
|
98
|
+
include Thot
|
99
|
+
template = Template::new list_token: [:name, :surname] , template_content: 'Hello %%NAME !!'
|
100
|
+
template.map {name: 'Romain', surname: 'Georges' }
|
101
|
+
puts template.output
|
102
|
+
````
|
103
|
+
|
104
|
+
return
|
105
|
+
|
106
|
+
Hello Romain !!
|
107
|
+
|
108
|
+
|
109
|
+
|
110
|
+
### CLI usage
|
111
|
+
|
112
|
+
Thot come with a CLI for templating :
|
113
|
+
- reading from STDIN or list files arguments
|
114
|
+
- getting values from variables file by argument [MANDATORY] --env-var-file FILENAME
|
115
|
+
- display output on STDOUT
|
116
|
+
- verbose mode on STDERR if -v options.
|
117
|
+
|
118
|
+
Note : CLI work only strict mode false, you could have unused keys in datas.
|
119
|
+
|
120
|
+
#### Pre-requisites
|
121
|
+
|
122
|
+
* a file 'template.txt' with : "Hello %%NAME%% !!"
|
123
|
+
* a variables file with lines, like :
|
124
|
+
```
|
125
|
+
key=value
|
126
|
+
key = value
|
127
|
+
key = value
|
128
|
+
# comments and other lines are ignored
|
129
|
+
```
|
130
|
+
|
131
|
+
sample, env.test:
|
132
|
+
|
133
|
+
```
|
134
|
+
name=Romain
|
135
|
+
```
|
136
|
+
|
137
|
+
In the same path
|
138
|
+
|
139
|
+
#### STDIN from echo
|
140
|
+
|
141
|
+
```
|
142
|
+
$ echo "Hello %%NAME%% !!" |thot -e env.test
|
143
|
+
```
|
144
|
+
|
145
|
+
#### STDIN from input
|
146
|
+
|
147
|
+
```
|
148
|
+
$ thot -e env.test < template.txt
|
149
|
+
```
|
150
|
+
|
151
|
+
#### Files list
|
152
|
+
|
153
|
+
```
|
154
|
+
$ thot -e env.test template1.txt template2.txt
|
155
|
+
```
|
156
|
+
|
157
|
+
#### Typical usage
|
158
|
+
|
159
|
+
```
|
160
|
+
$ thot -e env.test < template.txt > output.txt
|
161
|
+
```
|
162
|
+
|
163
|
+
###
|
164
|
+
|
24
165
|
|
25
166
|
## Development
|
26
167
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.1
|
data/exe/thot
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
|
4
|
+
require 'optparse'
|
5
|
+
require 'thot'
|
6
|
+
require 'thot/cli'
|
7
|
+
|
8
|
+
options = {}
|
9
|
+
OptionParser.new do |opts|
|
10
|
+
opts.banner = "Usage: example.rb [options]"
|
11
|
+
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
|
12
|
+
options[:verbose] = v
|
13
|
+
end
|
14
|
+
opts.on("-eFILENAME", "--env-var-file=FILENAME", "[MANDATORY] Environment variables file (key=value pairs by line)") do |file|
|
15
|
+
options[:env_var_file] = file
|
16
|
+
end
|
17
|
+
end.parse!
|
18
|
+
|
19
|
+
list_input = ARGV.dup
|
20
|
+
ARGV.clear
|
21
|
+
|
22
|
+
|
23
|
+
include Thot
|
24
|
+
|
25
|
+
cli = CLI::new options: options, list_templates_file: list_input
|
26
|
+
cli.generate
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
data/lib/thot/cli.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
module Thot
|
2
|
+
class CLI
|
3
|
+
|
4
|
+
def initialize(options: , list_templates_file: nil )
|
5
|
+
@list_templates_file = list_templates_file
|
6
|
+
@options = options
|
7
|
+
getting_data
|
8
|
+
getting_content
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
def generate
|
13
|
+
template = Template::new(list_token: @data.keys, template_content: @content, strict: false)
|
14
|
+
template.map @data
|
15
|
+
STDERR.puts "Generating output" if @options[:verbose]
|
16
|
+
puts template.output
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def getting_data
|
22
|
+
if @options[:env_var_file] then
|
23
|
+
STDERR.puts "Environment file given : #{@options[:env_var_file]}" if @options[:verbose]
|
24
|
+
@data = read_evt_file(@options[:env_var_file])
|
25
|
+
else
|
26
|
+
raise "Environment variables file argument missing, (--env-var-file) "
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def getting_content
|
31
|
+
if @list_templates_file.empty?
|
32
|
+
STDERR.puts "Reading content from STDIN" if @options[:verbose]
|
33
|
+
@content = ARGF.readlines.join
|
34
|
+
else
|
35
|
+
STDERR.puts "Reading content from file(s) : #{@list_templates_file}" if @options[:verbose]
|
36
|
+
@list_templates_file.each do |item|
|
37
|
+
if File::exist? item
|
38
|
+
@content.concat(File::readlines(item)).join
|
39
|
+
else
|
40
|
+
raise "file not found #{item}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def read_evt_file(file)
|
47
|
+
res = {}
|
48
|
+
if File::exist? file
|
49
|
+
content = File::readlines(file)
|
50
|
+
else
|
51
|
+
raise "Environment variables file not found #{file}"
|
52
|
+
end
|
53
|
+
content.each do |line|
|
54
|
+
next if line =~ /#/
|
55
|
+
key,value = line.split('=')
|
56
|
+
res[key.strip.to_sym] = value.strip if value
|
57
|
+
end
|
58
|
+
return res
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
data/lib/thot.rb
CHANGED
@@ -17,16 +17,24 @@ module Thot
|
|
17
17
|
attr_reader :content
|
18
18
|
|
19
19
|
# constructor : generate the pseudo accessor for template Class from token list
|
20
|
-
def initialize(template_file: , list_token: , strict: true)
|
20
|
+
def initialize(template_file: nil, list_token: , strict: true, template_content: nil)
|
21
21
|
|
22
22
|
@result = ""
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
if template_file
|
24
|
+
@template_file = template_file
|
25
|
+
raise NoTemplateFile::new('No template file found') unless File::exist?(@template_file)
|
26
|
+
begin
|
27
|
+
@content = IO::readlines(@template_file).join.chomp
|
28
|
+
rescue
|
29
|
+
raise NoTemplateFile::new('Template file read error')
|
30
|
+
end
|
31
|
+
elsif template_content
|
32
|
+
@content = template_content
|
33
|
+
else
|
34
|
+
raise NoTemplateFile::new('No template file found or template content')
|
29
35
|
end
|
36
|
+
|
37
|
+
|
30
38
|
token_from_template = @content.scan(/%%(\w+)%%/).flatten.uniq.map{ |item| item.downcase.to_sym}
|
31
39
|
begin
|
32
40
|
@list_token = list_token
|
data/samples/.env.prod
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Hello %%NAME.capitalize%% %%SURNAME.upcase%% !!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Romain GEORGES
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -125,7 +125,8 @@ dependencies:
|
|
125
125
|
description: the simpliest way to template in Ruby and command
|
126
126
|
email:
|
127
127
|
- gems@ultragreen.net
|
128
|
-
executables:
|
128
|
+
executables:
|
129
|
+
- thot
|
129
130
|
extensions: []
|
130
131
|
extra_rdoc_files: []
|
131
132
|
files:
|
@@ -139,8 +140,12 @@ files:
|
|
139
140
|
- VERSION
|
140
141
|
- bin/console
|
141
142
|
- bin/setup
|
143
|
+
- exe/thot
|
142
144
|
- lib/thot.rb
|
145
|
+
- lib/thot/cli.rb
|
143
146
|
- lib/thot/version.rb
|
147
|
+
- samples/.env.prod
|
148
|
+
- samples/template.txt
|
144
149
|
- thot.gemspec
|
145
150
|
- ultragreen_roodi_coding_convention.yml
|
146
151
|
homepage: https://github.com/Ultragreen/thot
|
@@ -148,7 +153,7 @@ licenses:
|
|
148
153
|
- MIT
|
149
154
|
metadata:
|
150
155
|
homepage_uri: https://github.com/Ultragreen/thot
|
151
|
-
post_install_message:
|
156
|
+
post_install_message:
|
152
157
|
rdoc_options: []
|
153
158
|
require_paths:
|
154
159
|
- lib
|
@@ -163,8 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
163
168
|
- !ruby/object:Gem::Version
|
164
169
|
version: '0'
|
165
170
|
requirements: []
|
166
|
-
rubygems_version: 3.2
|
167
|
-
signing_key:
|
171
|
+
rubygems_version: 3.1.2
|
172
|
+
signing_key:
|
168
173
|
specification_version: 4
|
169
174
|
summary: THe Operative Templating
|
170
175
|
test_files: []
|