gitenv 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source "http://rubygems.org"
4
4
  # gem "activesupport", ">= 2.3.5"
5
5
 
6
6
  gem 'paint', '~> 0.8.5'
7
+ gem 'commander', '~> 4.1.2'
7
8
 
8
9
  # Add dependencies to develop your gem here.
9
10
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -1,10 +1,13 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ commander (4.1.2)
5
+ highline (~> 1.6.11)
4
6
  diff-lcs (1.1.3)
5
7
  gem-release (0.4.1)
6
8
  gemcutter (0.7.1)
7
9
  git (1.2.5)
10
+ highline (1.6.15)
8
11
  jeweler (1.8.4)
9
12
  bundler (~> 1.0)
10
13
  git (>= 1.2.5)
@@ -36,6 +39,7 @@ PLATFORMS
36
39
 
37
40
  DEPENDENCIES
38
41
  bundler
42
+ commander (~> 4.1.2)
39
43
  gem-release
40
44
  gemcutter
41
45
  jeweler
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
data/bin/gitenv CHANGED
@@ -1,4 +1,2 @@
1
1
  #!/usr/bin/env ruby
2
- require File.join(File.dirname(__FILE__), '..', 'lib', 'gitenv')
3
-
4
- Gitenv::Runner.new.run
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'program')
data/gitenv.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "gitenv"
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["AlphaHydrae"]
12
- s.date = "2012-09-21"
12
+ s.date = "2012-09-23"
13
13
  s.description = "Creates symlinks to configuration files in a git repository."
14
14
  s.email = "hydrae.alpha@gmail.com"
15
15
  s.executables = ["gitenv"]
@@ -31,6 +31,16 @@ Gem::Specification.new do |s|
31
31
  "bin/gitenv",
32
32
  "gitenv.gemspec",
33
33
  "lib/gitenv.rb",
34
+ "lib/gitenv/actions.rb",
35
+ "lib/gitenv/all_files.rb",
36
+ "lib/gitenv/config.rb",
37
+ "lib/gitenv/context.rb",
38
+ "lib/gitenv/controller.rb",
39
+ "lib/gitenv/copy.rb",
40
+ "lib/gitenv/dot_files.rb",
41
+ "lib/gitenv/enumerator.rb",
42
+ "lib/gitenv/symlink.rb",
43
+ "lib/program.rb",
34
44
  "spec/helper.rb",
35
45
  "spec/version_spec.rb"
36
46
  ]
@@ -45,6 +55,7 @@ Gem::Specification.new do |s|
45
55
 
46
56
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
57
  s.add_runtime_dependency(%q<paint>, ["~> 0.8.5"])
58
+ s.add_runtime_dependency(%q<commander>, ["~> 4.1.2"])
48
59
  s.add_development_dependency(%q<bundler>, [">= 0"])
49
60
  s.add_development_dependency(%q<rake>, [">= 0"])
50
61
  s.add_development_dependency(%q<rspec>, [">= 0"])
@@ -55,6 +66,7 @@ Gem::Specification.new do |s|
55
66
  s.add_development_dependency(%q<simplecov>, [">= 0"])
56
67
  else
57
68
  s.add_dependency(%q<paint>, ["~> 0.8.5"])
69
+ s.add_dependency(%q<commander>, ["~> 4.1.2"])
58
70
  s.add_dependency(%q<bundler>, [">= 0"])
59
71
  s.add_dependency(%q<rake>, [">= 0"])
60
72
  s.add_dependency(%q<rspec>, [">= 0"])
@@ -66,6 +78,7 @@ Gem::Specification.new do |s|
66
78
  end
67
79
  else
68
80
  s.add_dependency(%q<paint>, ["~> 0.8.5"])
81
+ s.add_dependency(%q<commander>, ["~> 4.1.2"])
69
82
  s.add_dependency(%q<bundler>, [">= 0"])
70
83
  s.add_dependency(%q<rake>, [">= 0"])
71
84
  s.add_dependency(%q<rspec>, [">= 0"])
