Class: BranchIOCLI::Command::SetupCommand

Inherits:
Command
  • Object
show all
Includes:
Helper::Methods
Defined in:
lib/branch_io_cli/command/setup_command.rb

Instance Attribute Summary

Attributes inherited from Command

#config, #options

Instance Method Summary collapse

Methods included from Helper::Methods

#clear, #confirm, #sh

Methods inherited from Command

available_options, command_name, configuration_class, examples, #helper, #patch_helper, return_value

Constructor Details

#initialize(options) ⇒ SetupCommand

Returns a new instance of SetupCommand.



8
9
10
11
12
# File 'lib/branch_io_cli/command/setup_command.rb', line 8

def initialize(options)
  super
  @keys = config.keys
  @domains = config.all_domains
end

Instance Method Details

#check_repo_statusObject

rubocop: enable Metrics/PerceivedComplexity



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
# File 'lib/branch_io_cli/command/setup_command.rb', line 91

def check_repo_status
  # If the git command is not installed, there's not much we can do.
  # Don't want to use verify_git here, which will insist on installing
  # the command. The logic of that method could change.
  return if `which git`.empty? || !config.confirm

  unless Dir.exist? ".git"
    `git rev-parse --git-dir > /dev/null 2>&1`
    # Not a git repo
    return unless $?.success?
  end

  `git diff-index --quiet HEAD --`
  return if $?.success?

  # Show the user
  sh "git status"

  choice = choose do |menu|
    menu.header = "There are uncommitted changes in this repo. It's best to stash or commit them before continuing."
    menu.choice "Stash"
    menu.choice "Commit (You will be prompted for a commit message.)"
    menu.choice "Quit"
    menu.choice "Ignore and continue"
    menu.prompt = "Please enter one of the options above: "
  end

  case choice
  when /^Stash/
    sh "git stash -q"
  when /^Commit/
    message = ask "Please enter a commit message: "
    sh ["git", "commit", "-aqm", message]
  when /^Quit/
    say "Please stash or commit your changes before continuing."
    exit(-1)
  end
end

#run!Object

rubocop: disable Metrics/PerceivedComplexity



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
# File 'lib/branch_io_cli/command/setup_command.rb', line 15

def run!
  # Make sure the user stashes or commits before continuing.
  check_repo_status

  xcodeproj = config.xcodeproj

  is_app_target = !config.target.extension_target_type?

  if is_app_target && config.validate &&
     !helper.validate_team_and_bundle_ids_from_aasa_files(@domains)
    say "Universal Link configuration failed validation."
    helper.errors.each { |error| say " #{error}" }
    return 1 unless config.force
  elsif is_app_target && config.validate
    say "Universal Link configuration passed validation. ✅"
  end

  if config.podfile_path && File.exist?(config.podfile_path) && config.pod_install_required?
    helper.verify_cocoapods
    say "Installing pods to resolve current build settings"
    Dir.chdir(File.dirname(config.podfile_path)) do
      # We haven't modified anything yet. Don't use --repo-update at this stage.
      # This is unlikely to fail.
      sh "pod install"
    end
  end

  helper.add_custom_build_setting if config.setting

  helper.add_keys_to_info_plist @keys
  helper.add_branch_universal_link_domains_to_info_plist @domains if is_app_target
  helper.ensure_uri_scheme_in_info_plist if is_app_target # does nothing if already present

  if is_app_target
    config.xcodeproj.build_configurations.each do |c|
      new_path = helper.add_universal_links_to_project @domains, false, c.name
      sh ["git", "add", new_path] if config.commit && new_path
    end
  end

  config_helper.target.add_system_frameworks config.frameworks unless config.frameworks.nil? || config.frameworks.empty?

  xcodeproj.save

  case config.sdk_integration_mode
  when :cocoapods
    if File.exist? config.podfile_path
      helper.update_podfile config
    else
      helper.add_cocoapods config
    end
  when :carthage
    if File.exist? config.cartfile_path
      helper.update_cartfile config, xcodeproj
    else
      helper.add_carthage config
    end
  when :direct
    helper.add_direct config
  end

  patch_helper.patch_source xcodeproj if config.patch_source

  return 0 unless config.commit

  changes = helper.changes.to_a.map { |c| Pathname.new(File.expand_path(c)).relative_path_from(Pathname.pwd).to_s }

  commit_message = config.commit if config.commit.kind_of?(String)
  commit_message ||= "[branch_io_cli] Branch SDK integration #{config.relative_path(config.xcodeproj_path)} (#{config.target.name})"

  sh ["git", "commit", "-qm", commit_message, *changes]

  0
end