cocos 0.2.1 → 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: 40d870d3371a969672ba3e6b61dfb5cfbfae18809fa2edb96c8a3dbdfa44f1e8
4
- data.tar.gz: f811adb7be939ff1dd90108fe16bf036f45e40540cbbff81fb7d20d6591c1426
3
+ metadata.gz: 38e913cc7ec8d6951edd7bb59ac80626aadf3a3fbd7b13f82ccc123dffc6a7b1
4
+ data.tar.gz: 8883a9656f7aa48b6c778e9786966f507a80dbd13be670ca69ff9ed5b20656a8
5
5
  SHA512:
6
- metadata.gz: b504140b89069be5896ab2028263e191475d5d92423fc6df4be069523c2b037b0c28830ec0fa89426e0212007044344ed4c849345e016d12c2c0832575b85920
7
- data.tar.gz: 1b5a4741a03b48740fb4868bf54c054382dbee3a3f7b7cca243d2393c88cc0c6cb5b5d75e593d5c3bdec075d3df32a714eb4f7ec861572dfd9c3e4a2278f9548
6
+ metadata.gz: 1b9e85de98ce029a384d0ce1fa2e09905d3d7eeee956d59ab090c0eb9300b1de121ecc1bfeaaacdebdca7dd66227f7a9fefce1ef397d1d97fa5123cac32609c8
7
+ data.tar.gz: ba6fc944c1dc724c131c0f32390349140c3d658a4db80d876a421704eea1d13ac78ecedc3da6141ba7116367270e836d8aafcb420b12bd664ae604ac557af8a4
data/CHANGELOG.md CHANGED
@@ -1,3 +1,4 @@
1
+ ### 0.3.0
1
2
  ### 0.0.1 / 2022-08-01
2
3
 
3
4
  * Everything is new. First release.
data/Manifest.txt CHANGED
@@ -4,4 +4,5 @@ Manifest.txt
4
4
  README.md
5
5
  Rakefile
6
6
  lib/cocos.rb
7
+ lib/cocos/env.rb
7
8
  lib/cocos/version.rb
