mactag 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,7 +31,7 @@ Load the gem in **config/environments/development.rb**:
31
31
 
32
32
 
33
33
  ## Rails 3.x
34
- Version 0.1.1 is the latest version supporting Rails 3.x.
34
+ Version 0.2.0 is the latest version supporting Rails 3.x.
35
35
 
36
36
  ### Plugin
37
37
  Install the plugin:
@@ -43,7 +43,7 @@ Install the gem:
43
43
 
44
44
  Load the gem in **Gemfile**:
45
45
  group :development do
46
- gem 'mactag', '0.1.1'
46
+ gem 'mactag', '0.2.0'
47
47
  end
48
48
 
49
49
 
@@ -76,7 +76,7 @@ examples of how to configure Mactag.
76
76
  Mactag::Config.gem_home = '/usr/lib/ruby/gems/1.8/gems'
77
77
  Mactag::Config.binary = 'etags -o TAGS'
78
78
 
79
- Mactag::Table.generate do
79
+ Mactag do # This is "Mactag::Table.generate do" in Rails 2 applications
80
80
  app 'app/**/*.rb', 'lib/*.rb'
81
81
 
82
82
  plugins 'thinking-sphinx', 'whenever'
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -29,7 +29,7 @@
29
29
  #
30
30
  # Example configuration. Change according to your application.
31
31
  #
32
- Mactag::Table.generate do
32
+ Mactag do
33
33
  ##
34
34
  #
35
35
  # Index all ruby files in app recursive and all ruby files directly under lib.
@@ -1,5 +1,7 @@
1
1
  require 'mactag/config'
2
- require 'mactag/table'
2
+ require 'mactag/builder'
3
+ require 'mactag/parser'
4
+ require 'mactag/ctags'
3
5
  require 'mactag/tag'
4
6
 
5
7
  module Mactag
@@ -7,3 +9,7 @@ module Mactag
7
9
  $stderr.puts "Mactag Warning: #{message}"
8
10
  end
9
11
  end
12
+
13
+ def Mactag(&block)
14
+ Mactag::Builder.generate(&block)
15
+ end
@@ -0,0 +1,90 @@
1
+ module Mactag
2
+ class Builder
3
+ def initialize
4
+ @tags = []
5
+ end
6
+
7
+ ##
8
+ #
9
+ # Add +tag+ to list of tags.
10
+ #
11
+ def <<(tags)
12
+ @tags += Array(tags)
13
+ end
14
+
15
+ ##
16
+ #
17
+ # Returns a string with all files that should be tagged. The
18
+ # files are separated with a whitespace.
19
+ #
20
+ def tags
21
+ tags = all
22
+ tags.flatten!
23
+ tags.compact!
24
+ tags.collect! { |file| File.expand_path(file) }
25
+ tags.collect! { |file| Dir.glob(file) }
26
+ tags.uniq!
27
+ tags.join(' ')
28
+ end
29
+
30
+ def all
31
+ @all_tags ||= @tags.collect!(&:tag)
32
+ end
33
+
34
+ def gems?
35
+ all.flatten.compact.any?
36
+ end
37
+
38
+ ##
39
+ #
40
+ # Create the TAGS file.
41
+ #
42
+ def self.create
43
+ unless gem_home_exists?
44
+ Mactag.warn 'Gem home path does not exist on your system'
45
+ end
46
+
47
+ tags = Mactag::Ctags.new(@builder)
48
+ if tags.build
49
+ puts "Successfully generated TAGS file"
50
+ else
51
+ Mactag.warn 'You did not specify anything to tag'
52
+ end
53
+ end
54
+
55
+ ##
56
+ #
57
+ # Generates the TAGS-table.
58
+ #
59
+ # ==== Example
60
+ # Mactag::Builder.generate do
61
+ # app 'app/**/*.rb', 'lib/*.rb'
62
+ #
63
+ # plugins 'thinking-sphinx', 'whenever'
64
+ #
65
+ # gems 'paperclip', 'authlogic'
66
+ # gem 'formtastic', :version => '0.9.7'
67
+ #
68
+ # rails :except => :actionmailer, :version => '2.3.5'
69
+ # end
70
+ #
71
+ # See documentation for the methods *app*, *plugins*, *gems* and
72
+ # *rails* in respective tag class.
73
+ #
74
+ def self.generate(&block)
75
+ @builder = Mactag::Builder.new
76
+
77
+ parser = Mactag::Parser.new(@builder)
78
+ parser.instance_eval(&block)
79
+ end
80
+
81
+ ##
82
+ #
83
+ # Returns true if the specified gem home path exists on the
84
+ # system, false otherwise.
85
+ #
86
+ def self.gem_home_exists?
87
+ File.directory?(Mactag::Config.gem_home)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,23 @@
1
+ require 'rails'
2
+
3
+ module Mactag
4
+ ##
5
+ #
6
+ # Helper class for creating CTags files.
7
+ #
8
+ class Ctags
9
+ def initialize(builder)
10
+ @builder = builder
11
+ end
12
+
13
+ ##
14
+ #
15
+ # Creates the tags file. Returns true if anything was tagged,
16
+ # false otherwise.
17
+ #
18
+ def build
19
+ system "cd #{Rails.root} && #{Mactag::Config.binary} #{@builder.tags}"
20
+ @builder.gems?
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,61 @@
1
+ module Mactag
2
+ ##
3
+ #
4
+ # Parser for builder.
5
+ #
6
+ class Parser
7
+ def initialize(builder)
8
+ @builder = builder
9
+ end
10
+
11
+ ##
12
+ #
13
+ # @see Mactag::Tag::App
14
+ #
15
+ def app(*tags)
16
+ tags.each do |tag|
17
+ @builder << Mactag::Tag::App.new(tag)
18
+ end
19
+ end
20
+
21
+ ##
22
+ #
23
+ # @see Mactag::Tag::Plugin
24
+ #
25
+ def plugin(*plugins)
26
+ if plugins.empty?
27
+ plugins = Mactag::Tag::Plugin.all
28
+ end
29
+
30
+ plugins.each do |plugin|
31
+ @builder << Mactag::Tag::Plugin.new(plugin)
32
+ end
33
+ end
34
+ alias_method :plugins, :plugin
35
+
36
+ ##
37
+ #
38
+ # @see Mactag::Tag::Gem
39
+ #
40
+ def gem(*gems)
41
+ options = gems.extract_options!
42
+
43
+ if gems.empty?
44
+ @builder << Mactag::Tag::Gem.all
45
+ else
46
+ gems.each do |name|
47
+ @builder << Mactag::Tag::Gem.new(name, options[:version])
48
+ end
49
+ end
50
+ end
51
+ alias_method :gems, :gem
52
+
53
+ ##
54
+ #
55
+ # @see Mactag::Tag::Rails
56
+ #
57
+ def rails(options = {})
58
+ @builder << Mactag::Tag::Rails.new(options)
59
+ end
60
+ end
61
+ end
@@ -1,4 +1,3 @@
1
- require 'mactag/tag/parser'
2
1
  require 'mactag/tag/app'
