Class: Monk::Skeleton

Inherits:
Object
  • Object
show all
Includes:
FileUtils, Thor::CoreExt
Defined in:
lib/monk/skeleton.rb

Constant Summary collapse

DEFAULTS =
{ "remote_name" => "skeleton", "branch" => "master" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, opts = {}) ⇒ Skeleton

Returns a new instance of Skeleton.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
# File 'lib/monk/skeleton.rb', line 14

def initialize(url = nil, opts = {})     
  opts, url = opts.merge(url), nil if url.respond_to? :merge
  self.options = HashWithIndifferentAccess.new opts
  options[:url] ||= url
  raise ArgumentError, "no url given" unless options.url?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/monk/skeleton.rb', line 100

def method_missing(name, *args, &block)
  name = name.to_s
  return options[name] || DEFAULTS[name] if Monk.git_options.include? name
  result = options.send(name, *args, &block)
  if result.equal? options then self
  elsif result.is_a? Hash then Skeleton.new result
  else result
  end 
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/monk/skeleton.rb', line 10

def options
  @options
end

#targetObject

Returns the value of attribute target.



10
11
12
# File 'lib/monk/skeleton.rb', line 10

def target
  @target
end

Instance Method Details

#advanced_cloneObject



65
66
67
68
69
70
71
72
# File 'lib/monk/skeleton.rb', line 65

def advanced_clone
  mkdir_p target
  Dir.chdir target do
    system "(git init -q &&
             git remote add -t #{branch} -f #{remote_name} #{mirror_url} &&
             git checkout -t #{remote_name}/#{branch} -q) >/dev/null 2>&1"
  end
end

#advanced_clone?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/monk/skeleton.rb', line 49

def advanced_clone?
  branch? or keep_remote?
end

#clean_upObject



74
75
76
# File 'lib/monk/skeleton.rb', line 74

def clean_up
  Dir.chdir(target) { system "rm -Rf .git" }
end

#clone_commandObject



53
54
55
# File 'lib/monk/skeleton.rb', line 53

def clone_command
  advanced_clone? ? advanced_clone : fast_clone
end

#configObject



78
79
80
# File 'lib/monk/skeleton.rb', line 78

def config
  options.keys == ["url"] ? options["url"] : Hash.new.replace(options)
end

#create(directory) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/monk/skeleton.rb', line 39

def create(directory)
  update_mirror
  if Dir["#{directory}/*"].empty?
    self.target = directory
    return false unless clone_command
    clean_up unless keep_remote?
    true
  end
end

#descriptionObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/monk/skeleton.rb', line 86

def description
  parameters = options.map do |key, value|
    case key
    when "url", "mirror_path" then nil
    when "mirror", "keep_remote"
      "#{"no-" unless value}#{key.gsub("_", "-")}"
    else
      "#{key}: #{value.inspect}"
    end
  end.compact.join ", "
  parameters = "(#{parameters})" unless parameters.empty?
  "#{url} #{parameters}"
end

#fast_cloneObject



61
62
63
# File 'lib/monk/skeleton.rb', line 61

def fast_clone
  system "git clone -q --depth 1 #{mirror_url} #{target}"
end

#mirror_pathObject



21
22
23
24
25
26
27
# File 'lib/monk/skeleton.rb', line 21

def mirror_path
  return unless mirror?
  options[:mirror_path] ||= begin
    require 'digest/md5'
    File.join(Monk.monk_mirrors, Digest::MD5.hexdigest(options[:url]))
  end
end

#mirror_urlObject



57
58
59
# File 'lib/monk/skeleton.rb', line 57

def mirror_url
  mirror? ? mirror_path : url
end

#to_yaml(opts = {}) ⇒ Object



82
83
84
# File 'lib/monk/skeleton.rb', line 82

def to_yaml(opts = {})
  config.to_yaml(opts)
end

#update_mirrorObject



29
30
31
32
33
34
35
36
37
# File 'lib/monk/skeleton.rb', line 29

def update_mirror
  return unless mirror?
  if File.exist? mirror_path
    chdir(mirror_path) { system "git pull origin -q >/dev/null 2>&1"}
  else
    mkdir_p Monk.monk_mirrors
    system "git clone -q #{url} #{mirror_path}"
  end
end