mactag 0.4.0 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ module Mactag
2
+ class MactagError < StandardError
3
+ end
4
+
5
+ class PluginNotFoundError < MactagError
6
+ attr_reader :plugin
7
+
8
+ def initialize(plugin)
9
+ @plugin = plugin
10
+ end
11
+
12
+ def to_s
13
+ "Plugin #{plugin.name} not found"
14
+ end
15
+ end
16
+
17
+ class GemNotFoundError < MactagError
18
+ attr_reader :gem
19
+
20
+ def initialize(gem)
21
+ @gem = gem
22
+ end
23
+
24
+ def to_s
25
+ if gem.version
26
+ "Gem #{gem.name} with version #{gem.version} not found"
27
+ else
28
+ "Gem #{gem.name} not found"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -3,5 +3,7 @@ require 'mactag/tag/plugin'
3
3
  require 'mactag/tag/gem'
4
4
  require 'mactag/tag/rails'
5
5
 
6
- module Tag
6
+ module Mactag
7
+ module Tag
8
+ end
7
9
  end
@@ -2,17 +2,17 @@ module Mactag
2
2
  module Tag
3
3
  ##
4
4
  #
5
- # Tags files in current Rails application.
5
+ # Tag files in Rails project.
6
6
  #
7
7
  # ==== Examples
8
8
  # Mactag do
9
9
  # # Tag single file
10
10
  # app 'lib/super_duper.rb'
11
11
  #
12
- # # Tag all files in lib, recursive
12
+ # # Tag all ruby files in lib, recursive
13
13
  # app 'lib/**/*.rb'
14
14
  #
15
- # # Tag all helpers and models
15
+ # # Tag all helper and model ruby files
16
16
  # app 'app/helpers/*.rb', 'app/models/*.rb'
17
17
  #
18
18
  # # Same as above
@@ -20,6 +20,8 @@ module Mactag
20
20
  # do
21
21
  #
22
22
  class Gem
23
+ attr_accessor :name, :version
24
+
23
25
  def initialize(name, version = nil)
24
26
  @name = name
25
27
  @version = version
@@ -27,53 +29,51 @@ module Mactag
27
29
 
28
30
  def tag
29
31
  if exists?
30
- if @version
31
- gem = splash
32
- else
33
- gem = Gem.most_recent(@name)
32
+ unless version
33
+ @version = Mactag::Tag::Gem.last(name)
34
34
  end
35
- File.join(Mactag::Config.gem_home, gem, 'lib', '**', '*.rb')
35
+
36
+ File.join(Mactag::Config.gem_home, combo, 'lib', '**', '*.rb')
36
37
  else
37
- Mactag.warn "Gem #{@version ? splash : @name} not found"
38
+ raise GemNotFoundError.new(self)
38
39
  end
39
40
  end
40
41
 
41
- def self.all
42
- gems = {}
43
- Bundler.load.specs.each do |spec|
44
- gems[spec.name] = spec.version.to_s
45
- end
46
-
47
- default = Bundler.load.dependencies.select { |dependency| dependency.groups.include?(:default) }.collect(&:name)
48
- default.delete('rails')
49
- default.collect { |tmp| Gem.new(tmp, gems[tmp]) }
42
+ def exists?
43
+ Mactag::Tag::Gem.dirs(name).size > 0
50
44
  end
51
45
 
52
- def self.most_recent(name)
53
- versions = Dir.glob(File.join(Mactag::Config.gem_home, name) + "-*")
54
- unless versions.empty?
55
- if versions.size == 1
56
- gem = versions.first
57
- else
58
- gem = versions.sort.last
46
+ class << self
47
+ def all
48
+ # Mactag::Bundler.gems
49
+ bundler = Mactag::Bundler.new
50
+ bundler.gems
51
+ end
52
+
53
+ def last(name)
54
+ dirs = Mactag::Tag::Gem.dirs(name)
55
+ unless dirs.empty?
56
+ if dirs.size == 1
57
+ gem = dirs.first
58
+ else
59
+ gem = dirs.sort.last
60
+ end
61
+ if /-(.+)$/.match(gem)
62
+ $1
63
+ end
59
64
  end
60
- File.basename(gem)
65
+ end
66
+
67
+ def dirs(name)
68
+ Dir.glob(File.join(Mactag::Config.gem_home, "#{name}-*"))
61
69
  end
62
70
  end