data/lib/cocos/env.rb ADDED
@@ -0,0 +1,91 @@
1
+ ###
2
+ # simple read_env, load_env machinery
3
+ ## inspired by
4
+ ## dotenv gem -> https://github.com/bkeepers/dotenv
5
+ ## figaro -> https://github.com/laserlemon/figaro
6
+ ## and others
7
+
8
+
9
+ ## todo/check:
10
+ ## move to its own (standandaloen) envparser gem - why? why not?
11
+
12
+ module EnvParser
13
+ # returns a hash
14
+ # (compatible structure - works like YAML.load_file)
15
+ #
16
+ # change to .read(path) and parse( text) - why? why not?
17
+ def self.load_file( path )
18
+ text = File.open( path, 'r:utf-8' ) { |f| f.read }
19
+ parse( text )
20
+ end
21
+ def self.load( text ) parse( text ); end
22
+
23
+
24
+ class Error < StandardError; end
25
+
26
+ ## todo/check - what is JSON and YAML returning Parser/ParseError something else?
27
+ ## YAML uses ParseError and JSON uses ParserError
28
+ class ParseError < Error; end
29
+
30
+
31
+ ## todo/check - if support for empty values e.g. abc= is required/possible???
32
+ ## todo/ addd support for quoted values - why? why not?
33
+ ## add support for "inline" end of line comments - why? why not?
34
+ ## add support for escapes and multi-line values - why? why not?
35
+ LINE_RX = /\A(?<key>[A-Za-z][A-Za-z0-9_-]*)
36
+ [ ]*
37
+ =
38
+ [ ]*
39
+ (?<value>.+?) ## non-greedy
40
+ \z
41
+ /x
42
+
43
+ ## use a parser class - why? why not?
44
+ def self.parse( text )
45
+ h = {}
46
+
47
+ lineno = 0
48
+ text.each_line do |line|
49
+ lineno += 1 ## track line numbers for (parse) error reporting
50
+
51
+ line = line.strip ## check: use strip (or be more strict) - why? why not?
52
+ ## skip empty and comment lines
53
+ next if line.empty? || line.start_with?( '#' )
54
+
55
+ if m=LINE_RX.match(line)
56
+ key = m[:key]
57
+ value = m[:value]
58
+
59
+ ## todo/check - check/warn about duplicates - why? why not?
60
+ h[key] = value
61
+ else
62
+ raise ParseError, "line #{lineno} - unknown line type; cannot parse >#{line}<"
63
+ end
64
+ end
65
+ h
66
+ end # methdod self.parse
67
+ end # module EnvParser
68
+
69
+
70
+
71
+ __END__
72
+
73
+ # parser regex from dotenv
74
+
75
+ LINE = /
76
+ (?:^|\A) # beginning of line
77
+ \s* # leading whitespace
78
+ (?:export\s+)? # optional export
79
+ ([\w.]+) # key
80
+ (?:\s*=\s*?|:\s+?) # separator
81
+ ( # optional value begin
82
+ \s*'(?:\\'|[^'])*' # single quoted value
83
+ | # or
84
+ \s*"(?:\\"|[^"])*" # double quoted value
85
+ | # or
86
+ [^\#\r\n]+ # unquoted value
87
+ )? # value end
88
+ \s* # trailing whitespace
89
+ (?:\#.*)? # optional comment
90
+ (?:$|\z) # end of line
91
+ /x
data/lib/cocos/version.rb CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
2
  module Cocos
3
3
  MAJOR = 0 ## todo: namespace inside version or something - why? why not??
4
- MINOR = 2
5
- PATCH = 1
4
+ MINOR = 3
5
+ PATCH = 0
6
6
  VERSION = [MAJOR,MINOR,PATCH].join('.')
7
7
 
8
8
  def self.version
data/lib/cocos.rb CHANGED
@@ -30,7 +30,7 @@ require 'webclient'
30
30
  #####################
31
31
  # our own code
32
32
  require 'cocos/version' # note: let version always go first
33
-
33
+ require 'cocos/env' ## e.g. EnvParser
34
34
 
35
35
  ###
36
36
  ## read/parse convenience/helper shortcuts
@@ -151,6 +151,8 @@ def read_text( path )
151
151
  ## todo/check: add universal newline mode or such?
152
152
  ## e.g. will always convert all
153
153
  ## newline variants (\n|\r|\n\r) to "universal" \n only
154
+ ##
155
+ ## add r:bom - why? why not?
154
156
  txt = File.open( path, 'r:utf-8' ) do |f|
155
157
  f.read
156
158
  end
@@ -191,6 +193,33 @@ end
191
193
 
192
194
 
193
195
 
196
+ def read_env( path )
197
+ EnvParser.load( read_text( path ))
198
+ end
199
+
200
+ def parse_env( str )
201
+ EnvParser.load( str )
202
+ end
203
+
204
+
205
+ ##
206
+ ## todo/check - change path to *paths=['./.env']
207
+ ## and support more files - why? why not?
208
+ def load_env( path='./.env' )
209
+ if File.exist?( path )
210
+ puts "==> loading .env settings..."
211
+ env = read_env( path )
212
+ puts " applying .env settings... (merging into ENV)"
213
+ pp env
214
+ ## note: will only add .env setting if NOT present in ENV!!!
215
+ env.each do |k,v|
216
+ ENV[k] ||= v
217
+ end
218
+ end
219
+ end
220
+
221
+
222
+
194
223
 
195
224
  ######
196
225
  # add writers
@@ -204,7 +233,7 @@ def write_json( path, data )
204
233
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
205
234
 
206
235
  ## note: pretty print/reformat json
207
- File.open( path, "w:utf-8" ) do |f|
236
+ File.open( path, 'w:utf-8' ) do |f|
208
237
  f.write( JSON.pretty_generate( data ))
209
238
  end
210
239
  end
@@ -218,7 +247,7 @@ def write_blob( path, blob )
218
247
  dirname = File.dirname( path )
219
248
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
220
249
 
221
- File.open( path, "wb" ) do |f|
250
+ File.open( path, 'wb' ) do |f|
222
251
  f.write( blob )
223
252
  end
224
253
  end
@@ -234,7 +263,7 @@ def write_text( path, text )
234
263
  dirname = File.dirname( path )
235
264
  FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
236
265
 
237
- File.open( path, "w:utf-8" ) do |f|
266
+ File.open( path, 'w:utf-8' ) do |f|
238
267
  f.write( text )
239
268
  end
240
269
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cocos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gerald Bauer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-23 00:00:00.000000000 Z
11
+ date: 2023-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: csvreader
@@ -92,14 +92,14 @@ dependencies:
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '3.23'
95
+ version: '4.0'
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
- version: '3.23'
102
+ version: '4.0'
103
103
  description: cocos (code commons) - auto-include quick-starter prelude & prolog
104
104
  email: opensport@googlegroups.com
105
105
  executables: []
@@ -116,6 +116,7 @@ files:
116
116
  - README.md
117
117
  - Rakefile
118
118
  - lib/cocos.rb
119
+ - lib/cocos/env.rb
119
120
  - lib/cocos/version.rb
120
121
  homepage: https://github.com/rubycocos/cocos
121
122
  licenses:
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  - !ruby/object:Gem::Version
139
140
  version: '0'
140
141
  requirements: []
141
- rubygems_version: 3.3.7
142
+ rubygems_version: 3.4.10
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: cocos (code commons) - auto-include quick-starter prelude & prolog