|
1 | 1 | #! /usr/bin/env ruby
|
2 |
| -# |
3 |
| -# 18F Guides Jekyll template |
4 |
| -# |
5 |
| -# Written in 2015 by Mike Bland (michael.bland@gsa.gov) |
6 |
| -# on behalf of the 18F team, part of the US General Services Administration: |
7 |
| -# https://18f.gsa.gov/ |
8 |
| -# |
9 |
| -# To the extent possible under law, the author(s) have dedicated all copyright |
10 |
| -# and related and neighboring rights to this software to the public domain |
11 |
| -# worldwide. This software is distributed without any warranty. |
12 |
| -# |
13 |
| -# You should have received a copy of the CC0 Public Domain Dedication along |
14 |
| -# with this software. If not, see |
15 |
| -# <https://creativecommons.org/publicdomain/zero/1.0/>. |
16 |
| -# |
17 |
| -# @author Mike Bland (michael.bland@gsa.gov) |
18 |
| -# |
19 |
| -# ---- |
20 |
| -# |
21 |
| -# ./go script: unified development environment interface |
22 |
| -# |
23 |
| -# Inspired by: |
24 |
| -# http://www.thoughtworks.com/insights/blog/praise-go-script-part-i |
25 |
| -# http://www.thoughtworks.com/insights/blog/praise-go-script-part-ii |
26 |
| -# |
27 |
| -# Author: Mike Bland (michael.bland@gsa.gov) |
28 |
| -# Date: 2015-04-15 |
29 | 2 |
|
30 |
| -MIN_VERSION = "2.1.5" |
| 3 | +require 'English' |
31 | 4 |
|
32 |
| -unless RUBY_VERSION >= MIN_VERSION |
33 |
| - puts <<EOF |
| 5 | +Dir.chdir File.dirname(__FILE__) |
34 | 6 |
|
35 |
| -*** ABORTING: Unsupported Ruby version *** |
36 |
| -
|
37 |
| -Ruby version #{MIN_VERSION} or greater is required to work with the Hub, but |
38 |
| -this Ruby is version #{RUBY_VERSION}. Consider using a version manager such as |
39 |
| -rbenv (https://github.com/sstephenson/rbenv) or rvm (https://rvm.io/) |
40 |
| -to install a Ruby version specifically for Hub development. |
41 |
| -
|
42 |
| -EOF |
43 |
| - exit 1 |
| 7 | +def try_command_and_restart(command) |
| 8 | + exit $CHILD_STATUS.exitstatus unless system command |
| 9 | + exec RbConfig.ruby, *[$PROGRAM_NAME].concat(ARGV) |
44 | 10 | end
|
45 | 11 |
|
46 |
| -def exec_cmd(cmd) |
47 |
| - exit $?.exitstatus unless system(cmd) |
| 12 | +begin |
| 13 | + require 'bundler/setup' if File.exist? 'Gemfile' |
| 14 | +rescue LoadError |
| 15 | + try_command_and_restart 'gem install bundler' |
| 16 | +rescue SystemExit |
| 17 | + try_command_and_restart 'bundle install' |
48 | 18 | end
|
49 | 19 |
|
50 |
| -def init |
51 |
| - begin |
52 |
| - require 'bundler' |
53 |
| - rescue LoadError |
54 |
| - puts "Installing Bundler gem..." |
55 |
| - exec_cmd 'gem install bundler' |
56 |
| - puts "Bundler installed; installing gems" |
57 |
| - end |
58 |
| - exec_cmd 'bundle install' |
| 20 | +begin |
| 21 | + require 'go_script' |
| 22 | +rescue LoadError |
| 23 | + try_command_and_restart 'gem install go_script' unless File.exist? 'Gemfile' |
| 24 | + abort "Please add \"gem 'go_script'\" to your Gemfile" |
59 | 25 | end
|
60 | 26 |
|
61 |
| -def update_gems |
62 |
| - exec_cmd 'bundle update' |
63 |
| - exec_cmd 'git add Gemfile.lock' |
64 |
| -end |
| 27 | +require 'guides_style_18f' |
65 | 28 |
|
66 |
| -JEKYLL_BUILD_CMD = "exec jekyll build --trace" |
67 |
| -JEKYLL_SERVE_CMD = "exec jekyll serve --trace" |
| 29 | +extend GoScript |
| 30 | +check_ruby_version '2.1.5' |
68 | 31 |
|
69 |
| -def serve |
70 |
| - exec "bundle #{JEKYLL_SERVE_CMD}" |
71 |
| -end |
| 32 | +command_group :dev, 'Development commands' |
72 | 33 |
|
73 |
| -def build |
74 |
| - exec_cmd "bundle #{JEKYLL_BUILD_CMD}" |
| 34 | +def_command :update_nav, 'Update the \'navigation:\' data in _config.yml' do |
| 35 | + GuidesStyle18F.update_navigation_configuration Dir.pwd |
75 | 36 | end
|
76 | 37 |
|
77 |
| -# Groups a set of commands by common function. |
78 |
| -class CommandGroup |
79 |
| - attr_accessor :description, :commands |
80 |
| - private_class_method :new |
81 |
| - @@groups = Array.new |
82 |
| - |
83 |
| - # @param description [String] short description of the group |
84 |
| - # @param commands [Hash<Symbol,String>] mapping from command function name |
85 |
| - # to a brief description; each key must be the name of a function in this |
86 |
| - # script |
87 |
| - def initialize(description, commands) |
88 |
| - @description = description |
89 |
| - @commands = commands |
90 |
| - end |
91 |
| - |
92 |
| - def to_s |
93 |
| - padding = @commands.keys.max_by {|i| i.size}.size + 2 |
94 |
| - ["\n#{@description}"].concat( |
95 |
| - @commands.map {|name, desc| " %-#{padding}s#{desc}" % name}).join("\n") |
96 |
| - end |
97 |
| - |
98 |
| - def self.add_group(description, commands) |
99 |
| - @@groups << new(description, commands) |
100 |
| - end |
101 |
| - |
102 |
| - def self.groups |
103 |
| - @@groups |
104 |
| - end |
105 |
| - |
106 |
| - def self.check_command_exists(command_symbol) |
107 |
| - all_commands = @@groups.map {|i| i.commands.keys}.flatten |
108 |
| - unless all_commands.member? command_symbol |
109 |
| - puts "Unknown option or command: #{command_symbol}" |
110 |
| - usage(exitstatus: 1) |
111 |
| - end |
112 |
| - end |
| 38 | +def_command :update_theme, 'Update the guides_style_18f gem' do |
| 39 | + exec_cmd 'bundle update --source guides_style_18f' |
113 | 40 | end
|
114 | 41 |
|
115 |
| -CommandGroup.add_group( |
116 |
| - 'Development commands', |
117 |
| - { |
118 |
| - :init => 'Set up the dev environment', |
119 |
| - :update_gems => 'Execute Bundler to update gem set', |
120 |
| - :serve => 'Serves the site at localhost:4000', |
121 |
| - :build => 'Builds the site', |
122 |
| - }) |
123 |
| - |
124 |
| -def usage(exitstatus: 0) |
125 |
| - puts <<EOF |
126 |
| -Usage: #{$0} [options] [command] |
| 42 | +def_command :update_gems, 'Update Ruby gems' do |gems| |
| 43 | + update_gems gems |
| 44 | +end |
127 | 45 |
|
128 |
| -options: |
129 |
| - -h,--help Show this help |
130 |
| -EOF |
131 |
| - CommandGroup.groups.each {|s| puts s} |
132 |
| - exit exitstatus |
| 46 | +def_command :serve, 'Serve the site at localhost:4000' do |
| 47 | + serve_jekyll |
133 | 48 | end
|
134 | 49 |
|
135 |
| -usage(exitstatus: 1) unless ARGV.size == 1 |
136 |
| -command = ARGV.shift |
137 |
| -usage if ['-h', '--help'].include? command |
| 50 | +def_command :build, 'Build the site' do |
| 51 | + build_jekyll |
| 52 | +end |
138 | 53 |
|
139 |
| -command = command.to_sym |
140 |
| -CommandGroup.check_command_exists command |
141 |
| -send command |
| 54 | +execute_command ARGV |
0 commit comments