This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfresh_check.rb
113 lines (99 loc) · 2.85 KB
/
fresh_check.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env ruby
require 'bundler/setup'
require 'fresh_api/directory'
require 'tmpdir'
require 'open-uri'
def directory_lines
directory = FreshApi::Directory.new
html = URI.parse('https://github.com/freshshell/fresh/wiki/Directory').read
directory.load_github_wiki_page(html)
directory.entries.map(&:code)
end
def fresh_repo_path
File.dirname(__FILE__) + '/.fresh/source/freshshell/fresh'
end
def init_fresh_repo
unless File.directory? fresh_repo_path
FileUtils.mkdir_p File.dirname(fresh_repo_path)
system "git clone https://github.com/freshshell/fresh #{Shellwords.escape fresh_repo_path}"
raise unless $?.success?
end
end
def readme_lines
init_fresh_repo
File.read(fresh_repo_path + '/README.markdown').split(/[\n`]/).grep(%r[^fresh [^ /]+/[^ ]+ [^ ]+]).reject { |line| line =~ /example/ }
end
def with_env(new_env)
backup_env = {}
new_env.each do |key, value|
backup_env[key] = ENV[key]
ENV[key] = value
end
yield
ensure
backup_env.each do |key, value|
ENV[key] = value
end
end
def with_fresh_sandbox
init_fresh_repo
Dir.mktmpdir 'fresh_check' do |sandbox_dir|
paths = {
'HOME' => "#{sandbox_dir}/home",
'_BIN' => "#{sandbox_dir}/bin",
'PATH' => "#{sandbox_dir}/bin:/usr/bin:/bin",
'FRESH_RCFILE' => "#{sandbox_dir}/freshrc",
'FRESH_PATH' => "#{sandbox_dir}/fresh",
'FRESH_LOCAL' => "#{sandbox_dir}/dotfiles"
}
dummy_filters = %w[erb gpg]
FileUtils.mkdir_p paths.values_at('HOME', '_BIN', 'FRESH_PATH', 'FRESH_LOCAL')
FileUtils.ln_s File.expand_path(File.dirname(__FILE__) + '/.fresh/source'), "#{paths['FRESH_PATH']}/source"
FileUtils.ln_s "#{paths['FRESH_PATH']}/source/freshshell/fresh/bin/fresh", "#{paths['_BIN']}/fresh"
dummy_filters.each do |name|
FileUtils.ln_s `which true`.chomp, "#{paths['_BIN']}/#{name}"
end
with_env paths do
Dir.chdir paths['HOME'] do
yield
end
end
end
end
def check_line(line)
with_fresh_sandbox do
File.open ENV['FRESH_RCFILE'], 'w' do |io|
io.puts 'FRESH_NO_BIN_CHECK=true'
io.puts line
end
output = `fresh 2>&1`
$?.success? ? true : output
end
end
def output_result(line, result)
if result == true
$stderr.puts "\e[1;32m\u2713\e[0m #{line}"
else
$stderr.puts "\e[1;31m\u2717\e[0m #{line}"
$stdout.puts result.gsub(/^/, ' ')
end
end
def output_progress(line)
$stderr.write "\e[1;30m?\e[0m #{line}"
$stderr.write "\e[0G"
end
if $0 == __FILE__
status = true
(readme_lines + directory_lines).each do |line|
output_progress line
result = check_line line
status &= (result == true)
output_result line, result
end
if status
$stderr.puts "\e[1;32m\u2713\u2713\u2713 All good. \u2713\u2713\u2713\e[0m"
else
$stderr.puts "\e[1;31m\u2717\u2717\u2717 Errors were found. \u2717\u2717\u2717\e[0m"
end
exit status
end