Class: GALoader

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

Overview

Author:

  • Vittorio Monaco

Class Method Summary collapse

Class Method Details

.findFile(extension) ⇒ Object

Returns the first file with the given extension

Parameters:

  • extension (String)

    the file extension you want to search

Returns:

  • nil if no file is found, the first file with the given extension otherwise



49
50
51
52
53
54
# File 'lib/ga_loader.rb', line 49

def self.findFile(extension)
  files = Dir.glob("*.#{extension}")
  return nil if files.empty?
  
  return File.basename(files.first, ".*")
end

.findProjectObject

Note:

this searches only in the top level directory

Returns a possible project, in case one is not provided in the configuration



41
42
43
# File 'lib/ga_loader.rb', line 41

def self.findProject
  return findFile("xcodeproj")
end

.findWorkspaceObject

Note:

this searches only in the top level directory

Returns a possible workspace, in case one is not provided in the configuration



34
35
36
# File 'lib/ga_loader.rb', line 34

def self.findWorkspace
  return findFile("xcworkspace")
end

.getFileFromArray(files) ⇒ Object

Note:

the method will take the last element of the array

Note:

the method will take only the filename if the element of the array is a file, removing the extension and the path

Returns a filename from a given array

Parameters:

  • files (Array<String>)

    an array of strings



134
135
136
137
138
139
140
141
142
143
# File 'lib/ga_loader.rb', line 134

def self.getFileFromArray(files) 
  fileToTest = files.first
  
  if fileToTest.nil?
    GALogger.log('Failed to get file', :Error)
    abort
  else
    return File.basename(fileToTest, ".*")
  end
end

.outputConfiguration(configuration) ⇒ Object

Outputs a given configuration on the console

Parameters:



124
125
126
# File 'lib/ga_loader.rb', line 124

def self.outputConfiguration(configuration)
  GALogger.log(configuration.to_s, :Default, '')
end

.outputSampleConfiguration(eventuallyAbort = false) ⇒ Object

Outputs a sample configuration to let the user know how to create a valid one

(see #GAConfiguration)

Parameters:

  • eventuallyAbort (Bool) (defaults to: false)

    pass true if you want to abort after printing out the sample configuration. Default is false.



114
115
116
117
118
119
# File 'lib/ga_loader.rb', line 114

def self.outputSampleConfiguration(eventuallyAbort = false)
  GALogger.log('Sample configuration JSON: ' + GAConfiguration.sample.to_s, :Warning, '')
  if eventuallyAbort
    abort
  end
end

.readConfiguration(silent = false) ⇒ Object

Note:

if the file is not found, it will try to build with default values instead

Note:

this also outputs the final GAConfiguration built on the console

Reads the configuration from the file guardian_angel.json

(see #GAConfiguration)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ga_loader.rb', line 14

def self.readConfiguration(silent = false)
  configuration = GAConfiguration.new
  
  begin
    jsonDictionary = JSON.parse(File.read(CONFIG_FILENAME))
    configurationMerge = GAConfiguration.new(jsonDictionary)
    configuration = configuration.merge(configurationMerge)
  rescue
    GALogger.log("#{CONFIG_FILENAME} not found or not a valid JSON, using defaults", :Warning) unless silent
  end
  
  validateConfiguration(configuration)
  outputConfiguration(configuration) unless silent
  
  return configuration
end

.validateConfiguration(configuration) ⇒ Object

Note:

required attribute is workspace/project

Note:

if scheme is not specified, the same as the workspace/project will be used

Note:

if target is not specified, ‘workspace|projectTests’ will be used

Note:

the method will also make sure that the configured xctool executable can be found, or aborts otherwise

Validates a given configuration

Parameters:



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
98
99
100
101
102
103
104
105
106
107
# File 'lib/ga_loader.rb', line 63

def self.validateConfiguration(configuration)
  if configuration.workspace.nil? 
    possibleWorkspace = findWorkspace()
    
    if !possibleWorkspace
      possibleProject = configuration.project
      if possibleProject.nil?
        possibleProject = findProject()
        
        if !possibleProject
          GALogger.log("workspace or project was not specified and cannot be found, exiting", :Error)
          outputSampleConfiguration(eventuallyAbort = true)
        end
      end
      
      configurationMerge = GAConfiguration.new(GAConfiguration::GAConfigurationProject => possibleProject)
    else
      configurationMerge = GAConfiguration.new(GAConfiguration::GAConfigurationWorkspace => possibleWorkspace)
    end
    
    configuration = configuration.merge(configurationMerge)
  end
  
  if configuration.scheme.nil?
    unless configuration.project.nil?
      configurationMerge = GAConfiguration.new(GAConfiguration::GAConfigurationScheme => configuration.project)
    end
    unless configuration.workspace.nil?
      configurationMerge = GAConfiguration.new(GAConfiguration::GAConfigurationScheme => configuration.workspace)
    end
    
    configuration = configuration.merge(configurationMerge)
  end
  
  if configuration.target.nil?
    configurationMerge = GAConfiguration.new(GAConfiguration::GAConfigurationTarget => configuration.scheme + 'Tests')      
    configuration = configuration.merge(configurationMerge)
  end
  
  xctoolExists = system("which #{configuration.xctool_path} > /dev/null")
  if !xctoolExists
    GALogger.log(configuration.xctool_path + " can't be found. Aborting.", :Error)
    abort
  end
end