git-projects 0.0.2 → 0.0.3

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: 4504bc3fe630d0ea505b28aa524773333b5be322
4
- data.tar.gz: adc75f8bea5c45630d2874000b2dcd9cd3def3a1
3
+ metadata.gz: 1bf0e0af220a8b22f1b3543c7d929b4b4a6e0ed1
4
+ data.tar.gz: 11c9b54a6a523373e405cb361ea9a8ce8a14d15d
5
5
  SHA512:
6
- metadata.gz: 6bf2122f31976a419836eab66db89dc5faefabe44a3293f75bb3bfabdbc94f0ca00787ccf597e1e1192dab337f232e6780c9e6a07d176ea2c451f0b6f64d6d10
7
- data.tar.gz: e2e8df57c74dfd36c9d327f37d2e18f088ba9fcc036ee4b58a6aad3e65255241600aa7de44c8dd0e52663063f0277d900e9fc8116ba6ca94de14c17ab24bdc90
6
+ metadata.gz: 01d2cce71c5173d68e3fca746593a7f628be481f3175f6d1cc84737906692f373ef54950afd18561410d960316e227cb369d58d1ce07985b15830e9cc64b1cba
7
+ data.tar.gz: 72742b92441389515054902dad5edcbaf6ebf05a4676044459775fb95068787940f0667c228ed8a77ecb5ea912fec38b482c54a4af9f37fa738bf40aeedaad00
@@ -1,3 +1,3 @@
1
1
  module GitProjects
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
data/lib/git-projects.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'gli'
3
3
  require 'json'
4
+ require 'colorize'
4
5
  require_relative 'helpers/project'
5
6
  require_relative 'helpers/git_project'
6
7
 
@@ -8,40 +9,40 @@ include GLI::App
8
9
 
9
10
  program_desc 'Easily manage Git projects'
10
11
 
11
- pre do |global_options,command,options,args|
12
- if ENV['GIT_PROJECTS']
13
- p "Checking repositories. If things go wrong, update #{ENV['GIT_PROJECTS']}"
14
- else
15
- raise "Please add the path your git projects config. \n export GIT_PROJECTS=/path/to/git_projects.yml"
16
- end
17
- end
18
-
19
12
  desc 'Create a config'
20
- skips_pre
21
13
  command :config do |c|
22
14
  c.action do |global_options,options,args|
23
- config_path = "#{args.first}/git-projects.yml"
24
- GitProject.create_config(args)
15
+ config_path = "#{args[0]}/git-projects.yml"
16
+ group = args[1] || nil
17
+ GitProject.create_config(args[0], group)
25
18
  if File.open(config_path)
26
- p "successfully created git-projects.yml"
19
+ puts "Successfully created git-projects.yml".green
27
20
  end
28
21
  end
29
22
  end
30
23
 
31
- desc 'Clone all projects'
32
- command :clone do |c|
24
+ desc 'Clone or initialize all projects'
25
+ command :init do |c|
33
26
  c.action do
34
- if GitProject.new(ENV['GIT_PROJECTS']).clone_all
35
- p 'successfully cloned repositories'
36
- end
27
+ GitProject.check_config # the pre feature of GLI has a bug. investigate later.
28
+ GitProject.new(ENV['GIT_PROJECTS']).init
37
29
  end
38
30
  end
39
31
 
40
- desc 'Fetch changed from all remotes'
41
- command :fetch do |c|
32
+ desc 'Add missing remotes'
33
+ command :'add-remotes' do |c|
42
34
  c.action do
43
- if GitProject.new(ENV['GIT_PROJECTS']).fetch_all
44
- p 'successfully fetched changes'
35
+ GitProject.check_config
36
+ GitProject.new(ENV['GIT_PROJECTS']).add_remotes
37
+ end
38
+ end
39
+
40
+ desc 'Fetch changes from all remotes'
41
+ command :fetch do |c|
42
+ c.action do |global_options,options,args|
43
+ GitProject.check_config
44
+ if GitProject.new(ENV['GIT_PROJECTS']).fetch_all(args[0])
45
+ puts "Successfully fetched changes".green
45
46
  end
46
47
  end
47
48
  end
@@ -1,6 +1,7 @@
1
1
  require_relative 'project'
2
2
  require 'git'
3
3
  require 'yaml'
4
+ require 'colorize'
4
5
 
