Class: Jeweler::GemSpecHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/jeweler/gemspec_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec, base_dir = nil) {|spec| ... } ⇒ GemSpecHelper

Returns a new instance of GemSpecHelper.

Yields:



7
8
9
10
11
12
# File 'lib/jeweler/gemspec_helper.rb', line 7

def initialize(spec, base_dir = nil)
  self.spec = spec
  self.base_dir = base_dir || ''

  yield spec if block_given?
end

Instance Attribute Details

#base_dirObject

Returns the value of attribute base_dir.



5
6
7
# File 'lib/jeweler/gemspec_helper.rb', line 5

def base_dir
  @base_dir
end

#specObject

Returns the value of attribute spec.



5
6
7
# File 'lib/jeweler/gemspec_helper.rb', line 5

def spec
  @spec
end

Instance Method Details

#gem_pathObject



78
79
80
# File 'lib/jeweler/gemspec_helper.rb', line 78

def gem_path
  File.join(@base_dir, 'pkg', parse.file_name)
end

#has_version?Boolean

Checks whether it uses the version helper or the users defined version.

Returns:

  • (Boolean)


87
88
89
# File 'lib/jeweler/gemspec_helper.rb', line 87

def has_version?
  !@spec.version.nil?
end

#normalize_files(array_attribute) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/jeweler/gemspec_helper.rb', line 60

def normalize_files(array_attribute)
  array = @spec.send(array_attribute)
  # only keep files, no directories, and sort
  array = array.select do |path|
    File.file? File.join(@base_dir, path)
  end.sort

  @spec.send("#{array_attribute}=", array)
end

#parseObject



53
54
55
56
57
58
# File 'lib/jeweler/gemspec_helper.rb', line 53

def parse
  data = self.to_ruby
  parsed_gemspec = nil
  Thread.new { parsed_gemspec = eval("$SAFE = 3\n#{data}", binding, path) }.join
  parsed_gemspec
end

#pathObject



47
48
49
50
51
# File 'lib/jeweler/gemspec_helper.rb', line 47

def path
  denormalized_path = File.join(@base_dir, "#{@spec.name}.gemspec")
  absolute_path = File.expand_path(denormalized_path)
  absolute_path.gsub(Dir.getwd + File::SEPARATOR, '') 
end

#prettyify_array(gemspec_ruby, array_name) ⇒ Object

Adds extra space when outputting an array. This helps create better version control diffs, because otherwise it is all on the same line.



71
72
73
74
75
76
# File 'lib/jeweler/gemspec_helper.rb', line 71

def prettyify_array(gemspec_ruby, array_name)
  gemspec_ruby.gsub(/s\.#{array_name.to_s} = \[.+?\]/) do |match|
    leadin, files = match[0..-2].split("[")
    leadin + "[\n    #{files.split(",").join(",\n    ")}\n  ]"
  end
end

#to_rubyObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jeweler/gemspec_helper.rb', line 30

def to_ruby
  normalize_files(:files)
  normalize_files(:files)
  normalize_files(:extra_rdoc_files)

  gemspec_ruby = @spec.to_ruby
  gemspec_ruby = prettyify_array(gemspec_ruby, :files)
  gemspec_ruby = prettyify_array(gemspec_ruby, :test_files)
  gemspec_ruby = prettyify_array(gemspec_ruby, :extra_rdoc_files)
  gemspec_ruby = <<-END
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in #{Rake.application.rakefile}, and run the gemspec command
#{gemspec_ruby}
    END
end

#update_version(version) ⇒ Object



82
83
84
# File 'lib/jeweler/gemspec_helper.rb', line 82

def update_version(version)
  @spec.version = version.to_s
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
# File 'lib/jeweler/gemspec_helper.rb', line 14

def valid?
  begin
    parse
    true
  rescue
    false
  end
end

#writeObject



23
24
25
26
27
28
# File 'lib/jeweler/gemspec_helper.rb', line 23

def write

  File.open(path, 'w') do |f|
    f.write self.to_ruby
  end 
end