gpr 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 416f71fde2acd96aa5793a4727bc98684fe57074
4
- data.tar.gz: 0c4c0d3ea7b6e2c7ae98513d59228d90fe46c87d
3
+ metadata.gz: f91a087109c606f02532188032e4f9416afc3340
4
+ data.tar.gz: a3969ac73df86f4a3dc19a359da39a92321d9bae
5
5
  SHA512:
6
- metadata.gz: 30fadcd8ca2162d53eddd98b4ba60934e88b4f8ce4f4e835e469d40c4420eeca6d241bfcebad1c66d1a67019d4325975ffab229c70044382d6b9b7a258adb62f
7
- data.tar.gz: b430d3e905d80dcefd4c0711a64a03c22af9bed46af57809b87f2b7d15705f404077cf639f5789e69e5696d4f894796c64da68d71fb700513549cc33bd08aa3a
6
+ metadata.gz: 0d24746135344420454173fd37dd83c9c9671917316d8a83d236b760808491edbdd17cfde358186a03dd8ca012bf0e6f60fb89ea51e2937a69404ea467d4f5d1
7
+ data.tar.gz: d0c52d99232833be53e494eda4c5a1fe582876edca14895f5ee96cfa42e974676c0704ce5f0d2e0e00bdb8f5114b4611118d7731a9c7d385f0e8fb8970a2035f
data/README.md CHANGED
@@ -44,6 +44,28 @@ $ gpr get <host>/<user>
44
44
 
45
45
  ```sh
46
46
  $ gpr list
47
+
48
+ github.com - kaihar4/dotfiles
49
+ github.com - kaihar4/gpr
50
+
51
+ $ gpr list --paths
52
+
53
+ /home/kaihara/.gpr/github.com/kaihar4/dotfiles
54
+ /home/kaihara/.gpr/github.com/kaihar4/gpr
55
+ ```
56
+
57
+ ### Show the status of all registered repositories
58
+
59
+ ```sh
60
+ $ gpr status
61
+
62
+ +-----------------+------------------------------------------+---------------------------+
63
+ | REPOSITORY NAME | BRANCH STATUS | DIRECTORY STATUS |
64
+ +-----------------+------------------------------------------+---------------------------+
65
+ | dotfiles | up-to-date with 'origin/master' | Working directory clean |
66
+ +-----------------+------------------------------------------+---------------------------+
67
+ | gpr | ahead of 'origin/master' by 1 commit | Exist unstaged changes |
68
+ +-----------------+------------------------------------------+---------------------------+
47
69
  ```
48
70
 
49
71
  ### Search the specified keyword in registered repositories
@@ -63,7 +85,42 @@ Search the specified keyword in registered repositories
63
85
  ```
64
86
 
65
87
  ```sh