@@ -0,0 +1,28 @@
1
+
2
+ module Gitenv
3
+
4
+ class Actions
5
+
6
+ def initialize
7
+ @actions = []
8
+ end
9
+
10
+ def each &block
11
+ @actions.each &block
12
+ end
13
+
14
+ def from path
15
+ @actions.each{ |a| a.from path }
16
+ @actions
17
+ end
18
+
19
+ def to path
20
+ @actions.each{ |a| a.to path }
21
+ @actions
22
+ end
23
+
24
+ def << action
25
+ @actions << action
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module Gitenv
3
+
4
+ class AllFiles < FileEnumerator
5
+
6
+ def files
7
+ Dir.entries(@path).select{ |f| File.file? File.join(@path, f) }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,53 @@
1
+
2
+ module Gitenv
3
+
4
+ class Config
5
+ include Context
6
+
7
+ attr_reader :actions
8
+ attr_reader :repository
9
+ attr_reader :home
10
+
11
+ def initialize home
12
+ @home = home
13
+ @actions = Actions.new
14
+ end
15
+
16
+ def repo path
17
+ @repository = File.expand_path path
18
+ end
19
+
20
+ def symlink file
21
+
22
+ scope = self
23
+ current_actions = Actions.new
24
+ enumerator(file).each do |f|
25
+ current_actions << Symlink.new(scope, f).tap do |action|
26
+ @actions << action
27
+ end
28
+ end
29
+
30
+ current_actions
31
+ end
32
+
33
+ def all_files
34
+ :all_files
35
+ end
36
+
37
+ def dot_files
38
+ :dot_files
39
+ end
40
+
41
+ private
42
+
43
+ def enumerator file
44
+ if file == :all_files
45
+ AllFiles.new File.join(*[@repository, from_path].compact)
46
+ elsif file == :dot_files
47
+ DotFiles.new File.join(*[@repository, from_path].compact)
48
+ else
49
+ [ file ]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,46 @@
1
+
2
+ module Gitenv
3
+
4
+ module Context
5
+
6
+ def from path, &block
7
+ (@from_path ||= []) << path
8
+ if block
9
+ instance_eval &block
10
+ @from_path.pop
11
+ end
12
+ end
13
+
14
+ def from_path
15
+ @from_path ? File.join(*@from_path) : nil
16
+ end
17
+
18
+ def from_paths
19
+ @from_path || []
20
+ end
21
+
22
+ def from_paths= paths
23
+ @from_path = paths
24
+ end
25
+
26
+ def to path, &block
27
+ (@to_path ||= []) << path
28
+ if block
29
+ instance_eval &block
30
+ @to_path.pop
31
+ end
32
+ end
33
+
34
+ def to_path
35
+ @to_path ? File.join(*@to_path) : nil
36
+ end
37
+
38
+ def to_paths
39
+ @to_path || []
40
+ end
41
+
42
+ def to_paths= paths
43
+ @to_path = paths
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,46 @@
1
+
2
+ module Gitenv
3
+
4
+ class Controller
5
+
6
+ def initialize action, options
7
+
8
+ @action = action
9
+ @options = options
10
+
11
+ @home = File.expand_path '~'
12
+ @config = Config.new @home
13
+ end
14
+
15
+ def run
16
+ load_config_file!
17
+ @config.actions.each &:build!
18
+ @config.actions.each &:update! if @action == :update
19
+ @config.actions.each{ |a| puts a }
20
+ end
21
+
22
+ private
23
+
24
+ def load_config_file!
25
+
26
+ config_file = @options.config ? File.expand_path(@options.config) : File.join(@home, '.gitenv.rb')
27
+ if !File.exists?(config_file) and @options.config
28
+ abort "No such configuration file #{config_file}"
29
+ elsif !File.exists?(config_file)
30
+ abort "You must create a configuration file #{config_file} or load another one with the --config option"
31
+ elsif !File.file?(config_file)
32
+ abort "Configuration file #{config_file} is not a file"
33
+ elsif !File.readable?(config_file)
34
+ abort "Configuration file #{config_file} is not readable"
35
+ end
36
+
37
+ contents = File.open(config_file, 'r').read
38
+ @config.instance_eval contents, config_file
39
+ end
40
+
41
+ def abort msg, code = 1
42
+ warn ms
43
+ exit code
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+
2
+ module Gitenv
3
+
4
+ class Copy
5
+ include Context
6
+
7
+ def initialize config, file
8
+ @config, @file = config, file
9
+ self.from_paths = config.from_paths.dup
10
+ self.to_paths = config.to_paths.dup
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+
2
+ module Gitenv
3
+
4
+ class DotFiles < AllFiles
5
+
6
+ def files
7
+ super.select{ |f| f.match /^\.[^\.]/ }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+
2
+ module Gitenv
3
+
4
+ class FileEnumerator
5
+
6
+ def initialize path
7
+ @path = path
8
+ end
9
+
10
+ def files
11
+ raise '#files not implemented'
12
+ end
13
+
14
+ def each &block
15
+ files.each do |f|
16
+ block.call f
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ module Gitenv
4
+
5
+ class Symlink
6
+ include Context
7
+
8
+ def initialize config, file
9
+ @config, @file = config, file
10
+ self.from_paths = config.from_paths.dup
11
+ self.to_paths = config.to_paths.dup
12
+ end
13
+
14
+ def build!
15
+ @color, @mark, @message = if File.symlink? link
16
+ @current_target = File.expand_path File.readlink(link)
17
+ if @current_target == target
18
+ [ :green, "✓", "ok" ]
19
+ else
20
+ [ :yellow, "✗", "currently points to #{@current_target}" ]
21
+ end
22
+ elsif File.file? link
23
+ [ :red, "✗", "is a file" ]
24
+ elsif File.directory? link
25
+ [ :red, "✗", "is a directory" ]
26
+ elsif File.exists? link
27
+ [ :red, "✗", "exists but is not a symlink" ]
28
+ else
29
+ [ :blue, "✓", "is not set up" ]
30
+ end
31
+ end
32
+
33
+ def update!
34
+ if !File.exists? link
35
+ File.symlink target, link
36
+ @color, @mark, @message = :green, "✓", "ok"
37
+ end
38
+ end
39
+
40
+ def to_s
41
+ %/ #{status_mark} #{Paint[link, :cyan]} -> #{target} #{status_message}/
42
+ end
43
+
44
+ private
45
+
46
+ def status_mark
47
+ Paint["#{@mark}", @color]
48
+ end
49
+
50
+ def status_message
51
+ Paint["#{@message}", @color]
52
+ end
53
+
54
+ def link
55
+ File.join(*[ @config.home, to_path, @file].compact)
56
+ end
57
+
58
+ def target
59
+ File.join(*[ @config.repository, from_path, @file ].compact)
60
+ end
61
+ end
62
+ end
data/lib/gitenv.rb CHANGED
@@ -2,173 +2,9 @@
2
2
  require 'paint'
