Class: XCListen

Inherits:
Object
  • Object
show all
Defined in:
lib/xclisten.rb,
lib/xclisten/version.rb,
lib/xclisten/shell_task.rb

Defined Under Namespace

Classes: ShellTask

Constant Summary collapse

IOS_DEVICES =
{
  'iphone5s' => 'iPhone Retina (4-inch 64-bit)',
  'iphone5' => 'iPhone Retina (4-inch)',
  'iphone4' => 'iPhone Retina (3.5-inch)',
  'ipad2' => 'iPad',
  'ipad4' => 'iPad Retina',
  'ipad_air' => 'iPad Retina (64-bit)'
}
VERSION =
"0.0.8"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ XCListen

Returns a new instance of XCListen.



12
13
14
15
16
17
# File 'lib/xclisten.rb', line 12

def initialize(opts = {})
  @scheme = opts[:scheme] || project_name
  @sdk = opts[:sdk] || 'iphonesimulator'
  @workspace = opts[:workspace] || workspace_path
  @device = IOS_DEVICES[opts[:device]] || IOS_DEVICES['iphone5s']
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



7
8
9
# File 'lib/xclisten.rb', line 7

def device
  @device
end

#schemeObject (readonly)

Returns the value of attribute scheme.



8
9
10
# File 'lib/xclisten.rb', line 8

def scheme
  @scheme
end

#sdkObject (readonly)

Returns the value of attribute sdk.



9
10
11
# File 'lib/xclisten.rb', line 9

def sdk
  @sdk
end

#workspaceObject (readonly)

Returns the value of attribute workspace.



10
11
12
# File 'lib/xclisten.rb', line 10

def workspace
  @workspace
end

Instance Method Details

#can_run?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/xclisten.rb', line 62

def can_run?
  project_name && !project_name.empty?
end

#install_podsObject



42
43
44
45
46
47
48
# File 'lib/xclisten.rb', line 42

def install_pods
  Dir.chdir(File.dirname(workspace)) do
    system 'pod install'
    puts 'Giving Xcode some time to index...'
    sleep 10
  end
end

#listenObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/xclisten.rb', line 66

def listen
  validate_run
  puts "Started xclistening to #{workspace}"
  ignored = [/.git/, /.xc(odeproj|workspace|userdata|scheme|config)/, /.lock$/, /\.txt$/, /\.log$/]

  listener = Listen.to(Dir.pwd, :ignore => ignored) do |modified, added, removed|
    system 'clear'
    if modified.first =~ /Podfile$/
      install_pods
    else
      run_tests
    end
  end

  listener.start
  sleep
end

#project_nameObject



32
33
34
# File 'lib/xclisten.rb', line 32

def project_name
  @project_name ||= File.basename(workspace_path, ".*") if workspace_path
end

#run_testsObject



50
51
52
# File 'lib/xclisten.rb', line 50

def run_tests
  ShellTask.run("#{xcodebuild} test 2> xcodebuild_error.log | xcpretty -tc")
end

#validate_runObject

TODO TEST THIS SPIKE



55
56
57
58
59
60
# File 'lib/xclisten.rb', line 55

def validate_run
  unless can_run?
    puts "[!] No xcworkspace found in this directory (or any child directories)"
    exit 1
  end
end

#workspace_pathObject



28
29
30
# File 'lib/xclisten.rb', line 28

def workspace_path
  @workspace_path ||= Dir.glob("**/*.xcworkspace").sort_by(&:length).first
end

#xcodebuildObject



36
37
38
39
40
# File 'lib/xclisten.rb', line 36

def xcodebuild
  cmd = "xcodebuild -workspace #{workspace} -scheme #{scheme} -sdk #{sdk}"
  cmd += " -destination 'name=#{device}'" unless @sdk == 'macosx'
  cmd
end