66
- $ gpr search <keyword>
88
+ $ gpr search 'net/http'
89
+
90
+ github.com - kaihar4/gpr : lib/gpr.rb
91
+ github.com - kaihar4/kaihar4.com : source/articles/2014-06-29-mechanize-scraping.md
92
+
93
+ $ gpr search 'net/http' -f '.rb'
94
+
95
+ github.com - kaihar4/gpr : lib/gpr.rb
96
+
97
+ $ gpr search 'net/http' -r 'kaihar4/kaihar4.com'
98
+
99
+ github.com - kaihar4/kaihar4.com : source/articles/2014-06-29-mechanize-scraping.md
100
+ ```
101
+
102
+ ### Update the database to be used for search
103
+
104
+ ```sh
105
+ $ gpr update
106
+ ```
107
+
108
+ ### Fetch all registered repositories
109
+
110
+ ```sh
111
+ $ gpr fetch remote_repo
112
+ ```
113
+
114
+ If you run for short, it means `gpr fetch origin`.
115
+
116
+ ```sh
117
+ $ gpr fetch
118
+ ```
119
+
120
+ ## Change the directory to be cloned
121
+
122
+ ```sh
123
+ $ export GPR_ROOT=$HOME/develop
67
124
  ```
68
125
 
69
126
  ## Install the zsh completion
@@ -0,0 +1,43 @@
1
+ module Colorize
2
+ refine String do
3
+ COLORS = {
4
+ black: 0,
5
+ red: 1,
6
+ green: 2,
7
+ yellow: 3,
8
+ blue: 4,
9
+ magenta: 5,
10
+ cyan: 6,
11
+ white: 7
12
+ }
13
+
14
+ STYLES = {
15
+ reset: 0,
16
+ bold: 1,
17
+ underscore: 4,
18
+ blink: 5,
19
+ reverse: 6,
20
+ concealed: 7
21
+ }
22
+
23
+ def color(color)
24
+ to_ansi("3#{COLORS[color]}")
25
+ end
26
+
27
+ def bg_color(color)
28
+ to_ansi("4#{COLORS[color]}")
29
+ end
30
+
31
+ def style(style)
32
+ to_ansi(STYLES[style])
33
+ end
34
+
35
+ def to_ansi(code)
36
+ "\e[#{code.to_i}m#{self}\e[0m"
37
+ end
38
+
39
+ def to_pure
40
+ gsub(/\x1B\[[0-9]{1,3}[mK]/, '')
41
+ end
42
+ end
43
+ end
data/lib/gpr.rb CHANGED
@@ -6,12 +6,14 @@ require 'find'
6
6
  require 'thor'
7
7
  require 'groonga'
8
8
 
9
- require 'extends/colorize'
9
+ require 'colorize'
10
+ require 'table_builder'
10
11
  require 'gpr/version'
11
12
  require 'gpr/api_helper'
13
+ require 'gpr/git_helper'
12
14
  require 'gpr/cli'
13
15
 
14
16
  module Gpr
15
- APP_PATH = "#{ENV['HOME']}/.gpr"
17
+ APP_PATH = ENV['GPR_ROOT'] || "#{ENV['HOME']}/.gpr"
16
18
  DB_PATH = "#{APP_PATH}/.database"
17
19
  end
@@ -1,31 +1,34 @@
1
1
  module Gpr
2
2
  class CLI < Thor
3
+ using Colorize
4
+ include TableBuilder
5
+
3
6
  desc 'get', 'Get a repository'
4
7
  def get(param)
5
8
  case param
6
9
  when /.+(\/\/|@)(.+)\.git$/
7
10
  host, user, repository =
8
11
  param.match(/.+(\/\/|@)(.+)\.git$/)[2].split(/(:|\/)/).delete_if.with_index { |_c, index| index.odd? }
12
+ path = "#{APP_PATH}/#{host}/#{user}/#{repository}"
9
13
 
10
- if Dir.exist?("#{APP_PATH}/#{host}/#{user}/#{repository}")
14
+ if Dir.exist?(path)
11
15
  puts "#{user}/#{repository} is already registered."
12
16
  return
13
17
  end
14
18
 
15
19
  puts "#{user}/#{repository} is registering..."
16
- unless system("git clone #{param} #{APP_PATH}/#{host}/#{user}/#{repository} >/dev/null 2>&1")
20
+ unless GitHelper.clone(param, path)
17
21
  puts "Failed to clone #{user}/#{repository}."
18
22
  return
19
23
  end
20
- add_file("#{APP_PATH}/#{host}/#{user}/#{repository}")
24
+ add_file(path)
21
25
 
22
26
  when /^([^\/]+)\/([^\/]+)$/
23
27
  parsed_param = param.match(/^([^\/]+)\/([^\/]+)$/)
24
28
  host = parsed_param[1]
25
29
  user = parsed_param[2]
26
30
 
27
- clone_urls = APIHelper.get_repository_urls(host, user)
28
- clone_urls.each do |clone_url|
31
+ APIHelper.get_repository_urls(host, user).each do |clone_url|
29
32
  get(clone_url)
30
33
  end
31
34
  end
@@ -42,11 +45,9 @@ module Gpr
42
45
 
43
46
  else
44
47
  repositories.each do |repository|
45
- parsed_path = repository.match(/#{APP_PATH}\/([^\/]+)\/(.+)/)
46
- host = parsed_path[1]
47
- repository = parsed_path[2]
48
+ repo_info = parse_repository(repository)
48
49
 
49
- puts "#{host.color(:yellow)} - #{repository.color(:blue)}"
50
+ puts "#{repo_info[:host].color(:yellow)} - #{repo_info[:repository].color(:blue)}"
50
51
  end
51
52
  end
52
53
  end
@@ -84,6 +85,37 @@ module Gpr
84
85
  end
85
86
  end
86
87
 
88
+ desc 'fetch', 'Fetch the registered repositories'
89
+ def fetch(remote = 'origin', branch = nil)
90
+ repositories = get_repositories
91
+ repositories.each do |repository|
92
+ repo_info = parse_repository(repository)
93
+
94
+ puts "#{repo_info[:host].color(:yellow)} - #{repo_info[:repository].color(:blue)}"
95
+
96
+ GitHelper.fetch(repository, remote, branch)
97
+ end
98
+ end
99
+
100
+ desc 'status', 'Show the status of all registered repositories'
101
+ def status
102
+ repositories = get_repositories
103
+ table do
104
+ row do
105
+ column('REPOSITORY NAME'.style(:bold), 15)
106
+ column('BRANCH STATUS'.style(:bold), 40)
107
+ column('DIRECTORY STATUS'.style(:bold), 25)
108
+ end
109
+ repositories.each do |repository|
110
+ row do
111
+ column(repository.match(/.+\/(.+)/)[1])
112
+ column(GitHelper.status(repository)[:branch])
113
+ column(GitHelper.status(repository)[:directory])
114
+ end
115
+ end
116
+ end
117
+ end
118
+
87
119
  desc 'version', 'Display installed gpr version'
88
120
  map '-v' => :version
89
121
  def version
@@ -92,6 +124,14 @@ module Gpr
92
124
 
93
125
  private
94
126
 
127
+ def parse_repository(repository_path)
128
+ repo_info = {}
129
+ parsed_path = repository_path.match(/#{APP_PATH}\/([^\/]+)\/(.+)/)
130
+ repo_info[:host] = parsed_path[1]
131
+ repo_info[:repository] = parsed_path[2]
132
+ repo_info
133
+ end
134
+
95
135
  def get_repositories
96
136
  repositories = Dir.glob("#{APP_PATH}/*/*/*").select do |directory|
97
137
  FileTest.directory?(directory)
@@ -100,17 +140,16 @@ module Gpr
100
140
  end
101
141
 
102
142
  def add_file(path)
103
- parsed_path = path.match(/#{APP_PATH}\/([^\/]+)\/(.+)/)
143
+ repo_info = parse_repository(path)
104
144
 
105
- Dir.chdir(path)
106
- `git ls-files`.split("\n").each do |file|
145
+ GitHelper.ls_files(path).each do |file|
107
146
  next unless FileTest.file?(file)
108
147
  Groonga['Files'].add(
109
148
  File.expand_path(file),
110
149
  filename: File.basename(file),
111
150
  body: File.open(file).read,
112
- host: parsed_path[1],
113
- repository: parsed_path[2]
151
+ host: repo_info[:host],
152
+ repository: repo_info[:repository]
114
153
  )
115
154
  end
116
155
  end
@@ -0,0 +1,43 @@
1
+ module Gpr
2
+ module GitHelper
3
+ class << self
4
+ def clone(url, path)
5
+ system("git clone #{url} #{path} >/dev/null 2>&1")
6
+ end
7
+
8
+ def ls_files(path)
9
+ Dir.chdir(path)
10
+ `git ls-files`.split("\n")
11
+ end
12
+
13
+ def fetch(path, remote, branch)
14
+ Dir.chdir(path)
15
+ if branch.nil?
16
+ `git fetch #{remote}`
17
+ else
18
+ `git fetch #{remote} #{branch}`
19
+ end
20
+ end
21
+
22
+ def status(path)
23
+ Dir.chdir(path)
24
+ result = {}
25
+ status = `git status`
26
+
27
+ result[:branch] = status.match(/Your branch (is|and) (.+?)(,|\.)/)[2]
28
+
29
+ case status
30
+ when /nothing to commit, working directory clean/
31
+ result[:directory] = 'Working directory clean'
32
+
33
+ when /Changes not staged for commit:/
34
+ result[:directory] = 'Exist unstaged changes'
35
+
36
+ when /Untracked files:/
37
+ result[:directory] = 'Exist untracked files'
38
+ end
39
+ result
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
1
  module Gpr
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'table_builder/table'
2
+ require 'table_builder/row'
3
+ require 'table_builder/column_validator'
4
+ require 'table_builder/column'
5
+
6
+ module TableBuilder
7
+ def table(min_column_size = 10, &block)
8
+ @table = Table.new(min_column_size)
9
+ block.call
10
+ @table.to_s
11
+ end
12
+
13
+ def row(&block)
14
+ @row = Row.new
15
+ block.call
16
+ @table.add(@row)
17
+ end
18
+
19
+ def column(text, size = nil)
20
+ @column = Column.new(text, size)
21
+ @row.add(@column)
22
+ end
23
+ end
@@ -0,0 +1,16 @@
1
+ module TableBuilder
2
+ class Column
3
+ using Colorize
4
+ prepend ColumnValidator
5
+
6
+ attr_accessor :text
7
+
8
+ def initialize(text, size)
9
+ # Ignore ANSI escape sequences
10
+ pure_text = text.to_pure
11
+
12
+ text += ' ' * (size - pure_text.size) if !size.nil? && size > pure_text.size
13
+ @text = text
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module TableBuilder
2
+ module ColumnValidator
3
+ def initialize(text, size)
4
+ fail 'Invalid multibyte char' if text =~ /[^\x01-\x7E]/
5
+
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ module TableBuilder
2
+ class Row
3
+ attr_accessor :columns
4
+
5
+ def initialize
6
+ @columns = []
7
+ end
8
+
9
+ def add(column)
10
+ @columns << column
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,81 @@
1
+ module TableBuilder
2
+ class Table
3
+ using Colorize
4
+
5
+ def initialize(min_column_size)
6
+ @rows = []
7
+ @header_column_sizes = []
8
+ @min_column_size = min_column_size
9
+ end
10
+
11
+ def add(row)
12
+ # Means the header
13
+ if @rows.size.zero?
14
+ set_header_column_size(row)
15
+
16
+ else
17
+ if row.columns.size != @header_column_sizes.size
18
+ fail "Bud number of columns (#{row.columns.size} for #{@header_column_sizes.size})"
19
+ end
20
+ end
21
+
22
+ @rows << row
23
+ end
24
+
25
+ def to_s
26
+ puts separate
27
+
28
+ @rows.each.with_index do |row, index|
29
+ puts format_line(row)
30
+ puts separate if index != @rows.size - 1
31
+ end
32
+
33
+ puts separate
34
+ end
35
+
36
+ private
37
+
38
+ def set_header_column_size(row)
39
+ row.columns.each.with_index do |column, index|
40
+ # Ignore ANSI escape sequences
41
+ pure_text = column.text.to_pure
42
+
43
+ if pure_text.size > @min_column_size
44
+ @header_column_sizes[index] = pure_text.size
45
+ else
46
+ @header_column_sizes[index] = @min_column_size
47
+ end
48
+ end
49
+ end
50
+
51
+ def format_line(row)
52
+ line = '| '
53
+
54
+ row.columns.each.with_index do |column, index|
55
+ # Ignore ANSI escape sequences
56
+ pure_text = column.text.to_pure
57
+
58
+ if pure_text.size < @header_column_sizes[index]
59
+ line << column.text + ' ' * (@header_column_sizes[index] - pure_text.size)
60
+
61
+ elsif pure_text.size > @header_column_sizes[index]
62
+ line << column.text[0, @header_column_sizes[index] - 3] + '...'
63
+
64
+ else
65
+ line << column.text
66
+ end
67
+
68
+ line << ' | ' if index != row.columns.size - 1
69
+ end
70
+
71
+ line << ' |'
72
+ end
73
+
74
+ def separate
75
+ left = middle = right = '+'
76
+ bar = '-'
77
+
78
+ left + @header_column_sizes.map { |size| bar * (size + 2) }.join(middle) + right
79
+ end
80
+ end
81
+ end
@@ -33,6 +33,12 @@ function _gpr () {
33
33
  ;;
34
34
  (update)
35
35
  ;;
36
+ (fetch)
37
+ ;;
38
+ (status)
39
+ ;;
40
+ (version)
41
+ ;;
36
42
  esac
37
43
  ;;
38
44
  esac
@@ -48,6 +54,9 @@ __gpr_cmds () {
48
54
  'list:Show all registered repositories'
49
55
  'search:Search the specified keyword in registered repositories'
50
56
  'update:Update the database to be used for search'
57
+ 'fetch:Fetch the registered repositories'
58
+ 'status:Show the status of all registered repositories'
59
+ 'version:Display installed gpr version'
51
60
  )
52
61
 
53
62
  _describe -t commands Commands _c
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gpr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-08 00:00:00.000000000 Z
11
+ date: 2014-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -109,11 +109,17 @@ files:
109
109
  - Rakefile
110
110
  - bin/gpr
111
111
  - gpr.gemspec
112
- - lib/extends/colorize.rb
112
+ - lib/colorize.rb
113
113
  - lib/gpr.rb
114
114
  - lib/gpr/api_helper.rb
115
115
  - lib/gpr/cli.rb
116
+ - lib/gpr/git_helper.rb
116
117
  - lib/gpr/version.rb
118
+ - lib/table_builder.rb
119
+ - lib/table_builder/column.rb
120
+ - lib/table_builder/column_validator.rb
121
+ - lib/table_builder/row.rb
122
+ - lib/table_builder/table.rb
117
123
  - zsh/gpr.zsh
118
124
  homepage: https://github.com/kaihar4/gpr
119
125
  licenses:
@@ -135,9 +141,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
141
  version: '0'
136
142
  requirements: []
137
143
  rubyforge_project:
138
- rubygems_version: 2.0.14
144
+ rubygems_version: 2.2.2
139
145
  signing_key:
140
146
  specification_version: 4
141
147
  summary: Simple management of the git repository.
142
148
  test_files: []
143
- has_rdoc:
@@ -1,39 +0,0 @@
1
- class String
2
- COLORS = {
3
- black: 0,
4
- red: 1,
5
- green: 2,
6
- yellow: 3,
7
- blue: 4,
8
- magenta: 5,
9
- cyan: 6,
10
- white: 7
11
- }
12
-
13
- STYLES = {
14
- reset: 0,
15
- bold: 1,
16
- underscore: 4,
17
- blink: 5,
18
- reverse: 6,
19
- concealed: 7
20
- }
21
-
22
- def color(color)
23
- to_ansi("3#{COLORS[color]}")
24
- end
25
-
26
- def bg_color(color)
27
- to_ansi("4#{COLORS[color]}")
28
- end
29
-
30
- def style(style)
31
- to_ansi(STYLES[style])
32
- end
33
-
34
- protected
35
-
36
- def to_ansi(code)
37
- "\e[#{code.to_i}m#{self}\e[0m"
38
- end
39
- end