Class: HW::Sources

Inherits:
Thor
  • Object
show all
Extended by:
Actions, Thor::Actions, Thor::Shell
Defined in:
lib/hw/sources.rb

Constant Summary collapse

@@sources =
{}

Class Method Summary collapse

Methods included from Actions

bundle, error, git, header, info, migrate, migration, pbcopy, pbpaste, rake, replace_file, success, warn, worker

Class Method Details

.add(name, source) ⇒ Object



23
24
25
26
27
# File 'lib/hw/sources.rb', line 23

def add name, source
  self.destination_root = "."
  header "Appending #{name} to #{CONFIG_PATH}"
  append_file CONFIG_PATH, "#{name}=#{source}\n"
end

.allObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hw/sources.rb', line 10

def all
  if File.exists? CONFIG_PATH
    File.readlines(CONFIG_PATH).each do |line|
      source                = line.split("=")
      path                  = source[1].strip
      path                  = File.expand_path(path) if local_source?(path)
      @@sources[source[0]]  = path
    end
  end

  @@sources
end

.ensure_defaultsObject



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hw/sources.rb', line 29

def ensure_defaults
  empty_directory DIRECTORY     unless File.exists? DIRECTORY
  empty_directory SOURCES_PATH  unless File.exists? SOURCES_PATH

  unless File.exists? CONFIG_PATH
    header "Adding default source to #{CONFIG_PATH}"

    create_file CONFIG_PATH
    self.class.new.invoke :add_source, ["default", DEFAULT_SOURCE] # self.class.new added else RSpec screws up.    
  end
end

.fetchObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hw/sources.rb', line 41

def fetch
  self.all.each do |name, url|
    begin
      path  = "#{SOURCES_PATH}/#{name}"
      local = self.local_source?(url)
      
      unless local
        if File.exists?(path)
          Git.open(path, log: nil).lib.send(:command, 'pull') # See: https://github.com/schacon/ruby-git/issues/32
        else
          Git.clone(url, name, :path => SOURCES_PATH)
        end
      end

      if local and !File.exists?(url)
        error "Local directory '#{url}' not found. Please check your sources at #{CONFIG_PATH}"
      else
        success "Successfully pulled updates from #{url} to #{SOURCES_PATH}#{name}"
      end
    rescue Git::GitExecuteError
      warn "Nothing was pulled from #{url}"
    end
  end
end

.local_source?(path) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/hw/sources.rb', line 66

def local_source? path
  git_regex = /([A-Za-z0-9]+@|http(|s)\:\/\/)([A-Za-z0-9.]+)(:|\/)([A-Za-z0-9\-\/]+)/
  (path =~ git_regex).nil?
end