prj 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source :rubygems
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.11"
5
+ gem "rake", "~> 0.9"
6
+ gem 'simplecov', "~> 0.6", :require => false
7
+ gem "fakefs", "~> 0.4", :require => "fakefs/safe"
8
+ gem 'pry'
9
+ end
10
+
11
+
data/Gemfile.lock ADDED
@@ -0,0 +1,36 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ coderay (1.0.7)
5
+ diff-lcs (1.1.3)
6
+ fakefs (0.4.1)
7
+ method_source (0.8)
8
+ multi_json (1.3.7)
9
+ pry (0.9.10)
10
+ coderay (~> 1.0.5)
11
+ method_source (~> 0.8)
12
+ slop (~> 3.3.1)
13
+ rake (0.9.5)
14
+ rspec (2.12.0)
15
+ rspec-core (~> 2.12.0)
16
+ rspec-expectations (~> 2.12.0)
17
+ rspec-mocks (~> 2.12.0)
18
+ rspec-core (2.12.0)
19
+ rspec-expectations (2.12.0)
20
+ diff-lcs (~> 1.1.3)
21
+ rspec-mocks (2.12.0)
22
+ simplecov (0.7.1)
23
+ multi_json (~> 1.0)
24
+ simplecov-html (~> 0.7.1)
25
+ simplecov-html (0.7.1)
26
+ slop (3.3.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ fakefs (~> 0.4)
33
+ pry
34
+ rake (~> 0.9)
35
+ rspec (~> 2.11)
36
+ simplecov (~> 0.6)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Vladimir Yarotsky
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ Prj
2
+ ==========
3
+
4
+ Cd to your project the right way!
5
+
6
+ Prj chooses a project directory based on fuzzy matching
7
+
8
+ Usage:
9
+ ------
10
+
11
+ Put this into your .zshrc (.bashrc)
12
+
13
+ ```
14
+ function p() {
15
+ builtin cd "$(prj $1)"
16
+ }
17
+ ```
18
+
19
+ Put a project root directory into ~/.prj, i.e:
20
+
21
+ ```
22
+ ~/Projects
23
+ ```
24
+
25
+ and run ```p letters_from_your_project_folder_name```
26
+
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'rspec/core/rake_task'
15
+
16
+ RSpec::Core::RakeTask.new do |t|
17
+ t.pattern = 'spec/lib/**/*_spec.rb'
18
+ end
19
+
20
+ RSpec::Core::RakeTask.new do |t|
21
+ t.pattern = 'spec/acceptance/**/*_spec.rb'
22
+ t.name = :acceptance_spec
23
+ end
24
+
25
+ task :default => [:spec]
26
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/bin/prj ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+ # vi: set ft=ruby :
4
+
5
+ require 'rubygems'
6
+ require 'prj'
7
+ require 'prj/app'
8
+
9
+ include Prj
10
+
11
+ Signal.trap("SIGINT") do
12
+ puts "Terminating"
13
+ exit 1
14
+ end
15
+
16
+ exit App.new(STDOUT, ARGV).run
17
+
data/lib/prj/app.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'prj/finder'
2
+ require 'prj/filter'
3
+
4
+ module Prj
5
+ class App
6
+ class << self
7
+ attr_accessor :default_projects_root, :ignore_config
8
+ end
9
+ @default_projects_root = "~/Projects".freeze
10
+ @ignore_config = false
11
+
12
+ def initialize(sink, args = [])
13
+ @letters = String(args.first).each_char.to_a
14
+ @sink = sink
15
+ end
16
+
17
+ def run
18
+ if @letters.empty?
19
+ @sink.puts projects_root
20
+ else
21
+ finder = Finder.new(projects_root)
22
+ filter = Filter.new(@letters)
23
+
24
+ directories = finder.find_project_directories
25
+ filtered_directories = filter.filter(directories)
26
+
27
+ target_directory = File.join(projects_root, filtered_directories.first.to_s)
28
+
29
+ @sink.puts target_directory
30
+ end
31
+
32
+ return 0
33
+ end
34
+
35
+ def projects_root
36
+ @projects_root ||= begin
37
+ path = begin
38
+ raise "default config" if self.class.ignore_config
39
+ File.read(File.expand_path("~/.prj")).chomp
40
+ rescue
41
+ self.class.default_projects_root
42
+ end
43
+ File.expand_path(path).freeze
44
+ end
45
+ end
46
+ end
47
+ end
48
+
data/lib/prj/filter.rb ADDED
@@ -0,0 +1,56 @@
1
+ module Prj
2
+ class Filter
3
+ def initialize(letters)
4
+ @letters = letters.to_a
5
+ end
6
+
7
+ def filter(directories)
8
+ sort(directories.select { |d| filter_dir(d, @letters) })
9
+ end
10
+
11
+ def dispersion(dir)
12
+ return 0 if dir.empty? || @letters.empty?
13
+ indices = []
14
+ d = dir.dup
15
+ @letters.each do |letter|
16
+ idx = d.index(letter)
17
+ indices << idx
18
+ d = d[(idx + 1)..-1]
19
+ end
20
+ indices.inject(0, &:+) - indices.first
21
+ end
22
+
23
+ private
24
+
25
+ def filter_dir(dir, letters)
26
+ if letters.empty?
27
+ true
28
+ else
29
+ letter = letters.first
30
+ i = dir.index(letter)
31
+ i && filter_dir(dir[(i + 1)..-1], letters[1..-1])
32
+ end
33
+ end
34
+
35
+ def sort(directories)
36
+ directories.sort do |d1, d2|
37
+ disp1 = dispersion(d1)
38
+ disp2 = dispersion(d2)
39
+ if disp1 < disp2
40
+ -1
41
+ elsif disp1 > disp2
42
+ 1
43
+ else
44
+ if d1.length < d2.length
45
+ -1
46
+ elsif d1.length > d2.length
47
+ 1
48
+ else
49
+ 0
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+
data/lib/prj/finder.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'find'
2
+
3
+ module Prj
4
+ class Finder
5
+ def initialize(root)
6
+ @root = File.expand_path(root)
7
+ end
8
+
9
+ ##
10
+ # Returns directories containing .git/ directory, relative to @root
11
+ #
12
+ def find_project_directories
13
+ subdirectories = []
14
+ Find.find(@root) do |d|
15
+ subdirectories << d && Find.prune if File.exists?(File.join(d, ".git/"))
16
+ end
17
+ subdirectories.map { |r| r.gsub(@root, "") }
18
+ end
19
+ end
20
+ end
data/lib/prj.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Prj
2
+ version_file = File.expand_path('../VERSION', File.dirname(__FILE__))
3
+ VERSION = File.read(version_file).freeze
4
+ end
5
+
6
+ require 'prj/filter'
7
+ require 'prj/finder'
8
+
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ require 'prj/app'
3
+ require 'stringio'
4
+ require 'tmpdir'
5
+ require 'fileutils'
6
+
7
+ describe "Prj::App" do
8
+ around(:each) do |example|
9
+ Dir.mktmpdir do |root|
10
+ @root = root
11
+ @subdirectories = [
12
+ "foo/.git/",
13
+ "bar/",
14
+ "baz/qux/crisp/.git/",
15
+ "baz/craps/poops/.git/",
16
+ "any/thing/here/"
17
+ ].map { |d| File.join(@root, d) }
18
+ @subdirectories.each { |d| FileUtils.mkdir_p(d) }
19
+ @sink = StringIO.new
20
+ example.run
21
+ end
22
+ end
23
+
24
+ context "with default projcts root" do
25
+ around(:each) do |example|
26
+ with_projects_root(@root) do
27
+ example.run
28
+ end
29
+ end
30
+
31
+ it "prints matching directory and returns 0" do
32
+ Prj::App.new(@sink, ["ap"]).run.should == 0
33
+ @sink.string.chomp.should == File.join(@root, "baz/craps/poops")
34
+ end
35
+
36
+ it "prints projects root and returns 0 if directory not found" do
37
+ Prj::App.new(@sink, ["nothingtofind"]).run.should == 0
38
+ @sink.string.chomp.should == @root + "/"
39
+ end
40
+
41
+ end
42
+
43
+ it "Uses project root from config if available" do
44
+ File.should_receive(:read).with(File.expand_path("~/.prj"))
45
+ Prj::App.new(@sink, ["something"]).run.should == 0
46
+ end
47
+
48
+ def with_projects_root(root)
49
+ tmp = Prj::App.default_projects_root
50
+ Prj::App.default_projects_root = root
51
+ Prj::App.ignore_config = true
52
+ yield
53
+ Prj::App.default_projects_root = tmp
54
+ Prj::App.ignore_config = false
55
+ end
56
+ end
57
+
58
+
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ require 'prj/filter'
3
+
4
+ describe "Prj::Filter" do
5
+ def filter(letters)
6
+ Prj::Filter.new(letters)
7
+ end
8
+
9
+ describe "#dispersion" do
10
+ it "should calculate dispersion as difference between character positions except first character position" do
11
+ filter(%w(f o)).dispersion("foo bar").should == 0
12
+ filter(%w(f b)).dispersion("foo bar").should == 3
13
+ end
14
+ end
15
+
16
+ describe "#filter" do
17
+ it "filters based on ordered occurrences" do
18
+ dirs = ["foos", "bars", "quxs"]
19
+ filter(%w(f o)).filter(dirs).should == ["foos"]
20
+ end
21
+
22
+ it "counts same chars correctly" do
23
+ dirs = ["solar studio", "mars maraphone", "way too complex", "totla madness"]
24
+ filter(%w(s s)).filter(dirs).should =~ ["solar studio", "totla madness"]
25
+ filter(%w(o o)).filter(dirs).should =~ ["solar studio", "way too complex"]
26
+ filter(%w(a a)).filter(dirs).should =~ ["mars maraphone", "totla madness"]
27
+ filter(%w(r o)).filter(dirs).should =~ ["solar studio", "mars maraphone"]
28
+ filter(%w(x x)).filter(dirs).should == []
29
+ filter(%w(m a d n e)).filter(dirs).should =~ ["totla madness"]
30
+ end
31
+
32
+ it "sorts by dispersion" do
33
+ dirs = ["/koans/", "/omniauth/", "/ruby-download/"]
34
+ filter(%w(o a)).filter(dirs).should == ["/koans/", "/omniauth/", "/ruby-download/"]
35
+ end
36
+
37
+ it "sorts by length for lines when dispersion is same" do
38
+ dirs = ["/franchise/", "/granny/"]
39
+ filter(%w(a n)).filter(dirs).should == ["/granny/", "/franchise/"]
40
+
41
+ dirs = ["/granny/", "/franchise/"]
42
+ filter(%w(a n)).filter(dirs).should == ["/granny/", "/franchise/"]
43
+ end
44
+ end
45
+
46
+ end
47
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'fakefs/spec_helpers'
3
+ require 'prj/finder'
4
+
5
+ describe "Prj::Finder" do
6
+ include FakeFS::SpecHelpers
7
+
8
+ let(:root) { "~/projects" }
9
+
10
+ it "finds directories containing .git/ scoped to given root and returns their relative paths" do
11
+ make_directories(root, "/foo/.git", "/bar/qux", "/baz/.git")
12
+ Prj::Finder.new(root).find_project_directories.should =~ ["/foo", "/baz"]
13
+ end
14
+
15
+ it "does not find directories nested under directory containing .git/" do
16
+ make_directories(root, "/foo/.git", "/foo/bar", "/foo/baz/.git")
17
+ Prj::Finder.new(root).find_project_directories.should =~ ["/foo"]
18
+ end
19
+
20
+ def make_directories(root, *directories)
21
+ directories.each do |d|
22
+ FakeFS::FileSystem.add(File.join(root, d))
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ if ENV['COVERAGE']
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ add_filter "/spec/"
8
+
9
+ add_filter do |f|
10
+ f.lines.count < 5
11
+ end
12
+ end
13
+ end
14
+
15
+ require 'rspec'
16
+ require 'rspec/autorun'
17
+
18
+ $:.unshift File.expand_path('../../lib', __FILE__)
19
+
20
+ Dir[File.dirname(__FILE__) + "/support/matchers/*.rb"].each {|f| require f}
21
+
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prj
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vladimir Yarotsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.11'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.11'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.9'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.9'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.6'
62
+ - !ruby/object:Gem::Dependency
63
+ name: fakefs
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.4'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.4'
78
+ description: ! ' Prj is an utility to quickly go to project directory using fuzzy
79
+ matching
80
+
81
+ '
82
+ email: vladimir.yarotksy@gmail.com
83
+ executables:
84
+ - prj
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - bin/prj
89
+ - lib/prj/app.rb
90
+ - lib/prj/filter.rb
91
+ - lib/prj/finder.rb
92
+ - lib/prj.rb
93
+ - spec/acceptance/app_spec.rb
94
+ - spec/lib/prj/filter_spec.rb
95
+ - spec/lib/prj/finder_spec.rb
96
+ - spec/spec_helper.rb
97
+ - Gemfile
98
+ - Gemfile.lock
99
+ - Rakefile
100
+ - LICENSE.txt
101
+ - README.md
102
+ - VERSION
103
+ homepage: http://github.com/v-yarotsky/prj
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: 1.3.6
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.24
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Fuzzy-matching project finder
128
+ test_files: []
129
+ has_rdoc: