Class: JSONCop::Command::Install

Inherits:
JSONCop::Command show all
Defined in:
lib/jsoncop/command/install.rb

Constant Summary collapse

BUILD_PHASE_PREFIX =
"[JC] ".freeze
JSONCOP_INSTALL_PHASE_NAME =
"JSONCop Install Script".freeze

Instance Method Summary collapse

Methods inherited from JSONCop::Command

options, run

Constructor Details

#initialize(argv) ⇒ Install

Returns a new instance of Install.



15
16
17
# File 'lib/jsoncop/command/install.rb', line 15

def initialize(argv)
  super
end

Instance Method Details

#create_or_update_build_phase(target, phase_name, phase_class = Xcodeproj::Project::Object::PBXShellScriptBuildPhase) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/jsoncop/command/install.rb', line 28

def create_or_update_build_phase(target, phase_name, phase_class = Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
  prefixed_phase_name = BUILD_PHASE_PREFIX + phase_name
  build_phases = target.build_phases.grep(phase_class)
  build_phases.find { |phase| phase.name && phase.name.end_with?(phase_name) }.tap { |p| p.name = prefixed_phase_name if p } ||
    target.project.new(phase_class).tap do |phase|
      phase.name = prefixed_phase_name
      phase.show_env_vars_in_log = '0'
      target.build_phases.unshift phase
    end
end

#install_shell_scriptObject



39
40
41
42
43
44
45
# File 'lib/jsoncop/command/install.rb', line 39

def install_shell_script
  <<-SCRIPT#!/usr/bin/env ruby
require 'jsoncop'
Encoding.default_external = Encoding::UTF_8
JSONCop::Command.run(["generate"])
  SCRIPT
end

#runObject



19
20
21
22
23
24
25
26
# File 'lib/jsoncop/command/install.rb', line 19

def run
  project = Xcodeproj::Project.open(Dir.glob("*.xcodeproj").first)
  native_target = project.targets.first
  phase = create_or_update_build_phase(native_target, JSONCOP_INSTALL_PHASE_NAME)
  phase.shell_path = "/usr/bin/ruby"
  phase.shell_script = install_shell_script
  project.save
end