63
71
 
64
72
 
65
73
  private
66
74
 
67
- def exists?
68
- if @version
69
- File.directory?(File.join(Mactag::Config.gem_home, splash))
70
- else
71
- Gem.most_recent(@name)
72
- end
73
- end
74
-
75
- def splash
76
- "#{@name}-#{@version}"
75
+ def combo
76
+ "#{name}-#{version}"
77
77
  end
78
78
  end
79
79
  end
@@ -2,7 +2,7 @@ module Mactag
2
2
  module Tag
3
3
  ##
4
4
  #
5
- # Tags plugins in current Rails application.
5
+ # Tag plugins in Rails project.
6
6
  #
7
7
  # ==== Examples
8
8
  # Mactag do
@@ -17,29 +17,35 @@ module Mactag
17
17
  # do
18
18
  #
19
19
  class Plugin
20
- PLUGIN_PATH = File.join('vendor', 'plugins')
20
+ PLUGIN_PATH = %w(vendor plugins)
21
21
 
22
- def initialize(plugin)
23
- @plugin = plugin
22
+ attr_accessor :name
23
+
24
+ def initialize(name)
25
+ @name = name
24
26
  end
25
27
 
26
28
  def tag
27
29
  if exists?
28
- return File.join(PLUGIN_PATH, @plugin, 'lib', '**', '*.rb')
30
+ return File.join(PLUGIN_PATH, name, 'lib', '**', '*.rb')
29
31
  else
30
- Mactag.warn "Plugin #{@plugin} not found"
32
+ raise PluginNotFoundError.new(self)
31
33
  end
32
34
  end
33
-
34
- def self.all
35
- Dir.glob(File.join(PLUGIN_PATH, '*')).collect { |f| File.basename(f) }
36
- end
37
-
38
-
39
- private
40
-
35
+
41
36
  def exists?
42
- File.directory?(File.join(PLUGIN_PATH, @plugin))
37
+ File.directory?(path)
38
+ end
39
+
40
+ def path
41
+ File.join(PLUGIN_PATH, name)
42
+ end
43
+
44
+ def self.all
45
+ pattern = File.join(PLUGIN_PATH, '*')
46
+ Dir.glob(pattern).map do |f|
47
+ File.basename(f)
48
+ end
43
49
  end
44
50
  end
45
51
  end
@@ -41,52 +41,45 @@ module Mactag
41
41
  # do
42
42
  #
43
43
  class Rails
44
- PACKAGES = [:actionmailer, :actionpack, :activemodel, :activerecord, :activeresource, :railties, :activesupport]
45
44
 
46
- def initialize(options)
47
- @options = options
45
+ PACKAGES = %w(actionmailer actionpack activemodel activerecord activeresource activesupport railties)
46
+
47
+ attr_accessor :only, :except, :version
48
48
 
49
- @only = packagize!(options[:only])
50
- @except = packagize!(options[:except])
49
+ def initialize(options)
50
+ @only = packagize(options[:only])
51
+ @except = packagize(options[:except])
52
+ @version = options[:version]
51
53
  end
52
54
 
53
55
  def tag
54
- result = []
55
- packages.each do |package|
56
- if PACKAGES.include?(package)
57
- result << Gem.new(package.to_s, version).tag
58
- end
56
+ packages.inject([]) do |array, package|
57
+ array << Gem.new(package, version).tag
58
+ array
59
59
  end
60
- result
61
60
  end
62
61
 
63
-
64
- private
65
-
66
62
  def packages
67
- result = []
68
- unless @only || @except
69
- result = PACKAGES
63
+ if only || except
64
+ only || PACKAGES - except
70
65
  else
71
- if @only
72
- result = @only
73
- elsif @except
74
- result = PACKAGES - @except
75
- end
66
+ PACKAGES
76
67
  end
77
- result
78
68
  end
79
-
80
- def packagize!(pkgs)
69
+
70
+
71
+ private
72
+
73
+ def packagize(pkgs)
81
74
  return nil if pkgs.blank?
82
75
 
83
- Array(pkgs).collect do |pkg|
84
- "#{pkg}".gsub(/[^a-z]/, '').to_sym
76
+ Array(pkgs).map do |pkg|
77
+ "#{pkg}".gsub(/[^a-z]/, '')
85
78
  end
86
79
  end
87
-
80
+
88
81
  def version
