Class: Ufo::CLI::Central::Update
- Inherits:
-
Base
- Object
- Base
- Ufo::CLI::Central::Update
show all
- Defined in:
- lib/ufo/cli/central/update.rb
Instance Method Summary
collapse
Methods inherited from Base
#execute, #initialize, #log
#sure?
#pretty_home, #pretty_path, #pretty_time
#logger
Instance Method Details
#central_folder ⇒ Object
90
91
92
|
# File 'lib/ufo/cli/central/update.rb', line 90
def central_folder
ENV['UFO_CENTRAL_FOLDER']
end
|
#central_repo ⇒ Object
86
87
88
|
# File 'lib/ufo/cli/central/update.rb', line 86
def central_repo
ENV['UFO_CENTRAL_REPO']
end
|
#check_gitignore ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/ufo/cli/central/update.rb', line 98
def check_gitignore
ok = false
if File.exist?('.gitignore')
lines = IO.readlines('.gitignore')
ok = lines.detect do |line|
line =~ %r{/?.ufo/?$}
end
end
return if ok
log "No .ufo found in your .gitignore file".color(:yellow)
log <<~EOL
It's recommended to add .ufo to the .gitignore
When using ufo in a central fashion
EOL
end
|
#org ⇒ Object
76
77
78
79
80
|
# File 'lib/ufo/cli/central/update.rb', line 76
def org
base = central_repo.split('/')[-2] base.gsub!(/.*:/,'') base
end
|
#pull ⇒ Object
14
15
16
17
18
19
20
21
22
|
# File 'lib/ufo/cli/central/update.rb', line 14
def pull
Dir.chdir(tmp_area) do
if File.exist?(repo)
execute "cd #{repo} && git pull"
else
execute "git clone #{central_repo}"
end
end
end
|
#repo ⇒ Object
82
83
84
|
# File 'lib/ufo/cli/central/update.rb', line 82
def repo
File.basename(central_repo)
end
|
#report_broken_symlink ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/ufo/cli/central/update.rb', line 43
def report_broken_symlink
return unless File.symlink?('.ufo')
message =<<~EOL.color(:red)
ERROR: The .ufo symlink appears to pointing to a missing folder.
Please double check that the folder exist in the repo/
EOL
begin
target = File.readlink('.ufo')
unless File.exist?(target)
logger.error message
logger.error "Symlink: .ufo -> #{target}"
exit 1
end
rescue Errno::EEXIST
logger.error message
exit 1
end
end
|
#run ⇒ Object
3
4
5
6
7
8
9
10
11
12
|
# File 'lib/ufo/cli/central/update.rb', line 3
def run
validate!
action = File.exist?(".ufo") ? "update" : "create"
sure?("Will #{action} the .ufo symlink") log "Updating .ufo with #{central_repo}"
FileUtils.mkdir_p(tmp_area)
pull
symlink
check_gitignore
end
|
#symlink ⇒ Object
Always update the symlink in case use changes UFO_CENTRAL_REPO
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/ufo/cli/central/update.rb', line 25
def symlink
src = "#{tmp_area}/#{repo}"
src += "/#{central_folder}" if central_folder
FileUtils.mv(".ufo", ".ufo.bak") if File.exist?(".ufo") && File.directory?(".ufo")
src.sub!(/\.git$/,'')
FileUtils.ln_sf(src, ".ufo", verbose: false) FileUtils.rm_rf(".ufo.bak")
report_broken_symlink
log "The .ufo symlink has been updated"
log "Symlink: .ufo -> #{pretty_home(src)}"
end
|
#tmp_area ⇒ Object
94
95
96
|
# File 'lib/ufo/cli/central/update.rb', line 94
def tmp_area
"#{ENV['HOME']}/.ufo/central/#{org}"
end
|
#validate! ⇒ Object
63
64
65
66
67
68
|
# File 'lib/ufo/cli/central/update.rb', line 63
def validate!
return if central_repo
log "ERROR: Please set the env var: UFO_CENTRAL_REPO".color(:red)
log "The ufo central update command requires it."
exit 1
end
|