gitenv 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/VERSION +1 -1
  2. data/bin/gitenv +1 -1
  3. data/gitenv.gemspec +79 -0
  4. data/lib/gitenv.rb +171 -1
  5. metadata +2 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
data/bin/gitenv CHANGED
@@ -1,4 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
2
  require File.join(File.dirname(__FILE__), '..', 'lib', 'gitenv')
3
3
 
4
- puts 'Not yet implemented...'
4
+ Gitenv::Runner.new.run
data/gitenv.gemspec ADDED
@@ -0,0 +1,79 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "gitenv"
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["AlphaHydrae"]
12
+ s.date = "2012-09-21"
13
+ s.description = "Creates symlinks to configuration files in a git repository."
14
+ s.email = "hydrae.alpha@gmail.com"
15
+ s.executables = ["gitenv"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".rspec",
22
+ ".rvmrc",
23
+ ".screenrc",
24
+ ".travis.yml",
25
+ "Gemfile",
26
+ "Gemfile.lock",
27
+ "LICENSE.txt",
28
+ "README.md",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "bin/gitenv",
32
+ "gitenv.gemspec",
33
+ "lib/gitenv.rb",
34
+ "spec/helper.rb",
35
+ "spec/version_spec.rb"
36
+ ]
37
+ s.homepage = "http://github.com/AlphaHydrae/gitenv"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.24"
41
+ s.summary = "Git environment project manager."
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_runtime_dependency(%q<paint>, ["~> 0.8.5"])
48
+ s.add_development_dependency(%q<bundler>, [">= 0"])
49
+ s.add_development_dependency(%q<rake>, [">= 0"])
50
+ s.add_development_dependency(%q<rspec>, [">= 0"])
51
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
52
+ s.add_development_dependency(%q<gemcutter>, [">= 0"])
53
+ s.add_development_dependency(%q<gem-release>, [">= 0"])
54
+ s.add_development_dependency(%q<rake-version>, [">= 0"])
55
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
56
+ else
57
+ s.add_dependency(%q<paint>, ["~> 0.8.5"])
58
+ s.add_dependency(%q<bundler>, [">= 0"])
59
+ s.add_dependency(%q<rake>, [">= 0"])
60
+ s.add_dependency(%q<rspec>, [">= 0"])
61
+ s.add_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_dependency(%q<gemcutter>, [">= 0"])
63
+ s.add_dependency(%q<gem-release>, [">= 0"])
64
+ s.add_dependency(%q<rake-version>, [">= 0"])
65
+ s.add_dependency(%q<simplecov>, [">= 0"])
66
+ end
67
+ else
68
+ s.add_dependency(%q<paint>, ["~> 0.8.5"])
69
+ s.add_dependency(%q<bundler>, [">= 0"])
70
+ s.add_dependency(%q<rake>, [">= 0"])
71
+ s.add_dependency(%q<rspec>, [">= 0"])
72
+ s.add_dependency(%q<jeweler>, [">= 0"])
73
+ s.add_dependency(%q<gemcutter>, [">= 0"])
74
+ s.add_dependency(%q<gem-release>, [">= 0"])
75
+ s.add_dependency(%q<rake-version>, [">= 0"])
76
+ s.add_dependency(%q<simplecov>, [">= 0"])
77
+ end
78
+ end
79
+
data/lib/gitenv.rb CHANGED
@@ -1,4 +1,174 @@
1
+ # encoding: UTF-8
2
+ require 'paint'
1
3
 
2
4
  module Gitenv
3
- VERSION = '0.0.1'
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
169
+
170
+ def method_missing method, *args, &block
171
+ @self_before_instance_eval.send method, *args, &block
172
+ end
173
+ end
4
174
  end
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -175,6 +175,7 @@ files:
175
175
  - Rakefile
176
176
  - VERSION
177
177
  - bin/gitenv
178
+ - gitenv.gemspec
178
179
  - lib/gitenv.rb
179
180
  - spec/helper.rb
180
181
  - spec/version_spec.rb