Module: Kintsugi

Defined in:
lib/kintsugi.rb,
lib/kintsugi/apply_change_to_project.rb

Class Method Summary collapse

Class Method Details

.apply_change_to_project(project, change) ⇒ void

This method returns an undefined value.

Applies the change specified by ‘change` to `project`.

Parameters:

  • project (Xcodeproj::Project)

    Project to which to apply the change.

  • change (Hash)

    Change to apply to ‘project`. Assumed to be in the format emitted by Xcodeproj::Differ#project_diff where its `key_1` and `key_2` parameters have values of `:added` and `:removed` respectively.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kintsugi/apply_change_to_project.rb', line 23

def apply_change_to_project(project, change)
  # We iterate over the main group and project references first because they might create file
  # or project references that are referenced in other parts.
  unless change["rootObject"]["mainGroup"].nil?
    if project.root_object.main_group.nil?
      puts "Warning: Main group doesn't exist, ignoring changes to it."
    else
      apply_change_to_component(project.root_object, "mainGroup",
                                change["rootObject"]["mainGroup"])
    end
  end

  unless change["rootObject"]["projectReferences"].nil?
    apply_change_to_component(project.root_object, "projectReferences",
                              change["rootObject"]["projectReferences"])
  end

  apply_change_to_component(project, "rootObject",
                            change["rootObject"].reject { |key|
                              %w[mainGroup projectReferences].include?(key)
                            })
end

.resolve_conflicts(project_file_path, changes_output_path) ⇒ void

This method returns an undefined value.

Resolves git conflicts of a pbxproj file specified by ‘project_file_path`.

Parameters:

  • project_file_path (String)

    Project to which to apply the changes.

  • output_changes_path (String)

    Path to where the changes to apply to the project are written in JSON format.

Raises:

  • (ArgumentError)

    If the file extension is not ‘pbxproj` or the file doesn’t exist

  • (RuntimeError)

    If no rebase, cherry-pick, or merge is in progress, or the project file couldn’t be opened, or there was an error applying the change to the project.



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
# File 'lib/kintsugi.rb', line 30

def resolve_conflicts(project_file_path, changes_output_path)
  validate_project(project_file_path)

  project_in_temp_directory =
    open_project_of_current_commit_in_temporary_directory(project_file_path)

  change = change_of_conflicting_commit_with_parent(project_file_path)

  if changes_output_path
    File.write(changes_output_path, JSON.pretty_generate(change))
  end

  apply_change_to_project(project_in_temp_directory, change)

  project_in_temp_directory.save

  Dir.chdir(File.dirname(project_file_path)) do
    `git reset #{project_file_path}`
  end
  FileUtils.cp(File.join(project_in_temp_directory.path, "project.pbxproj"), project_file_path)

  # Some of the metadata in a `pbxproj` file include a part of the name of the directory it's
  # inside. The modified project is stored in a temporary directory and then copied to the
  # original path, therefore its metadata is incorrect. To fix this, the project at the original
  # path is opened and saved.
  Xcodeproj::Project.open(File.dirname(project_file_path)).save
end