-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev.rb
More file actions
executable file
·195 lines (163 loc) · 4.37 KB
/
dev.rb
File metadata and controls
executable file
·195 lines (163 loc) · 4.37 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env ruby
# encoding: UTF-8
# frozen_string_literal: true
###
# For rapid development.
#
# Show usage:
# ./scripts/dev.rb
#
# Example usage:
# # Use `-n` to only perform a dry-run.
# # Use `-j [jobs]` to set the number of jobs in parallel.
#
# # Configure build.
# ./scripts/dev.rb -c
#
# # Clean build files.
# ./scripts/dev.rb -C
#
# # Build Release or Debug.
# ./scripts/dev.rb -b
# ./scripts/dev.rb -b -d
#
# # Run Release or Debug.
# ./scripts/dev.rb -r
# ./scripts/dev.rb -r -d
#
# # Configure/Build/Run for Web.
# ./scripts/dev.rb -c -w
# ./scripts/dev.rb -b -w
# ./scripts/dev.rb -r -w
#
# # Check code quality.
# ./scripts/dev.rb -k
#
# # Build Linux AppImage.
# ./scripts/dev.rb -A
#
# # Package up.
# ./scripts/dev.rb -P
#
# @author Bradley Whited
###
require 'optparse'
require 'shellwords'
def main
DevApp.new.run
end
class DevApp
VERSION = '0.1.9'
CMAKE_CMD = %w[cmake].freeze
def initialize
@dry_run = true
@preset = 'default'
@config = 'Release'
@extra_args = []
@extra_build_args = {'-j' => 5}
end
def run
opt_parser = OptionParser.new do |op|
op.program_name = File.basename($PROGRAM_NAME)
op.version = VERSION
op.summary_width = 16
si = op.summary_indent
op.separator ''
op.separator "v#{op.version}"
op.separator ''
op.separator 'Options'
op.on('-c',nil,"[config] configure build w/ preset '#{@preset}' for '#{@config}'")
op.on('-C',nil,"[clean] clean '#{@config}'")
op.on('-b',nil,"[build] build '#{@config}'")
op.on('-r',nil,"[run] run '#{@config}'")
op.separator ''
op.on('-k',nil,'[check] check code quality (`cppcheck`)')
op.on('-A',nil,'[AppImage] build AppImage (always uses Release)')
op.on('-P',nil,'[pkg] package up AppImage & files using CPack')
op.separator ''
op.on('-j [jobs]','set number of jobs in parallel; if no number, uses 1') do |j|
j = j.to_s.strip
j = j.empty? ? 1 : j.to_i
@extra_build_args['-j'] = j
j
end
op.on('-d',nil,"use 'Debug' config")
op.on('-w',nil,"use 'web' preset")
op.on('-p <preset>','use <preset> for the preset') { |p| p.to_s.strip }
op.separator ''
op.separator 'Basic Options'
op.on('-n',nil,'no-clobber dry run')
op.separator ''
op.separator 'Notes'
op.separator "#{si}# Any trailing options/args after '--' will be passed to the command directly:"
op.separator "#{si}#{op.program_name} -c -- --warn-uninitialized"
end
args = ARGV
dash_i = args.find_index('--')
if !dash_i.nil?
@extra_args = args[(dash_i + 1)..]
args = args[0...dash_i]
end
opts = {}
opt_parser.parse(args,into: opts)
if opts.empty?
puts opt_parser.help
exit
end
@dry_run = opts[:n]
@config = 'Debug' if opts[:d]
if opts[:w]
@preset = 'web'
elsif !(opts[:p].nil? || opts[:p].empty?)
@preset = opts[:p]
end
# Order matters! Because user can specify all opts.
config_build if opts[:c]
check_code if opts[:k]
clean_build if opts[:C]
build if opts[:b]
build_appimage if opts[:A]
pkg_up if opts[:P]
run_app if opts[:r]
end
def config_build
run_cmd(CMAKE_CMD,'--preset',@preset)
end
def check_code
build(target: 'check')
end
def clean_build
build(target: 'clean')
end
def build(target: nil,**kargs)
extra = @extra_build_args.delete_if { |_k,v| v == false }
.transform_values { |v| (v == true) ? nil : v }
.to_a.flatten.compact
cmd = [CMAKE_CMD,'--build','--preset',@preset,'--config',@config,*extra]
cmd.push('--target',target) unless target.nil?
run_cmd(cmd,**kargs)
end
def build_appimage
build(target: 'appimage')
end
def pkg_up
build(target: 'package')
end
def run_app
# For the web run, we need to not use a subshell because of Ctrl+C to stop the server.
build(target: 'run',subshell: false)
end
def run_cmd(*cmd,subshell: true)
cmd += @extra_args
cmd = cmd.flatten.compact.map(&:to_s)
puts cmd.map { |a| Shellwords.escape(a) }.join(' ')
return if @dry_run
if subshell
system(*cmd,exception: true)
puts # Separation between multiple command runs.
else
exec(*cmd)
end
end
end
main if __FILE__ == $PROGRAM_NAME