Class: XCRes::ProjectCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/xcres/command/project_command.rb

Overview

The ProjectCommand is the base class for commands, which analyze or modify Xcode projects.

Direct Known Subclasses

BuildCommand, InstallCommand

Xcode Project collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#configure_logger, #logger, #run

Class Method Details

.inherit_parameters!Object

Define parameter in an inheritable way



23
24
25
# File 'lib/xcres/command/project_command.rb', line 23

def self.inherit_parameters!
  parameter '[XCODEPROJ]', 'Xcode project file to inspect (automatically located on base of the current directory if not given)', attribute_name: :xcodeproj_file_path
end

Instance Method Details

#application_targetsArray<PBXNativeTarget>

Find all application targets in the project

Returns:

  • (Array<PBXNativeTarget>)


84
85
86
87
88
# File 'lib/xcres/command/project_command.rb', line 84

def application_targets
  @application_targets ||= native_targets.select do |target|
    target.product_type == Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application]
  end
end

#discover_xcodeproj_file_path!Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/xcres/command/project_command.rb', line 103

def discover_xcodeproj_file_path!
  if xcodeproj_file_path.nil?
    warn 'Argument XCODEPROJ is not set. Using the current directory.'
    discover_xcodeproj_file_path_in_dir! '.'
  elsif Dir.exist?(xcodeproj_file_path) && !File.fnmatch('*.xcodeproj', xcodeproj_file_path)
    warn 'Argument XCODEPROJ is a directory. ' \
         'Try to locate the Xcode project in this directory.'
    discover_xcodeproj_file_path_in_dir! xcodeproj_file_path
  else
    xcodeproj_file_path
  end
end

#discover_xcodeproj_file_path_in_dir!(dir) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/xcres/command/project_command.rb', line 116

def discover_xcodeproj_file_path_in_dir! dir
  xcodeproj_file_paths = Dir[dir + '/*.xcodeproj']
  if xcodeproj_file_paths.count == 0
    raise ArgumentError.new 'Argument XCODEPROJ was not given and no ' \
                            '*.xcodeproj was found in current directory.'
  end
  xcodeproj_file_paths.first
end

#executeObject



28
29
30
31
32
33
# File 'lib/xcres/command/project_command.rb', line 28

def execute
  super

  # Try to discover Xcode project at given path.
  self.xcodeproj_file_path = find_xcodeproj
end

#find_xcodeprojObject



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/xcres/command/project_command.rb', line 90

def find_xcodeproj
  path = discover_xcodeproj_file_path!

  if !Dir.exist?(path) || !File.exist?(path + '/project.pbxproj')
    raise ArgumentError.new 'XCODEPROJ at %s was not found or is not a ' \
      'valid Xcode project.' % path
  end

  success 'Use %s as XCODEPROJ.', path

  return path
end

#native_targetsArray<PBXNativeTarget>

Find all native targets in the project

Returns:

  • (Array<PBXNativeTarget>)


74
75
76
77
78
# File 'lib/xcres/command/project_command.rb', line 74

def native_targets
  @native_targets ||= project.targets.select do |target|
    target.is_a?(Xcodeproj::Project::Object::PBXNativeTarget)
  end
end

#projectXcodeproj::Project

Opens the Xcode project, if not already opened

Returns:

  • (Xcodeproj::Project)

    the Xcode project



45
46
47
# File 'lib/xcres/command/project_command.rb', line 45

def project
  @project ||= Xcodeproj::Project.open(xcodeproj_file_path)
end

#targetPBXNativeTarget

Find the application target to use

Returns:

  • (PBXNativeTarget)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xcres/command/project_command.rb', line 53

def target
  @target ||= if target_name != nil
    target = native_targets.find { |t| t.name == target_name }
    if target.nil?
      raise ArgumentError.new "Unknown target '#{target_name}'. "
    end
    target
  else
    if application_targets.count == 1
      application_targets.first
    else
      raise ArgumentError.new 'Multiple application target in project. ' \
        'Please select one by specifying the option `--target TARGET`.'
    end
  end
end