-
Notifications
You must be signed in to change notification settings - Fork 3
/
grit.rb
executable file
·177 lines (146 loc) · 4.3 KB
/
grit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
# Grit is a way to manage multiple repos in a git repository seamlessly
# using the git CL tool
# It serves 'only' to be a mapping tool; it doesn't create/delete repositories
# only .grit/config.yml
# What Grit should do:
# - Proxy the git API
# - Don't get in the way
# - Allow the user to make the normal git choices due to that proxy
# Sample config.yml
# ---
# :root: /Users/john/my_project
# :repositories:
# - :name: Sproutcore
# :path: frameworks/sproutcore
# - :name: SCUI
# :path: frameworks/scui
require 'yaml'
require 'fileutils'
class Grit
def initialize_grit (args)
location = args[0] || Dir.pwd
if File.directory?(location)
directory = File.join(location,'.grit')
FileUtils.mkdir(directory) unless File.directory?(directory)
add_profile("config")
set_profile("config")
else
puts "Directory doesn't exist!"
end
end
def get_config(profile=nil)
profile = get_current_profile if profile == nil
f = ".grit/#{profile}.yml" % {:profile => profile}
return open(File.join(FileUtils.pwd,f)) {|f| YAML.load(f)}
end
def write_config (config, profile = nil)
profile = get_current_profile if profile == nil
f = ".grit/#{profile}.yml" % {:profile => profile}
return open(File.join(FileUtils.pwd,f),'w') {|f| YAML.dump(config,f)}
end
def add_profile (profile = nil, root = nil)
return if profile == nil
location = Dir.pwd
directory = File.join(location,'.grit')
file = profile + ".yml"
config_file = directory+'/'+file
if !File.exists?(config_file)
config = {}
config[:root] ||= ((root == nil || root.empty?) ? Dir.pwd : root)
config[:repositories] ||= []
open(config_file,'w') {|f| YAML.dump(config,f)}
end
end
def add_repository (args)
config = get_config
name,path = args[0],args[1]
config[:repositories].push({:name => name, :path => path})
self.write_config(config)
end
def get_repository(name)
config = get_config
return config[:repositories].detect{|f| f[:name] == name}
end
def perform_on (repo_name,args)
repo = get_repository(repo_name)
args = args.join(' ') unless args.class == String
if repo.nil? || repo[:path].nil? || !File.exists?(repo[:path])
puts "Can't find repository: #{repo_name} at location #{repo[:path]}"
abort
end
Dir.chdir(repo[:path]) do |d|
perform(args,repo[:name])
end
end
def set_profile(profile = "config")
profile = "config" if (profile == nil || profile.empty?)
location = Dir.pwd
f = File.new(".grit/current_profile", 'w')
f.write(profile)
f.close
end
def get_current_profile
location = Dir.pwd
if (File.exists?(".grit/current_profile"))
return File.read(".grit/current_profile")
end
return "config"
end
# opting to not remove the directory
def remove_repository (names)
config = get_config
match = get_repository(names[0])
unless match.nil?
if config[:repositories].delete(match)
write_config(config)
puts "Removed repository #{match} from grit"
else
puts "Unable to remove repository #{match}"
end
else
puts "Could not find repository"
end
end
def perform (to_do,name)
puts "-"*80
puts "# #{name.upcase} -- git #{to_do}" unless name.nil?
puts `git #{to_do}`
puts "-"*80
puts ""
end
def proceed (args)
config = get_config
repositories = config[:repositories].unshift({:name => 'Root',:path => config[:root]})
to_do = args.map{|x| if x.include?(" "); "\"#{x}\""; else; x; end}.join(" ")
repositories.each do |repo|
if repo[:path].nil?
puts "Can't find repository: #{repo[:path]}"
continue
end
Dir.chdir(repo[:path]) do |d|
perform(to_do,repo[:name])
end
end
end
end
# TODO ... this better
project = Grit.new
case ARGV[0]
when 'init'
project.initialize_grit(ARGV[1..-1])
when 'ar','add-repository'
project.add_repository(ARGV[1..-1])
when 'rr','remove-repository'
project.remove_repository(ARGV[1..-1])
when 'ap','add-profile'
project.add_profile(ARGV[1], ARGV[2])
when 'sp','set-profile'
project.set_profile(ARGV[1])
when 'wp','which-profile'
puts project.get_current_profile
when 'on'
project.perform_on(ARGV[1],ARGV[2..-1])
else
project.proceed ARGV
end