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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
|
# File 'lib/mysigner/cli/diagnostic_commands.rb', line 7
def self.included(base)
base.class_eval do
desc 'doctor', 'đŠē Run health check and diagnose setup issues (run this if stuck)'
method_option :platform, type: :string, desc: 'Check specific platform only: ios, android, or all (default)'
def doctor
say 'đŠē My Signer Health Check', :cyan
say '=' * 80, :cyan
say ''
issues = []
warnings = []
platform_filter = options[:platform]&.downcase
check_ios = platform_filter.nil? || platform_filter == 'all' || platform_filter == 'ios'
check_android = platform_filter.nil? || platform_filter == 'all' || platform_filter == 'android'
if platform_filter && !%w[ios android all].include?(platform_filter)
error "Invalid platform: #{platform_filter}"
say 'Valid options: ios, android, all', :yellow
exit 1
end
if check_ios
say 'Checking Xcode...', :yellow
if system('which xcodebuild > /dev/null 2>&1')
xcode_version = begin
`xcodebuild -version`.lines.first.strip
rescue StandardError
'Unknown'
end
say " â Xcode installed: #{xcode_version}", :green
else
say ' â Xcode not found', :red
issues << 'Xcode is not installed or not in PATH'
end
say ''
say 'Checking Command Line Tools...', :yellow
if system('xcode-select -p > /dev/null 2>&1')
say ' â Command Line Tools installed', :green
else
say ' â Command Line Tools not found', :red
issues << 'Install with: xcode-select --install'
end
say ''
say 'Checking upload tools...', :yellow
if system('xcrun --find altool > /dev/null 2>&1')
say ' â xcrun altool available', :green
else
say ' â ī¸ xcrun altool not found', :yellow
warnings << 'altool not available (upload may fail)'
end
transporter_paths = [
'/Applications/Xcode.app/Contents/Developer/usr/bin/iTMSTransporter',
'/Applications/Transporter.app/Contents/itms/bin/iTMSTransporter'
]
transporter_found = transporter_paths.any? { |path| File.exist?(path) }
if transporter_found
say ' â iTMSTransporter available (fallback)', :green
else
say ' â ī¸ iTMSTransporter not found (optional)', :yellow
end
say ''
say 'Checking My Signer configuration...', :yellow
client = nil
org_data = nil
config = if Config.env_configured?
Config.from_env
else
Config.new
end
if config.from_env? || config.exists?
config.load unless config.from_env?
say config.from_env? ? ' â Configured via environment variables' : ' â Logged in', :green
begin
client = Client.new(api_url: config.api_url, api_token: config.api_token,
user_email: config.user_email)
client.test_connection
say ' â API connection working', :green
if config.current_organization_id
org_response = client.get("/api/v1/organizations/#{config.current_organization_id}")
org_data = org_response[:data]
end
rescue Mysigner::UnauthorizedError
say ' â Token is invalid or expired', :red
issues << "Token authentication failed - run 'mysigner onboard' to re-authenticate"
client = nil
rescue Mysigner::ConnectionError => e
say " â Cannot connect to API: #{e.message}", :red
issues << 'API connection failed - check your network or API URL'
client = nil
rescue StandardError => e
say " â API error: #{e.message}", :red
issues << 'API connection failed - check your configuration'
client = nil
end
else
say ' â Not logged in', :red
issues << "Run 'mysigner onboard' to authenticate"
end
say ''
if client && org_data && org_data['app_store_connect_team_id']
say 'Checking signing identity for team...', :yellow
team_id = org_data['app_store_connect_team_id']
identities = `security find-identity -v -p codesigning 2>/dev/null | grep -i "#{team_id}"`
has_identity = $CHILD_STATUS.success? && !identities.strip.empty?
if has_identity
say " â Signing identity found for team #{team_id}", :green
else
say " â No signing identity for team #{team_id}", :red
say ''
say ' CRITICAL: You need to sign into Xcode and download certificates:', :red
say ' 1. Open Xcode â Settings â Accounts', :yellow
say " 2. Verify you're signed in with your Apple ID", :yellow
say " 3. Select team '#{team_id}'", :yellow
say " 4. Click 'Download Manual Profiles' (or 'Manage Certificates')", :yellow
say ' 5. The certificate should appear in keychain', :yellow
say ''
issues << "No signing identity for team #{team_id} in keychain"
end
say ''
end
if client && org_data
say 'Checking App Store Connect credentials...', :yellow
creds_status = org_data['credentials_status'] || {}
if creds_status['needs_setup'] || !org_data['app_store_connect_configured']
say ' â App Store Connect not configured', :red
say ''
if yes_with_default?('Would you like to set it up now?', :green)
say ''
if respond_to?(:setup_app_store_connect_credentials, true)
asc_configured = setup_app_store_connect_credentials(client, config,
config.current_organization_id)
else
say ' â Setup helper not available', :red
say " Please run 'mysigner onboard' instead", :yellow
asc_configured = false
end
say ''
if asc_configured
say ' â App Store Connect configured successfully!', :green
say ''
org_response = client.get("/api/v1/organizations/#{config.current_organization_id}")
org_data = org_response[:data]
else
issues << "App Store Connect setup incomplete - run 'mysigner onboard' to try again"
end
else
issues << "App Store Connect not configured - run 'mysigner onboard' to set it up"
end
elsif !creds_status['team_id_set']
say ' â ī¸ Team ID not set', :yellow
warnings << 'Team ID missing - may cause issues. Re-sync to extract it.'
else
say ' â App Store Connect configured', :green
say " Team ID: #{org_data['app_store_connect_team_id']}", :cyan if org_data['app_store_connect_team_id']
end
say ''
end
say 'Checking Xcode license...', :yellow
begin
license_check = `sudo -n xcodebuild -checkFirstLaunchStatus 2>&1`
license_status = $CHILD_STATUS.success?
if license_status
say ' â Xcode license accepted', :green
elsif license_check.include?('password') || license_check.include?('sudo')
say ' âšī¸ Cannot check license (needs sudo)', :cyan
else
say ' â ī¸ Xcode license may not be accepted', :yellow
say ' Run: sudo xcodebuild -license accept', :cyan
warnings << 'Xcode license may need acceptance'
end
rescue StandardError
say ' âšī¸ Could not check Xcode license', :cyan
end
say ''
say 'Checking disk space...', :yellow
begin
df_output = `df -h . 2>/dev/null | tail -1`.strip
if df_output =~ /(\d+)%/
usage = ::Regexp.last_match(1).to_i
if usage > 95
say " â Critical: Disk space very low (#{usage}% used)", :red
issues << 'Free up disk space before building'
elsif usage > 90
say " â ī¸ Low disk space: #{usage}% used", :yellow
warnings << 'Low disk space may cause build failures'
else
say " â Sufficient disk space: #{usage}% used", :green
end
else
say ' â ī¸ Could not check disk space', :yellow
end
rescue StandardError
say ' â ī¸ Could not check disk space', :yellow
end
say ''
say 'Checking network connectivity...', :yellow
begin
require 'socket'
Socket.tcp('apple.com', 443, connect_timeout: 5, &:close)
say ' â Internet connection working', :green
rescue StandardError => e
say ' â No internet connection', :red
issues << 'Cannot reach Apple servers - check your network connection'
end
say ''
say 'Checking current directory...', :yellow
project_info = nil
begin
project_info = Build::Detector.detect
framework = case project_info[:framework]
when :capacitor then 'Capacitor/Ionic'
when :react_native then 'React Native'
when :flutter then 'Flutter'
else 'Native iOS'
end
say " â Found #{framework} project: #{File.basename(project_info[:path])}", :green
rescue StandardError
say ' âšī¸ No project detected in current directory', :cyan
end
say ''
if client && org_data && org_data['app_store_connect_configured']
say 'Checking organization resources...', :yellow
stats = org_data['stats'] || {}
certs_count = stats['certificates_count'] || 0
if certs_count.zero?
say ' â ī¸ No certificates synced', :yellow
warnings << 'Run sync in web dashboard to fetch certificates from Apple'
else
say " â Certificates: #{certs_count}", :green
end
devices_count = stats['devices_count'] || 0
if devices_count.zero?
say ' â ī¸ No devices registered', :yellow
warnings << 'Add devices for development/adhoc builds'
else
say " â Devices: #{devices_count}", :green
end
bundle_ids_count = stats['bundle_ids_count'] || 0
if bundle_ids_count.zero?
say ' â ī¸ No bundle IDs synced', :yellow
say ' This is normal for new accounts', :cyan
else
say " â Bundle IDs: #{bundle_ids_count}", :green
end
profiles_count = stats['profiles_count'] || 0
invalid_profiles = stats['invalid_profiles_count'] || 0
if profiles_count.zero?
say ' â ī¸ No provisioning profiles', :yellow
warnings << 'Create profiles for your projects'
elsif invalid_profiles.positive?
say " â Profiles: #{profiles_count} (â ī¸ #{invalid_profiles} invalid)", :yellow
warnings << "#{invalid_profiles} profile(s) invalid - may need regeneration"
else
say " â Profiles: #{profiles_count}", :green
end
say ''
end
if project_info && client && org_data && org_data['app_store_connect_configured']
say 'Checking project signing setup...', :yellow
begin
parser = Build::Parser.new(project_info)
main_target = parser.main_target
if main_target
target_name = main_target.name
bundle_id = parser.bundle_id(target_name, 'Release')
say " Project: #{target_name}", :cyan
say " Bundle ID: #{bundle_id}", :cyan
say ''
say ' Checking bundle ID registration...', :yellow
bundle_ids_response = client.get(
"/api/v1/organizations/#{config.current_organization_id}/bundle_ids",
params: { q: bundle_id }
)
bundle_id_exists = (bundle_ids_response[:data]['bundle_ids'] || []).any? do |bid|
bid['identifier'] == bundle_id
end
if bundle_id_exists
say ' â Bundle ID registered', :green
say ' Checking provisioning profiles...', :yellow
profiles_response = client.get(
"/api/v1/organizations/#{config.current_organization_id}/profiles",
params: { bundle_id: bundle_id }
)
profiles = profiles_response[:data]['profiles'] || []
appstore_profiles = profiles.select do |p|
p['profile_type'] == 'IOS_APP_STORE' && p['state'] == 'ACTIVE'
end
if appstore_profiles.empty?
say " â No App Store provisioning profile for #{bundle_id}", :red
say ''
if yes_with_default?('Create App Store profile automatically?', :green)
say ''
auto_create_profile(client, config, bundle_id, 'appstore')
else
issues << "Missing App Store profile for #{bundle_id} - run 'mysigner signing configure'"
end
else
say ' â App Store provisioning profile exists', :green
expired = appstore_profiles.select do |p|
expires_at = begin
Time.parse(p['expires_at'])
rescue StandardError
nil
end
expires_at && expires_at < Time.now
end
if expired.any?
say " â ī¸ #{expired.length} profile(s) expired", :yellow
warnings << 'Some profiles are expired - sync to refresh'
end
end
dev_profiles = profiles.select do |p|
p['profile_type'] == 'IOS_APP_DEVELOPMENT' && p['state'] == 'ACTIVE'
end
if dev_profiles.empty?
say ' â ī¸ No Development profile (optional but recommended)', :yellow
say ''
say ' đą Development profiles let you:', :cyan
say ' âĸ Test your app on physical devices (iPhone/iPad)', :cyan
say ' âĸ Debug before uploading to TestFlight', :cyan
say ' âĸ Share with your team for testing', :cyan
say ''
if yes_with_default?('Create Development profile for local testing?', :yellow)
say ''
auto_create_profile(client, config, bundle_id, 'development')
else
warnings << "No development profile - you won't be able to test on devices"
end
else
say ' â Development profile exists', :green
end
else
say " â Bundle ID '#{bundle_id}' not registered in App Store Connect", :red
say ''
all_bundle_ids = bundle_ids_response[:data]['bundle_ids'] || []
if all_bundle_ids.any?
say ' Registered bundle IDs in your organization:', :cyan
all_bundle_ids.first(5).each do |bid|
say " âĸ #{bid['identifier']}", :cyan
end
say " ... and #{all_bundle_ids.length - 5} more", :cyan if all_bundle_ids.length > 5
say ''
end
say ' Options:', :yellow
say " A. Register '#{bundle_id}' in App Store Connect:", :yellow
say ' 1. Go to: https://developer.apple.com/account/resources/identifiers/add', :cyan
say " 2. Select 'App IDs'", :cyan
say " 3. Register: #{bundle_id}", :cyan
say ' 4. Sync in web dashboard', :cyan
say " 5. Run 'mysigner doctor' again", :cyan
say ''
say ' B. Or change your Xcode project to use an existing bundle ID', :yellow
say ''
issues << "Bundle ID #{bundle_id} not registered in App Store Connect"
end
end
rescue StandardError => e
say " â ī¸ Could not check project signing: #{e.message}", :yellow
warnings << 'Project signing check failed'
end
say ''
elsif project_info && (!client || !org_data)
say 'â ī¸ Project detected but cannot check signing (not logged in)', :yellow
say ''
end
end
if check_android
say 'Checking Android development environment...', :yellow
android_available = false
if system('which java > /dev/null 2>&1')
java_version = begin
`java -version 2>&1`.lines.first.strip
rescue StandardError
'Unknown'
end
say " â Java installed: #{java_version}", :green
android_available = true
java_home = ENV.fetch('JAVA_HOME', nil)
if java_home && !java_home.empty?
if Dir.exist?(java_home)
say " â JAVA_HOME: #{java_home}", :green
else
say " â JAVA_HOME invalid: #{java_home}", :red
detected_java_home = detect_java_home
if detected_java_home
say " đĄ Detected valid Java at: #{detected_java_home}", :yellow
say ''
if yes_with_default?(' Would you like to fix JAVA_HOME in your shell config?', :green)
fix_java_home(detected_java_home)
else
say ' To fix manually, add to ~/.zshrc:', :yellow
say " export JAVA_HOME=#{detected_java_home}", :cyan
issues << 'JAVA_HOME points to non-existent directory'
end
else
issues << 'JAVA_HOME points to non-existent directory and no Java found'
end
end
else
detected_java_home = detect_java_home
if detected_java_home
say ' â ī¸ JAVA_HOME not set', :yellow
say " đĄ Detected Java at: #{detected_java_home}", :yellow
say ''
if yes_with_default?(' Would you like to set JAVA_HOME in your shell config?', :green)
fix_java_home(detected_java_home)
else
warnings << "JAVA_HOME not set (recommended: export JAVA_HOME=#{detected_java_home})"
end
else
say ' â ī¸ JAVA_HOME not set', :yellow
end
end
else
say ' âšī¸ Java not found (required for Android)', :cyan
end
android_home = ENV['ANDROID_HOME'] || ENV.fetch('ANDROID_SDK_ROOT', nil)
if android_home && Dir.exist?(android_home)
say " â Android SDK: #{android_home}", :green
android_available = true
else
say ' âšī¸ Android SDK not found (set ANDROID_HOME)', :cyan
end
if system('which gradle > /dev/null 2>&1') || (android_home && File.exist?("#{android_home}/../gradle"))
gradle_version = begin
`gradle --version 2>&1 | grep 'Gradle '`.strip
rescue StandardError
''
end
if gradle_version.empty?
say ' â Gradle available (version check skipped)', :green
else
say " â #{gradle_version}", :green
end
else
say ' âšī¸ Gradle not found (will use project gradlew)', :cyan
end
say ''
if client && org_data
say 'Checking Google Play configuration...', :yellow
if org_data['google_play_configured']
say ' â Google Play credentials configured', :green
else
say ' âšī¸ Google Play not configured', :cyan
say " Configure in My Signer dashboard or run 'mysigner doctor'", :cyan
end
begin
require_relative '../signing/keystore_manager'
manager = Signing::KeystoreManager.new(client, config.current_organization_id)
keystores = manager.list
if keystores.any?
active = keystores.find { |k| k['active'] }
if active
say " â Active keystore: #{active['name']}", :green
else
say " â ī¸ #{keystores.count} keystores, none active", :yellow
warnings << 'No active keystore - activate one with: mysigner keystore activate ID'
end
else
say ' âšī¸ No Android keystores', :cyan
say ' Upload with: mysigner keystore upload PATH', :cyan
end
rescue StandardError => e
say " â ī¸ Could not check keystores: #{e.message}", :yellow
end
say ''
end
nil
begin
android_project = Build::Detector.detect_android
framework = case android_project[:framework]
when :capacitor then 'Capacitor/Ionic'
when :react_native then 'React Native'
when :flutter then 'Flutter'
else 'Native Android'
end
say 'Checking Android project...', :yellow
say " â Found #{framework} Android project", :green
require_relative '../build/android_parser'
parser = Build::AndroidParser.new(android_project)
say " Package: #{parser.application_id}", :cyan
say " Version: #{parser.version_name} (#{parser.version_code})", :cyan
say " Gradle wrapper: #{parser.gradle_wrapper_exists? ? 'â' : 'â'}", :cyan
say ''
rescue Build::Detector::NoProjectError
rescue StandardError => e
say " â ī¸ Could not analyze Android project: #{e.message}", :yellow if android_available
end
end
say '=' * 80, :cyan
say 'Health Report', :bold
say '=' * 80, :cyan
say ''
if issues.empty? && warnings.empty?
say "đ All checks passed! You're good to go!", :green
say ''
say 'Try: mysigner ship testflight', :cyan
elsif issues.empty?
say "â ī¸ #{warnings.length} warning(s), but you're mostly good!", :yellow
say ''
warnings.each do |warning|
say " âĸ #{warning}", :yellow
end
else
say "â #{issues.length} issue(s) found:", :red
say ''
issues.each do |issue|
say " âĸ #{issue}", :red
end
if warnings.any?
say ''
say "â ī¸ #{warnings.length} warning(s):", :yellow
warnings.each do |warning|
say " âĸ #{warning}", :yellow
end
end
end
say ''
end
no_commands do
def yes_with_default?(statement, color = nil)
unless $stdin.tty?
say "#{statement} [Y/n] (non-interactive: assuming no)", color
return false
end
response = ask("#{statement} [Y/n]", color).to_s.strip.downcase
response.empty? || response == 'y' || response == 'yes'
end
def generate_csr(email)
require 'openssl'
say ' Generating CSR...', :cyan
begin
csr_dir = File.expand_path('~/Downloads')
FileUtils.mkdir_p(csr_dir)
key = OpenSSL::PKey::RSA.new(2048)
csr = OpenSSL::X509::Request.new
csr.version = 0
csr.subject = OpenSSL::X509::Name.new([
['CN', email || 'My Signer User'],
['emailAddress', email || 'user@example.com']
])
csr.public_key = key.public_key
csr.sign(key, OpenSSL::Digest.new('SHA256'))
timestamp = Time.now.strftime('%Y%m%d_%H%M%S')
csr_filename = "CertificateSigningRequest_#{timestamp}.certSigningRequest"
key_filename = "private_key_#{timestamp}.pem"
csr_path = File.join(csr_dir, csr_filename)
key_dir = File.expand_path('~/.mysigner/keys')
FileUtils.mkdir_p(key_dir)
key_path = File.join(key_dir, key_filename)
File.write(csr_path, csr.to_pem)
File.write(key_path, key.to_pem)
`security import #{key_path} -k ~/Library/Keychains/login.keychain-db -T /usr/bin/codesign -T /usr/bin/security 2>&1`
import_success = $CHILD_STATUS.success?
say ' â CSR saved to Downloads', :green
if import_success
say ' â Private key imported to keychain', :green
begin
File.delete(key_path)
rescue StandardError
nil
end
else
say " â Private key saved to: #{key_path}", :green
say " â ī¸ Import it with: security import #{key_path} -k ~/Library/Keychains/login.keychain-db",
:yellow
end
csr_path
rescue StandardError => e
say " â Failed to generate CSR: #{e.message}", :red
nil
end
end
def auto_create_profile(client, config, bundle_id, profile_type)
say "Creating #{profile_type} profile for #{bundle_id}...", :yellow
say ''
apple_profile_type = case profile_type.to_s.downcase
when 'appstore', 'store' then 'IOS_APP_STORE'
when 'development', 'dev' then 'IOS_APP_DEVELOPMENT'
when 'adhoc' then 'IOS_APP_ADHOC'
else profile_type
end
begin
say ' Syncing organization resources...', :cyan
client.post("/api/v1/organizations/#{config.current_organization_id}/sync_app_store_connect")
sleep 2
max_wait = 15 waited = 0
sync_complete = false
while waited < max_wait
status_response = client.get("/api/v1/organizations/#{config.current_organization_id}/sync/status")
sync_data = status_response[:data]['sync']
unless sync_data['running']
sync_complete = true
break
end
sleep 1
waited += 1
end
if sync_complete
say ' â Sync complete', :green
else
say ' â ī¸ Sync still running, continuing anyway...', :yellow
end
say ''
say " Creating #{apple_profile_type} profile...", :cyan
response = client.post(
"/api/v1/organizations/#{config.current_organization_id}/profiles/auto_create",
body: {
bundle_id: bundle_id,
profile_type: apple_profile_type
}
)
if response[:success]
profile = response[:data]['profile']
say " â Created profile: #{profile['name']}", :green
say " UUID: #{profile['uuid']}", :cyan
say " Expires: #{profile['expires_at']}", :cyan
say ''
say ' Downloading profile...', :cyan
download_url = "/api/v1/organizations/#{config.current_organization_id}/profiles/#{profile['id']}/download"
conn = Faraday.new(url: config.api_url) do |f|
f.request :authorization, 'Bearer', config.api_token
f.adapter Faraday.default_adapter
end
download_response = conn.get(download_url) do |req|
req.options.timeout = 30
req.options.open_timeout = 10
end
if download_response.success?
profiles_dir = File.expand_path('~/Library/MobileDevice/Provisioning Profiles')
FileUtils.mkdir_p(profiles_dir)
profile_path = File.join(profiles_dir, "#{profile['uuid']}.mobileprovision")
File.binwrite(profile_path, download_response.body)
say ' â Profile installed to Xcode', :green
else
say " â ī¸ Could not download profile: HTTP #{download_response.status}", :yellow
end
say ''
true
else
say ' â Failed to create profile', :red
false
end
rescue Mysigner::ClientError => e
error_msg = e.message
if error_msg.include?('bundle_id_not_found')
say " â Bundle ID '#{bundle_id}' not found in App Store Connect", :red
say ''
say ' You need to register this bundle ID first:', :yellow
say ' 1. Go to: https://developer.apple.com/account/resources/identifiers/list', :cyan
say " 2. Register bundle ID: #{bundle_id}", :cyan
say " 3. Run 'mysigner doctor' again", :cyan
elsif error_msg.include?('certificates found') || error_msg.include?('no_certificates')
cert_type = apple_profile_type == 'IOS_APP_STORE' ? 'Distribution' : 'Development'
cert_name = apple_profile_type == 'IOS_APP_STORE' ? 'Apple Distribution' : 'Apple Development'
say " â No #{cert_type} certificates found", :red
say ''
say ''
if yes_with_default?('Generate CSR and get step-by-step guide?', :green)
csr_path = generate_csr(config.user_email)
if csr_path
say ''
say " â CSR ready: #{File.basename(csr_path)}", :green
say ''
say ' đ Next steps:', :cyan
say ' 1. Go to: https://developer.apple.com/account/resources/certificates/add',
:green
say " 2. Select: '#{cert_name}'", :green
say " 3. Upload CSR: #{csr_path}", :green
say ' 4. Download .cer file and double-click to install', :green
say " 5. Sync in My Signer â Run 'mysigner doctor' again", :green
say ''
end
else
say ' đ Quick guide:', :cyan
say ' 1. Open Keychain Access â Request Certificate (save as CSR)', :green
say ' 2. https://developer.apple.com/account/resources/certificates/add',
:green
say " 3. Select '#{cert_name}' â Upload CSR â Download .cer", :green
say ' 4. Double-click .cer to install â Sync My Signer', :green
say ''
end
elsif error_msg.include?('no_devices') || error_msg.include?('devices found')
say ' â No test devices (needed for dev profiles)', :red
say ''
say ' đ Quick fix:', :cyan
say ' âĸ Get UDID: Connect device â Finder â Click serial number', :green
say ' âĸ Run: mysigner device add <NAME> <UDID>', :green
say " âĸ Or add in: #{client.api_url}/organizations/#{config.current_organization_id}", :green
say ''
else
say " â Failed: #{error_msg}", :red
end
say ''
false
rescue StandardError => e
say " â Unexpected error: #{e.message}", :red
say ''
false
end
end
end
desc 'sync [PLATFORM]', 'đ Sync data from App Store Connect or Google Play'
long_desc <<~DESC
Sync your organization's data from app stores.
PLATFORMS (optional):
ios : Sync from App Store Connect (default)
android : Sync from Google Play
all : Sync from both platforms
Without a platform argument, syncs iOS (App Store Connect) data.
EXAMPLES:
mysigner sync # Sync iOS data
mysigner sync ios # Sync iOS data
mysigner sync android # Sync Android data
mysigner sync all # Sync both platforms
DESC
option :force, type: :boolean, aliases: '-f', desc: 'Force sync even if recently synced'
def sync(platform = 'ios')
config = load_config
client = create_client(config)
platform = platform.to_s.downcase
case platform
when 'ios', 'apple', 'appstore'
sync_ios(client, config)
when 'android', 'google', 'googleplay', 'play'
sync_android(client, config)
when 'all', 'both'
sync_ios(client, config)
say ''
sync_android(client, config)
else
error "Unknown platform: #{platform}"
say 'Valid platforms: ios, android, all', :yellow
exit 1
end
end
no_commands do
def sync_ios(client, config)
say 'đ Syncing data from App Store Connect...', :cyan
say ''
begin
response = client.post(
"/api/v1/organizations/#{config.current_organization_id}/sync",
body: { force: options[:force] }
)
if response[:success]
data = response[:data]['data'] || response[:data]
say 'â iOS sync completed!', :green
say ''
say "Last synced: #{data['synced_at']}", :cyan if data['synced_at']
if data['summary']
say ''
say 'đ Summary:', :cyan
summary = data['summary']
say " âĸ Apps: #{summary['apps']}" if summary['apps']
say " âĸ Builds: #{summary['builds']}" if summary['builds']
say " âĸ Certificates: #{summary['certificates']}" if summary['certificates']
say " âĸ Devices: #{summary['devices']}" if summary['devices']
say " âĸ Profiles: #{summary['profiles']}" if summary['profiles']
end
else
say "â iOS sync failed: #{response[:error]}", :red
end
rescue StandardError => e
say "â iOS sync failed: #{e.message}", :red
end
end
def sync_android(client, config)
say 'đ Syncing data from Google Play...', :cyan
say ''
begin
response = client.post(
"/api/v1/organizations/#{config.current_organization_id}/sync_google_play",
body: { force: options[:force] }
)
if response[:success]
say 'â Android sync started!', :green
say ''
say 'Sync runs in the background. Check status with:', :cyan
say ' mysigner apps --platform android', :green
say ''
say 'đĄ Google Play sync may take a few minutes.', :yellow
say " Unlike iOS, Google Play doesn't auto-discover apps.", :yellow
say ' If no apps appear, add them in the web dashboard first.', :yellow
else
say "â Android sync failed: #{response[:error] || 'Unknown error'}", :red
end
rescue Mysigner::ClientError => e
if e.message.include?('No active Google Play credential')
say 'â Google Play not configured', :red
say ''
say 'Set up credentials first:', :yellow
say ' Configure Google Play in My Signer dashboard', :green
else
say "â Android sync failed: #{e.message}", :red
end
rescue StandardError => e
say "â Android sync failed: #{e.message}", :red
end
end
def detect_java_home(version: nil)
if system('which /usr/libexec/java_home > /dev/null 2>&1')
cmd = '/usr/libexec/java_home'
cmd += " -v #{version}" if version
java_home = `#{cmd} 2>/dev/null`.strip
return java_home if !java_home.empty? && Dir.exist?(java_home)
end
homebrew_paths = %w[
/opt/homebrew/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home
/opt/homebrew/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home
/opt/homebrew/opt/openjdk/libexec/openjdk.jdk/Contents/Home
]
homebrew_paths.each do |path|
return path if Dir.exist?(path)
end
intel_paths = %w[
/usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home
/usr/local/opt/openjdk@21/libexec/openjdk.jdk/Contents/Home
/usr/local/opt/openjdk/libexec/openjdk.jdk/Contents/Home
]
intel_paths.each do |path|
return path if Dir.exist?(path)
end
system_paths = Dir.glob('/Library/Java/JavaVirtualMachines/*/Contents/Home')
return system_paths.first if system_paths.any?
nil
end
def fix_java_home(java_home)
shell_config = File.expand_path('~/.zshrc')
shell_config = File.expand_path('~/.bash_profile') unless File.exist?(shell_config)
content = File.exist?(shell_config) ? File.read(shell_config) : ''
if content.include?('export JAVA_HOME=')
new_content = content.gsub(/^export JAVA_HOME=.*$/, "export JAVA_HOME=\"#{java_home}\"")
File.write(shell_config, new_content)
say " â Updated JAVA_HOME in #{shell_config}", :green
else
File.open(shell_config, 'a') do |f|
f.puts ''
f.puts '# Added by mysigner doctor'
f.puts "export JAVA_HOME=\"#{java_home}\""
end
say " â Added JAVA_HOME to #{shell_config}", :green
end
say ''
say ' To apply now, run:', :yellow
say " source #{shell_config}", :cyan
say ''
say ' Or restart your terminal.', :yellow
end
end
end
end
|