Class: TM::Bundle

Inherits:
Object
  • Object
show all
Defined in:
lib/tmb/bundle.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result = nil, options = {}) ⇒ Bundle

Returns a new instance of Bundle.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/tmb/bundle.rb', line 123

def initialize(result=nil, options={})
  if result.nil?
    @repository = options[:repo]
    @result = {
      "name" => self.class.name_from_repo(@repository), 
      "bundle_name" => options[:bundle_name],
      "url" => @repository 
    }
  else
    @before_hooks =      @result     = result
    @repository = git_repo
  end
  #before(:git_install_script, :update_db)
end

Instance Attribute Details

#before_hooksObject

Returns the value of attribute before_hooks.



108
109
110
# File 'lib/tmb/bundle.rb', line 108

def before_hooks
  @before_hooks
end

#install_outputObject

Returns the value of attribute install_output.



108
109
110
# File 'lib/tmb/bundle.rb', line 108

def install_output
  @install_output
end

#outputObject

Returns the value of attribute output.



108
109
110
# File 'lib/tmb/bundle.rb', line 108

def output
  @output
end

#repositoryObject

Returns the value of attribute repository.



108
109
110
# File 'lib/tmb/bundle.rb', line 108

def repository
  @repository
end

#resultObject

Returns the value of attribute result.



108
109
110
# File 'lib/tmb/bundle.rb', line 108

def result
  @result
end

Class Method Details

.before(instance, hook, method, args = {}) ⇒ Object



110
111
112
# File 'lib/tmb/bundle.rb', line 110

def self.before(instance, hook, method, args={})
  instance.before_hooks[hook] = {:method => method, :args => args}
end

