Class: BranchIOCLI::Configuration::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/branch_io_cli/configuration/configuration.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



24
25
26
27
28
29
30
31
32
# File 'lib/branch_io_cli/configuration/configuration.rb', line 24

def initialize(options)
  @options = options
  @pod_repo_update = options.pod_repo_update
  Configuration.current = self

  print_identification self.class.name.sub(/^.*::(.*?)Configuration$/, '\1').downcase
  validate_options
  log
end

Class Attribute Details

.currentObject

Returns the value of attribute current.



9
10
11
# File 'lib/branch_io_cli/configuration/configuration.rb', line 9

def current
  @current
end

Instance Attribute Details

#cartfile_pathObject (readonly)

Returns the value of attribute cartfile_path.



18
19
20
# File 'lib/branch_io_cli/configuration/configuration.rb', line 18

def cartfile_path
  @cartfile_path
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/branch_io_cli/configuration/configuration.rb', line 12

def options
  @options
end

#pod_repo_updateObject (readonly)

Returns the value of attribute pod_repo_update.



22
23
24
# File 'lib/branch_io_cli/configuration/configuration.rb', line 22

def pod_repo_update
  @pod_repo_update
end

#podfileObject (readonly)

Returns the value of attribute podfile.



16
17
18
# File 'lib/branch_io_cli/configuration/configuration.rb', line 16

def podfile
  @podfile
end

#podfile_pathObject (readonly)

Returns the value of attribute podfile_path.



17
18
19
# File 'lib/branch_io_cli/configuration/configuration.rb', line 17

def podfile_path
  @podfile_path
end

#sdk_integration_modeObject (readonly)

Returns the value of attribute sdk_integration_mode.



19
20
21
# File 'lib/branch_io_cli/configuration/configuration.rb', line 19

def sdk_integration_mode
  @sdk_integration_mode
end

#targetObject (readonly)

Returns the value of attribute target.



15
16
17
# File 'lib/branch_io_cli/configuration/configuration.rb', line 15

def target
  @target
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



20
21
22
# File 'lib/branch_io_cli/configuration/configuration.rb', line 20

def workspace
  @workspace
end

#workspace_pathObject (readonly)

Returns the value of attribute workspace_path.



21
22
23
# File 'lib/branch_io_cli/configuration/configuration.rb', line 21

def workspace_path
  @workspace_path
end

#xcodeprojObject (readonly)

Returns the value of attribute xcodeproj.



13
14
15
# File 'lib/branch_io_cli/configuration/configuration.rb', line 13

def xcodeproj
  @xcodeproj
end

#xcodeproj_pathObject (readonly)

Returns the value of attribute xcodeproj_path.



14
15
16
# File 'lib/branch_io_cli/configuration/configuration.rb', line 14

def xcodeproj_path
  @xcodeproj_path
end

Instance Method Details

#bridging_header_pathObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/branch_io_cli/configuration/configuration.rb', line 191

def bridging_header_path
  return @bridging_header_path if @bridging_header_path

  return nil unless target
  path = helper.expanded_build_setting target, "SWIFT_OBJC_BRIDGING_HEADER", "Release"
  return nil unless path

  @bridging_header_path = File.expand_path path, File.dirname(xcodeproj_path)
  @bridging_header_path
end

#bridging_header_required?Boolean

Returns:

  • (Boolean)


175
176
177
178
179
180
# File 'lib/branch_io_cli/configuration/configuration.rb', line 175

def bridging_header_required?
  return false unless swift_version
  # If there is a Podfile and use_frameworks! is not present for this
  # target, we need a bridging header.
  podfile && !uses_frameworks?
end

#helperObject



54
55
56
# File 'lib/branch_io_cli/configuration/configuration.rb', line 54

def helper
  Helper::BranchHelper
end

#logObject



38
39
40
41
42
43
44
# File 'lib/branch_io_cli/configuration/configuration.rb', line 38

def log
  say <<EOF
<%= color('Configuration:', [CYAN, BOLD, UNDERLINE]) %>

EOF
  # subclass implementation follows
end

#modules_enabled?Boolean

TODO: How many of these can vary by configuration?

Returns:

  • (Boolean)


184
185
186
187
188
189
# File 'lib/branch_io_cli/configuration/configuration.rb', line 184

def modules_enabled?
  return nil unless target
  setting = target.resolved_build_setting("CLANG_ENABLE_MODULES")["Release"]
  return nil unless setting
  setting == "YES"
end

#open_podfile(path) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/branch_io_cli/configuration/configuration.rb', line 140

def open_podfile(path)
  @podfile = Pod::Podfile.from_file path
  @podfile_path = path
  true
rescue RuntimeError => e
  say e.message
  false
end


