-
Notifications
You must be signed in to change notification settings - Fork 35
/
Thorfile
151 lines (119 loc) · 3.75 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
require 'yaml'
require 'fileutils'
require 'tmpdir'
require 'digest'
require 'thor/group'
module Middleman
class Generator < ::Thor::Group
include ::Thor::Actions
TEMPLATE_VERSION_FILE = '.template_version'.freeze
class_option 'template',
aliases: '-T',
default: 'alphagov/tech-docs-template'
class_option 'verbose',
type: :boolean,
alias: '-V',
default: false
source_root __dir__
def detect_if_first_time_install
@first_time = option_set?('FIRST_TIME') || !File.exist?('config.rb')
end
def clone_existing_version
return if @first_time || !File.exist?(TEMPLATE_VERSION_FILE)
template = YAML.load_file(TEMPLATE_VERSION_FILE)
dir = Dir.mktmpdir
files = {}
begin
run("git clone #{template[:remote]} #{dir}")
inside(dir) do
run("git reset --hard #{template[:revision]}")
inside('template') do
Dir.glob('**/*', File::FNM_DOTMATCH).reject { |f| File.directory?(f) }.each do |f|
files[f] = Digest::MD5.file(f).hexdigest
end
end
end
ensure
FileUtils.remove_entry(dir)
end
files.each do |filename, template_hash|
begin
local_hash = Digest::MD5.file(filename).hexdigest
if template_hash == local_hash
remove_file(filename)
else
log "Keeping #{filename}, local changes made vs template"
end
rescue Errno::ENOENT
log "File #{filename} not found locally, doing nothing"
end
end
end
def copy_template_files
directory 'template', '.', exclude_pattern: /\.DS_Store$/
end
def ask_about_paas
return unless @first_time
if option_set?('USE_PAAS')
@use_paas = parse_boolean('USE_PAAS')
else
@use_paas = yes?('Will you be deploying this on GOV.UK PaaS?')
end
return unless @use_paas
@application_name = ENV['APPLICATION_NAME'] || ask(
<<-MESSAGE
What is the name of your application on PaaS?
If your application URL is larry-the-cat.cloudapps.digital, this will be "larry-the-cat".
MESSAGE
)
end
def ask_about_canonical_host
return unless @first_time
@canonical_host = ENV['CANONICAL_HOST'] || ask(
<<-MESSAGE
What is the canonical hostname of your application?
e.g. docs.larry-the-cat.service.gov.uk
MESSAGE
)
end
def configure_paas
return unless @first_time
return unless @use_paas
template 'optional/manifest.yml', 'manifest.yml'
copy_file 'optional/nginx.conf', 'source/nginx.conf'
gsub_file 'source/nginx.conf', '__CANONICAL_HOST__', @canonical_host
directory 'optional/script', 'script'
end
def configure_tech_docs
return unless @first_time
template 'optional/config/tech-docs.yml', 'config/tech-docs.yml'
end
def copy_example_documentation
return unless @first_time
directory 'optional/source', 'source'
copy_file 'optional/README.md', 'README.md'
end
def save_version_file
remote = nil
revision = nil
inside(__dir__) do
remote = run('git remote get-url origin', capture: true).strip
revision = run('git rev-parse head', capture: true).strip
end
raise 'Unable to get remote / revision' unless remote && revision
remove_file TEMPLATE_VERSION_FILE
create_file TEMPLATE_VERSION_FILE, { remote: remote, revision: revision }.to_yaml
end
private
def option_set?(key)
ENV.key?(key)
end
def parse_boolean(key)
ENV[key] == 'true'
end
def log(*args)
return unless options[:verbose]
puts(*args)
end
end
end