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



25
26
27
28
29
# File 'lib/hw/sources.rb', line 25

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

.allObject



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

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



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

def ensure_defaults
  self.destination_root = "~/.hw/"

  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
    add "default", DEFAULT_SOURCE
  end
end

.fetchObject



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

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)


69
70
71
72
# File 'lib/hw/sources.rb', line 69

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