89
- @options[:version] || ::Rails.version
82
+ @version || ::Rails.version
90
83
  end
91
84
  end
92
85
  end
@@ -1,21 +1,8 @@
1
1
  task :environment
2
2
 
3
- def load_configuration
3
+ desc 'Creates a Ctags file'
4
+ task :mactag => :environment do
4
5
  require File.join(Rails.root, 'config', 'mactag')
5
- end
6
-
7
- namespace :mactag do
8
- desc 'Creates Ctags file'
9
- task :create => :environment do
10
- load_configuration
11
-
12
- Mactag::Builder.create
13
- end
14
-
15
- desc 'Starts the Mactag server'
16
- task :server => :environment do
17
- load_configuration
18
6
 
19
- Mactag::Server.start
20
- end
7
+ Mactag::Builder.create
21
8
  end
@@ -10,124 +10,143 @@ describe Mactag::Builder do
10
10
  @tag = Mactag::Tag::App.new('app')
11
11
  end
12
12
 
13
- it 'should add single tag' do
13
+ it 'adds single tag' do
14
14
  @builder << @tag
15
15
 
16
- @builder.instance_variable_get('@tags').should =~ [@tag]
16
+ @builder.instance_variable_get('@tags').should eq([@tag])
17
17
  end
18
18
 
19
- it 'should add multiple tags' do
19
+ it 'adds multiple tags' do
20
20
  @builder << [@tag, @tag]
21
21
 
22
- @builder.instance_variable_get('@tags').should =~ [@tag, @tag]
22
+ @builder.instance_variable_get('@tags').should eq([@tag, @tag])
23
23
  end
24
24
  end
25
25
 
26
26
  describe '#files' do
27
27
  before do
28
- Dir.stub!(:glob).and_return { |file| [file] }
29
- File.stub!(:expand_path).and_return { |file| file }
30
- File.stub!(:directory?).and_return(false)
28
+ Dir.stub(:glob) { |file| [file] }
29
+ File.stub(:expand_path) { |file| file }
30
+ File.stub(:directory?) { false }
31
31
  end
32
32
 
33
- it 'should flatten all files' do
34
- @builder.stub!(:all).and_return([['app'], 'lib'])
35
- @builder.files.should =~ ['app', 'lib']
33
+ it 'flattens all files' do
34
+ @builder.stub(:all) { [['app'], 'lib'] }
35
+ @builder.files.should eq(['app', 'lib'])
36
36
  end
37
37
 
38
- it 'should compact all files' do
39
- @builder.stub!(:all).and_return([nil, 'app', nil, 'lib', nil])
40
- @builder.files.should =~ ['app', 'lib']
38
+ it 'compacts all files' do
39
+ @builder.stub(:all) { [nil, 'app', nil, 'lib', nil] }
40
+ @builder.files.should eq(['app', 'lib'])
41
41
  end
42
42
 
43
- it 'should expand all files' do
44
- @builder.stub!(:all).and_return(['app', 'lib'])
43
+ it 'expands all files' do
44
+ @builder.stub(:all) { ['app', 'lib'] }
45
45
  File.should_receive(:expand_path).with('app')
46
46
  File.should_receive(:expand_path).with('lib')
47
47
  @builder.files
48
48
  end
49
49
 
50
- it 'should glob all files' do
51
- @builder.stub!(:all).and_return(['app', 'lib'])
50
+ it 'globs all files' do
51
+ @builder.stub(:all) { ['app', 'lib'] }
52
52
  Dir.should_receive(:glob).with('app')
53
53
  Dir.should_receive(:glob).with('lib')
54
54
  @builder.files
55
55
  end
56
56
 
57
- it 'should uniquify files' do
58
- @builder.stub!(:all).and_return(['app', 'lib', 'lib', 'app'])
59
- @builder.files.should =~ ['app', 'lib']
57
+ it 'uniquifies files' do
58
+ @builder.stub(:all) { ['app', 'lib', 'lib', 'app'] }
59
+ @builder.files.should eq(['app', 'lib'])
60
60
  end
61
-
62
- it 'should not return directories' do
63
- @builder.stub!(:all).and_return(['app'])
64
- Dir.stub!(:glob).and_return(['app'])
65
- File.stub!(:directory?).and_return(true)
61
+
62
+ it 'does not return directories' do
63
+ @builder.stub(:all) { ['app'] }
64
+ Dir.stub(:glob) { ['app'] }
65
+ File.stub(:directory?) { true }
66
66
  @builder.files.should be_empty