46
47
48
49
50
51
52
# File 'lib/branch_io_cli/configuration/configuration.rb', line 46

def print_identification(command)
  say <<EOF

<%= color("branch_io #{command} v. #{VERSION}", BOLD) %>

EOF
end

#swift_versionObject



202
203
204
205
206
207
208
# File 'lib/branch_io_cli/configuration/configuration.rb', line 202

def swift_version
  return @swift_version if @swift_version

  return nil unless target
  @swift_version = target.resolved_build_setting("SWIFT_VERSION")["Release"]
  @swift_version
end

#uses_frameworks?Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
# File 'lib/branch_io_cli/configuration/configuration.rb', line 168

def uses_frameworks?
  return nil unless podfile
  target_definition = podfile.target_definition_list.find { |t| t.name == target.name }
  return nil unless target_definition
  target_definition.uses_frameworks?
end

#validate_buildfile_at_path(buildfile_path, filename) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/branch_io_cli/configuration/configuration.rb', line 149

def validate_buildfile_at_path(buildfile_path, filename)
  valid = buildfile_path =~ %r{/?#{filename}$}
  say "#{filename} path must end in /#{filename}." unless valid

  if valid
    valid = File.exist? buildfile_path
    say "#{buildfile_path} not found." unless valid
  end

  if filename == "Podfile" && open_podfile(buildfile_path)
    true
  elsif filename == "Cartfile"
    @cartfile_path = buildfile_path
    true
  else
    false
  end
end

#validate_buildfile_path(buildfile_path, filename) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/branch_io_cli/configuration/configuration.rb', line 121

def validate_buildfile_path(buildfile_path, filename)
  # Disable Podfile/Cartfile update if --no-add-sdk is present
  return unless sdk_integration_mode.nil?

  # No --podfile or --cartfile option
  if buildfile_path.nil?
    # Check for Podfile/Cartfile next to workspace or project
    buildfile_path = File.expand_path "../#{filename}", (workspace_path || xcodeproj_path)
    return unless File.exist? buildfile_path
  end

  # Validate. Prompt if not valid.
  while !buildfile_path || !validate_buildfile_at_path(buildfile_path, filename)
    buildfile_path = ask "Please enter the path to your #{filename}: "
  end

  @sdk_integration_mode = filename == "Podfile" ? :cocoapods : :carthage
end

#validate_optionsObject



34
35
36
# File 'lib/branch_io_cli/configuration/configuration.rb', line 34

def validate_options
  # implemented in subclasses
end

#validate_target(allow_extensions = true) ⇒ Object



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
# File 'lib/branch_io_cli/configuration/configuration.rb', line 93

def validate_target(allow_extensions = true)
  non_test_targets = xcodeproj.targets.reject(&:test_target_type?)
  raise "No non-test target found in project" if non_test_targets.empty?

  valid_targets = non_test_targets.reject { |t| !allow_extensions && t.extension_target_type? }

  begin
    target = helper.target_from_project xcodeproj, options.target

    # If a test target was explicitly specified.
    raise "Cannot use test targets" if target.test_target_type?

    # If an extension target was explicitly specified for validation.
    raise "Extension targets not allowed for this command" if !allow_extensions && target.extension_target_type?

    @target = target
  rescue StandardError => e
    say e.message

    choice = choose do |menu|
      valid_targets.each { |t| menu.choice t.name }
      menu.prompt = "Which target do you wish to use? "
    end

    @target = xcodeproj.targets.find { |t| t.name = choice }
  end
end

#validate_xcodeproj_pathObject

  1. Look for options.xcodeproj.

  2. If not specified, look for projects under . (excluding anything in Pods or Carthage folder).

  3. If none or more than one found, prompt the user.



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
# File 'lib/branch_io_cli/configuration/configuration.rb', line 61

def validate_xcodeproj_path
  if options.xcodeproj
    path = options.xcodeproj
  else
    all_xcodeproj_paths = Dir[File.expand_path(File.join(".", "**/*.xcodeproj"))]
    # find an xcodeproj (ignoring the Pods and Carthage folders)
    # TODO: Improve this filter
    xcodeproj_paths = all_xcodeproj_paths.select do |p|
      valid = true
      Pathname.new(p).each_filename do |f|
        valid = false && break if f == "Carthage" || f == "Pods"
      end
      valid
    end

    path = xcodeproj_paths.first if xcodeproj_paths.count == 1
  end

  loop do
    path = ask "Please enter the path to your Xcode project or use --xcodeproj: " if path.nil?
    # TODO: Allow the user to choose if xcodeproj_paths.count > 0
    begin
      @xcodeproj = Xcodeproj::Project.open path
      @xcodeproj_path = path
      return
    rescue StandardError => e
      say e.message
      path = nil
    end
  end
end