class XcodeMove::File

Attributes

path[R]

Public Class Methods

new(path) click to toggle source
# File lib/xcmv/file.rb, line 6
def initialize(path)
  path = Pathname.new path
  @path = path.expand_path
end

Public Instance Methods

add_to_targets(target_names, header_visibility) click to toggle source
# File lib/xcmv/file.rb, line 57
def add_to_targets(target_names, header_visibility)
  unless target_names
    group = GroupMembership.new(pbx_file.parent)
    targets = group.inferred_targets

    if targets.empty?
      # fallback: if we can't infer any target membership,
      # we just assign the first target of the project and emit a warning
      fallback_target = project.targets.select{ |t| t.respond_to?(:source_build_phase) }[0] 
      targets = [fallback_target]
      warn "⚠️  Warning: Unable to infer target membership of #{path} -- assigning to #{fallback_target.name}."
    end
  else
    name_set = target_names.to_set
    targets = project.targets.select{ |t| name_set.include?(t.name) }
    abort "🛑  Error: No targets found in #{target_names}." if targets.empty?
  end

  targets.each do |target|
    build_file = target.add_file_references([@pbx_file])
    if header?
      visibility = header_visibility || HeaderVisibility.default_for_target(target)
      build_file.each{ |b| b.settings = visibility.file_settings }
    end
  end
end
create_file_reference() click to toggle source

Uses the `path` to create a file reference in `project`, setting `pbx_file` along the way.

# File lib/xcmv/file.rb, line 45
def create_file_reference
  relative_path = path.relative_path_from(project.path.dirname)
  group = project.main_group
  relative_path.descend do |subpath|
    if subpath == relative_path
      @pbx_file = insert_at_group(group)
    else
      group = find_or_create_relative_group(group, subpath.basename)
    end
  end
end
header?() click to toggle source
# File lib/xcmv/file.rb, line 19
def header?
  path.extname == '.h'
end
pbx_file() click to toggle source
# File lib/xcmv/file.rb, line 15
def pbx_file
  @pbx_file ||= pbx_load
end
project() click to toggle source
# File lib/xcmv/file.rb, line 11
def project
  @project ||= project_load
end
reachable_projects() click to toggle source

Traverses up from the `path` to enumerate over xcodeproj directories

# File lib/xcmv/file.rb, line 29
def reachable_projects
  path.ascend.find_all{ |p| p.exist? and p.directory? }.flat_map do |dir|
    dir.children.select{ |p| p.extname == '.xcodeproj' }
  end
end
remove_from_project() click to toggle source
# File lib/xcmv/file.rb, line 35
def remove_from_project
  project.targets.select{ |t| t.respond_to?(:build_phases) }.each do |native_target|
    native_target.build_phases.each{ |p| p.remove_file_reference(pbx_file) }
  end
  pbx_file.remove_from_project
  @pbx_file = nil
end
save_and_close() click to toggle source
# File lib/xcmv/file.rb, line 84
def save_and_close
  project.save
  @project = nil
end
with_dirname(root_path) click to toggle source
# File lib/xcmv/file.rb, line 23
def with_dirname(root_path)
  new_path = root_path + path.basename
  self.class.new(new_path) # want to return the same kind of object
end

Private Instance Methods

find_or_create_relative_group(parent_group, group_name) click to toggle source
# File lib/xcmv/file.rb, line 91
def find_or_create_relative_group(parent_group, group_name)
  parent_group.children.find { |g| g.path == group_name.to_s } ||
    parent_group.new_group(group_name.to_s, group_name)
end
insert_at_group(group) click to toggle source
# File lib/xcmv/file.rb, line 96
def insert_at_group(group)
  group.new_file(path)
end
pbx_load() click to toggle source
# File lib/xcmv/file.rb, line 106
def pbx_load
  @pbx_file = project.files.find{ |f| f.real_path == path }
end
project_load() click to toggle source

Finds a reachable project that contains this file, and sets `project` and `pbx_file`.

# File lib/xcmv/file.rb, line 101
def project_load
  project_dir = reachable_projects.first || abort("Could not find a project file containing #{path}")
  @project = ProjectCache.open(project_dir)
end