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



67
68
69
# File 'lib/jeweler/gemspec_helper.rb', line 67

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

#normalize_files(array_name) ⇒ Object



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

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

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

#parseObject



43
44
45
46
47
48
# File 'lib/jeweler/gemspec_helper.rb', line 43

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

#pathObject



37
38
39
40
41
# File 'lib/jeweler/gemspec_helper.rb', line 37

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



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

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

#update_version(version) ⇒ Object



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

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
29
30
31
32
33
34
35
# File 'lib/jeweler/gemspec_helper.rb', line 23

def write
  normalize_files(:files)
  normalize_files(:files)
  normalize_files(:extra_rdoc_files)

  File.open(path, 'w') do |f|
    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)
    f.write gemspec_ruby
  end 
end