Class: Gemify

Inherits:
Object
  • Object
show all
Defined in:
lib/gemify.rb,
lib/gemify/vcs.rb

Defined Under Namespace

Classes: Exit, VCS

Constant Summary collapse

SETTINGS =
".gemified"
MANIFEST =
["MANIFEST", "Manifest.txt", ".manifest"]
REQUIRED =
[:name, :summary, :version]
OPTIONAL =
[:author, :email, :homepage, :rubyforge_project, :has_rdoc, :dependencies]
ALL =
REQUIRED+OPTIONAL
REPLACE =
{
  :rubyforge_project => "RubyForge project",
  :has_rdoc => "documentation",
}
TYPE =
{
  :has_rdoc => :boolean,
  :dependencies => :array,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGemify

Returns a new instance of Gemify.



25
26
27
28
29
30
31
32
# File 'lib/gemify.rb', line 25

def initialize
  @settings = {}
  @from_vcs = false

  if File.exists? SETTINGS
    load!
  end
end

Instance Attribute Details

#from_vcsObject

Returns the value of attribute from_vcs.



8
9
10
# File 'lib/gemify.rb', line 8

def from_vcs
  @from_vcs
end

Instance Method Details

#binObject



44
45
46
# File 'lib/gemify.rb', line 44

def bin
  files.select { |file| file =~ /^bin\// }
end

#build!Object

Special tasks

Raises:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/gemify.rb', line 102

def build!
  require_files!
  Gem::Builder.new(Gem::Specification.new do |s|
    (@settings.delete(:dependencies)||[]).each do |dep|
      s.add_dependency dep
    end
    
    @settings.each { |key, value| s.send("#{key}=",value) }
    s.platform = Gem::Platform::RUBY
    s.files = files
    s.bindir = "bin"
    s.require_path = "lib"

    unless bin.empty?
      s.executables = bin.map{|x|x[4..-1]}
    end
    
    unless extensions.empty?
      s.extensions = extensions
    end
    
  end).build
  raise Exit
end

#build_task(m) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/gemify.rb', line 143

def build_task(m)
  index = (ALL.index(m)||0)+1
  unless type(m) == :boolean
    verb,now = if @settings.keys.include?(m)
      ["Change"," = " + inspect_setting(m)]
    else
      ["Set",""]
    end
  else
    verb, now = ["Toogle"," = " + inspect_setting(m)]
  end
  req = REQUIRED.include?(m) ? " (required)" : ""
  "#{index}) #{verb} #{show(m)}#{req}#{now}"
end

#clearObject



158
159
160
# File 'lib/gemify.rb', line 158

def clear
  system("cls") || print("\e[H\e[2J")
end

#extensionsObject



48
49
50
# File 'lib/gemify.rb', line 48

def extensions
  files.select { |file| file =~ /extconf\.rb$/ }
end

#filesObject



34
35
36
37
38
39
40
41
42
# File 'lib/gemify.rb', line 34

def files
  @files ||= if @from_vcs
    VCS.files
  elsif m=MANIFEST.detect{|x|File.exist?(x)}                 
    File.read(m).split(/\r?\n/)
  else
    VCS.files(:unknown)
  end
end

#getsObject



180
181
182
183
# File 'lib/gemify.rb', line 180

def gets
  print("> ")
  $stdin.gets.strip
end

#inspect_setting(m) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
# File 'lib/gemify.rb', line 205

def inspect_setting(m)
  i=@settings[m]     
  case type(m)
  when :array
    i.join(", ")
  when :boolean
    (i||false).to_s
  when :string
    i.to_s
  end
end

#load!Object



127
128
129
130
131
132
133
134
# File 'lib/gemify.rb', line 127

def load!
  @settings = YAML.load(File.read(SETTINGS))
  @settings.keys.each do |key|
    @settings[key] = value(key)
  end
rescue Errno::EACCES
  @result = "Can't read #{SETTINGS}"
end

#mainObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gemify.rb', line 52

def main
  loop do
    menu
    puts @result if @result
    @result = nil
    l=(o=gets).downcase[0]
    i=o.to_i
    
    case l
    when ?x
      raise Exit
    when ?b
      build!
    when ?s
      save!
      next
    when ?i
      @result = "Included files:#{$/}" + files.join($/)
      next
    end      
    
    if (1..ALL.length).include? i
      set(ALL[i-1])
      next
    end
    
    @result = "Can't find the task named '#{o}'"
  end
end


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/gemify.rb', line 82

def menu
  require_files!
  clear
  puts "Welcome to Gemify!"
  puts
  puts "Which task would you like to invoke?"
  ALL.each do |m|
    puts build_task(m)
  end
  puts
  puts "s) Save"
  puts "i) Show included files"
  puts
  puts "b) Build gem"
  puts "x) Exit"
  puts
end

#save!Object



136
137
138
139
140
141
# File 'lib/gemify.rb', line 136

def save!
  File.open(SETTINGS,"w"){|f|f << YAML.dump(@settings)}
  @result = "Saved!"
rescue Errno::EACCES
  @result = "Can't write to #{SETTINGS}"
end

#set(m) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/gemify.rb', line 162

def set(m)
  menu
  case type(m)
  when :array
    puts "Split by ENTER and press ENTER twice when you're done"
    puts "> #{show(m).capitalize}: "
    @settings[m] = $stdin.gets($/*2).strip.split($/)
    @settings.delete(m) if @settings[m].empty?
  when :boolean
    @settings[m] = !@settings[m]
  when :string
    print "> #{show(m).capitalize}: "
    @settings[m] = $stdin.gets.strip
    @settings.delete(m) if @settings[m].empty?
  end
  @result = "Updated '#{m}'"
end

#show(m) ⇒ Object



185
186
187
# File 'lib/gemify.rb', line 185

def show(m)
  REPLACE[m]||m.to_s
end

#type(m) ⇒ Object



189
190
191
# File 'lib/gemify.rb', line 189

def type(m)
  TYPE[m]||:string
end

#value(m) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/gemify.rb', line 193

def value(m)
  i=@settings[m]
  case type(m)
  when :array
    i.to_a
  when :boolean
    !!i
  when :string
    i.to_s
  end
end