3
3
 
4
4
  module Gitenv
5
- VERSION = '0.0.2'
6
-
7
- class Runner
8
-
9
- def initialize
10
- @home = File.expand_path '~'
11
- @config_file = File.join @home, '.gitenv.rb'
12
- end
13
-
14
- def run
15
-
16
- @dsl = DSL.new
17
- @dsl.instance_eval File.open(@config_file, 'r').read, @config_file
18
-
19
- action = ARGV.shift || 'info'
20
-
21
- puts
22
- puts "gitenv v#{VERSION}"
23
- puts "configuration #{@config_file}"
24
- puts
25
- case action
26
- when 'info'
27
- puts Paint["Listing current state", :bright, :bold]
28
- when 'update'
29
- puts Paint["Creating missing links", :blue, :bold]
30
- end
31
-
32
- @dsl.symlinks.each do |link|
33
-
34
- link_file_path = File.join @home, link[:file]
35
- real_file = link[:source] || link[:file]
36
- real_file_path = File.join File.expand_path(@dsl.repository), real_file
37
-
38
- msg = %/#{Paint[link_file_path, :cyan]} -> #{real_file_path}/
39
- data = analyze link_file_path, real_file_path
40
-
41
- case action
42
- when 'info'
43
- puts %/#{info_mark data}#{msg} #{info_msg data}/
44
- when 'update'
45
- unless File.exists? link_file_path
46
- File.symlink real_file_path, link_file_path
47
- end
48
- puts %/#{update_mark data}#{msg} #{update_msg data}/
49
- else
50
- raise "No such action #{action}"
51
- end
52
- end
53
-
54
- puts
55
- end
56
-
57
- private
58
-
59
- def update_mark data
60
- case data[:status]
61
- when :setup
62
- Paint[" ✓ ", :green]
63
- when :wrong_link
64
- Paint[" ✗ ", :yellow]
65
- when :not_link
66
- Paint[" ✗ ", :yellow]
67
- when :void
68
- Paint[" ✓ ", :green]
69
- else
70
- Paint[" ? ", :red]
71
- end
72
- end
73
-
74
- def update_msg data
75
- case data[:status]
76
- when :setup
77
- Paint["already here", :green]
78
- when :wrong_link
79
- Paint["skipped (points to #{data[:target]})", :yellow]
80
- when :not_link
81
- Paint["skipped (not a symlink)", :yellow]
82
- when :void
83
- Paint["set up", :green]
84
- else
85
- Paint["unknown", :red]
86
- end
87
- end
88
-
89
- def info_msg data
90
- case data[:status]
91
- when :setup
92
- Paint["already here", :green]
93
- when :wrong_link
94
- Paint["points to #{data[:target]}", :yellow]
95
- when :not_link
96
- Paint["is not a symlink", :red]
97
- when :void
98
- Paint["is not yet set up", :blue]
99
- else
100
- Paint["unknown", :red]
101
- end
102
- end
103
-
104
- def info_mark data
105
- case data[:status]
106
- when :setup
107
- Paint[" ✓ ", :green]
108
- when :wrong_link
109
- Paint[" ✗ ", :red]
110
- when :not_link
111
- Paint[" ✗ ", :red]
112
- when :void
113
- Paint[" ✓ ", :green]
114
- else
115
- Paint[" ? ", :red]
116
- end
117
- end
118
-
119
- def analyze link_file_path, real_file_path
120
- if File.symlink? link_file_path
121
- current_real_file_path = File.expand_path(File.readlink(link_file_path))
122
- if current_real_file_path == real_file_path
123
- { :status => :setup }
124
- else
125
- { :status => :wrong_link, :target => current_real_file_path }
126
- end
127
- elsif File.exists? link_file_path
128
- { :status => :not_link }
129
- else
130
- { :status => :void }
131
- end
132
- end
133
- end
134
-
135
- class DSL
136
- attr_reader :repository
137
- attr_reader :symlinks
138
-
139
- def initialize
140
- @symlinks = []
141
- end
142
-
143
- def repo path
144
- @repository = File.expand_path path
145
- raise unless File.directory? @repository
146
- end
147
-
148
- def within path
149
- path = File.expand_path path
150
- raise unless File.directory? path
151
- @within = path
152
- yield
153
- @within = nil
154
- end
155
-
156
- def symlink file, source = nil
157
- source ||= file
158
- s = source || file
159
- f = File.join @repository, s
160
- raise "Unknown file #{f}" unless File.exists? f
161
- @symlinks << { :file => file, :source => source, :within => @within }
162
- end
163
-
164
- def configure &block
165
- @self_before_instance_eval = eval "self", block.binding
166
- instance_eval &block
167
- @self_before_instance_eval = nil
168
- end
5
+ VERSION = '0.0.3'
6
+ end
169
7
 
