/
monk.rb
executable file
·95 lines (77 loc) · 2.2 KB
/
monk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env ruby
require "thor"
require "yaml"
class Monk < Thor
include Thor::Actions
[:skip, :pretend, :force, :quiet].each do |task|
class_options.delete task
end
desc "init", "Initialize a Monk application"
method_option :skeleton, :type => :string, :aliases => "-s"
def init(target = ".")
clone(source(options[:skeleton] || "default") || options[:skeleton], target) ?
cleanup(target) :
say_status(:error, clone_error(target))
end
desc "show NAME", "Display the repository address for NAME"
def show(name)
say_status name, source(name) || "repository not found"
end
desc "list", "Lists the configured repositories"
def list
monk_config.keys.sort.each do |key|
show(key)
end
end
desc "add NAME REPOSITORY", "Add the repository to the configuration file"
def add(name, repository)
monk_config[name] = repository
write_monk_config_file
end
desc "rm NAME", "Remove the repository from the configuration file"
def rm(name)
monk_config.delete(name)
write_monk_config_file
end
private
def clone(source, target)
if Dir["#{target}/*"].empty?
say_status :fetching, source
system "git clone -q --depth 1 #{source} #{target}"
$?.success?
end
end
def cleanup(target)
inside(target) { remove_file ".git" }
say_status :initialized, target
end
def source(name = "default")
monk_config[name]
end
def monk_config_file
@monk_config_file ||= File.join(monk_home, ".monk")
end
def monk_config
@monk_config ||= begin
write_monk_config_file unless File.exists?(monk_config_file)
YAML.load_file(monk_config_file)
end
end
def write_monk_config_file
remove_file(monk_config_file, :verbose => false)
create_file(monk_config_file, nil, :verbose => false) do
config = @monk_config || { "default" => "git://github.com/monkrb/skeleton.git" }
config.to_yaml
end
end
def self.source_root
"."
end
def clone_error(target)
"Couldn't clone repository into target directory '#{target}'. " +
"You must have git installed and the target directory must be empty."
end
def monk_home
ENV["MONK_HOME"] || File.join(Thor::Util.user_home)
end
end