Class: XcodeMove::File

Inherits:
Object
  • Object
show all
Defined in:
lib/xcmv/file.rb

Direct Known Subclasses

Group

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



6
7
8
9
# File 'lib/xcmv/file.rb', line 6

def initialize(path)
  path = Pathname.new path
  @path = path.expand_path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/xcmv/file.rb', line 4

def path
  @path
end

Instance Method Details

#add_to_targets(target_names, header_visibility) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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_referenceObject

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



45
46
47
48
49
50
51
52
53
54
55
# 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?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/xcmv/file.rb', line 19

def header?
  path.extname == '.h'
end

#pbx_fileObject



15
16
17
# File 'lib/xcmv/file.rb', line 15

def pbx_file
  @pbx_file ||= pbx_load
end

#projectObject



11
12
13
# File 'lib/xcmv/file.rb', line 11

def project
  @project ||= project_load
end

#reachable_projectsObject

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



29
30
31
32
33
# 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_projectObject



35
36
37
38
39
40
41
# 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_closeObject



84
85
86
87
# File 'lib/xcmv/file.rb', line 84

def save_and_close
  project.save
  @project = nil
end

#with_dirname(root_path) ⇒ Object



23
24
25
26
# 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