forked from evolvingweb/sitediff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thorfile
executable file
·126 lines (106 loc) · 3.29 KB
/
Thorfile
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
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env ruby
# frozen_string_literal: true
# TODO: Determine the utility of this file.
LIB_DIR = File.join(File.dirname(__FILE__), 'lib')
$LOAD_PATH << LIB_DIR
require 'sitediff/webserver'
require 'sitediff/webserver/resultserver'
# Thor Base class.
class Base < Thor
method_options local: true
# Adds the option to all Base subclasses.
# method_options() takes different arguments than option().
def initialize(*args)
super(*args)
@local = options['local']
end
# gives us run()
include Thor::Actions
# Thor, by default, exits with 0 no matter what!
def self.exit_on_failure?
true
end
protected
def executable(gem)
gem = './bin/sitediff' if (gem == 'sitediff') && @local
"#{'bundle exec' if @local} #{gem}"
end
end
# Thor for Docker.
class Docker < Base
IMAGE = 'evolvingweb/sitediff'
desc 'build', 'Build a docker image for sitediff'
def build
run "docker build -t #{IMAGE} . "
end
# NOTE We can't override run() (which is reserved by Thor). Luckily, Thor only
# checks for the first N necessary characters to match a command with a
# method. Cf. Thor::normalize_command_name()
desc 'run', 'Run a rake task (or a login shell if none given) inside docker'
def run_(task = 'bash')
docker_opts = ['-t', "-v #{File.dirname(__FILE__)}:/sitediff"]
finish_exec(task, docker_opts)
end
desc 'compose', 'Run a task inside docker without volume mounting (not supported with compose)'
def compose(task = 'bash')
docker_opts = ['-t']
finish_exec(task, docker_opts)
end
no_commands do
def finish_exec(task, docker_opts)
if task == 'bash'
cmd = 'bash'
docker_opts << '-i'
else
# pass down the local flag to docker command
cmd = "#{executable('thor')} #{task} #{@local ? '--local' : '--no-local'}"
end
puts "docker run #{docker_opts.join(' ')} #{IMAGE} #{cmd}"
run "docker run #{docker_opts.join(' ')} #{IMAGE} #{cmd}"
end
end
end
# Thor for Spec.
class Spec < Base
desc 'unit', 'run RSpec unit tests'
def unit
puts "#{executable('rspec')} spec/unit"
run "#{executable('rspec')} spec/unit"
end
desc 'fixture', 'run RSpec integration tests'
def fixture
puts "#{executable('rspec')} spec/unit"
run "#{executable('rspec')} spec/fixtures"
end
# hidden task to lump together multiple tasks
desc 'all', 'runs both unit and fixture tests', hide: true
def all
unit
fixture
end
default_task :all
end
# Thor for fixtures.
class Fixture < Base
desc 'local', 'Run a sitediff test case'
def local
run "#{executable('sitediff')} diff --cached=none spec/fixtures/cli/config.yaml"
end
desc 'http', 'Run a sitediff test case, using web servers'
def http
cmd = "#{executable('sitediff')} diff --cached=none spec/fixtures/cli/config.yaml"
http_fixtures(cmd).kill
end
desc 'serve', 'Serve the result of the fixture test'
def serve
cmd = "#{executable('sitediff')} diff --cached=none --paths-file=spec/sites/ruby-doc.org/paths.txt spec/unit/cli/config.yaml"
http_fixtures(cmd)
SiteDiff::Webserver::ResultServer.new(nil, 'sitediff', quiet: true).wait
end
private
def http_fixtures(cmd)
serv = SiteDiff::Webserver::FixtureServer.new
run "#{cmd} --before #{serv.before} --after #{serv.after}"
serv
end
end