3
2
  require 'mactag/tag/plugin'
4
3
  require 'mactag/tag/gem'
@@ -5,7 +5,7 @@ module Mactag
5
5
  # Tags files in current Rails application.
6
6
  #
7
7
  # ==== Examples
8
- # Mactag::Table.generate do
8
+ # Mactag do
9
9
  # # Tag single file
10
10
  # app 'lib/super_duper.rb'
11
11
  #
@@ -20,10 +20,10 @@ module Mactag
20
20
  # do
21
21
  #
22
22
  class App
23
- attr_reader :files
23
+ attr_reader :tag
24
24
 
25
- def initialize(*files)
26
- @files = files
25
+ def initialize(tag)
26
+ @tag = tag
27
27
  end
28
28
  end
29
29
  end
@@ -5,7 +5,7 @@ module Mactag
5
5
  # Tags ruby gems.
6
6
  #
7
7
  # ==== Examples
8
- # Mactag::Table.generate do
8
+ # Mactag do
9
9
  # # Tag all gems given by *...*
10
10
  # gems
11
11
  #
@@ -20,66 +20,71 @@ module Mactag
20
20
  # do
21
21
  #
22
22
  class Gem
23
- def initialize(*gems)
24
- if gems.blank?
25
- @gems = all
26
- @options = {}
27
- else
28
- @gems = gems
29
- @options = gems.extract_options!
30
- end
31
- end
23
+ autoload :Bundler, 'bundler'
32
24
 
33
- def files
34
- result = []
35
- @gems.each do |gem_name|
36
- if version = @options[:version]
37
- gem = File.join(Mactag::Config.gem_home, "#{gem_name}-#{version}")
38
- else
39
- gem = latest(gem_name)
40
- end
25
+ def initialize(name, version = nil)
26
+ @name = name
27
+ @version = version
28
+ end
41
29
 
42
- if exists?(gem)
43
- result << File.join(gem, "lib", "**", "*.rb")
30
+ def tag
31
+ if exists?
32
+ if @version
33
+ gem = splash
44
34
  else
45
- Mactag.warn "Gem #{gem_name} not found"
35
+ gem = Gem.most_recent(@name)
46
36
  end