5
6
  class GitProject
6
7
 
@@ -11,6 +12,7 @@ class GitProject
11
12
  end
12
13
 
13
14
  class << self
15
+
14
16
  # Create YAML file
15
17
  def create_yaml_file(config_file, projects)
16
18
  File.open(config_file, "w") do |f|
@@ -33,18 +35,22 @@ class GitProject
33
35
  end
34
36
 
35
37
  # Create a configuration file based on a root path
36
- def create_config(dir)
38
+ def create_config(dir, group=nil)
37
39
  dir = dir.is_a?(Array) ? dir.first : dir
38
40
  config_file = "#{dir}/git-projects.yml"
41
+ group ||= dir.split(File::SEPARATOR).last
42
+
39
43
  unless File.exists?(config_file)
40
44
  projects = []
41
45
  Dir.entries(dir)[2..-1].each do |project|
42
46
  g = Git.open("#{dir}/#{project}")
43
47
  p = {}
44
- p[project] = add_remotes_to_hash(g, p, dir)
48
+ p[project] = add_remotes_to_hash(g, p, dir)
49
+ p[project]['group'] = group
45
50
  projects << p
46
51
  create_yaml_file(config_file, projects)
47
52
  end
53
+ puts "You can later fetch changes through: git-projects fetch #{group}".green
48
54
  else
49
55
  raise "The config file, #{config_file} exists"
50
56
  end
@@ -57,8 +63,8 @@ class GitProject
57
63
  # Create dir unless it exists
58
64
  def create_directory(path)
59
65
  unless File.directory?(path)
60
- `mkdir -p #{path}`
61
- printf("Creating directory: %s\n", path)
66
+ `mkdir -puts #{path}`
67
+ puts "Creating directory: ".green+"#{path}".blue
62
68
  end
63
69
  end
64
70
 
@@ -67,38 +73,48 @@ class GitProject
67
73
  GitProject.create_directory(path) unless File.directory?(path)
68
74
  end
69
75
 
76
+ # Check for the config
77
+ def check_config
78
+ if ENV['GIT_PROJECTS']
79
+ puts "Checking repositories. If things go wrong, update #{ENV['GIT_PROJECTS']}".green
80
+ else
81
+ raise "Please add the path your git projects config. \n export GIT_PROJECTS=/path/to/git_projects.yml"
82
+ end
83
+ end
84
+
70
85
  # Clone unless dir exists
71
86
  def clone(url, name, path)
72
87
  r = "#{path}/#{name}"
73
88
  if Git.open(r)
74
- p "Already cloned #{url}"
89
+ puts "Already cloned ".yellow+ "#{url}".blue
75
90
  else
76
91
  Git.clone(url, name, path: path) || Git.init(r)
77
92
  g = Git.open(r)
78
- p "Cloning #{url} as #{name} into #{path}"
93
+ puts "Cloning #{url} as #{name} into #{path}".green
79
94
  end
80
95
  g
81
96
  end
82
97
 
83
98
  # Add remote
84
99
  def add_remote(g, v)
85
- g.add_remote('all', v['origin'])
100
+ g.add_remote('all', v['origin']) unless g.remotes.map(&:name).include?('all')
86
101
  v.each do |name, remote|
87
- unless ['root_dir', 'origin', 'all'].include?(name)
88
- g.add_remote(name, remote) unless g.remotes.map(&:name).include?(name)
89
-
90
- # add to all remote
91
- # useful when you want to do git push all --all
92
- `git remote set-url --add all #{remote}`
102
+ unless ['root_dir', 'all', 'group'].include?(name) || g.remotes.map(&:name).include?(name)
103
+ if g.add_remote(name, remote)
104
+ # add to all remote
105
+ # useful when you want to do git push all --all
106
+ `git remote set-url --add all #{remote}`
107
+ puts "Added remote #{name}".green
108
+ end
93
109
  end
94
110
  end
95
111
  end
96
112
 
97
113
  def fetch(g, url)
98
114
  g.remotes.each do |r|
99
- unless r.name=='all'
115
+ unless ['root_dir', 'all', 'group'].include?(name)
100
116
  r.fetch
101
- p "Fetching updates from #{r.name} : #{r.url}"
117
+ puts "Fetching updates from #{r.name}: #{r.url}".green
102
118
  end
103
119
  end
104
120
  end
