class MFXcode::Plugins::Addfile

Adds files to a group in the Xcode project

Public Instance Methods

create_group_hierarchy_if_not_present(main_group, sub_groups) click to toggle source

create a hierarchy of group if not present

# File lib/mfxcode/plugins/addfile.rb, line 98
def create_group_hierarchy_if_not_present(main_group, sub_groups)
  g = sub_groups.shift

  if main_group[g].nil?
    main_group.new_group(g)
    
    if main_group.path.nil?
      main_group[g].path = g
    else
      main_group[g].path = main_group.path + "/" + g
    end

  end
 
  create_group_hierarchy_if_not_present(main_group[g], sub_groups) unless sub_groups.empty?
end
help() click to toggle source
# File lib/mfxcode/plugins/addfile.rb, line 27
  def help
    {:short => 'adds files to a group in Xcode project',
     :long => <<"END" }
Usage: addfile project_path group file [file file ...]

Adds files to a group, in the project at the given path.
END
  end
run(args) click to toggle source
# File lib/mfxcode/plugins/addfile.rb, line 36
def run(args)
 
  if args.count >= 3

    project_path = args.shift
    group_name_arg = args.shift
    files = args
    
    project = Xcodeproj::Project.open(project_path)

    # filter files which are already included
    project_files = []
    project.files.each { |f| project_files << f.path }
    files = files.select { |f| ! project_files.include? f }

    create_group_hierarchy_if_not_present(project.main_group, group_name_arg.split("/"))

    group = project[group_name_arg]

    main_target_name = project_path.split("/").last.split(".").first
    test_target_name = main_target_name + "Tests"
      
    #puts "Main target is #{main_target_name}. files will be added to this target."
        #puts "#{project.targets}"
        #puts "#{project.pretty_print}"

    main_target = project.targets.select {|t| t.name == main_target_name}.first
    test_target = project.targets.select {|t| t.name == test_target_name}.first

    files.each do |file|
        #puts "current file: #{file}"
        f = group.new_file(file,'SOURCE_ROOT')
        #puts "current file: #{f}"
        #puts "current file: #{f.name}"
        #puts "#{group_name_arg}"
        fext = f.name.split(".").last

        case fext
        when "h"
        when "pch"
        when "contents"
        when "m"
              #puts "Adding source file #{f.name}"
              main_target.add_file_references [f]
  test_target.add_file_references [f]
        else
              #puts "Adding ressource file #{f.name}"
              main_target.build_phases.select {|bp| bp.isa == 'PBXResourcesBuildPhase'}.first.add_file_reference f
        end
    end

        #puts "#{project_path}"
    project.save(project_path) 
  
  else
     puts "Too few arguments"
     puts help[:long]
     exit 1
  end
end