changit 0.1.1 → 0.2.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
  SHA1:
3
- metadata.gz: 2b4a8ebb640fc22a9ebe8670d46e0573634e0c8b
4
- data.tar.gz: 031dd2c48bbfbef3cd134dc0a965ceabda6b017d
3
+ metadata.gz: d9bf79ac4c29a50011f960a3087ae267347014be
4
+ data.tar.gz: 43c7ec5b8cfad72701db0b6f04b12c3b2b1e1e51
5
5
  SHA512:
6
- metadata.gz: e56e10a4970f8063ca601941300660f72508ae23d91b7196b06e2d7a5696bb5dc555abfa3b73a80285ebb1cd8689e577c295b7871df81447b867f71fa694a0c7
7
- data.tar.gz: 9068aed8fca24cdf5c173a3b66ac226a0596e7cdd1bcf6a482450dede815b71a0b8cd9c86acbaad7fa569b5e1e0e794e36bb6b87794dff8b9112b36932c41640
6
+ metadata.gz: 342448f498b87b8a5834782e0ceaa217e5d8ec58d023539094f858093b5f143477da2abfe2d67590ad194fcd627ac23d4e93400b7fb6deab097cf3b80e5b4b51
7
+ data.tar.gz: a6a1a5af52b4df8c79419b0bf03d02ba80648155f5c6e3cec0d6ac835fb4a24c6323533d916007fd2b3e5b4514b18c65ff5c21fea0d195b175a8e408c3d922fa
data/demo.gif CHANGED
Binary file
@@ -8,56 +8,35 @@ module Changit
8
8
  end
9
9
 
10
10
  def write
11
- hsrc = parse(@config)
11
+ config_lexer = Lexer.new(@config)
12
+ config_token_hash = config_lexer.token_hash
12
13
 
13
14
  @config_files.each do |path|
14
- hdst = parse(File.read(path))
15
- hmerged = comp_and_merge(hsrc, hdst)
16
-
17
- out = ""
18
- hmerged.each do |k, v|
19
- out += "#{k}\n"
20
- v.each do |v_k, v_v|
21
- out += "\t#{v_k} = #{v_v}\n"
15
+ file_lexer = Lexer.new(File.read(path))
16
+ file_token_hash = file_lexer.token_hash
17
+
18
+ unless file_token_hash.empty?
19
+ file_token_hash.each do |k, v|
20
+ if config_token_hash[k]
21
+ # calling `sort!` changes the order of the key_value pairs
22
+ config_token_hash[k].sort!
23
+ file_token_hash[k].sort!
24
+
25
+ config_token_hash[k].each_with_index do |e, i|
26
+ if v.include?(e)
27
+ # update it
28
+ file_token_hash[k][i].rhs = e.rhs
29
+ else
30
+ # push it
31
+ file_token_hash[k].push(e)
32
+ end
33
+ end
34
+ end
22
35
  end
23
36
  end
24
37
 
25
- File.open(path, 'w') { |f| f.write(out); f.close }
26
- end
27
- end
28
-
29
- def parse(config)
30
- h = Hash.new
31
-
32
- config.each_line do |line|
33
- line.strip!
34
- line.gsub!(/\s+/, '')
35
-
36
- if line =~ /^\[\w+\]$/
37
- h[line] = {}
38
- else
39
- key, value = line.split('=')
40
- h[h.to_a.last[0]][key] = value
41
- end
42
- end
43
-
44
- h
45
- end
46
-
47
- def comp_and_merge(hsrc, hdst)
48
- hdifference = hsrc.dup.delete_if { |k, _| hdst.key?(k) }
49
-
50
- hdst.each do |k, v|
51
- if hsrc[k]
52
- hdst[k] = hdst[k].merge(hsrc[k])
53
- end
54
- end
55
-
56
- unless hdifference.empty?
57
- hdst.merge!(hdifference)
38
+ File.open(path, 'w') { |f| f.write(config_lexer.to_s); f.close }
58
39
  end
59
-
60
- hdst
61
40
  end
62
41
  end
63
42
  end
@@ -0,0 +1,22 @@
1
+ module Changit
2
+ class Lexer
3
+ class KeyValueToken
4
+ include Comparable
5
+
6
+ attr_reader :lhs # left-hand side
7
+ attr_accessor :rhs # right-hand side
8
+
9
+ def initialize(key_value)
10
+ @lhs, @rhs = key_value.split('=').map(&:strip)
11
+ end
12
+
13
+ def to_s
14
+ "\t#{@lhs} = #{@rhs}\n"
15
+ end
16
+
17
+ def <=>(another)
18
+ self.lhs <=> another.lhs
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,15 @@
1
+ module Changit
2
+ class Lexer
3
+ class SectionToken
4
+ attr_reader :value
5
+
6
+ def initialize(section)
7
+ @section = section
8
+ end
9
+
10
+ def to_s
11
+ "#{@section}\n"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ module Changit
2
+ class Lexer
3
+ RULES = [
4
+ [/\[(.*?)\]/, SectionToken].freeze,
5
+ [/^[^(=|\[|\s)]*\s*\=\s*[^(=|\[|\])]*/, KeyValueToken].freeze
6
+ ].freeze
7
+
8
+ attr_reader :original_expression, :tokens, :token_hash
9
+
10
+ def initialize(expression)
11
+ @original_expression = expression
12
+ @tokens = tokenize
13
+ @token_hash = to_hash
14
+ end
15
+
16
+ def to_s
17
+ @tokens.map(&:to_s).join
18
+ end
19
+
20
+ private
21
+
22
+ def tokenize
23
+ expression = @original_expression.gsub(/\t/, '')
24
+
25
+ tokens = []
26
+ expression.each_line do |line|
27
+ RULES.each do |rule, token_type|
28
+ lexmeme = line.slice(rule)
29
+
30
+ if (lexmeme)
31
+ tokens << token_type.new(lexmeme)
32
+ break
33
+ end
34
+ end
35
+ end
36
+
37
+ tokens
38
+ end
39
+
40
+ def to_hash
41
+ token_hash = {}
42
+
43
+ @tokens.map do |token|
44
+ if token.class == SectionToken
45
+ token_hash[token] = []
46
+ else
47
+ token_hash[token_hash.to_a.last[0]].push(token)
48
+ end
49
+ end
50
+
51
+ token_hash
52
+ end
53
+ end
54
+ end
@@ -1,3 +1,3 @@
1
1
  module Changit
2
- VERSION='0.1.1'
2
+ VERSION='0.2.0'
3
3
  end
data/lib/changit.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'changit/version'
2
2
  require 'changit/git_dir_finder'
3
3
  require 'changit/config_reader'
4
+ require 'changit/lexer/section_token'
5
+ require 'changit/lexer/key_value_token'
6
+ require 'changit/lexer'
4
7
  require 'changit/config_writer'
5
8
 
6
9
  module Changit
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: changit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aaooki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-29 00:00:00.000000000 Z
11
+ date: 2016-12-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An over-engineered tool to change git config for multiple projects at
14
14
  once.
@@ -30,6 +30,9 @@ files:
30
30
  - lib/changit/config_reader.rb
31
31
  - lib/changit/config_writer.rb
32
32
  - lib/changit/git_dir_finder.rb
33
+ - lib/changit/lexer.rb
34
+ - lib/changit/lexer/key_value_token.rb
35
+ - lib/changit/lexer/section_token.rb
33
36
  - lib/changit/version.rb
34
37
  homepage: https://github.com/aaooki/changit
35
38
  licenses: []