ghq-cache 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29cd86f52bcc22a0415e90d28f864dec7c96fffe
4
+ data.tar.gz: 8318e679df920185bd0534fac31f68124f125761
5
+ SHA512:
6
+ metadata.gz: 46a38cfd91b5c330379fda4f998b9f839782cc38ba82cf0220f6681e219e7b0a891594bee5f582525c4900a3e29db71ac86ed86f3c45eb491100138ebee9129d
7
+ data.tar.gz: abf738478f10137d12295bb5f1b857709e5b3de1447a7d5d47531019575dc28bee311acdf6865300f5ffb411ccf2a92c945a1bb3b7e05c5b3cac70a8e82eb6d9
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ghq-cache.gemspec
4
+ gemspec
@@ -0,0 +1,71 @@
1
+ # ghq-cache
2
+
3
+ Show frequently used repositories first in [`ghq list`](https://github.com/motemen/ghq).
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ brew install motemen/ghq/ghq
9
+ gem install ghq-cache
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```bash
15
+ # Build `ghq list` cache to ~/.ghq-cache
16
+ # with ordering of host, user or repositories.
17
+ ghq-cache refresh
18
+
19
+ # Log your repository access to update ~/.ghq-cache order.
20
+ ghq-cache log /Users/k0kubun/src/github.com/k0kubun/ghq-cache
21
+ ```
22
+
23
+ ### Recommended usage
24
+
25
+ ```bash
26
+ export GOPATH=$HOME
27
+ export GHQ="/usr/local/bin/ghq"
28
+
29
+ function ghq() {
30
+ case $1 in
31
+ get )
32
+ $GHQ $@
33
+
34
+ # hook after ghq get
35
+ ghq-cache refresh &
36
+ ;;
37
+ list )
38
+ if [ ! -e ~/.ghq-cache ]; then
39
+ ghq-cache refresh
40
+ fi
41
+
42
+ # use ghq list ordered by ghq-cache
43
+ cat ~/.ghq-cache
44
+ ;;
45
+ * )
46
+ $GHQ $@
47
+ ;;
48
+ esac
49
+ }
50
+
51
+ function peco-src() {
52
+ local selected_dir=$(ghq list | peco --query "$LBUFFER" --prompt "[ghq list]")
53
+ if [ -n "$selected_dir" ]; then
54
+ full_dir="${GOPATH}/src/${selected_dir}"
55
+
56
+ # Log repository access to ghq-cache
57
+ ghq-cache log $full_dir
58
+
59
+ BUFFER="cd ${full_dir}"
60
+ zle accept-line
61
+ fi
62
+ zle redisplay
63
+ }
64
+ zle -N peco-src
65
+ stty -ixon
66
+ bindkey '^s' peco-src
67
+ ```
68
+
69
+ ## License
70
+
71
+ MIT License
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.expand_path('./lib', __dir__)
4
+ require 'ghq/cache'
5
+
6
+ Ghq::Cache::CLI.start(ARGV)
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ghq/cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ghq-cache"
8
+ spec.version = Ghq::Cache::VERSION
9
+ spec.authors = ["Takashi Kokubun"]
10
+ spec.email = ["takashikkbn@gmail.com"]
11
+
12
+ spec.summary = %q{Show frequently used repositories first in ghq list.}
13
+ spec.description = %q{Show frequently used repositories first in ghq list.}
14
+ spec.homepage = "https://github.com/k0kubun/ghq-cache"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "bin"
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "thor", "~> 0.19"
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,5 @@
1
+ require 'ghq/cache/cli'
2
+ require 'ghq/cache/directory'
3
+ require 'ghq/cache/builder'
4
+ require 'ghq/cache/logger'
5
+ require 'ghq/cache/version'
@@ -0,0 +1,51 @@
1
+ module Ghq
2
+ module Cache
3
+ class Builder
4
+ CACHE_PATH = File.expand_path('~/.ghq-cache')
5
+ GHQ_PATH = ENV['GHQ'] || '/usr/local/bin/ghq'
6
+
7
+ class << self
8
+ def build
9
+ root = Logger.resume
10
+ ghq_list.each do |path|
11
+ root.register_path(File.join(root.name, path))
12
+ end
13
+
14
+ cache = build_paths(root).join("\n").concat("\n")
15
+ File.write(CACHE_PATH, cache)
16
+ end
17
+
18
+ private
19
+
20
+ def ghq_list
21
+ `#{GHQ_PATH} list`.split("\n")
22
+ end
23
+
24
+ def build_paths(directory)
25
+ paths = []
26
+ sort_children(directory.children).each do |child|
27
+ if child.children.empty?
28
+ paths << File.join(directory.name, child.name)
29
+ next
30
+ end
31
+
32
+ build_paths(child).each do |path|
33
+ if directory.root?
34
+ paths << path
35
+ next
36
+ end
37
+ paths << File.join(directory.name, path)
38
+ end
39
+ end
40
+ paths
41
+ end
42
+
43
+ def sort_children(children)
44
+ children.sort_by do |child|
45
+ [-1 * child.count, child.name]
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,17 @@
1
+ require 'thor'
2
+
3
+ module Ghq
4
+ module Cache
5
+ class CLI < Thor
6
+ desc 'refresh', 'Refresh ~/.ghq-cache'
7
+ def refresh
8
+ Builder.build
9
+ end
10
+
11
+ desc 'log PATH', 'Log your repository access'
12
+ def log(path)
13
+ Logger.log(path)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,49 @@
1
+ module Ghq
2
+ module Cache
3
+ class Directory < Struct.new(:name, :count, :children)
4
+ def initialize(*)
5
+ super
6
+ self.count ||= 0
7
+ self.children ||= []
8
+ end
9
+
10
+ def count_path(path)
11
+ abort "Path didn't matched to Directory" unless path =~ /^#{name}/
12
+ self.count += 1
13
+
14
+ relative_path = path.gsub(%r[^#{name}/?], '')
15
+ return if relative_path.empty?
16
+
17
+ child = create_or_find(relative_path.split('/').first)
18
+ child.count_path(relative_path)
19
+ end
20
+
21
+ def register_path(path)
22
+ abort "Path didn't matched to Directory" unless path =~ /^#{name}/
23
+
24
+ relative_path = path.gsub(%r[^#{name}/?], '')
25
+ return if relative_path.empty?
26
+
27
+ child = create_or_find(relative_path.split('/').first)
28
+ child.register_path(relative_path)
29
+ end
30
+
31
+ def root?
32
+ self.name.include?('/')
33
+ end
34
+
35
+ private
36
+
37
+ def create_or_find(child_name)
38
+ child = self.children.find do |directory|
39
+ directory.name == child_name
40
+ end
41
+ return child if child
42
+
43
+ child = Directory.new(child_name)
44
+ self.children << child
45
+ child
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,45 @@
1
+ require 'yaml'
2
+
3
+ module Ghq
4
+ module Cache
5
+ class Logger
6
+ LOG_PATH = File.expand_path('~/.ghq-cahe.log')
7
+
8
+ class << self
9
+ def log(path)
10
+ initialize_log! unless initialized?
11
+
12
+ root = resume
13
+ root.count_path(path)
14
+ save_log(root)
15
+ end
16
+
17
+ def resume
18
+ initialize_log! unless initialized?
19
+
20
+ yaml = File.read(LOG_PATH)
21
+ YAML.load(yaml)
22
+ end
23
+
24
+ private
25
+
26
+ def initialize_log!
27
+ root = Directory.new(ghq_root)
28
+ save_log(root)
29
+ end
30
+
31
+ def initialized?
32
+ File.exist?(LOG_PATH)
33
+ end
34
+
35
+ def save_log(object)
36
+ File.write(LOG_PATH, object.to_yaml)
37
+ end
38
+
39
+ def ghq_root
40
+ File.join(ENV['GOPATH'], 'src')
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Ghq
2
+ module Cache
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ghq-cache
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
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'
69
+ description: Show frequently used repositories first in ghq list.
70
+ email:
71
+ - takashikkbn@gmail.com
72
+ executables:
73
+ - ghq-cache
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - README.md
81
+ - Rakefile
82
+ - bin/ghq-cache
83
+ - ghq-cache.gemspec
84
+ - lib/ghq/cache.rb
85
+ - lib/ghq/cache/builder.rb
86
+ - lib/ghq/cache/cli.rb
87
+ - lib/ghq/cache/directory.rb
88
+ - lib/ghq/cache/logger.rb
89
+ - lib/ghq/cache/version.rb
90
+ homepage: https://github.com/k0kubun/ghq-cache
91
+ licenses: []
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.4.5
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Show frequently used repositories first in ghq list.
113
+ test_files: []