Class: BranchIOCLI::Command::SetupCommand

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

Instance Attribute Summary collapse

Attributes inherited from Command

#options

Instance Method Summary collapse

Methods inherited from Command

#helper, #patch_helper

Constructor Details

#initialize(options) ⇒ SetupCommand

Returns a new instance of SetupCommand.



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

def initialize(options)
  super
  @config = Configuration::SetupConfiguration.new options
  @keys = config.keys
  @domains = config.all_domains
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/branch_io_cli/command/setup_command.rb', line 7

def config
  @config
end

Instance Method Details

#check_repo_statusObject

rubocop: enable Metrics/PerceivedComplexity



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

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?

  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.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 #{Shellwords.escape(message)}"
  else
    say "Please stash or commit your changes before continuing."
    exit(-1)
  end
end

#run!Object

rubocop: disable Metrics/PerceivedComplexity



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

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 && options.validate &&
     !helper.validate_team_and_bundle_ids_from_aasa_files(@domains)
    say "Universal Link configuration failed validation."
    helper.errors.each { |error| say " #{error}" }
    return unless options.force
  elsif is_app_target && options.validate
    say "Universal Link configuration passed validation. ✅"
  end

  # the following calls can all raise IOError
  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

  new_path = helper.add_universal_links_to_project @domains, false if is_app_target
  sh "git add #{Shellwords.escape(new_path)}" if options.commit && new_path

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

  xcodeproj.save

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

  patch_helper.patch_source xcodeproj if options.patch_source

  return unless options.commit

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

  sh "git commit -qm '[branch_io_cli] Branch SDK integration' #{changes.join(' ')}"
end