This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
/
Rakefile
128 lines (111 loc) · 3.82 KB
/
Rakefile
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
# Originally adopted from Raimonds Simanovskis Rakefile,
# http://github.com/rsim/blog.rayapps.com/blob/master/Rakefile
# which was in turn adopted from Tate Johnson's Rakefile
# http://github.com/tatey/tatey.com/blob/master/Rakefile
require 'webrick'
require 'directory_watcher'
require "term/ansicolor"
require "jekyll"
require "yaml"
include Term::ANSIColor
include WEBrick
task :default => :develop
desc 'Build site with Jekyll.'
task :build do
printHeader "Compiling website..."
options = Jekyll.configuration({})
@site = Jekyll::Site.new(options)
@site.process
end
def globs(source)
Dir.chdir(source) do
dirs = Dir['*'].select { |x| File.directory?(x) }
dirs -= ['_site']
dirs = dirs.map { |x| "#{x}/**/*" }
dirs += ['*']
end
end
desc 'Enter development mode.'
task :develop => :build do
printHeader "Auto-regenerating enabled."
directoryWatcher = DirectoryWatcher.new("./")
directoryWatcher.interval = 1
directoryWatcher.glob = globs(Dir.pwd)
directoryWatcher.add_observer do |*args| @site.process end
directoryWatcher.start
mimeTypes = WEBrick::HTTPUtils::DefaultMimeTypes
mimeTypes.store 'js', 'application/javascript'
server = HTTPServer.new(
:BindAddress => "localhost",
:Port => 4000,
:DocumentRoot => "_site",
:MimeTypes => mimeTypes,
:Logger => Log.new($stderr, Log::ERROR),
:AccessLog => [["/dev/null", AccessLog::COMBINED_LOG_FORMAT ]]
)
thread = Thread.new { server.start }
trap("INT") { server.shutdown }
printHeader "Development server started at http://localhost:4000/"
printHeader "Opening website in default web browser..."
%x[open http://localhost:4000/]
printHeader "Development mode entered."
thread.join()
end
desc 'Remove all built files.'
task :clean do
printHeader "Cleaning build directory..."
%x[rm -rf _site]
end
def ask message
print message
STDIN.gets.chomp
end
def printHeader headerText
print bold + blue + "==> " + reset
print bold + headerText + reset + "\n"
end
namespace :router do
task :router_environment do
Bundler.require :router, :default
require 'logger'
@logger = Logger.new STDOUT
@logger.level = Logger::DEBUG
@router = Router::Client.new :logger => @logger
end
task :register_application => :router_environment do
platform = ENV['FACTER_govuk_platform']
app_id = "designprinciples"
url = "#{app_id}.#{platform}.alphagov.co.uk/"
@logger.info "Registering #{app_id} application against #{url}..."
@router.applications.update application_id: app_id, backend_url: url
end
task :register_routes => [ :router_environment ] do
app_id = "designprinciples"
begin
@logger.info "Registering full route /designprinciples"
@router.routes.update application_id: app_id, route_type: :full,
incoming_path: "/designprinciples"
rescue Router::Conflict => conflict_error
@logger.error "Route already exists: #{conflict_error.existing}"
raise conflict_error
end
begin
@logger.info "Registering full route /designprinciples/styleguide"
@router.routes.update application_id: app_id, route_type: :full,
incoming_path: "/designprinciples/styleguide"
rescue Router::Conflict => conflict_error
@logger.error "Route already exists: #{conflict_error.existing}"
raise conflict_error
end
begin
@logger.info "Registering temporary path for designprinciples assets"
@router.routes.update application_id: app_id, route_type: :prefix,
incoming_path: "/app"
rescue Router::Conflict => conflict_error
@logger.error "Route already exists: #{conflict_error.existing}"
raise conflict_error
end
end
desc "Register design principles application and routes with the router (run this task on server in cluster)"
task :register => [ :register_application, :register_routes ]
end