glimmer-dsl-libui 0.8.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,799 @@
1
+ # Copyright (c) 2021-2023 Andy Maleh
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining
4
+ # a copy of this software and associated documentation files (the
5
+ # "Software"), to deal in the Software without restriction, including
6
+ # without limitation the rights to use, copy, modify, merge, publish,
7
+ # distribute, sublicense, and/or sell copies of the Software, and to
8
+ # permit persons to whom the Software is furnished to do so, subject to
9
+ # the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be
12
+ # included in all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ require 'fileutils'
23
+ require 'os'
24
+ require 'facets'
25
+
26
+ # TODO refactor to nest under RakeTask namespace
27
+
28
+ MAIN_OBJECT = self
29
+
30
+ module Glimmer
31
+ module RakeTask
32
+ class Scaffold
33
+ class << self
34
+ include FileUtils
35
+
36
+ VERSION = File.read(File.expand_path('../../../../VERSION', __FILE__)).strip
37
+ RUBY_VERSION = File.read(File.expand_path('../../../../RUBY_VERSION', __FILE__)).strip
38
+
39
+ # TODO externalize all constants into scaffold/files
40
+
41
+ GITIGNORE = <<~MULTI_LINE_STRING
42
+ *.gem
43
+ *.rbc
44
+ /.config
45
+ /.mvn/
46
+ /coverage/
47
+ /InstalledFiles
48
+ /pkg/
49
+ /spec/reports/
50
+ /spec/examples.txt
51
+ /test/tmp/
52
+ /test/version_tmp/
53
+ /tmp/
54
+
55
+ # Used by dotenv library to load environment variables.
56
+ # .env
57
+
58
+ ## Specific to RubyMotion:
59
+ .dat*
60
+ .repl_history
61
+ build/
62
+ *.bridgesupport
63
+ build-iPhoneOS/
64
+ build-iPhoneSimulator/
65
+
66
+ ## Specific to RubyMotion (use of CocoaPods):
67
+ #
68
+ # We recommend against adding the Pods directory to your .gitignore. However
69
+ # you should judge for yourself, the pros and cons are mentioned at:
70
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
71
+ #
72
+ # vendor/Pods/
73
+
74
+ ## Documentation cache and generated files:
75
+ /.yardoc/
76
+ /_yardoc/
77
+ /doc/
78
+ /rdoc/
79
+
80
+ ## Environment normalization:
81
+ /.bundle/
82
+ /vendor/bundle
83
+ /lib/bundler/man/
84
+
85
+ # for a library or gem, you might want to ignore these files since the code is
86
+ # intended to run in multiple environments; otherwise, check them in:
87
+ # Gemfile.lock
88
+ # .ruby-version
89
+ # .ruby-gemset
90
+
91
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
92
+ .rvmrc
93
+
94
+ # Mac
95
+ .DS_Store
96
+
97
+ # Gladiator (Glimmer Editor)
98
+ .gladiator
99
+ .gladiator-scratchpad
100
+
101
+ # Glimmer
102
+ /dist/
103
+ /packages/
104
+ MULTI_LINE_STRING
105
+
106
+ GEMFILE_PREFIX = <<~MULTI_LINE_STRING
107
+ # frozen_string_literal: true
108
+
109
+ source 'https://rubygems.org'
110
+
111
+ git_source(:github) {|repo_name| "https://github.com/\#{repo_name}" }
112
+ MULTI_LINE_STRING
113
+ GEMFILE_APP_MIDFIX = <<~MULTI_LINE_STRING
114
+
115
+ gem 'glimmer-dsl-libui', '~> #{VERSION}'
116
+ MULTI_LINE_STRING
117
+ GEMFILE_GEM_MIDFIX = <<~MULTI_LINE_STRING
118
+
119
+ gem 'glimmer-dsl-libui', '~> #{VERSION.split('.')[0...2].join('.')}'
120
+ MULTI_LINE_STRING
121
+ GEMFILE_SUFFIX = <<~MULTI_LINE_STRING
122
+
123
+ group :development do
124
+ gem 'rspec', '~> 3.5.0'
125
+ gem 'juwelier', '2.4.9'
126
+ gem 'simplecov', '>= 0'
127
+ end
128
+ MULTI_LINE_STRING
129
+ APP_GEMFILE = GEMFILE_PREFIX + GEMFILE_APP_MIDFIX + GEMFILE_SUFFIX
130
+ GEM_GEMFILE = GEMFILE_PREFIX + GEMFILE_GEM_MIDFIX + GEMFILE_SUFFIX
131
+
132
+ def app(app_name)
133
+ common_app(app_name)
134
+ end
135
+
136
+ def common_app(app_name, window_type = :app, window_options = {})
137
+ gem_name = file_name(app_name)
138
+ gem_summary = human_name(app_name)
139
+ return puts("The directory '#{gem_name}' already exists. Please either remove or pick a different name.") if Dir.exist?(gem_name)
140
+ # system "ruby -S gem install bundler --no-document" if OS.windows? # resolves freezing issue with warbler and bundler 2.2.29 included in JRuby
141
+ system "ruby -S gem install juwelier -v2.4.9 --no-document" unless juwelier_exist?
142
+ system "ruby -S juwelier --markdown --rspec --summary '#{gem_summary}' --description '#{gem_summary}' #{gem_name}"
143
+ return puts('Your Git user.name and/or github.user are missing! Please add in for Juwelier to help Glimmer with Scaffolding.') if `git config --get github.user`.strip.empty? || `git config --get user.name`.strip.empty?
144
+ cd gem_name
145
+ rm_rf 'lib'
146
+ write '.gitignore', GITIGNORE
147
+ write '.ruby-version', RUBY_VERSION
148
+ write '.ruby-gemset', app_name
149
+ write 'VERSION', '1.0.0'
150
+ write 'LICENSE.txt', "Copyright (c) #{Time.now.year} #{app_name}"
151
+ write 'Gemfile', gemfile(window_type)
152
+ write 'Rakefile', gem_rakefile(app_name, nil, gem_name)
153
+ mkdir 'app'
154
+ write "app/#{file_name(app_name)}.rb", app_main_file(app_name)
155
+ mkdir_p "app/#{file_name(app_name)}/model"
156
+ mkdir_p "app/#{file_name(app_name)}/view"
157
+ custom_window(class_name(app_name), current_dir_name, window_type, custom_window_class_name: 'Application')
158
+
159
+ mkdir_p 'icons/windows'
160
+ icon_file = "icons/windows/#{human_name(app_name)}.ico"
161
+ cp File.expand_path('../../../../icons/scaffold_app.ico', __FILE__), icon_file
162
+ puts "Created #{current_dir_name}/#{icon_file}"
163
+
164
+ mkdir_p 'icons/macosx'
165
+ icon_file = "icons/macosx/#{human_name(app_name)}.icns"
166
+ cp File.expand_path('../../../../icons/scaffold_app.icns', __FILE__), icon_file
167
+ puts "Created #{current_dir_name}/#{icon_file}"
168
+
169
+ mkdir_p 'icons/linux'
170
+ icon_file = "icons/linux/#{human_name(app_name)}.png"
171
+ cp File.expand_path('../../../../icons/scaffold_app.png', __FILE__), icon_file
172
+ puts "Created #{current_dir_name}/#{icon_file}"
173
+
174
+ mkdir_p "app/#{file_name(app_name)}"
175
+ write "app/#{file_name(app_name)}/launch.rb", app_launch_file(app_name)
176
+ mkdir_p 'bin'
177
+ write "bin/#{file_name(app_name)}", app_bin_command_file(app_name)
178
+ FileUtils.chmod 0755, "bin/#{file_name(app_name)}"
179
+ if OS.windows?
180
+ system "bundle"
181
+ system "rspec --init"
182
+ else
183
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n bundle\n rspec --init\n'"
184
+ end
185
+ write 'spec/spec_helper.rb', spec_helper_file
186
+ if OS.mac? || OS.linux?
187
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n glimmer run\n'"
188
+ else
189
+ system "glimmer run"
190
+ end
191
+ end
192
+
193
+ def custom_window(custom_window_name, namespace, window_type = nil, window_options = {})
194
+ namespace ||= current_dir_name
195
+ root_dir = File.exist?('app') ? 'app' : 'lib'
196
+ parent_dir = "#{root_dir}/#{file_name(namespace)}/view"
197
+ return puts("The file '#{parent_dir}/#{file_name(custom_window_name)}.rb' already exists. Please either remove or pick a different name.") if File.exist?("#{parent_dir}/#{file_name(custom_window_name)}.rb")
198
+ mkdir_p parent_dir unless File.exist?(parent_dir)
199
+ write "#{parent_dir}/#{file_name(custom_window_name)}.rb", custom_window_file(custom_window_name, namespace, window_type, window_options)
200
+ end
201
+
202
+ def custom_control(custom_control_name, namespace)
203
+ namespace ||= current_dir_name
204
+ root_dir = File.exist?('app') ? 'app' : 'lib'
205
+ parent_dir = "#{root_dir}/#{file_name(namespace)}/view"
206
+ return puts("The file '#{parent_dir}/#{file_name(custom_control_name)}.rb' already exists. Please either remove or pick a different name.") if File.exist?("#{parent_dir}/#{file_name(custom_control_name)}.rb")
207
+ mkdir_p parent_dir unless File.exist?(parent_dir)
208
+ write "#{parent_dir}/#{file_name(custom_control_name)}.rb", custom_control_file(custom_control_name, namespace)
209
+ end
210
+
211
+ def custom_shape(custom_shape_name, namespace)
212
+ namespace ||= current_dir_name
213
+ root_dir = File.exist?('app') ? 'app' : 'lib'
214
+ parent_dir = "#{root_dir}/#{file_name(namespace)}/view"
215
+ return puts("The file '#{parent_dir}/#{file_name(custom_shape_name)}.rb' already exists. Please either remove or pick a different name.") if File.exist?("#{parent_dir}/#{file_name(custom_shape_name)}.rb")
216
+ mkdir_p parent_dir unless File.exist?(parent_dir)
217
+ write "#{parent_dir}/#{file_name(custom_shape_name)}.rb", custom_shape_file(custom_shape_name, namespace)
218
+ end
219
+
220
+ def custom_window_gem(custom_window_name, namespace)
221
+ gem_name = "glimmer-cs-#{compact_name(custom_window_name)}"
222
+ gem_summary = "#{human_name(custom_window_name)} - Glimmer Custom Window"
223
+ begin
224
+ custom_window_keyword = dsl_control_name(custom_window_name)
225
+ MAIN_OBJECT.method(custom_window_keyword)
226
+ return puts("CustomWindow keyword `#{custom_window_keyword}` is unavailable (occupied by a built-in Ruby method)! Please pick a different name.")
227
+ rescue NameError
228
+ # No Op (keyword is not taken by a built in Ruby method)
229
+ end
230
+ if namespace
231
+ gem_name += "-#{compact_name(namespace)}"
232
+ gem_summary += " (#{human_name(namespace)})"
233
+ else
234
+ return puts('Namespace is required! Usage: glimmer scaffold:gem:customwindow[name,namespace]') unless `git config --get github.user`.to_s.strip == 'AndyObtiva'
235
+ namespace = 'glimmer'
236
+ end
237
+ return puts("The directory '#{gem_name}' already exists. Please either remove or pick a different name.") if Dir.exist?(gem_name)
238
+ # system "ruby -S gem install bundler --no-document" if OS.windows? # resolves freezing issue with warbler and bundler 2.2.29 included in JRuby
239
+ system "ruby -S gem install juwelier -v2.4.9 --no-document" unless juwelier_exist?
240
+ system "ruby -S juwelier --markdown --rspec --summary '#{gem_summary}' --description '#{gem_summary}' #{gem_name}"
241
+ return puts('Your Git user.name and/or github.user are missing! Please add in for Juwelier to help Glimmer with Scaffolding.') if `git config --get github.user`.strip.empty? && `git config --get user.name`.strip.empty?
242
+ cd gem_name
243
+ write '.gitignore', GITIGNORE
244
+ write '.ruby-version', RUBY_VERSION
245
+ write '.ruby-gemset', gem_name
246
+ write 'VERSION', '1.0.0'
247
+ write 'Gemfile', GEM_GEMFILE
248
+ write 'Rakefile', gem_rakefile(custom_window_name, namespace, gem_name)
249
+ append "lib/#{gem_name}.rb", gem_main_file(custom_window_name, namespace)
250
+ custom_window(custom_window_name, namespace, :gem)
251
+
252
+ mkdir_p "lib/#{gem_name}"
253
+ write "lib/#{gem_name}/launch.rb", gem_launch_file(gem_name, custom_window_name, namespace)
254
+ mkdir_p 'bin'
255
+ write "bin/#{file_name(custom_window_name)}", app_bin_command_file(gem_name, custom_window_name, namespace)
256
+ FileUtils.chmod 0755, "bin/#{file_name(custom_window_name)}"
257
+ if OS.windows?
258
+ system "bundle"
259
+ system "rspec --init"
260
+ else
261
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n bundle\n rspec --init\n'"
262
+ end
263
+ write 'spec/spec_helper.rb', spec_helper_file
264
+
265
+ mkdir_p 'icons/windows'
266
+ icon_file = "icons/windows/#{human_name(gem_name)}.ico"
267
+ cp File.expand_path('../../../../icons/scaffold_app.ico', __FILE__), icon_file
268
+ puts "Created #{current_dir_name}/#{icon_file}"
269
+
270
+ mkdir_p 'icons/macosx'
271
+ icon_file = "icons/macosx/#{human_name(gem_name)}.icns"
272
+ cp File.expand_path('../../../../icons/scaffold_app.icns', __FILE__), icon_file
273
+ puts "Created #{current_dir_name}/#{icon_file}"
274
+
275
+ mkdir_p 'icons/linux'
276
+ icon_file = "icons/linux/#{human_name(gem_name)}.png"
277
+ cp File.expand_path('../../../../icons/scaffold_app.png', __FILE__), icon_file
278
+ puts "Created #{current_dir_name}/#{icon_file}"
279
+
280
+ if OS.mac? || OS.linux?
281
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n glimmer run\n'"
282
+ else
283
+ system "glimmer run"
284
+ end
285
+ puts "Finished creating #{gem_name} Ruby gem."
286
+ puts 'Edit Rakefile to configure gem details.'
287
+ puts 'Run `rake` to execute specs.'
288
+ puts 'Run `rake build` to build gem.'
289
+ puts 'Run `rake release` to release into rubygems.org once ready.'
290
+ end
291
+
292
+ def custom_control_gem(custom_control_name, namespace)
293
+ gem_name = "glimmer-cw-#{compact_name(custom_control_name)}"
294
+ gem_summary = "#{human_name(custom_control_name)} - Glimmer Custom Control"
295
+ if namespace
296
+ gem_name += "-#{compact_name(namespace)}"
297
+ gem_summary += " (#{human_name(namespace)})"
298
+ else
299
+ return puts('Namespace is required! Usage: glimmer scaffold:custom_control_gem[custom_control_name,namespace]') unless `git config --get github.user`.to_s.strip == 'AndyObtiva'
300
+ namespace = 'glimmer'
301
+ end
302
+
303
+ return puts("The directory '#{gem_name}' already exists. Please either remove or pick a different name.") if Dir.exist?(gem_name)
304
+ # system "ruby -S gem install bundler --no-document" if OS.windows? # resolves freezing issue with warbler and bundler 2.2.29 included in JRuby
305
+ system "ruby -S gem install juwelier -v2.4.9 --no-document" unless juwelier_exist?
306
+ system "ruby -S juwelier --markdown --rspec --summary '#{gem_summary}' --description '#{gem_summary}' #{gem_name}"
307
+ return puts('Your Git user.name and/or github.user are missing! Please add in for Juwelier to help Glimmer with Scaffolding.') if `git config --get github.user`.strip.empty? && `git config --get user.name`.strip.empty?
308
+ cd gem_name
309
+ write '.gitignore', GITIGNORE
310
+ write '.ruby-version', RUBY_VERSION
311
+ write '.ruby-gemset', gem_name
312
+ write 'VERSION', '1.0.0'
313
+ write 'Gemfile', GEM_GEMFILE
314
+ write 'Rakefile', gem_rakefile
315
+ append "lib/#{gem_name}.rb", gem_main_file(custom_control_name, namespace)
316
+ custom_control(custom_control_name, namespace)
317
+ if OS.windows?
318
+ system "bundle"
319
+ system "rspec --init"
320
+ else
321
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n bundle\n rspec --init\n'"
322
+ end
323
+ write 'spec/spec_helper.rb', spec_helper_file
324
+ puts "Finished creating #{gem_name} Ruby gem."
325
+ puts 'Edit Rakefile to configure gem details.'
326
+ puts 'Run `rake` to execute specs.'
327
+ puts 'Run `rake build` to build gem.'
328
+ puts 'Run `rake release` to release into rubygems.org once ready.'
329
+ end
330
+
331
+ def custom_shape_gem(custom_shape_name, namespace)
332
+ gem_name = "glimmer-cp-#{compact_name(custom_shape_name)}"
333
+ gem_summary = "#{human_name(custom_shape_name)} - Glimmer Custom Shape"
334
+ if namespace
335
+ gem_name += "-#{compact_name(namespace)}"
336
+ gem_summary += " (#{human_name(namespace)})"
337
+ else
338
+ return puts('Namespace is required! Usage: glimmer scaffold:custom_shape_gem[custom_shape_name,namespace]') unless `git config --get github.user`.to_s.strip == 'AndyObtiva'
339
+ namespace = 'glimmer'
340
+ end
341
+
342
+ return puts("The directory '#{gem_name}' already exists. Please either remove or pick a different name.") if Dir.exist?(gem_name)
343
+ # system "ruby -S gem install bundler --no-document" if OS.windows? # resolves freezing issue with warbler and bundler 2.2.29 included in JRuby
344
+ system "ruby -S gem install juwelier -v2.4.9 --no-document" unless juwelier_exist?
345
+ system "ruby -S juwelier --markdown --rspec --summary '#{gem_summary}' --description '#{gem_summary}' #{gem_name}"
346
+ return puts('Your Git user.name and/or github.user are missing! Please add in for Juwelier to help Glimmer with Scaffolding.') if `git config --get github.user`.strip.empty? && `git config --get user.name`.strip.empty?
347
+ cd gem_name
348
+ write '.gitignore', GITIGNORE
349
+ write '.ruby-version', RUBY_VERSION
350
+ write '.ruby-gemset', gem_name
351
+ write 'VERSION', '1.0.0'
352
+ write 'Gemfile', GEM_GEMFILE
353
+ write 'Rakefile', gem_rakefile
354
+ append "lib/#{gem_name}.rb", gem_main_file(custom_shape_name, namespace)
355
+ custom_shape(custom_shape_name, namespace)
356
+ if OS.windows?
357
+ system "bundle"
358
+ system "rspec --init"
359
+ else
360
+ system "bash -c '#{RVM_FUNCTION}\n cd .\n bundle\n rspec --init\n'"
361
+ end
362
+ write 'spec/spec_helper.rb', spec_helper_file
363
+ puts "Finished creating #{gem_name} Ruby gem."
364
+ puts 'Edit Rakefile to configure gem details.'
365
+ puts 'Run `rake` to execute specs.'
366
+ puts 'Run `rake build` to build gem.'
367
+ puts 'Run `rake release` to release into rubygems.org once ready.'
368
+ end
369
+
370
+ private
371
+
372
+ def juwelier_exist?
373
+ OS.windows? ? system('where juwelier') : system('which juwelier')
374
+ end
375
+
376
+ def write(file, content)
377
+ File.write file, content
378
+ file_path = File.expand_path(file)
379
+ puts "Created #{current_dir_name}/#{file}"
380
+ end
381
+
382
+ def append(file, content)
383
+ File.open(file, 'a') do |f|
384
+ f.write(content)
385
+ end
386
+ end
387
+
388
+ def current_dir_name
389
+ File.basename(File.expand_path('.'))
390
+ end
391
+
392
+ def class_name(app_name)
393
+ app_name.underscore.camelcase(:upper)
394
+ end
395
+
396
+ def file_name(app_name)
397
+ app_name.underscore
398
+ end
399
+ alias dsl_control_name file_name
400
+
401
+ def human_name(app_name)
402
+ app_name.underscore.titlecase
403
+ end
404
+
405
+ def compact_name(gem_name)
406
+ gem_name.underscore.camelcase.downcase
407
+ end
408
+
409
+ def gemfile(window_type)
410
+ APP_GEMFILE
411
+ end
412
+
413
+ def app_main_file(app_name)
414
+ <<~MULTI_LINE_STRING
415
+ $LOAD_PATH.unshift(File.expand_path('..', __FILE__))
416
+
417
+ begin
418
+ require 'bundler/setup'
419
+ Bundler.require(:default)
420
+ rescue StandardError, Gem::LoadError
421
+ # this runs when packaged as a gem (no bundler)
422
+ require 'glimmer-dsl-libui'
423
+ # add more gems if needed
424
+ end
425
+
426
+ class #{class_name(app_name)}
427
+ include Glimmer
428
+
429
+ APP_ROOT = File.expand_path('../..', __FILE__)
430
+ VERSION = File.read(File.join(APP_ROOT, 'VERSION'))
431
+ LICENSE = File.read(File.join(APP_ROOT, 'LICENSE.txt'))
432
+ end
433
+
434
+ require '#{file_name(app_name)}/view/#{file_name(app_name)}'
435
+ MULTI_LINE_STRING
436
+ end
437
+
438
+ def gem_main_file(custom_control_name, namespace = nil)
439
+ custom_control_file_path = ''
440
+ custom_control_file_path += "#{file_name(namespace)}/" if namespace
441
+ custom_control_file_path += "view"
442
+ custom_control_file_path += "/#{file_name(custom_control_name)}"
443
+
444
+ <<~MULTI_LINE_STRING
445
+ $LOAD_PATH.unshift(File.expand_path('..', __FILE__))
446
+
447
+ require 'glimmer-dsl-libui'
448
+ require '#{custom_control_file_path}'
449
+ MULTI_LINE_STRING
450
+ end
451
+
452
+ def app_launch_file(app_name)
453
+ <<~MULTI_LINE_STRING
454
+ require_relative '../#{file_name(app_name)}'
455
+
456
+ #{class_name(app_name)}::View::#{class_name(app_name)}.launch
457
+ MULTI_LINE_STRING
458
+ end
459
+
460
+ def app_bin_command_file(app_name_or_gem_name, custom_window_name=nil, namespace=nil)
461
+ if custom_window_name.nil?
462
+ runner = "File.expand_path('../../app/#{file_name(app_name_or_gem_name)}/launch.rb', __FILE__)"
463
+ else
464
+ runner = "File.expand_path('../../lib/#{app_name_or_gem_name}/launch.rb', __FILE__)"
465
+ end
466
+ <<~MULTI_LINE_STRING
467
+ #!/usr/bin/env ruby
468
+
469
+ runner = #{runner}
470
+
471
+ require 'glimmer/launcher'
472
+
473
+ launcher = Glimmer::Launcher.new([runner] + ARGV)
474
+ launcher.launch
475
+ MULTI_LINE_STRING
476
+ end
477
+
478
+ def gem_launch_file(gem_name, custom_window_name, namespace)
479
+ # TODO change this so that it does not mix Glimmer unto the main object
480
+ <<~MULTI_LINE_STRING
481
+ require_relative '../#{gem_name}'
482
+
483
+ #{class_name(namespace)}::View::#{class_name(custom_window_name)}.launch
484
+ MULTI_LINE_STRING
485
+ end
486
+
487
+ def gem_rakefile(custom_window_name = nil, namespace = nil, gem_name = nil)
488
+ rakefile_content = File.read('Rakefile')
489
+ lines = rakefile_content.split("\n")
490
+ require_rake_line_index = lines.index(lines.detect {|l| l.include?("require 'rake'") })
491
+ lines.insert(require_rake_line_index, "require 'glimmer/launcher'")
492
+ gem_files_line_index = lines.index(lines.detect {|l| l.include?('# dependencies defined in Gemfile') })
493
+ if custom_window_name
494
+ lines.insert(gem_files_line_index, " gem.files = Dir['VERSION', 'LICENSE.txt', 'app/**/*', 'bin/**/*', 'config/**/*', 'db/**/*', 'docs/**/*', 'fonts/**/*', 'icons/**/*', 'images/**/*', 'lib/**/*', 'script/**/*', 'sounds/**/*', 'videos/**/*']")
495
+ lines.insert(gem_files_line_index+1, " gem.require_paths = ['lib', 'app']")
496
+ lines.insert(gem_files_line_index+2, " gem.executables = ['#{file_name(custom_window_name)}']") if custom_window_name
497
+ else
498
+ lines.insert(gem_files_line_index, " gem.files = Dir['VERSION', 'LICENSE.txt', 'lib/**/*']")
499
+ end
500
+ lines << "\nrequire 'glimmer/rake_task'\n"
501
+ lines.join("\n")
502
+ end
503
+
504
+ def spec_helper_file
505
+ content = File.read('spec/spec_helper.rb')
506
+ lines = content.split("\n")
507
+ require_line_index = lines.index(lines.detect {|l| l.include?('RSpec.configure do') })
508
+ lines[require_line_index...require_line_index] = [
509
+ "require 'bundler/setup'",
510
+ 'Bundler.require(:default, :development)',
511
+ ]
512
+ configure_block_line_index = lines.index(lines.detect {|l| l.include?('RSpec.configure do') }) + 1
513
+ lines[configure_block_line_index...configure_block_line_index] = [
514
+ ' # The following ensures rspec tests that instantiate and set Glimmer DSL controls in @target get cleaned after',
515
+ ' config.after do',
516
+ ' @target.dispose if @target && @target.respond_to?(:dispose)',
517
+ ' Glimmer::DSL::Engine.reset',
518
+ ' end',
519
+ ]
520
+
521
+ lines << "\nrequire 'glimmer/rake_task'\n"
522
+ lines.join("\n")
523
+ end
524
+
525
+ def custom_window_file(custom_window_name, namespace, window_type, window_options = {})
526
+ window_options ||= {}
527
+ window_options[:custom_window_class_name] ||= 'CustomWindow'
528
+ namespace_type = class_name(namespace) == class_name(current_dir_name) ? 'class' : 'module'
529
+
530
+ custom_window_file_content = <<-MULTI_LINE_STRING
531
+ #{namespace_type} #{class_name(namespace)}
532
+ module View
533
+ class #{class_name(custom_window_name)}
534
+ include Glimmer::LibUI::#{window_options[:custom_window_class_name]}
535
+
536
+ MULTI_LINE_STRING
537
+
538
+ if window_type == :gem
539
+ custom_window_file_content += <<-MULTI_LINE_STRING
540
+ APP_ROOT = File.expand_path('../../../..', __FILE__)
541
+ VERSION = File.read(File.join(APP_ROOT, 'VERSION'))
542
+ LICENSE = File.read(File.join(APP_ROOT, 'LICENSE.txt'))
543
+ MULTI_LINE_STRING
544
+ end
545
+
546
+ if %i[gem app].include?(window_type)
547
+ custom_window_file_content += <<-MULTI_LINE_STRING
548
+ GREETINGS = [
549
+ 'Hello, World!',
550
+ 'Howdy, Partner!'
551
+ ]
552
+ MULTI_LINE_STRING
553
+ end
554
+
555
+ custom_window_file_content += <<-MULTI_LINE_STRING
556
+
557
+ ## Add options like the following to configure CustomWindow by outside consumers
558
+ #
559
+ # options :title, :background_color
560
+ # option :width, default: 320
561
+ # option :height, default: 240
562
+ MULTI_LINE_STRING
563
+
564
+ if %i[gem app].include?(window_type)
565
+ custom_window_file_content += <<-MULTI_LINE_STRING
566
+
567
+ # GREETING Array selected index
568
+ option :greeting_index, default: 0
569
+ MULTI_LINE_STRING
570
+ end
571
+
572
+ custom_window_file_content += <<-MULTI_LINE_STRING
573
+
574
+ ## Use before_body block to pre-initialize variables to use in body and
575
+ # to setup application menu
576
+ #
577
+ MULTI_LINE_STRING
578
+
579
+ if %i[gem app].include?(window_type)
580
+ custom_window_file_content += <<-MULTI_LINE_STRING
581
+ before_body do
582
+ menu('File') {
583
+ menu_item('Preferences...') {
584
+ on_clicked do
585
+ display_preferences_dialog
586
+ end
587
+ }
588
+
589
+ # Enables quitting with CMD+Q on Mac with Mac Quit menu item
590
+ quit_menu_item if OS.mac?
591
+ }
592
+ menu('Help') {
593
+ if OS.mac?
594
+ about_menu_item {
595
+ on_clicked do
596
+ display_about_dialog
597
+ end
598
+ }
599
+ end
600
+
601
+ menu_item('About') {
602
+ on_clicked do
603
+ display_about_dialog
604
+ end
605
+ }
606
+ }
607
+ end
608
+ MULTI_LINE_STRING
609
+ else
610
+ custom_window_file_content += <<-MULTI_LINE_STRING
611
+ # before_body do
612
+ #
613
+ # end
614
+ MULTI_LINE_STRING
615
+ end
616
+
617
+ custom_window_file_content += <<-MULTI_LINE_STRING
618
+
619
+ ## Use after_body block to setup observers for controls in body
620
+ #
621
+ # after_body do
622
+ #
623
+ # end
624
+
625
+ ## Add control content inside custom window body
626
+ ## Top-most control must be a window or another custom window
627
+ #
628
+ body {
629
+ window {
630
+ # Replace example content below with custom window content
631
+ content_size 240, 240
632
+ title '#{human_name(namespace)}'
633
+
634
+ margined true
635
+
636
+ label {
637
+ text <= [self, :greeting_index, on_read: -> { greeting }]
638
+ }
639
+ }
640
+ }
641
+ MULTI_LINE_STRING
642
+
643
+ if %i[gem app].include?(window_type)
644
+ custom_window_file_content += <<-MULTI_LINE_STRING
645
+
646
+ def greeting
647
+ GREETINGS[greeting_index]
648
+ end
649
+
650
+ def display_about_dialog
651
+ message = "#{human_name(namespace)}#{" - #{human_name(custom_window_name)}" if window_type == :gem} \#{VERSION}\\n\\n\#{LICENSE}"
652
+ msg_box('About', message)
653
+ end
654
+
655
+ MULTI_LINE_STRING
656
+ end
657
+
658
+ if %i[gem app].include?(window_type)
659
+ custom_window_file_content += <<-MULTI_LINE_STRING
660
+ def display_preferences_dialog
661
+ window {
662
+ title 'Preferences'
663
+ content_size 200, 100
664
+
665
+ margined true
666
+
667
+ vertical_box {
668
+ padded true
669
+
670
+ label('Greeting:') {
671
+ stretchy false
672
+ }
673
+
674
+ radio_buttons {
675
+ stretchy false
676
+
677
+ items GREETINGS
678
+ selected <=> [self, :greeting_index]
679
+ }
680
+ }
681
+ }.show
682
+ end
683
+ MULTI_LINE_STRING
684
+ end
685
+
686
+ custom_window_file_content += <<-MULTI_LINE_STRING
687
+ end
688
+ end
689
+ end
690
+ MULTI_LINE_STRING
691
+ end
692
+
693
+ def custom_control_file(custom_control_name, namespace)
694
+ namespace_type = class_name(namespace) == class_name(current_dir_name) ? 'class' : 'module'
695
+
696
+ <<-MULTI_LINE_STRING
697
+ #{namespace_type} #{class_name(namespace)}
698
+ module View
699
+ class #{class_name(custom_control_name)}
700
+ include Glimmer::LibUI::CustomControl
701
+
702
+ ## Add options like the following to configure CustomControl by outside consumers
703
+ #
704
+ # options :custom_text, :background_color
705
+ # option :foreground_color, default: :red
706
+
707
+ ## Use before_body block to pre-initialize variables to use in body
708
+ #
709
+ #
710
+ # before_body do
711
+ #
712
+ # end
713
+
714
+ ## Use after_body block to setup observers for controls in body
715
+ #
716
+ # after_body do
717
+ #
718
+ # end
719
+
720
+ ## Add control content under custom control body
721
+ ##
722
+ ## If you want to add a window as the top-most control,
723
+ ## consider creating a custom window instead
724
+ ## (Glimmer::LibUI::CustomWindow offers window convenience methods, like show and hide)
725
+ #
726
+ body {
727
+ # Replace example content below with custom control content
728
+ label {
729
+ background :red
730
+ }
731
+ }
732
+
733
+ end
734
+ end
735
+ end
736
+ MULTI_LINE_STRING
737
+ end
738
+
739
+ def custom_shape_file(custom_shape_name, namespace)
740
+ namespace_type = class_name(namespace) == class_name(current_dir_name) ? 'class' : 'module'
741
+
742
+ <<-MULTI_LINE_STRING
743
+ #{namespace_type} #{class_name(namespace)}
744
+ module View
745
+ class #{class_name(custom_shape_name)}
746
+ include Glimmer::LibUI::CustomShape
747
+
748
+ ## Add options like the following to configure CustomShape by outside consumers
749
+ #
750
+ # options :option1, option2, option3
751
+ option :background_color, default: :red
752
+ option :size_width, default: 100
753
+ option :size_height, default: 100
754
+ option :location_x, default: 0
755
+ option :location_y, default: 0
756
+
757
+ ## Use before_body block to pre-initialize variables to use in body
758
+ #
759
+ #
760
+ # before_body do
761
+ #
762
+ # end
763
+
764
+ ## Use after_body block to setup observers for shapes in body
765
+ #
766
+ # after_body do
767
+ #
768
+ # end
769
+
770
+ ## Add shape content under custom shape body
771
+ #
772
+ body {
773
+ # Replace example content below with custom shape content
774
+ shape(location_x, location_y) {
775
+ path {
776
+ background background_color
777
+ cubic size_width - size_width*0.66, size_height/2 - size_height*0.33, size_width*0.65 - size_width*0.66, 0 - size_height*0.33, size_width/2 - size_width*0.66, size_height*0.75 - size_height*0.33, size_width - size_width*0.66, size_height - size_height*0.33
778
+ }
779
+
780
+ path {
781
+ background background_color
782
+ cubic size_width - size_width*0.66, size_height/2 - size_height*0.33, size_width*1.35 - size_width*0.66, 0 - size_height*0.33, size_width*1.5 - size_width*0.66, size_height*0.75 - size_height*0.33, size_width - size_width*0.66, size_height - size_height*0.33
783
+ }
784
+ }
785
+ }
786
+
787
+ end
788
+ end
789
+ end
790
+ MULTI_LINE_STRING
791
+ end
792
+
793
+ end
794
+
795
+ end
796
+
797
+ end
798
+
799
+ end