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 +4 -4
- data/demo.gif +0 -0
- data/lib/changit/config_writer.rb +23 -44
- data/lib/changit/lexer/key_value_token.rb +22 -0
- data/lib/changit/lexer/section_token.rb +15 -0
- data/lib/changit/lexer.rb +54 -0
- data/lib/changit/version.rb +1 -1
- data/lib/changit.rb +3 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9bf79ac4c29a50011f960a3087ae267347014be
|
4
|
+
data.tar.gz: 43c7ec5b8cfad72701db0b6f04b12c3b2b1e1e51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
11
|
+
config_lexer = Lexer.new(@config)
|
12
|
+
config_token_hash = config_lexer.token_hash
|
12
13
|
|
13
14
|
@config_files.each do |path|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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(
|
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,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
|
data/lib/changit/version.rb
CHANGED
data/lib/changit.rb
CHANGED
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.
|
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
|
+
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: []
|