37
+ File.join(Mactag::Config.gem_home, gem, 'lib', '**', '*.rb')
38
+ else
39
+ Mactag.warn "Gem #{@name} not found"
47
40
  end
48
- result
49
41
  end
50
42
 
51
-
52
- private
43
+ ##
44
+ #
45
+ # Returns all application gems.
46
+ #
47
+ def self.all
48
+ Bundler.load.specs.collect { |spec| Gem.new(spec.name, spec.version.to_s) }
49
+ end
53
50
 
54
51
  ##
55
52
  #
56
- # Returns the latest version of +gem+. If only one gem, that gem
57
- # is returned.
53
+ # Returns the most recent gem with +name+.
58
54
  #
59
- def latest(gem)
60
- versions = Dir.glob(File.join(Mactag::Config.gem_home, gem) + "-*")
61
- if versions.size == 1
62
- gem = versions.first
63
- else
64
- gem = versions.sort.last
55
+ def self.most_recent(name)
56
+ versions = Dir.glob(File.join(Mactag::Config.gem_home, name) + "-*")
57
+ unless versions.empty?
58
+ if versions.size == 1
59
+ gem = versions.first
60
+ else
61
+ gem = versions.sort.last
62
+ end
63
+ File.basename(gem)
65
64
  end
66
- gem
67
65
  end
68
66
 
67
+
68
+ private
69
+
69
70
  ##
70
71
  #
71
72
  # Returns true if +gem+ exists, false otherwise.
72
73
  #
73
- def exists?(gem)
74
- gem && File.directory?(gem)
74
+ def exists?
75
+ if @version
76
+ File.directory?(File.join(Mactag::Config.gem_home, splash))
77
+ else
78
+ Gem.most_recent(@name)
79
+ end
75
80
  end
76
81
 
77
82
  ##
78
83
  #
79
- # Returns all gems in this application.
84
+ # Returns the gem name, dash, version.
80
85
  #
81
- def all
82
- Bundler.environment.dependencies.select { |d| d.groups.include?(:default) }.collect(&:name)
86
+ def splash
87
+ "#{@name}-#{@version}"
83
88
  end
84
89
  end
85
90
  end
@@ -5,7 +5,7 @@ module Mactag
5
5
  # Tags plugins in current Rails application.
6
6
  #
7
7
  # ==== Examples
8
- # Mactag::Table.generate do
8
+ # Mactag do
9
9
  # # Tag single plugin
10
10
  # plugin 'whenever'
11
11
  #
@@ -19,22 +19,27 @@ module Mactag
19
19
  class Plugin
20
20
  PLUGIN_PATH = File.join('vendor', 'plugins')
21
21
 
22
- def initialize(*plugins)
23
- @plugins = plugins
22
+ def initialize(plugin)
23
+ @plugin = plugin
24
24
  end
25
25
 
26
- def files
27
- return File.join(PLUGIN_PATH, '*', 'lib', '**', '*.rb') if @plugins.empty?
28
-
29
- result = []
30
- @plugins.each do |plugin|
31
- if File.exist?(File.join(PLUGIN_PATH, plugin))
32
- result << File.join(PLUGIN_PATH, plugin, 'lib', '**', '*.rb')
33
- else
34
- Mactag.warn "Plugin #{plugin} not found"
35
- end
26
+ def tag
27
+ if exists?
28
+ return File.join(PLUGIN_PATH, @plugin, 'lib', '**', '*.rb')
29
+ else
30
+ Mactag.warn "Plugin #{@plugin} not found"
36
31
  end
37
- result
32
+ 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
+
41
+ def exists?
42
+ File.directory?(File.join(PLUGIN_PATH, @plugin))
38
43
  end
39
44
  end
40
45
  end
@@ -17,7 +17,7 @@ module Mactag
17
17
  #
18
18
  #
19
19
  # ==== Examples
20
- # Mactag::Table.generate do
20
+ # Mactag do
21
21
  # # Tag all rails packages, latest version
22
22
  # rails
23
23
  #
@@ -58,16 +58,11 @@ module Mactag
58
58
  @except = packagize!(options[:except])
59
59
  end
60
60
 
61
- def files
61
+ def tag
62
62
  result = []
63
63
  packages.each do |package|
64
64
  if PACKAGES.include?(package)
65
- if version = @options[:version]
66
- gem = Gem.new(package.to_s, :version => version)
67
- else
68
- gem = Gem.new(package.to_s)
69
- end
70
- result << gem.files
65
+ result << Gem.new(package.to_s, @options[:version]).tag
71
66
  end
72
67
  end
73
68
  result