Class: IOSBox::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/ios-box/cli.rb

Instance Method Summary collapse

Instance Method Details

#integrate(project = nil) ⇒ Object



24
25
26
27
28
29
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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ios-box/cli.rb', line 24

def integrate(project = nil)
  shell = Thor::Shell::Basic.new

  # Find our project
  if project.nil?
    project = Dir["*.xcodeproj"].first
  end

  unless File.exists?(project) and File.directory?(project)
    shell.error "Project #{project} is not valid."
    exit
  end

  shell.say "Integrating to project #{project}"

  # Find our project file, either from command-line or just using first one
  xcode = project || Dir["*.xcodeproj"].first
  raise "Cannot find project.pbxproj in #{xcode}" unless File.file? "#{xcode}/project.pbxproj"

  # Load our project file
  pbx = PBXProject::PBXProject.new :file => "#{xcode}/project.pbxproj"
  pbx.parse

  # If target missing, fetch first target
  # Find all targets
  targets = pbx.find_item :type => PBXProject::PBXTypes::PBXNativeTarget
  if targets.nil? or targets.empty?
    raise "XCode project does not have any targets!"
  end

  # Generate build phase
  # Try to find build phases
  # prebuilds = pbx.find_item(:type => PBXProject::PBXTypes::PBXShellScriptBuildPhase) || []
  # prebuilds.select! do |c|
  #   !c.comment.match(/ios-box prepare/).nil?
  # end
  #
  prebuilds = [] # Always add new build phase
  if prebuilds.empty?
    initPhase = PBXProject::PBXTypes::PBXShellScriptBuildPhase.new(
      :shellPath => '/bin/sh',
      :shellScript => "\"(cd $PROJECT_DIR; ios-box build prepare)\"",
      :showEnvVarsInLog => 0,
      :name => '"ios-box prepare"'
    )
    initPhase.comment = "ios-box prepare"

    pbx.add_item initPhase
  else
    initPhase = prebuilds.first
  end

  targets.each do |target|
    if shell.yes?("Integrate with target #{target.name.value}? [yn]")
      # Inject buildphase to target
      # Add to target
      target.add_build_phase initPhase, 0
    end
  end

  # Create iosbox configuration file
  config = Config.new
  config.project = project
  config.targets = targets.collect{|c| c.name.value}
  config.growl = true # Enable Growl support
  config.deploy = {'autonotes' => true} # Generate deployment changelogs from GIT logs
  config.save(".iosbox")

  # Append buildcache to gitignore
  send((File.exists?(".gitignore") ? :append_to_file : :create_file), ".gitignore", ".buildcache\n")

  # Write project file
  pbx.write_to :file => "#{xcode}/project.pbxproj"
end