Class: Shoes::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/shoes/setup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(script, &blk) ⇒ Setup



85
86
87
88
89
90
91
92
# File 'lib/shoes/setup.rb', line 85

def initialize(script, &blk)
  @steps = []
  @script = script
  instance_eval &blk
  unless no_steps?
    app = self.class.setup_app(self)
  end
end

Instance Attribute Details

#scriptObject

Returns the value of attribute script



83
84
85
# File 'lib/shoes/setup.rb', line 83

def script
  @script
end

#stepsObject

Returns the value of attribute steps



83
84
85
# File 'lib/shoes/setup.rb', line 83

def steps
  @steps
end

Class Method Details

.gem_resetObject



32
33
34
# File 'lib/shoes/setup.rb', line 32

def self.gem_reset
  Gem.use_paths(GEM_DIR, [GEM_DIR, GEM_CENTRAL_DIR])
end

.initObject



25
26
27
28
29
30
# File 'lib/shoes/setup.rb', line 25

def self.init
  gem_reset
  Gem::Specification.find_by_name('sources')
rescue Gem::LoadError
  install_sources
end

.install_sourcesObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/shoes/setup.rb', line 207

def self.install_sources
  require 'base64'
  sources_gem = File.join(LIB_DIR, "sources-0.0.1.gem")
  File.open(sources_gem, "wb") do |f|
    f << Base64.decode64( <<-GEM.gsub(/^ +/, '') )
      ZGF0YS50YXIuZ3oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAADAwMDA2NDQAMDAwMDAwMAAwMDAwMDAwADAwMDAwMDAwMjYw
      ADAwMDAwMDAwMDAwADAxMzMxNQAgMAA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AAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
      AAAAAAAAAAAAAAAA
    GEM
  end
  Gem::Installer.new(sources_gem).install()
end

.setup_app(setup) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
81
# File 'lib/shoes/setup.rb', line 36

def self.setup_app(setup)
  appt = "Setting up for #{setup.script}"
  Shoes.app :width => 370, :height => 158, :resizable => false, :title => appt do
    background "#EEE".."#9AA"
    image :top => 0, :left => 0 do
      stroke "#FFF"; strokewidth 0.1
      (0..158).step(3) { |i| line 0, i, 370, i }
    end
    @pulse = stack :top => 0, :left => 0
    @logo = image "#{DIR}/static/shoes-icon-blue.png", :top => -20, :right => -20
    stack :margin => 18 do
      title "Shoes Setup", :size => 12, :weight => "bold", :margin => 0
      para "Preparing #{setup.script}", :size => 8, :margin => 0, :margin_top => 8, :width => 220
      @pr = progress :width => 1.0, :top => 70, :height => 20
      button "Cancel", :top => 98, :left => 0.4 do
        self.close
      end

      start do
        @th =
          Thread.start(self.app) do |app|
            begin
              setup.start(app)
            rescue => e
              puts e.message
            end
          end
      end
    end

    animate 10 do |i|
      i %= 10
      @pulse.clear do
        fill black(0.2 - (i * 0.02))
        strokewidth(3.0 - (i * 0.2))
        stroke rgb(0.7, 0.7, 0.9, 1.0 - (i * 0.1))
        oval(@logo.left - i, @logo.top - i, @logo.width + (i * 2))
      end
      @pr.fraction = $fraction
      if @script
        Shoes.visit(@script)
        close
      end
    end
  end
end

Instance Method Details

#activate_gem(name, version) ⇒ Object



128
129
130
# File 'lib/shoes/setup.rb', line 128

def activate_gem(name, version)
  Gem::Specification.find_by_name(name, version).activate
end

#bundler(options = {}) ⇒ Object

TODO: add GUI

Note: this feature is experimental, and uses private Bundler APIs. Don't expect this to always work, though it seems to for now. Bundler expects to be activated before the rest of your application. Please see the notes here: github.com/shoes/shoes/commit/9114457d487353a0c16e521284ad164835c64b4e#diff-0



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/shoes/setup.rb', line 112

def bundler options = {}
  bundler_version = options[:version] || Gem::Requirement.default
  bundler_file = options[:file] || "Gemfile"
  Gem::Specification.find_by_name( "bundler", bundler_version ).activate
rescue Gem::LoadError
  Gem::DependencyInstaller.new.install("bundler", bundler_version)
ensure
  require 'bundler'
  require 'bundler/cli'
  Bundler::CLI.new(:gemfile => bundler_file).install
end

#gem(name, version = nil) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/shoes/setup.rb', line 98

def gem name, version = nil
  arg = "#{name} #{version}".strip
  name, version = arg.split(/\s+/, 2)
  Gem::Specification.find_by_name(name, version)
rescue Gem::LoadError
  @steps << [:gem, arg]
end

#no_steps?Boolean



94
95
96
# File 'lib/shoes/setup.rb', line 94

def no_steps?
  (@steps.map { |s| s[0] }.uniq - [:source]).empty?
end

#source(uri) ⇒ Object



124
125
126
# File 'lib/shoes/setup.rb', line 124

def source uri
  @steps << [:source, uri]
end

#start(app) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/shoes/setup.rb', line 132

def start(app)
  old_ui = Gem::DefaultUserInteraction.ui
  ui = Gem::DefaultUserInteraction.ui = Gem::ShoesFace.new(app)
  count, total = 0.5, @steps.length
  ui.progress count, total

  steps.each do |act, arg|
    case act
    when :gem
      name, version = arg.split(/\s+/, 2)
      count += 1
      ui.say "Looking for #{name}"
      begin
        Gem::Specification.find_by_name(name, version)
      rescue Gem::LoadError
        ui.title "Installing #{name}"
        installer = Gem::DependencyInstaller.new
        begin
          installer.install(name, version || Gem::Requirement.default)
          self.class.gem_reset
          activate_gem(name, version)
          ui.say "Finished installing #{name}"
        rescue Object => e
          ui.error "while installing #{name}", e
          raise e
        end
      end
    when :source
      ui.title "Switching Gem servers"
      ui.say "Pulling from #{arg}"
      Gem.sources.clear << arg
      self.class.gem_reset
    end
    ui.progress count, total
  end
  Gem::DefaultUserInteraction.ui = old_ui
  app.instance_variable_set("@script", @script)
end

#svn(dir, save_as = nil, &blk) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/shoes/setup.rb', line 171

def svn(dir, save_as = nil, &blk)
  dir.gsub! /(.)\/*$/, '\1/'
  if save_as.nil? or save_as.empty?
    save_as = File.join(GEM_DIR, 'svn', '1')
    save_as.succ! while File.exists? save_as
  elsif save_as.index(GEM_DIR) != 0
    save_as = File.join(GEM_DIR, 'svn', save_as)
  end
  mkdir_p(save_as)
  puts "** Pulling down #{dir}..."
  svnuri = URI.parse(dir)
  case svnuri.scheme
  when "http", "https"
    REXML::Document.new(svnuri.open { |f| f.read }).
      each_element("/svn/index/*") do |ele|
        fname, href = ele.attributes['name'], ele.attributes['href']
        case ele.name
        when "file"
          puts "- #{dir}#{href}"
          URI.parse("#{dir}#{href}").open do |f|
            File.open(File.join(save_as, fname), 'wb') do |f2|
              f2 << f.read(16384) until f.eof?
            end
          end
        when "dir"
          svn("#{dir}#{href}", File.join(save_as, fname))
        end
      end
  else
    raise NotSupportedByShoes, "Only HTTP addresses are supported by Shoes's Subversion module."
  end
  if blk
    Dir.chdir(save_as, &blk)
  end
end