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, #tool_helper

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

#add_sdkObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/branch_io_cli/command/setup_command.rb', line 78

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

#check_repo_statusObject



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

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 true if `which git`.empty? || !config.confirm

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

  `git diff-index --quiet HEAD --`
  return true 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.readline = true
    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."
    return false
  end

  true
end

#commit_changesObject



97
98
99
100
101
102
103
104
# File 'lib/branch_io_cli/command/setup_command.rb', line 97

def commit_changes
  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
end

#run!Object



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

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

  # Validate Universal Link configuration in an application target.
  if config.validate && config.target.symbol_type == :application
    valid = validate_universal_links
    return 1 unless valid || config.force
  end

  # Make sure we can resolve all build settings in a project using
  # CocoaPods.
  if config.podfile_path && File.exist?(config.podfile_path) && config.pod_install_required?
    tool_helper.verify_cocoapods
    say "Installing pods to resolve current build settings"
    # We haven't modified anything yet. Don't use --repo-update at this stage.
    # This is unlikely to fail.
    sh "pod install", chdir: File.dirname(config.podfile_path)
  end

  # Add SDK via CocoaPods, Carthage or direct download (no-op if disabled).
  add_sdk

  # Set up Universal Links and Branch key(s)
  update_project_settings

  # Patch source code if so instructed.
  patch_helper.patch_source config.xcodeproj if config.patch_source

  # Commit changes if so instructed.
  commit_changes if config.commit

  # Return success.
  0
end

#update_project_settingsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/branch_io_cli/command/setup_command.rb', line 61

def update_project_settings
  helper.add_custom_build_setting if config.setting
  helper.add_keys_to_info_plist @keys
  config.target.add_system_frameworks config.frameworks unless config.frameworks.blank?

  return unless config.target.symbol_type == :application

  helper.add_branch_universal_link_domains_to_info_plist @domains
  helper.ensure_uri_scheme_in_info_plist
  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
ensure
  config.xcodeproj.save
end


50
51
52
53
54
55
56
57
58
59
# File 'lib/branch_io_cli/command/setup_command.rb', line 50

def validate_universal_links
  valid = helper.validate_team_and_bundle_ids_from_aasa_files @domains
  if valid
    say "Universal Link configuration passed validation. ✅"
  else
    say "Universal Link configuration failed validation."
    helper.errors.each { |error| say " #{error}" }
  end
  valid
end