forked from amirrajan/rubymotion-command
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoctor.rb
1154 lines (1045 loc) · 33 KB
/
doctor.rb
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: utf-8
# Copyright (c) 2017, Scratchwork Development LLC and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
require 'open3'
# require 'pp'
# Represents a set of test data comprising the result of a single test.
#
# Ex: a collection of detected OSX framework versions.
class TestResult
# The name of the test result. Used as the label in test reports.
attr_accessor :name,
# An array of TestDatum objects comprising the TestResult
:data
def initialize name='', data=[]
@name = name
@data = data
end
def add datum
@data << datum
end
end
# Represents a single data point of a TestResult.
class TestDatum
# The value of the test datum.
attr_accessor :value
# The status of the test datum. May be:
#
# * +:good+ - Indicates success or an acceptable value
# * +:maybe+ - Indicates a possible, but not certain problem
# * +:bad+ - Indicates failure or a problematic value
# * +:neutral+ - Indicates an informational result, or that we just don't care
attr_accessor :status
# Meta information/description that we'd like to pass along with the result
# is :status_maybe or :status_bad
attr_accessor :meta
def initialize value=nil, status=:good, meta=nil
@value = value
@status = status
@meta = meta
end
def is_good?
status == :good
end
def is_maybe?
status == :maybe
end
def is_bad?
status == :maybe
end
def is_netural?
status == :neutral
end
end
# Represents the result of a shell command
class CommandResult
# Stdout capture of command
attr_accessor :stdout
# Stderr capture of command
attr_accessor :stderr
# Exit status of the command, *if* it could be executed
attr_accessor :exit
# Overall status of the command. May be:
#
# * +:success+ - command executed with exit code 0
# * +:failure+ - command executed with non-zero exit code
# * +:not_found+ - command not found
# * +:sys_failure+ - command couldn't execute for reasons other than command not found
attr_accessor :status
# The SystemCallError produced if the command couldn't be executed.
attr_accessor :syserror
def initialize command
begin
@stdout, @stderr, @exit = Open3.capture3 command
if @exit == 0
@status = :success
else
@status = :failure
end
rescue Errno::ENOENT => e
@syserror = e
@status = :not_found
rescue SystemCallError => e
@syserror = e
@status = :sys_failure
end
end
def success?
@exit == 0
end
def executed?
@syserror.nil?
end
end
module Motion; class Command
class Doctor < Command
self.summary = 'Troubleshoot RubyMotion installation.'
self.description = "This command will audit your development environment " \
"and make sure it's in a good state."
# =============================================================================
# Output
# =============================================================================
# TODO: proper termcap-fu
LABEL_WIDTH = 33
COLS = 80
ANSI = true
if ANSI
ANSI_PRE_GOOD = "\033[0;32m"
ANSI_PRE_MAYBE = "\033[0;33m"
ANSI_PRE_BAD = "\033[0;31m"
ANSI_POST = "\033[0m"
end
def self.print_report_header title
print "\n"
print_spacer '='
puts title
print_spacer '='
print "\n"
end
def self.print_section_header title
print "\n"
print_spacer '-'
puts title
print_spacer '-'
print "\n"
end
def self.print_spacer fill_string, width=COLS
print ''.ljust(width, fill_string)
print "\n"
end
# Pretty-prints a TestResult object
def self.print_test_result result
result.data.each do |datum|
if datum.equal? result.data.first
print "#{result.name.ljust(LABEL_WIDTH)}: "
else
print(''.ljust(LABEL_WIDTH + 2, ' '))
end
case datum.status
when :good
print "#{ANSI_PRE_GOOD}#{datum.value}"
when :maybe
print "#{ANSI_PRE_MAYBE}#{datum.value}"
print " (#{datum.meta})" unless datum.meta.to_s.empty?
when :bad
print "#{ANSI_PRE_BAD}#{datum.value}"
print " (#{datum.meta})" unless datum.meta.to_s.empty?
else
print datum.value
end
print "#{ANSI_POST}\n"
end
end
# =============================================================================
# Environment
# =============================================================================
# Determines the OSX version using `sw_vers`
#
# ==== Return
#
# A Hash describing the OSX version if `sw_vers` succeeds, or describing the
# failure otherwise.
def self.sense_osx
cmd = CommandResult.new 'sw_vers'
case cmd.status
when :success
lines = cmd.stdout.split("\n")
major = lines[1].sub(/^ProductVersion:\W+(\d+)/, '\1').to_i
minor = lines[1].sub(/^ProductVersion:\W+\d+\.(\d+)/, '\1').to_i
versions_to_code_names = {
'10.0' => 'Cheeta',
'10.1' => 'Puma',
'10.2' => 'Jaguar',
'10.3' => 'Panther',
'10.4' => 'Tiger',
'10.5' => 'Leopard',
'10.6' => 'Snow Leopard',
'10.7' => 'Lion',
'10.8' => 'Mountain Lion',
'10.9' => 'Mavericks',
'10.10' => 'Yosemite',
'10.11' => 'El Capitan',
'10.12' => 'Sierra',
'10.13' => 'High Sierra',
'10.14' => 'Mojave',
}
{
:state => :present,
:version => {
:major => major,
:minor => minor,
:very_minor => lines[1].sub(/^ProductVersion:\W+\d+\.\d+\.(\d+)/, '\1').to_i,
:build => lines[2].sub(/^BuildVersion:\W+(\w+)/, '\1'),
:code_name => versions_to_code_names["#{major}.#{minor}"],
}
}
when :not_found
{
:state => :absent,
}
when :failure
{
:state => :failed,
:fail_source => 'sw_vers',
:err => cmd.stderr,
}
when :sys_failure
{
:state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message,
}
end
end
# Tests the OSX version.
#
# ==== Attributes
#
# * +osx+ - The OSX state as reported by +sense_osx+
#
# ==== Return
#
# A TestResult object.
def self.test_osx_version osx
result = TestResult.new 'OSX version'
case osx[:state]
when :present
very_minor_component =
".#{osx[:version][:very_minor]}" if osx[:version][:very_minor] > 0
code_name_component =
" (#{osx[:version][:code_name]})" if osx[:version][:code_name]
result.add(
TestDatum.new "#{osx[:version][:major]}" \
".#{osx[:version][:minor]}" \
"#{very_minor_component}" \
"#{code_name_component}")
if osx[:version][:major] < 10 or
(osx[:version][:major] == 10 and osx[:version][:minor] < 12)
result.data[0].status = :bad
end
when :absent
result.add(
TestDatum.new('Indeterminate',
:bad,
'`sw_vers` not found'))
when :failed
result.add(
TestDatum.new('Indeterminate',
:bad,
"#{osx[:fail_source]} reports: #{osx[:err]}"))
end
result
end
# Determines the working directory
def self.get_working_directory
Dir.pwd
end
# Tests the working directory.
#
# ==== Attributes
#
# * +wd+ - The working directory as reported by +get_working_directory+
#
# ==== Return
#
# A TestResult object.
def self.test_working_directory wd
TestResult.new(
'Working directory',
[TestDatum.new(wd, :neutral)])
end
# =============================================================================
# Installation/Toolchain
# =============================================================================
# -----------------------------------------------------------------------------
# RubyMotion
# -----------------------------------------------------------------------------
# version/presence
# ----------------
# Detects the presence of the `motion` command and, if present, determines
# its version.
#
# ==== Return
#
# A Hash describing the state of the `motion` command.
def self.sense_rubymotion
cmd = CommandResult.new 'motion --version'
case cmd.status
when :success
{
:state => :present,
:version => {
:major => cmd.stdout.chop.sub(/^(\d+).\d+$/, '\1').to_i,
:minor => cmd.stdout.chop.sub(/^\d+.(\d+)$/, '\1').to_i,
}
}
when :not_found
{
:state => :absent,
}
when :failure
{
:state => :failed,
:fail_source => 'motion',
:err => cmd.stderr,
}
when :sys_failure
{
:state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message,
}
end
end
# Tests the RubyMotion version.
#
# ==== Attributes
#
# * +motion+ - The RubyMotion status information, as produced by +sense_rubymotion+.
#
# ==== Return
#
# A +TestResult+ object
def self.test_rubymotion_version motion
# TODO: are some versions considered deprecated? EOL?
result = TestResult.new 'RubyMotion version'
case motion[:state]
when :present
result.add TestDatum.new("#{motion[:version][:major]}" \
".#{motion[:version][:minor]}",
:neutral)
when :absent
result.add TestDatum.new(
'Not found',
:bad)
when :failed
result.add TestDatum.new(
'Failed',
:bad,
"#{motion[:fail_source]} reports: '#{motion[:error]}'")
end
result
end
# available SDKs
# --------------
# Determines which SDK versions are present for the specified framework
#
# ==== Attributes
#
# * +framework_name+ - The 'pretty' name of the framework. Ex: "iOS"
# * +framework_subdir+ - The subdirectory name for the framework. Ex: "ios"
#
# ==== Return
#
# An array of Hashes, each of which describes a detected SDK version.
def self.get_rubymotion_sdks framework_name,
framework_subdir=framework_name.downcase
rm_data_path = '/Library/RubyMotion/data'
framework_path = "#{rm_data_path}/#{framework_subdir}"
return [] unless File.directory? framework_path
sdks = []
Dir.entries(framework_path).each do |entry|
if File.directory? "#{framework_path}/#{entry}" and
entry.match('^\d+(\.\d+)*$')
entry_split = entry.split('.')
sdk_version = {
:major => entry_split[0].to_i,
:minor => entry_split[1].to_i,
:very_minor => entry_split[2].to_i,
}
sdks << sdk_version
end
end
sdks
end
# Tests the available SDK versions for a framework
#
# ==== Attributes
#
# * +sdks+ - An array of SDK versions, as produced by +get_rubymotion_sdks+
# * +framework_name+ - The 'pretty' name of the framework. Ex: "iOS"
#
# ==== Return
#
# A TestResult object
def self.test_rubymotion_sdks sdks,
framework_name
# TODO: Are any of the frameworks considered mandatory?
result = TestResult.new "Supported #{framework_name} frameworks"
sdks.each do |sdk|
minor_component = ".#{sdk[:minor]}" if sdk[:minor] > 0
very_minor_component = ".#{sdk[:very_minor]}" if sdk[:very_minor] > 0
result.add(
TestDatum.new(
"#{sdk[:major]}#{minor_component}#{very_minor_component}",
:neutral))
end
if result.data.count == 0
result.add(TestDatum.new 'None', :neutral)
end
result
end
# -----------------------------------------------------------------------------
# rbenv
# -----------------------------------------------------------------------------
# version
# -------
# Detects the presence of the `rbenv` command and, if present, determines
# its version.
#
# ==== Return
#
# A Hash describing the state of the `rbenv` command.
def self.sense_rbenv
cmd = CommandResult.new 'rbenv --version'
case cmd.status
when :success
version_split = cmd.stdout.split(' ')[1].split('.')
{
:state => :present,
:version => {
:major => version_split[0],
:minor => version_split[1],
:very_minor => version_split[2],
}
}
when :not_found
{
:state => :absent,
}
when :failure
{
:state => :failed,
:fail_source => :cmd,
:err => cmd.stderr,
}
when :sys_failure
{
:state => :failed,
:fail_source => :system,
:err => cmd.syserror.message,
}
end
end
# Tests the `rbenv` version.
#
# ==== Attributes
#
# * +rbenv+ - The `rbenv` status information, as produced by +sense_rbenv+.
#
# ==== Return
#
# A +TestResult+ object
def self.test_rbenv_version rbenv
result = TestResult.new 'rbenv version'
case rbenv[:state]
when :present
result.add(
TestDatum.new "#{rbenv[:version][:major]}" \
".#{rbenv[:version][:minor]}" \
".#{rbenv[:version][:very_minor]}",
:neutral)
when :absent
result.add(
TestDatum.new 'Not found',
:maybe,
'Recommended, but not required')
when :failed
# Even though rbenv isn't required, it's a problem if it's broken
result.add TestDatum.new(
'Not found',
:bad,
"#{rbenv[:fail_source]} reports: '#{rbenv[:error]}'")
end
result
end
# rbenv Ruby versions
# -------------------
# Determines which versions of Ruby are provided by `rbenv`.
# its version.
#
# ==== Attributes
#
# * +rbenv_state+ - The state of the `rbenv` command, as reported by +sense_rbenv+.
#
# ==== Return
#
# If `rbenv` is present, an Array of hashes, each of which describes a Ruby
# version. Otherwise, an empty Array.
def self.get_rbenv_ruby_versions rbenv_state
return [] unless rbenv_state == :present
# TODO: do we consider "system" to be relevant? `rbenv versions --bare`
# omits it, since it isn't supplied by rbenv
cmd = CommandResult.new 'rbenv versions --bare'
lines = cmd.stdout.split("\n")
versions = []
lines.each do |version_str|
version_split = version_str.split('.')
version = {
:major => version_split[0].to_i,
:minor => version_split[1].to_i,
:very_minor => version_split[2].to_i,
}
versions << version
end
versions
end
# Tests the versions of Ruby provided by `rbenv`.
#
# ==== Attributes
#
# * +versions+ - The array of Ruby versions, as produced by +get_rbenv_ruby_versions+.
#
# ==== Return
#
# A +TestResult+ object
def self.test_rbenv_ruby_versions versions
result = TestResult.new 'rbenv-supplied Ruby versions'
if versions.nil?
result.add(
TestDatum.new(
'None',
:neutral))
else
versions.each do |version|
result.add(
TestDatum.new(
"#{version[:major]}.#{version[:minor]}.#{version[:very_minor]}",
:neutral))
end
end
result
end
# -----------------------------------------------------------------------------
# Xcode
# -----------------------------------------------------------------------------
# xcode-select
# ------------
# Detects the presence of the `xcode-select` command and, if present,
# determines its version.
#
# ==== Return
#
# A Hash describing the state of the `xcode-select` command.
def self.sense_xcode_select
cmd = CommandResult.new 'xcode-select --version'
case cmd.status
when :success
{
:state => :present,
:version => cmd.stdout.chop.sub(
/^xcode-select version ([\.\d]+)\.$/,
'\1').to_i
}
when :not_found
{ :state => :absent }
when :failure
{ :state => :failed,
:fail_source => 'xcode-select',
:err => cmd.stderr }
when :sys_failure
{ :state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message }
end
end
# Tests the versions of xcode-select.
#
# ==== Attributes
#
# * +xcode_select+ - The `xcode-select` state as produced by +sense_xcode_select+
#
# ==== Return
#
# A +TestResult+ object
def self.test_xcode_select_version xcode_select
result = TestResult.new 'xcode-select version'
case xcode_select[:state]
when :present
# TODO:
# According to https://github.com/amirrajan/rubymotion-applied/issues/58
# Xcode 9.2 should be paired with 2349. As far as I can tell from my own system,
# that's still the xcode-select version present as late as 9.4.1 (latest non-beta)
#
# Since the RM version parities only go as far back as Xcode 9.2, which also
# requires 2349, I think maybe we just unconditionally want 2349 now? I'm
# also not entirely clear on what exactly controls the xcode-select
# version. I'm pretty sure this stands alone from Xcode, and would be
# relegated to the OSX version.
result.add TestDatum.new(xcode_select[:version])
result.data[0].status = :bad unless xcode_select[:version] == 2349
when :absent
result.add TestDatum.new(
'Not found',
:bad)
when :failed
result.add TestDatum.new(
'Failed',
:bad,
"#{motion[:fail_source]} reports: '#{motion[:error]}'")
end
result
end
# Determines the active developer directory. I.e. `xcode-select --print-path`
#
# ==== Attributes
#
# * +xcode_select_state+ - The state of `xcode-select` as determined by
# +sense_xcode_select+
#
# ==== Return
#
# A string containing the Xcode path, if Xcode is installed. Otherwise,
# a string indicating that Xcode is not present.
def self.get_xcode_path xcode_select_state
return 'Xcode not installed' unless xcode_select_state == :present
(CommandResult.new 'xcode-select --print-path').stdout.chop
end
# Tests the active developer directory (i.e. Xcode directory).
#
# ==== Attributes
#
# * +path+ - The path as reported by +get_xcode_path+
#
# ==== Return
#
# A TestResult object
def self.test_xcode_path path
result = TestResult.new 'Xcode path'
case path
when /CommandLineTools/
result.add(
TestDatum.new(
path,
:bad,
'path indicates a CLI-tool-only Xcode installation'))
when /Xcode\.app/
result.add(TestDatum.new path)
when /Xcode-beta\.app/
result.add(
TestDatum.new(
path,
:maybe,
'path indicates a beta Xcode installation'))
else
# TODO: I'm not sure what exactly constitutes a valid
# path. `xcode-select -s` won't let you set an invalid path, but that's
# not to say that the path might point to an unsuitable version of
# Xcode in spite of a passing `xcodebuild -version` result in
# 'test_xcode_version'
result.add(
TestDatum.new(
path,
:maybe,
'custom path detected'))
end
result
end
# Detects the presence of Xcode and, if present, determines its version.
#
# ==== Attributes
#
# * +xcode_path+ - The Xcode path as reported by +get_xcode_path+
#
# ==== Return
#
# A Hash describing the state of Xcode.
def self.sense_xcode xcode_path
# Bail early if xcode-select doesn't give us a valid path. This indicates
# that Xcode isn't installed at all. Running `xcodebuild` in this case
# won't work, and will only prompt the user to install Xcode in a modal
# dialog.
return { :state => :absent } unless xcode_path
cmd = CommandResult.new 'xcodebuild -version'
case cmd.status
when :success
version_split = cmd.stdout.split("\n")[0].split(' ')[1].split('.')
{
:state => :present,
:version => {
:major => version_split[0].to_i,
:minor => version_split[1].to_i,
:very_minor => version_split[2].to_i,
}
}
when :not_found
{ :state => :absent }
when :failure
{ :state => :failed,
:fail_source => 'xcodebuild',
:err => cmd.stderr, }
when :sys_failure
{ :state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message, }
end
end
# Tests the version of Xcode against the RubyMotion version for version parity.
#
# ==== Attributes
#
# * +xcode+ - The Xcode state as reported by +sense_xcode+
# * +rm_version+ - The installed version of RubyMotion as detected by +sense_rubymotion
#
# ==== Return
#
# A TestResult object
def self.test_xcode_version xcode, rm_version
result = TestResult.new 'Xcode version'
if xcode[:state] != :present
then
result.add(
TestDatum.new(
'Not installed',
:bad))
return result
end
rm_5_7 = { :major => 5, :minor => 7 }
rm_5_8 = { :major => 5, :minor => 8 }
rm_5_9 = { :major => 5, :minor => 9 }
rm_5_10 = { :major => 5, :minor => 10 }
xc_9_2 = { :major => 9, :minor => 2, :very_minor => 0 }
xc_9_3 = { :major => 9, :minor => 3, :very_minor => 0 }
xc_9_4 = { :major => 9, :minor => 4, :very_minor => 0 }
rm_to_xcode_parities = {
rm_5_7 => xc_9_2,
rm_5_8 => xc_9_3,
rm_5_9 => xc_9_4,
rm_5_10 => xc_9_4,
}
expected_version = rm_to_xcode_parities[rm_version]
# If the RubyMotion version wasn't detected, assume we want 9.4
expected_version ||= xc_9_4
very_minor_component =
".#{xcode[:version][:very_minor]}" if xcode[:version][:very_minor] > 0
datum = TestDatum.new(
"#{xcode[:version][:major]}." \
"#{xcode[:version][:minor]}" \
"#{very_minor_component}")
if xcode[:version] != expected_version
datum.meta = "expected #{expected_version[:major]}" \
".#{expected_version[:minor]}" \
".#{expected_version[:very_minor]}"
datum.status =
# Having a very minor version above the expected is *maybe* okay.
# TODO: This might be perfectly fine. Ask Amir. 9.4.1 is current, so
# this is going to flag for a decent number of people.
if xcode[:version][:major] = expected_version[:major] and
xcode[:version][:minor] = expected_version[:minor] and
xcode[:version][:very_minor] > expected_version[:very_minor]
:maybe
else
:bad
end
end
result.add datum
result
end
# -----------------------------------------------------------------------------
# Java
# -----------------------------------------------------------------------------
# Detects the presence of the `javac` command and, if present, determines
# its version.
#
# ==== Return
#
# A Hash describing the state of the `javac` command.
def self.sense_java
cmd = CommandResult.new 'javac -version'
case cmd.status
when :success
# TIL: javac spits out -version to stderr...
version_split = cmd.stderr.chop.split(' ')[1].split('.')
{
:state => :present,
:version => {
:major => version_split[0].to_i,
:minor => version_split[1].to_i,
:very_minor => version_split[2].sub(/^(\d+)_\d+$/, '\1').to_i,
:build => version_split[2].sub(/^\d+_(\d+)$/, '\1').to_i,
}
}
when :not_found
{
:state => :absent,
}
when :failure
{
:state => :failed,
:fail_source => 'motion',
:err => cmd.stderr,
}
when :sys_failure
{
:state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message,
}
end
end
# Tests the Java version.
#
# ==== Attributes
#
# * +java+ - The Java status information, as produced by +sense_java+.
#
# ==== Return
#
# A +TestResult+ object
def self.test_java_version java
result = TestResult.new 'Java version'
case java[:state]
when :present
result.add(
TestDatum.new(
"#{java[:version][:major]}" \
".#{java[:version][:minor]}" \
".#{java[:version][:very_minor]}" \
"_#{java[:version][:build]}",
:neutral))
# TODO: minimum Java? 1.8 preferred? I think the guide is out of date.
# result.data[0].status = :bad if java[:version][:minor] < 7
when :absent
result.add TestDatum.new(
'Not found',
:bad)
when :failed
result.add TestDatum.new(
'Failed',
:bad,
"#{java[:fail_source]} reports: '#{motion[:error]}'")
end
result
end
# Gets the JAVA_HOME environment variable.
def self.get_java_home
ENV['JAVA_HOME']
end
# Tests the JAVA_HOME environment variable.
#
# ==== Attributes
#
# * +home+ - The JAVA_HOME value
#
# ==== Return
#
# A +TestResult+ object
def self.test_java_home home
result = TestResult.new 'Java home'
if home
result.add(TestDatum.new home, :neutral)
else
result.add(TestDatum.new 'Not set', :bad)
end
result
end
# =============================================================================
# Project
# =============================================================================
def self.get_rake_config
cmd = CommandResult.new 'rake config'
case cmd.status
when :success
config = {}
cmd.stdout.split("\n").each do |line|
key = line.sub(/^(\w+)\W.*$/, '\1').to_sym
# TODO: Warning, danger Wil Robinson
value = eval line.sub(/^\w+\W+:[ \t]+(.*)$/, '\1')
config[key] = value
end
config
when :not_found
{
:state => :absent,
}
when :failure
{
:state => :failed,
:fail_source => 'rake',
:err => cmd.stderr,
}
when :sys_failure
{
:state => :failed,
:fail_source => 'system',
:err => cmd.syserror.message,
}
end
end
# =============================================================================
# Get{Foo}/Test{Foo}
# =============================================================================
# Environment
# -----------
def self.get_environment_data
{
:osx => sense_osx,