170
- def method_missing method, *args, &block
171
- @self_before_instance_eval.send method, *args, &block
172
- end
173
- end
8
+ [ :context, :config, :controller, :symlink, :copy, :enumerator, :all_files, :dot_files, :actions ].each do |lib|
9
+ require File.join(File.dirname(__FILE__), 'gitenv', lib.to_s)
174
10
  end
data/lib/program.rb ADDED
@@ -0,0 +1,23 @@
1
+ require File.join File.dirname(__FILE__), 'gitenv'
2
+ require 'commander/import'
3
+
4
+ program :name, 'gitenv'
5
+ program :version, Gitenv::VERSION
6
+ program :description, 'Symlink manager for git repositories with configuration files.'
7
+
8
+ command :info do |c|
9
+ c.action do |args,options|
10
+ Gitenv::Controller.new(:info, options).run
11
+ end
12
+ end
13
+
14
+ command :update do |c|
15
+ c.action do |args,options|
16
+ Gitenv::Controller.new(:update, options).run
17
+ end
18
+ end
19
+
20
+ global_option '-c', '--config FILE', 'Use a custom configuration file (defaults to ~/.gitenv.rb)'
21
+
22
+ default_command :info
23
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
12
+ date: 2012-09-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: paint
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.8.5
30
+ - !ruby/object:Gem::Dependency
31
+ name: commander
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 4.1.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 4.1.2
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: bundler
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -177,6 +193,16 @@ files:
177
193
  - bin/gitenv
178
194
  - gitenv.gemspec
179
195
  - lib/gitenv.rb
196
+ - lib/gitenv/actions.rb
197
+ - lib/gitenv/all_files.rb
198
+ - lib/gitenv/config.rb
199
+ - lib/gitenv/context.rb
200
+ - lib/gitenv/controller.rb
201
+ - lib/gitenv/copy.rb
202
+ - lib/gitenv/dot_files.rb
203
+ - lib/gitenv/enumerator.rb
204
+ - lib/gitenv/symlink.rb
205
+ - lib/program.rb
180
206
  - spec/helper.rb
181
207
  - spec/version_spec.rb
182
208
  homepage: http://github.com/AlphaHydrae/gitenv