@@ -106,11 +122,11 @@ class GitProject
106
122
 
107
123
  # 1. Clone all repositories based on the origin key
108
124
  # 2. Add all other remotes unless it is origin
109
- def clone_all
125
+ def init
110
126
  @project.all.each do |k,v|
111
127
  begin
112
- p "root_dir isn't defined for #{k}" unless v['root_dir']
113
- p "The dir #{v['root_dir']} does not exist" unless File.directory?(v['root_dir'])
128
+ puts "root_dir isn't defined for #{k}" unless v['root_dir']
129
+ puts "The dir #{v['root_dir']} does not exist" unless File.directory?(v['root_dir'])
114
130
  GitProject.create_root_dir(v['root_dir'])
115
131
  g = GitProject.clone(v.values[0], k, v['root_dir'])
116
132
  GitProject.add_remote(g,v) if g
@@ -120,22 +136,48 @@ class GitProject
120
136
  GitProject.add_remote(g,v)
121
137
  GitProject.fetch(g, v)
122
138
  end
123
- p "Please check paths and permissions for #{k}. Error: #{e}"
124
- p "Failed to clone #{v.values[0]}. Initialized & fetch update from remotes instead."
139
+ puts "Please check paths and permissions for #{k}. Error: #{e}".red
140
+ puts "Failed to clone #{v.values[0]}. Initialized & fetch update from remotes instead.".yellow
125
141
  end
126
142
  end
127
143
  end
128
144
 
129
- # Fetch all updates for remotes for all projects
130
- def fetch_all
131
- p "Found #{@project.all.size} projects"
145
+ def projects
146
+ @project.all
147
+ end
148
+
149
+ # Add a new remote
150
+ def new_remote(project, name, url)
151
+ @project.new_remote(project, name, url)
152
+ end
153
+
154
+ # Change group
155
+ def new_group(project, name)
156
+ @project.new_group(project, name)
157
+ end
158
+
159
+ # Add missing remotes
160
+ def add_remotes
161
+ puts "Found #{@project.all.size} projects".green
132
162
  @project.all.each do |k,v|
133
- p "Fetching changes for #{k}"
163
+ working_dir = "#{v['root_dir']}/#{k}"
164
+ g = Git.open(working_dir) || Git.init(working_dir)
165
+ GitProject.add_remote(g,v)
166
+ puts "Checking new remotes for #{k}".green
167
+ end
168
+ end
169
+
170
+ # Fetch all updates
171
+ # Group is optional
172
+ # By default, fetch from all
173
+ def fetch_all(group=nil)
174
+ puts "Found #{@project.all.size} projects".green
175
+ @project.all(group).each do |k,v|
176
+ puts "Fetching changes for #{k}".green
134
177
  GitProject.create_root_dir(v['root_dir'])
135
178
  working_dir = "#{v['root_dir']}/#{k}"
136
179
  g = Git.open(working_dir) || Git.init(working_dir)
137
180
  GitProject.fetch(g, v)
138
- GitProject.add_remote(g,v)
139
181
  end
140
182
  return true
141
183
  end
@@ -1,4 +1,5 @@
1
1
  require 'yaml'
2
+ require 'colorize'
2
3
 
3
4
  class Project
4
5
 
@@ -8,8 +9,8 @@ class Project
8
9
  @projects = YAML.load_file(config)
9
10
  end
10
11
 
11
- def all
12
- @projects
12
+ def all(group=nil)
13
+ group ? @projects.select { |k, v| v['group'].include?(group) } : @projects
13
14
  end
14
15
 
15
16
  def first
@@ -23,4 +24,16 @@ class Project
23
24
  end
24
25
  end
25
26
  end
27
+
28
+ def new_remote(remote, name, url)
29
+ @projects.tap do |project|
30
+ project[remote][name] = url
31
+ end
32
+ end
33
+
34
+ def new_group(remote, name)
35
+ @projects.tap do |project|
36
+ project[remote]['group'] = name
37
+ end
38
+ end
26
39
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-projects
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Katherine Pe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-29 00:00:00.000000000 Z
11
+ date: 2014-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: git
@@ -25,13 +25,13 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.2.6
27
27
  - !ruby/object:Gem::Dependency
28
- name: rake
28
+ name: colorize
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :development
34
+ type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: gli
57
71
  requirement: !ruby/object:Gem::Requirement