Class: Snapshot::SnapshotConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot/snapshot_config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = './Snapfile') ⇒ SnapshotConfig

Returns a new instance of SnapshotConfig.

Parameters:

  • path (String) (defaults to: './Snapfile')

    the path to the config file to use (including the file name)



41
42
43
44
45
46
47
48
49
# File 'lib/snapshot/snapshot_config.rb', line 41

def initialize(path = './Snapfile')
  set_defaults

  if path and File.exists?path
    self.snapshot_file = SnapshotFile.new(path, self)
  else
    Helper.log.error "Could not find './Snapfile'. It is recommended to create a file using 'snapshot init' into the current directory. Using the defaults now."
  end
end

Instance Attribute Details

#build_commandString

Returns The build command, wich should build the app to ‘/tmp/snapshot’.

Returns:

  • (String)

    The build command, wich should build the app to ‘/tmp/snapshot’



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

def build_command
  @build_command
end

#devicesArray

Returns List of simulators to use.

Returns:

  • (Array)

    List of simulators to use



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

def devices
  @devices
end

#html_pathString

Returns The path, in which the HTML file should be exported to.

Returns:

  • (String)

    The path, in which the HTML file should be exported to



29
30
31
# File 'lib/snapshot/snapshot_config.rb', line 29

def html_path
  @html_path
end

#ios_versionString

Returns The iOS version (e.g. 8.1).

Returns:

  • (String)

    The iOS version (e.g. 8.1)



14
15
16
# File 'lib/snapshot/snapshot_config.rb', line 14

def ios_version
  @ios_version
end

#languagesArray

Returns A list of languages which should be used.

Returns:

  • (Array)

    A list of languages which should be used



11
12
13
# File 'lib/snapshot/snapshot_config.rb', line 11

def languages
  @languages
end

#manual_js_fileString

Returns The path to the JavaScript file to use.

Returns:

  • (String)

    The path to the JavaScript file to use



23
24
25
# File 'lib/snapshot/snapshot_config.rb', line 23

def manual_js_file
  @manual_js_file
end

#manual_schemeString

Returns The name of a scheme, manually set by the user using the config file.

Returns:

  • (String)

    The name of a scheme, manually set by the user using the config file



20
21
22
# File 'lib/snapshot/snapshot_config.rb', line 20

def manual_scheme
  @manual_scheme
end

#project_pathString

Returns The path to the project/workspace.

Returns:

  • (String)

    The path to the project/workspace



17
18
19
# File 'lib/snapshot/snapshot_config.rb', line 17

def project_path
  @project_path
end

#screenshots_pathString

Returns The path, in which the screenshots should be stored.

Returns:

  • (String)

    The path, in which the screenshots should be stored



26
27
28
# File 'lib/snapshot/snapshot_config.rb', line 26

def screenshots_path
  @screenshots_path
end

#snapshot_fileSnapshotFile

Returns:



5
6
7
# File 'lib/snapshot/snapshot_config.rb', line 5

def snapshot_file
  @snapshot_file
end

Class Method Details

.shared_instanceObject

A shared singleton



36
37
38
# File 'lib/snapshot/snapshot_config.rb', line 36

def self.shared_instance
  @@instance ||= SnapshotConfig.new
end

Instance Method Details

#js_fileObject

The JavaScript UIAutomation file



82
83
84
85
86
87
88
89
90
91
# File 'lib/snapshot/snapshot_config.rb', line 82

def js_file
  return manual_js_file if manual_js_file

  files = Dir.glob("./*.js").delete_if { |path| path.include?"SnapshotHelper.js" }
  if files.count == 1
    return files.first
  else
    raise "Could not determine which UIAutomation file to use. Please pass a path to your Javascript file using 'js_file'.".red
  end
end

#project_nameObject

Returns the file name of the project



77
78
79
# File 'lib/snapshot/snapshot_config.rb', line 77

def project_name
  (self.project_path.split('/').last.split('.').first rescue nil)
end

#schemeObject

The scheme to use (either it’s set, or there is only one, or user has to enter it)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/snapshot/snapshot_config.rb', line 94

def scheme
  begin
    command = "cd '#{project_path.split('/')[0..-2].join('/')}'; xcodebuild -list"
    schemes = `#{command}`.split("Schemes:").last.split("\n").each { |a| a.strip! }.delete_if { |a| a == '' }
    Helper.log.debug "Found available schemes: #{schemes}"

    self.manual_scheme = schemes.first if schemes.count == 1

    if self.manual_scheme
      if not schemes.include?manual_scheme
        raise "Could not find requested scheme '#{self.manual_scheme}' in Xcode's schemes #{schemes}"
      else
        return self.manual_scheme
      end
    else
      # We have to ask the user first
      puts "Found the following schemes in your project:".green
      puts "You can use 'scheme \"Name\"' in your Snapfile".green
      puts "--------------------------------------------".green
      while not schemes.include?self.manual_scheme
        schemes.each_with_index do |current, index|
          puts "#{index + 1}) #{current}"
        end
        val = gets.strip.to_i
        if val > 0
          self.manual_scheme = (schemes[val - 1] rescue nil)
        end
      end
      return self.manual_scheme
    end
  rescue Exception => ex
    raise "Could not fetch available schemes: #{ex}".red
  end
end

#set_defaultsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/snapshot/snapshot_config.rb', line 51

def set_defaults
  self.ios_version = '8.1'

  self.devices = [
    "iPhone 6 (#{self.ios_version} Simulator)",
    "iPhone 6 Plus (#{self.ios_version} Simulator)",
    "iPhone 5 (#{self.ios_version} Simulator)",
    "iPhone 4s (#{self.ios_version} Simulator)"
  ]

  self.languages = [
    'de-DE',
    'en-US'
  ]

  self.screenshots_path = './screenshots'

  self.project_path = (Dir.glob("./*.xcworkspace").first rescue nil) # prefer workspaces ofc
  self.project_path ||= (Dir.glob("./*.xcodeproj").first rescue nil)

  self.html_path = './screenshots.html'
end