Class: CsProj::DetectValues

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

Class Method Summary collapse

Class Method Details

.abs_path(path) ⇒ Object



161
162
163
164
165
166
# File 'lib/csproj/detect_values.rb', line 161

def self.abs_path(path)
  return nil if path.nil?

  path = path.tr('\\', "/")
  File.expand_path(path)
end

.abs_project_path(path) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/csproj/detect_values.rb', line 153

def self.abs_project_path(path)
  return nil if path.nil?

  path = path.tr('\\', "/")
  platform_path = CsProj.config[:project_path]
  "#{File.dirname(platform_path)}/#{path}"
end

.detect_assembly_name(doc_csproj) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/csproj/detect_values.rb', line 112

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

  if [Platform::IOS, Platform::OSX].include? CsProj.config[:platform]
    CsProj.config[:assembly_name] = doc_csproj.css("PropertyGroup > AssemblyName").text
  elsif CsProj.config[:platform] == Platform::ANDROID
    doc = get_parser_handle CsProj.config[:manifest_path]
    CsProj.config[:assembly_name] = doc.xpath("string(//manifest/@package)")
  end
end

.detect_info_plistObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/csproj/detect_values.rb', line 95

def self.detect_info_plist
  return if CsProj.config[:plist_path] || (CsProj.config[:platform] != Platform::IOS)

  plist_path = abs_project_path find_file("Info.plist", 1)

  unless plist_path
    plist_path = abs_project_path "Info.plist"

    unless File.exist? plist_path
      puts "Not able to find Info.plist automatically, try to specify it via `plist_path` parameter."
      exit
    end
  end

  CsProj.config[:plist_path] = plist_path
end

.detect_manifest(doc_csproj) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/csproj/detect_values.rb', line 87

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

  doc_node = doc_csproj.css("AndroidManifest").first

  CsProj.config[:manifest_path] = abs_project_path doc_node.text
end

.detect_output_path(doc_csproj) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/csproj/detect_values.rb', line 69

def self.detect_output_path(doc_csproj)
  return if CsProj.config[:output_path] ||
    !CsProj.config[:build_configuration]

  configuration = CsProj.config[:build_configuration]
  platform = CsProj.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

  unless output_path
    puts "Not able to find output path automatically, try to specify it via `output_path` parameter."
    exit
  end

  CsProj.config[:output_path] = abs_project_path output_path
end

.detect_projectObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/csproj/detect_values.rb', line 44

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

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

  unless projects.any?
    puts "Not able to find any project in solution, that matches the platform `#{CsProj.config[:platform]}`."
    exit
  end

  project = projects.first
  csproj = fix_path_relative project.project_path

  unless csproj
    puts "Not able to find project file automatically, try to specify it via `project_path` parameter."
    exit
  end

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

.detect_solutionObject

Helper Methods



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/csproj/detect_values.rb', line 31

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

  sln = find_file("*.sln", 3)

  unless sln
    puts "Not able to find solution file automatically, try to specify it via `solution_path` parameter."
    exit
  end

  CsProj.config[:solution_path] = abs_path sln
end

.find_file(query, depth) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/csproj/detect_values.rb', line 125

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

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

  files.first
end

.fix_path_relative(path) ⇒ Object



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

def self.fix_path_relative(path)
  root = File.dirname CsProj.config[:solution_path]
  path = "#{root}/#{path}"
  path
end

.get_parser_handle(filename) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/csproj/detect_values.rb', line 139

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

  doc
end

.set_additional_default_valuesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/csproj/detect_values.rb', line 7

def self.set_additional_default_values
  config = CsProj.config

  if config[:platform] == Platform::ANDROID
    config[:build_platform] = "AnyCPU"
  end

  # Detect the project
  CsProj.project = Msbuild::Project.new(config)
  detect_solution
  detect_project

  doc_csproj = get_parser_handle config[:project_path]

  detect_output_path doc_csproj
  detect_manifest doc_csproj
  detect_info_plist
  detect_assembly_name doc_csproj

  config
end