67
67
  end
68
68
  end
69
-
69
+
70
70
  describe '#directories' do
71
- it 'should return all file dirnames' do
72
- @builder.stub!(:files).and_return(['app/models/user.rb', 'lib/validate.rb'])
73
- @builder.directories.should =~ ['app/models', 'lib']
71
+ it 'returns all file dirnames' do
72
+ @builder.stub(:files) { ['app/models/user.rb', 'lib/validate.rb'] }
73
+ @builder.directories.should eq(['app/models', 'lib'])
74
74
  end
75
-
76
- it 'should only return uniq directories' do
77
- @builder.stub!(:files).and_return(['app/models/user.rb', 'app/models/post.rb'])
78
- @builder.directories.should =~ ['app/models']
75
+
76
+ it 'returns uniq directories' do
77
+ @builder.stub(:files) { ['app/models/user.rb', 'app/models/post.rb'] }
78
+ @builder.directories.should eq(['app/models'])
79
79
  end
80
80
  end
81
81
 
82
82
  describe '#all' do
83
- it 'should return all files' do
83
+ it 'returns all files' do
84
84
  @builder << Mactag::Tag::App.new('app')
85
85
  @builder << Mactag::Tag::App.new('lib')
86
86
 
87
- @builder.all.should =~ ['app', 'lib']
87
+ @builder.all.should eq(['app', 'lib'])
88
88
  end
89
89
 
90
- it 'should return empty array when no tags' do
90
+ it 'returns empty array when no tags' do
91
91
  @builder.all.should be_empty
92
92
  end
93
93
  end
94
94
 
95
95
  describe '#gems?' do
96
- it 'should be true when tags exists' do
97
- @builder.stub!(:all).and_return(['app'])
96
+ it 'is true when tags exists' do
97
+ @builder.stub(:all) { ['app'] }
98
+
99
+ @builder.should have_gems
100
+ end
101
+
102
+ it 'is false when no tags exists' do
103
+ @builder.stub(:all) { [] }
98
104
 
99
- @builder.gems?.should be_true
105
+ @builder.should_not have_gems
100
106
  end
107
+ end
101
108
 
102
- it 'should be false when no tags exists' do
103
- @builder.stub!(:all).and_return([])
109
+ describe '#create' do
110
+ it 'raises error when gem home does not exist' do
111
+ Mactag::Builder.stub(:gem_home_exists?) { false }
112
+ proc {
113
+ Mactag::Builder.create
114
+ }.should raise_exception(Mactag::MactagError)
115
+ end
104
116
 
105
- @builder.gems?.should be_false
117
+ it 'raises errors when gem home exists but there are no gems' do
118
+ Mactag::Builder.stub(:gem_home_exists?) { true }
119
+ Mactag::Builder.stub(:builder) { mock(:has_gems? => false) }
120
+ proc {
121
+ Mactag::Builder.create
122
+ }.should raise_exception(Mactag::MactagError)
106
123
  end
107
124
  end
108
125
 
109
126
  describe '#generate' do
110
- it 'should accept a block as argument' do
111
- lambda { Mactag::Builder.generate {} }.should_not raise_exception
127
+ it 'accepts a block as argument' do
128
+ proc {
129
+ Mactag::Builder.generate {}
130
+ }.should_not raise_exception
112
131
  end
113
132
  end
114
133
 
115
134
  describe '#gem_home_exists?' do
116
- it 'should exist when directory exists' do
117
- File.stub!(:directory?).and_return(true)
135
+ it 'exists when directory exists' do
136
+ File.stub(:directory?) { true }
118
137
 
119
138
  Mactag::Builder.gem_home_exists?.should be_true
120
139
  end
121
140
 
122
- it 'should not exist when directory does not exist' do
141
+ it 'does not exist when directory does not exist' do
123
142
  File.stub!(:directory?).and_return(false)
124
143
 
125
144
  Mactag::Builder.gem_home_exists?.should be_false
126
145
  end
127
146
  end
128
-
147
+
129
148
  describe '#builder' do
130
- it 'should return builder instance' do
149
+ it 'returns builder instance' do
131
150
  Mactag::Builder.generate {}
132
151
  Mactag::Builder.builder.should == Mactag::Builder.instance_variable_get('@builder')
133
152
  end