class MateBundle

Constants

CAPITALIZATION_EXCEPTIONS
LOCAL_LOCATION

Public Instance Methods

install(name="") click to toggle source
# File bin/matebundle, line 40
def install(name="")
  FileUtils.mkdir_p LOCAL_LOCATION
  puts "Checking out #{name}...\n"
  
  repos = search_bundles(denormalize_github_repo_name(name))
  
  cmd = case repos.size
        when 0
          'echo "Sorry, no such bundle found"'
        when 1
          git_clone(repos.first)
        else
          puts "Multiple bundles with that name found. Please choose which one you want to install:"
          repos.each_with_index do |repo, idx|
            puts "%d: %s by %s" %
            [
              idx + 1,
              normalize_github_repo_name(repo["name"]),
              repo["username"]
            ]
          end
          print "Your choice: "
    
          choice = Integer(STDIN.gets.chomp) rescue nil
          until choice && (0..repos.size).include?(choice-1) do
            print "Sorry, invalid choice. Please enter a valid number or Ctrl+C to stop: "
            choice = Integer(STDIN.gets.chomp) rescue nil
          end
    
          git_clone(repos[choice-1])
        end
        
  %x{#{cmd}}
end
list(name="") click to toggle source
# File bin/matebundle, line 30
def list(name="")
  puts "\n" << "Local Bundles\n\n"
  
  puts Dir["#{LOCAL_LOCATION}/*.tmbundle"].map { |x| x.split("/").last.split(".").first }.
    select { |x| x.downcase.match(name.downcase) }.join("\n")
end
uninstall(name) click to toggle source
# File bin/matebundle, line 91
def uninstall(name)
  bundle_path = "#{LOCAL_LOCATION}/#{name}.tmbundle"
  
  puts "Removing bundle..."
  
  if File.exist? bundle_path
    %x[rm -rf #{bundle_path.shellescape}]
  else
    say "There is no bundle by that name in the system", :red
    exit
  end
end
update() click to toggle source
# File bin/matebundle, line 76
def update
  Dir.new(LOCAL_LOCATION).each do |dir|
    path = File.join(LOCAL_LOCATION, dir)
    
    if !['.', '..'].include?(dir) and File.directory?(path)
      git_path = File.join(path, '.git')
      if File.exist?(git_path)
        puts "Updating #{dir}..."
        %x[git --git-dir=#{git_path.shellescape} --work-tree=#{path.shellescape} pull]
      end
    end
  end
end

Private Instance Methods

bundle_path(repo) click to toggle source
# File bin/matebundle, line 146
def bundle_path(repo)
  bundle_name = normalize_github_repo_name(repo["name"])
  "#{LOCAL_LOCATION}/#{bundle_name}"
end
denormalize_github_repo_name(name) click to toggle source
# File bin/matebundle, line 116
def denormalize_github_repo_name(name)
  # name = name.split(' ').each{|part| part.downcase!}.join(' ').gsub(' ', '-')
  name = name.split(' ').each{|part| part.downcase!}.join('-')
  name += ".tmbundle" unless name =~ /.tmbundle$/
  name
end
git_clone(repo) click to toggle source
# File bin/matebundle, line 151
def git_clone(repo)
  path = bundle_path(repo)
  
  if File.directory?(path)
    say "Sorry, that bundle is already installed. Please uninstall it first.", :red
    exit
  end
  
  puts "Installing #{repo['name']}..."
  
  %[git clone git@github.com:#{URI.escape(repo['full_name'])}.git #{path.shellescape}]
end
normalize_github_repo_name(name) click to toggle source
# File bin/matebundle, line 110
def normalize_github_repo_name(name)
  name = name.gsub(/[\-\.]/, " ").split.each{|part| part.capitalize! unless CAPITALIZATION_EXCEPTIONS.include? part}.join(" ")
  name[-9] = ?. if name =~ / tmbundle$/
  name
end
search_bundles(search_term) click to toggle source
# File bin/matebundle, line 131
def search_bundles(search_term)
  page = 1
  repositories = github_api_search(search_term, page)
  results = []
  until repositories.empty?
    results += repositories.find_all do |repo|
      repo_name = repo["name"].downcase
      repo_name.match(search_term.downcase) and repo_name =~ /tmbundle$/
    end
    page += 1
    repositories = github_api_search(search_term, page)
  end
  results.sort_by { |result| result["name"] }
end