ruby-normalizer 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 780587cd27473a23ca113b8f58f4ef78d3602f21
4
+ data.tar.gz: 99a12832d038f3f40b40bdad2b5d0e10382171be
5
+ SHA512:
6
+ metadata.gz: d1545585fe9e11b436d74030e3bc84aeb9deee3db4657b26e68391a7b6216f24605ae226b6e17ac994f4a394ab4485bfc65df82fa2c3a1001129f3c4d19bc39b
7
+ data.tar.gz: c3167e2a96cdb6a9005165e07f8a8a74e26b838f56d16d9158922edaae815f007b20fa1b42255c1553fac1f6c607c87808821905e78355edf68338bff193ebe2
@@ -0,0 +1,6 @@
1
+ = Normalizer
2
+
3
+ Describe your project here
4
+
5
+ :include:ruby-normalizer.rdoc
6
+
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env ruby
2
+ require 'optparse'
3
+ require 'normalizer'
4
+
5
+ options = {file: nil, env: nil, output: nil}
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: #{__FILE__} [options]"
8
+
9
+ opts.on("-f=FILE", "--file=FILE", "The file to read in and parse") do |v|
10
+ if File.exists? v
11
+ options[:file] = v
12
+ else
13
+ raise IOError.new(format('No such file %s', v))
14
+ end
15
+ end
16
+
17
+ opts.on("-e=FILE", "--env=FILE", "Load additonal variables from a file (key value pair") do |v|
18
+ if File.exists? v
19
+ options[:env] = v
20
+ else
21
+ raise IOError.new(format('No such file %s', v))
22
+ end
23
+ end
24
+
25
+ opts.on('-o=FILE', '--output=FILE', 'The file to save to') do |v|
26
+ options[:output] = v
27
+ end
28
+
29
+ opts.on_tail('-h', '--help', 'Display help message') do |v|
30
+ puts opts
31
+ exit
32
+ end
33
+ end.parse!
34
+
35
+ raise OptionParser::MissingArgument.new('You must pass in the -file argument') if options[:file].nil?
36
+
37
+ nom = Normalizer.new options[:file], properties: options[:env], output: options[:output]
38
+ nom.parse
@@ -0,0 +1,62 @@
1
+ class Normalizer
2
+ def initialize(file, options = {})
3
+ @source = file
4
+ @options = options
5
+ @env = environment
6
+
7
+ parse
8
+ end
9
+
10
+ def parse
11
+ content = File.read(@source)
12
+
13
+ # Scan the content of the file, and search for *unique* variable names
14
+ # that we need to do a search and replace for.
15
+ #
16
+ variables = content.scan(/\{\w+\}/).uniq
17
+
18
+ variables.each do |var|
19
+ # Since the place holders are in the file like such {MY_VAR}, we can
20
+ # assume that the first character is the open bracket, and the last
21
+ # is the closing bracket so lets just trim those off.
22
+ #
23
+ env_var = var[1..-2]
24
+
25
+ if @env.key? env_var
26
+ content.gsub! var, @env[env_var].chomp
27
+
28
+ else
29
+ # NOTE: mdelaney
30
+ # I'm not too sure if we should fail, i.e. return a non zero, or just
31
+ # print out a message to the console. For the moment, we'll just print
32
+ # out a message to the console. Should we want to fail the build that's
33
+ # a trivial change to make.
34
+ #
35
+ puts format('WARNING: No such environment variable %s', env_var)
36
+ end
37
+ end
38
+
39
+ output_file = @options[:output] ||= @source
40
+ File.write(output_file, content)
41
+ end
42
+
43
+ private
44
+
45
+ def environment
46
+ env = ENV.to_h
47
+ env.merge! load_properties(@options[:properties])
48
+ end
49
+
50
+ def load_properties(file)
51
+ props = {}
52
+ return props if file.nil?
53
+
54
+ properties = File.read(file)
55
+ properties.each_line do |line|
56
+ parts = line.split '='
57
+ props[parts[0]] = parts[1]
58
+ end
59
+
60
+ props
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module Version
2
+ VERSION = '0.1.3'
3
+ end
@@ -0,0 +1 @@
1
+ = normalizer
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-normalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ platform: ruby
6
+ authors:
7
+ - Mike Delaney
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: github@mldelaney.com
15
+ executables:
16
+ - normalize
17
+ extensions: []
18
+ extra_rdoc_files:
19
+ - README.rdoc
20
+ - ruby-normalizer.rdoc
21
+ files:
22
+ - lib/normalizer/version.rb
23
+ - lib/normalizer.rb
24
+ - README.rdoc
25
+ - ruby-normalizer.rdoc
26
+ - bin/normalize
27
+ homepage: http://github.com/madelaney
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options:
32
+ - --title
33
+ - ruby-normalizer
34
+ - --main
35
+ - README.rdoc
36
+ - -ri
37
+ require_paths:
38
+ - lib
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 2.0.14.1
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: A simple ruby class that searchers for place holders and inserts environment
56
+ variable values in their place.
57
+ test_files: []