Class: Souyuz::DetectValues

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

Overview

This class detects all kinds of default values

Class Method Summary collapse

Class Method Details

.abs_path(path) ⇒ Object



153
154
155
156
157
# File 'lib/souyuz/detect_values.rb', line 153

def self.abs_path(path)
  path = path.gsub('\\', '/') # dir separator fix
  path = File.expand_path(path) # absolute dir
  path
end

.abs_project_path(path) ⇒ Object



146
147
148
149
150
151
# File 'lib/souyuz/detect_values.rb', line 146

def self.abs_project_path(path)
  path = path.gsub('\\', '/') # dir separator fix
  platform_path = Souyuz.config[:project_path]
  path = "#{File.dirname platform_path}/#{path}"
  path
end

.detect_assembly_name(doc_csproj) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/souyuz/detect_values.rb', line 105

def self.detect_assembly_name(doc_csproj)
  return if Souyuz.config[:assembly_name]

  if [Platform::IOS, Platform::OSX].include? Souyuz.config[:platform]
    Souyuz.config[:assembly_name] = doc_csproj.css('PropertyGroup > AssemblyName').text
  elsif Souyuz.config[:platform] == Platform::ANDROID
    doc = get_parser_handle Souyuz.config[:manifest_path] # explicitly for this call, no cache needed
    Souyuz.config[:assembly_name] = doc.xpath('string(//manifest/@package)')
  end
end

.detect_build_toolsObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/souyuz/detect_values.rb', line 82

def self.detect_build_tools
  return if Souyuz.config[:buildtools_path]

  UI.user_error! "Please ensure that the Android SDK is installed and the ANDROID_HOME variable is set correctly" unless ENV['ANDROID_HOME']

  # determine latest buildtool version
  buildtools = File.join(ENV['ANDROID_HOME'], 'build-tools')
  version = Dir.entries(buildtools).sort.last

  UI.success "Using Buildtools Version: #{version}..."

  Souyuz.config[:buildtools_path] = File.join(buildtools, version)
end

.detect_info_plistObject



96
97
98
99
100
101
102
103
# File 'lib/souyuz/detect_values.rb', line 96

def self.detect_info_plist
  return if Souyuz.config[:plist_path] or Souyuz.config[:platform] != Platform::IOS

  plist_path = find_file('Info.plist', 1) # search for plist
  UI.user_error! 'Not able to find Info.plist automatically, try to specify it via `plist_path` parameter.' unless plist_path

  Souyuz.config[:plist_path] = abs_project_path plist_path
end

.detect_manifest(doc_csproj) ⇒ Object



75
76
77
78
79
80
# File 'lib/souyuz/detect_values.rb', line 75

def self.detect_manifest(doc_csproj)
  return if Souyuz.config[:manifest_path] or Souyuz.config[:platform] != Platform::ANDROID

  doc_node = doc_csproj.css('PropertyGroup > AndroidManifest')
  Souyuz.config[:manifest_path] = abs_project_path doc_node.text
end

.detect_output_path(doc_csproj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/souyuz/detect_values.rb', line 62

def self.detect_output_path(doc_csproj)
  return if Souyuz.config[:output_path]

  configuration = Souyuz.config[:build_configuration]
  platform = Souyuz.config[:build_platform]

  doc_node = doc_csproj.xpath("/*[local-name()='Project']/*[local-name()='PropertyGroup'][translate(@*[local-name() = 'Condition'],'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = \" '$(configuration)|$(platform)' == '#{configuration.downcase}|#{platform.downcase}' \"]/*[local-name()='OutputPath']/text()")
  output_path = doc_node.text
  UI.user_error! 'Not able to find output path automatically, try to specify it via `output_path` parameter.' unless output_path

  Souyuz.config[:output_path] = abs_project_path output_path
end

.detect_projectObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/souyuz/detect_values.rb', line 45

def self.detect_project
  return if Souyuz.config[:project_path]

  path = Souyuz.config[:solution_path]
  projects = Msbuild::SolutionParser.parse(path)
                                    .get_platform Souyuz.config[:platform]

  UI.user_error! "Not able to find any project in solution, that matches the platform `#{Souyuz.config[:platform]}`." unless projects.any?

  project = projects.first
  csproj = fix_path_relative project.project_path # get path relative to project root
  UI.user_error! 'Not able to find project file automatically, try to specify it via `project_path` parameter.' unless csproj

  Souyuz.config[:project_name] = project.project_name
  Souyuz.config[:project_path] = abs_path csproj
end

.detect_solutionObject

Helper Methods



36
37
38
39
40
41
42
43
# File 'lib/souyuz/detect_values.rb', line 36

def self.detect_solution
  return if Souyuz.config[:solution_path]

  sln = find_file('*.sln', 3) # search for solution
  UI.user_error! 'Not able to find solution file automatically, try to specify it via `solution_path` parameter.' unless sln

  Souyuz.config[:solution_path] = abs_path sln
end

.find_file(query, depth) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/souyuz/detect_values.rb', line 118

def self.find_file(query, depth)
  itr = 0
  files = []

  loop do
    files = Dir.glob(query)
    query = "../#{query}"
    itr += 1
    break if files.any? or itr > depth
  end

  return files.first # pick first file
end

.fix_path_relative(path) ⇒ Object



140
141
142
143
144
# File 'lib/souyuz/detect_values.rb', line 140

def self.fix_path_relative(path)
  root = File.dirname Souyuz.config[:solution_path] # failsafe to __FILE__ and __DIR__
  path = "#{root}/#{path}"
  path
end

.get_parser_handle(filename) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/souyuz/detect_values.rb', line 132

def self.get_parser_handle(filename)
  f = File.open(filename)
  doc = Nokogiri::XML(f)
  f.close

  return doc
end

.set_additional_default_valuesObject

This is needed as these are more complex default values Returns the finished config object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/souyuz/detect_values.rb', line 8

def self.set_additional_default_values
  config = Souyuz.config

  # TODO: detect_platform automatically for :platform config

  # set correct implicit build platform for android
  if config[:platform] == Platform::ANDROID
    config[:build_platform] = 'AnyCPU'
  end

  # Detect the project
  Souyuz.project = Msbuild::Project.new(config)
  detect_solution
  detect_project # we can only do that *after* we detected the solution

  doc_csproj = get_parser_handle config[:project_path]

  detect_output_path doc_csproj
  detect_manifest doc_csproj
  detect_build_tools
  detect_info_plist
  detect_assembly_name doc_csproj # we can only do that for android *after* we detected the android manitfest

  return config
end