.db(settings = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/tmb/bundle.rb', line 150

def self.db(settings=nil)
  #puts "with settings...#{settings.inspect}"
  if !File.exist?(db_filename)
    puts "DB doesn't exist (#{db_filename}).  Skipping addition of bundle to install db."
    return 
  end
  f = File.open(db_filename,File::RDWR|File::CREAT)
  unless settings.nil? or !( settings.class.name =~ /^Hash/ )
    parsed = settings.merge self.existing_installdb
    f.puts YAML::dump( parsed )
  end
  db_content = f.read
  f.close
  self.existing_installdb
end

.db_filenameObject



142
143
144
# File 'lib/tmb/bundle.rb', line 142

def self.db_filename
  File.join(SettingsDirectory, DB)
end

.existing_installdbObject



146
147
148
# File 'lib/tmb/bundle.rb', line 146

def self.existing_installdb
  ( YAML.load File.read( self.db_filename ) rescue nil ) || {}
end

.infoObject



166
167
168
# File 'lib/tmb/bundle.rb', line 166

def self.info
  File.read(db_filename)
end

.installedObject



138
139
140
# File 'lib/tmb/bundle.rb', line 138

def self.installed
  Dir.glob("#{BundleDirectory}/*.tmbundle").sort{|a,b| a.downcase <=> b.downcase }
end

.installed_bundle_filenamesObject



170
171
172
173
174
# File 'lib/tmb/bundle.rb', line 170

def self.installed_bundle_filenames
  installed.map do |b|
    File.basename(b)
  end
end

.installed_bundle_titlesObject



176
177
178
179
180
# File 'lib/tmb/bundle.rb', line 176

def self.installed_bundle_titles
  installed.map do |b|
    TM::BundleListPrefix + File.basename(b).gsub(/\.tmbundle$/,'')
  end
end

.listObject



182
183
184
185
186
# File 'lib/tmb/bundle.rb', line 182

def self.list
  installed.each do |b|
    puts  TM::BundleListPrefix + File.basename(b)
  end
end

.name_from_repo(repository_url) ⇒ Object



206
207
208
# File 'lib/tmb/bundle.rb', line 206

def self.name_from_repo(repository_url)
  repository_url.scan(/(\w\-0-9)\.git$/)
end

.select(bundle) ⇒ Object



188
189
190
# File 'lib/tmb/bundle.rb', line 188

def self.select(bundle)
  installed.select{|b| b =~ Regexp.new(bundle + ".*\.tmbundle$") }
end

.uninstall(bundle) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/tmb/bundle.rb', line 192

def self.uninstall(bundle)
  if bundle.nil? || bundle == ""
    puts "We couldn't find an appropriate bundle to uninstall...Exiting"
    return 
  end
  bundle_dir = File.basename(bundle)
  #{}` rm -Rf #{ File.join(BundleDirectory, bundle_dir) }`
  FileUtils.rm_r File.join(BundleDirectory, bundle_dir)
end

Instance Method Details

#archive_install_scriptObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/tmb/bundle.rb', line 360

def archive_install_script
  <<-eos
    #{common_install_script}
    cd $bundle_dir
    if [[ -n $bundle_name ]]
    then
      rm -R "$bundle_dir/$bundle_name"*
    fi
    curl -o "$bundle_dir/$file_name" $1
    #{ smart_extract_script('$bundle_dir/$file_name')}
    bundle=$(find $bundle_dir/$bundle_name | grep -P "tmbundle$")
    if [[ -n $bundle ]]
    then
      cp -R $bundle $bundle_dir
    fi
    non_bundles=$(find $bundle_dir -d 1 | grep -v -P "tmbundle$|^\.")
    echo $non_bundles | xargs -Ixxx rm -Rf "xxx"
    cd $current
    osascript -e 'tell app "TextMate" to reload bundles'
  eos
end

#as_selection(index) ⇒ Object



291
292
293
# File 'lib/tmb/bundle.rb', line 291

def as_selection(index)
   "#{(index + 1).to_s}) #{git_repo.ljust(60)} \033[1m#{short_stats}\033[0m"
end

#before(hook, method, args = nil) ⇒ Object



114
115
116
117
# File 'lib/tmb/bundle.rb', line 114

def before(hook, method, args=nil)
  self.before_hooks[hook] ||= {}
  self.before_hooks[hook] = { :method => method, :args => args }
end

#bundle_exists?Boolean

Returns:

  • (Boolean)


415
416
417
# File 'lib/tmb/bundle.rb', line 415

def bundle_exists?
  File.exists?(destination)
end

#bundle_nameObject



214
215
216
# File 'lib/tmb/bundle.rb', line 214

def bundle_name
  result["bundle_name"] || name.gsub("tmbundle",'').gsub(/^[[:punct:]]+|[[:punct:]]+$/,'')
end

#bundle_reload_scriptObject



333
334
335
# File 'lib/tmb/bundle.rb', line 333

def bundle_reload_script
  "osascript -e 'tell app \"TextMate\" to reload bundles'"
end

#common_install_scriptObject



315
316
317
318
319
320
321
322
323
324
# File 'lib/tmb/bundle.rb', line 315

def common_install_script
  <<-eos
    bundle_dir="#{BundleDirectory}"
    mkdir -p "$bundle_dir"
    file_name=$(echo -e #{@repository} | grep -o -P "[-\w\.]+$")
    bundle_name=$(echo -e $file_name |  sed -E -e 's/\.[a-zA-Z0-9]+$//g' -e 's/\.tmbundle//g')
    dest="#{destination}"
    rm -Rf "$dest"
  eos
end

#db_hashObject



402
403
404
# File 'lib/tmb/bundle.rb', line 402

def db_hash
  { bundle_name => {"url" => repository, "dir" => destination, "installed" => Time.now.to_s, "sha1" => hash}}
end

#destinationObject



311
312
313
# File 'lib/tmb/bundle.rb', line 311

def destination
  File.join(BundleDirectory, "#{bundle_name}.tmbundle")
end

#display_key(key, options = {}) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/tmb/bundle.rb', line 222

def display_key(key, options={})
  defaults = {:ljust => Justify, :rjust => Justify, :delimiter => Delimiter, :key_prefix => "", :key_suffix => ""}
  options = defaults.merge options

  if options[:bold_key]
    options[:key_prefix] = "\033[1m" + options[:key_prefix]
    options[:key_suffix] = options[:key_suffix] +"\033[0m"
  end
  if options[:title].nil? || options[:title].strip.length == 0
    options[:title] = ""
    options[:delimiter] = ""#(" " * options[:delimiter].length)
    options[:ljust] = 0
  end
  options[:title] ||= key.to_s
  options[:key_prefix] + (options[:title].capitalize + options[:delimiter]).ljust(options[:ljust]) + options[:key_suffix]
end

#display_keypair(key, options = {}) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/tmb/bundle.rb', line 239

def display_keypair(key, options={})
  defaults = {:title => key.to_s, :ljust => Justify, :rjust => Justify, :delimiter => Delimiter, :key_prefix => "", :key_suffix => "", :value_prefix => "", :value_suffix => ""}
  options = defaults.merge(options)
  if options[:bold]
    options[:bold_value] ||= true
    options[:bold_key] ||= true
  end
  if options[:bold_value]
    options[:value_prefix] = "\033[1m" + options[:value_prefix]
    options[:value_suffix] = options[:value_suffix] +"\033[0m"
  end
  [
    display_key(options[:title], options),
    options[:value_prefix] + ((options[:value] || (key.is_a?(Symbol) && self.methods.include?(key.to_s)) ? self.send(key) : @result[key.to_s]).to_s) + options[:value_suffix]
  ].compact.reject{|s| s.strip == "" }.join
end

#display_mapObject



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/tmb/bundle.rb', line 295

def display_map
  [
    {:v=> "name", :bold => true, :indent => true, :title => "", :value_prefix => "\e[1;32m"},
    {:v => "username", :title => "Author", :bold_value => true, :newline => 1},
    {:v => "homepage"},
    {:v => :repository, :bold_value => true},
    {:v => :stats, :delimiter => "",  :title => ""},
    {:v => "created_at", :newline => 1},
    {:v=> "description", :indent => false, :newline => 2, :title => "", :wrap => true}
    ]
end

#display_value(key, options = {}) ⇒ Object



256
257
258
# File 'lib/tmb/bundle.rb', line 256

def display_value(key, options={})
  display_keypair(key, options)
end

#extended_display(key, options = {}) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/tmb/bundle.rb', line 260

def extended_display(key, options={})
  #options[:indent] ||= true
  ed = display_value(key, options).to_s
  unless options[:indent] == false
    ed = ed.indent
  end
  if options[:wrap]
    ed = ed.text_wrap
  end
  if options[:newline]
    ed = ("\n" * options[:newline]) + ed
  end
  ed
end

#git_install_scriptObject



326
327
328
329
330
331
# File 'lib/tmb/bundle.rb', line 326

def git_install_script
  <<-eos
    #{common_install_script}
    git clone #{@repository} "$dest"
  eos
end

#git_repoObject



218
219
220
# File 'lib/tmb/bundle.rb', line 218

def git_repo
   "https://github.com/#{@result["username"]}/#{@result["name"]}.git"
end

#git_repo?Boolean

Returns:

  • (Boolean)


382
383
384
# File 'lib/tmb/bundle.rb', line 382

def git_repo?
  File.exists?(File.join(destination,".git"))
end

#hash(digester = "sha1") ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/tmb/bundle.rb', line 386

def hash(digester="sha1")
  if git_repo?
    master_file = File.join(destination,".git","refs","heads","master")
    begin
      op = File.read(master_file)
    rescue => e
      puts "An error occurred while attempting to read #{master_file}...skipping\n   #{e.message}"
      `ls "#{ File.dirname(master_file) }"`
      op = ""
    end
  else
    op = IO.popen("cat `ls -F #{destination} | grep -v -E '\/$'` | #{digester} | tr '\n' ''").read
  end
  op.strip
end

#install(prompt_on_reinstall = true) ⇒ Object



419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/tmb/bundle.rb', line 419

def install(prompt_on_reinstall=true)
  if bundle_exists? && prompt_on_reinstall
    puts "That bundle already appears to be installed at #{destination}.\n\nReinstall? [Yn]"
    answer = STDIN.gets.chomp
    exit if answer == "n"
  end
  puts "#{prompt_on_reinstall ? 'Installing' : 'Updating bundle'} from #{@repository}"
  run_script(:git_install_script)
  puts "Bundle installed to #{destination}.  \n\nReloading bundles..."
  run_script(:bundle_reload_script)
  puts "All done!"
  update_db
end

#nameObject



210
211
212
# File 'lib/tmb/bundle.rb', line 210

def name
  result["name"] || repository.scan(/(\w\-0-9)\.git$/)
end

#popularityObject



283
284
285
# File 'lib/tmb/bundle.rb', line 283

def popularity
  result["watchers"].to_i || 0
end

#run_before(method) ⇒ Object



119
120
121
# File 'lib/tmb/bundle.rb', line 119

def run_before(method)
  self.send(self.before_hooks[method][:method], self.before_hooks[method][:args]) if self.before_hooks[method]
end

#run_script(method) ⇒ Object



411
412
413
# File 'lib/tmb/bundle.rb', line 411

def run_script(method)
  @output = IO.popen(self.send(method)).read
end

#short_resultObject



202
203
204
# File 'lib/tmb/bundle.rb', line 202

def short_result
  {:description => result["description"], :repo => repository }
end

#short_statsObject



287
288
289
# File 'lib/tmb/bundle.rb', line 287

def short_stats
  "(" + ["followers", "forks", "watchers"].map{|s| result[s.to_s].to_s + " " + s.to_s}.join(", ") + ")"
end

#smart_extract_script(archive) ⇒ Object



337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/tmb/bundle.rb', line 337

def smart_extract_script(archive)
  <<-eos
    arch=#{archive}
    if [ -f $arch ]; then
        case $arch in
            *.tar.bz2)  tar -jxvf $arch        ;;
            *.tar.gz)   tar -zxvf $arch        ;;
            *.bz2)      bunzip2 $arch          ;;
            *.dmg)      hdiutil mount $arch    ;;
            *.gz)       gunzip $arch           ;;
            *.tar)      tar -xvf $arch         ;;
            *.tbz2)     tar -jxvf $arch        ;;
            *.tgz)      tar -zxvf $arch        ;;
            *.zip)      unzip $arch            ;;
            *.Z)        uncompress $arch       ;;
            *)          echo "'$arch' cannot be extracted/mounted via smartextract()" ;;
        esac
    else
        echo "'$arch' is not a valid file"
    fi
  eos
end

#statsObject



275
276
277
278
279
280
281
# File 'lib/tmb/bundle.rb', line 275

def stats
  "\n" + [
    display_value(:followers, :ljust => Justify ),
    display_value(:forks, :ljust => Justify ),
    display_value(:watchers, :ljust => Justify )
  ].map{|v| v.indent(" " * (IndentSpacing + Justify))}.join("\n")
end

#to_sObject



307
308
309
# File 'lib/tmb/bundle.rb', line 307

def to_s
  display_map.map{|d| extended_display(d.delete(:v), d) }.join("\n") + "\n"
end

#update_dbObject



406
407
408
409
# File 'lib/tmb/bundle.rb', line 406

def update_db
  puts "Updating install db"
  self.class.db(db_hash)
end