Class: Albacore::Project

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/albacore/project.rb

Overview

A project encapsulates the properties from a xxproj file.

Direct Known Subclasses

CsharpProject, FsharpProject, VbProject

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#debug, #err, #error, #fatal, #info, #puts, #trace, #warn

Constructor Details

#initialize(proj_path) ⇒ Project

Returns a new instance of Project.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
# File 'lib/albacore/project.rb', line 87

def initialize proj_path
  raise ArgumentError, 'project path does not exist' unless File.exists? proj_path.to_s
  proj_path                       = proj_path.to_s unless proj_path.is_a? String
  @proj_xml_node                  = Nokogiri.XML(open(proj_path))
  @proj_path_base, @proj_filename = File.split proj_path
  @ext = File.extname @proj_filename
  @proj_filename_noext = File.basename @proj_filename, ext
  sanity_checks
end

Instance Attribute Details

#extObject (readonly)

Returns the value of attribute ext.



80
81
82
# File 'lib/albacore/project.rb', line 80

def ext
  @ext
end

#proj_filenameObject (readonly)

Returns the value of attribute proj_filename.



80
81
82
# File 'lib/albacore/project.rb', line 80

def proj_filename
  @proj_filename
end

#proj_filename_noextObject (readonly)

Returns the value of attribute proj_filename_noext.



80
81
82
# File 'lib/albacore/project.rb', line 80

def proj_filename_noext
  @proj_filename_noext
end

#proj_path_baseObject (readonly)

Returns the value of attribute proj_path_base.



80
81
82
# File 'lib/albacore/project.rb', line 80

def proj_path_base
  @proj_path_base
end

#proj_xml_nodeObject (readonly)

Returns the value of attribute proj_xml_node.



80
81
82
# File 'lib/albacore/project.rb', line 80

def proj_xml_node
  @proj_xml_node
end

Instance Method Details

#asmnameObject

get the assembly name specified in the project file



128
129
130
# File 'lib/albacore/project.rb', line 128

def asmname
  read_property('AssemblyName') || proj_filename_noext
end

#assembly_info_pathObject

Get AssemblyInfo path

Returns:

  • string or project base path if path not found



383
384
385
386
387
388
389
390
391
# File 'lib/albacore/project.rb', line 383

def assembly_info_path
  result=@proj_xml_node.css("Compile[Include*='AssemblyInfo']").first #
  p     = if result.nil?
    @proj_path_base
  else
    File.expand_path(File.join(@proj_path_base, '/', Albacore::Paths.normalise_slashes(result.attributes["Include"].value)))
  end
  p
end

#authorsObject

gets any authors from the project file



143
144
145
# File 'lib/albacore/project.rb', line 143

def authors
  read_property 'Authors'
end

#debug_type(conf) ⇒ Object



197
198
199
# File 'lib/albacore/project.rb', line 197

def debug_type conf
  dt = read_property "DebugType", conf
end

#declared_packagesObject



309
310
311
# File 'lib/albacore/project.rb', line 309

def declared_packages
  return nuget_packages || paket_packages || []
end

#declared_projectsObject



313
314
315
316
317
318
319
320
321
# File 'lib/albacore/project.rb', line 313

def declared_projects
  @proj_xml_node.css("ProjectReference").collect do |proj_ref|
    debug do
      ref_name = proj_ref.css("Name").inner_text
      "found project reference: #{name} => #{ref_name} [albacore: project]"
    end
    Project.new(File.join(@proj_path_base, Albacore::Paths.normalise_slashes(proj_ref['Include'])))
  end
end

#default_assembly_versionObject

Reads assembly version information Returns 1.0.0.0 if AssemblyVersion is not found

Returns:

  • string



396
397
398
399
400
401
402
403
404
405
406
# File 'lib/albacore/project.rb', line 396

def default_assembly_version
  begin
    info= File.read(assembly_info_path)
    v   = info.each_line
              .select { |l| !(l.start_with?('//')||l.start_with?('/*')) && l.include?('AssemblyVersion(') }.first
    reg = /"(.*?)"/
    reg.match(v).captures.first
  rescue
    '1.0.0.0'
  end
end

#default_platformObject



193
194
195
# File 'lib/albacore/project.rb', line 193

def default_platform
  @proj_xml_node.css('Project PropertyGroup Platform').first.inner_text || 'AnyCPU'
end

#descriptionObject



147
148
149
# File 'lib/albacore/project.rb', line 147

def description
  read_property 'Description'
end

#faulty_refsObject



289
290
291
# File 'lib/albacore/project.rb', line 289

def faulty_refs
  find_refs.to_a.keep_if { |r| r.children.css("HintPath").empty? }
end

#find_packagesObject

Find all packages that have been declared and can be found in ./src/packages. This is mostly useful if you have that repository structure. returns enumerable Package



342
343
344
345
346
347
348
# File 'lib/albacore/project.rb', line 342

def find_packages
  declared_packages.collect do |package|
    guess = ::Albacore::PackageRepo.new(%w|./packages ./src/packages|).find_latest package.id
    debug "#{name}: guess: #{guess} [albacore: project]"
    guess
  end
end

#find_refsObject

find the NodeList reference list



284
285
286
287
# File 'lib/albacore/project.rb', line 284

def find_refs
  # should always be there
  @proj_xml_node.css("Project Reference")
end

#guidObject

Get the project GUID without ‘or ‘’ characters.



98
99
100
# File 'lib/albacore/project.rb', line 98

def guid
  guid_raw.gsub /[\{\}]/, ''
end

#guid_rawObject

Get the project GUID as it is in the project file.



103
104
105
# File 'lib/albacore/project.rb', line 103

def guid_raw
  read_property 'ProjectGuid'
end

#has_faulty_refs?Boolean

Returns:

  • (Boolean)


293
294
295
# File 'lib/albacore/project.rb', line 293

def has_faulty_refs?
  faulty_refs.any?
end

#has_packages_config?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/albacore/project.rb', line 297

def has_packages_config?
  File.exists? package_config
end

#has_paket_deps?Boolean

Returns:

  • (Boolean)


301
302
303
# File 'lib/albacore/project.rb', line 301

def has_paket_deps?
  File.exists? paket_deps
end

#has_paket_refs?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/albacore/project.rb', line 305

def has_paket_refs?
  File.exists? paket_refs
end

#idObject

Get the project id specified in the project file. Defaults to #name.



108
109
110
# File 'lib/albacore/project.rb', line 108

def id
  (read_property 'Id') || name
end

#included_filesObject

returns a list of the files included in the project



324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/albacore/project.rb', line 324

def included_files
  ['Compile', 'Content', 'EmbeddedResource', 'None'].map { |item_name|
    proj_xml_node.xpath("/x:Project/x:ItemGroup/x:#{item_name}",
                        'x' => "http://schemas.microsoft.com/developer/msbuild/2003").collect { |f|
      debug "#{name}: #included_files looking at '#{f}' [albacore: project]"
      link = f.elements.select { |el| el.name == 'Link' }.map { |el| el.content }.first
      OpenStruct.new(
          :item_name => item_name.downcase,
          :link      => link,
          :include   => f['Include']
      )
    }
  }.flatten
end

#licenseObject

the license that the project has defined in the metadata in the xxproj file.



152
153
154
# File 'lib/albacore/project.rb', line 152

def license
  read_property 'License'
end

#nameObject Also known as: title

Get the project name specified in the project file. This is the same as the title of the nuspec and, if Id is not specified, also the id of the nuspec.



115
116
117
# File 'lib/albacore/project.rb', line 115

def name
  read_property('Name') || asmname || proj_filename_noext
end

#namespaceObject

Get the root namespace of the project



133
134
135
# File 'lib/albacore/project.rb', line 133

def namespace
  read_property 'RootNamespace'
end

#netcore?Boolean

The project is a .Net Core project.

Returns:

  • (Boolean)


123
124
125
# File 'lib/albacore/project.rb', line 123

def netcore?
  ! @proj_xml_node.css('Project').attr('Sdk').nil?
end

#output_dll(conf, fw) ⇒ Object

Gets the relative location (to the project base path) of the dll that it will output



279
280
281
# File 'lib/albacore/project.rb', line 279

def output_dll conf, fw
  output_paths(conf, fw).keep_if { |o| o.library? }.first
end

#output_file_extObject

“.exe”?, “.dll”?



184
185
186
187
188
189
190
191
# File 'lib/albacore/project.rb', line 184

def output_file_ext
  case output_type
  when OutputArtifact::LIBRARY
    ".dll"
  when OutputArtifact::EXECUTABLE
    ".exe"
  end
end

#output_typeObject

OutputArtifact::LIBRARY OutputArtifact::EXECUTABLE



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/albacore/project.rb', line 171

def output_type
  ot = read_property 'OutputType'
  case ot
  when 'Library'
    OutputArtifact::LIBRARY
  when 'Exe'
    OutputArtifact::EXECUTABLE
  else
    ot
  end
end

#outputs(conf, fw) ⇒ Object

Returns OutputArtifact[] or throws an error



243
244
245
246
247
248
249
250
# File 'lib/albacore/project.rb', line 243

def outputs conf, fw
  os = try_outputs(conf, fw)
  if os.empty?
    raise(ConfigurationNotFoundError, "could not find configuration '#{conf}'")
  else
    os
  end
end

#package_configObject

get the full path of ‘packages.config’



362
363
364
# File 'lib/albacore/project.rb', line 362

def package_config
  File.join @proj_path_base, 'packages.config'
end

#paket_depsObject

Get the full path of ‘paket.dependencies’



367
368
369
# File 'lib/albacore/project.rb', line 367

def paket_deps
  File.join @proj_path_base, 'paket.dependencies'
end

#paket_refsObject

Get the full path of ‘paket.references’



372
373
374
# File 'lib/albacore/project.rb', line 372

def paket_refs
  File.join @proj_path_base, 'paket.references'
end

#pathObject

get the path of the project file



351
352
353
# File 'lib/albacore/project.rb', line 351

def path
  File.join @proj_path_base, @proj_filename
end

#save(output = nil) ⇒ Object

save the xml



356
357
358
359
# File 'lib/albacore/project.rb', line 356

def save(output = nil)
  output = path unless output
  File.open(output, 'w') { |f| @proj_xml_node.write_xml_to f }
end

#symbols?(conf = 'Debug', platform = 'AnyCPU') ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/albacore/project.rb', line 165

def symbols? conf='Debug', platform='AnyCPU'
  read_property('DebugSymbols', conf) == 'true'
end

#target_frameworkObject

the target .NET Framework / .NET version



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/albacore/project.rb', line 202

def target_framework
  read = read_property('TargetFrameworkVersion')
  case read
  when 'v3.5'
    'net35'
  when 'v3.5.1'
    'net351'
  when 'v4.0'
    'net40'
  when 'v4.5'
    'net45'
  when 'v4.5.1'
    'net451'
  when 'v4.6'
    'net46'
  when 'v4.6.1'
    'net461'
  when 'v4.6.2'
    'net462'
  when 'v5.0'
    'net50'
  when 'v5.0.1'
    'net501'
  else
    read
  end
end

#target_frameworksObject

Gets the target frameworks as specified by .Net Core syntax



231
232
233
234
235
236
237
238
239
240
# File 'lib/albacore/project.rb', line 231

def target_frameworks
  if netcore?
    tfw = @proj_xml_node.css('Project PropertyGroup TargetFramework').inner_text
    tfws = @proj_xml_node.css('Project PropertyGroup TargetFrameworks').inner_text
    nfws = if tfw.nil? || tfw == '' then tfws else tfw end
    fws = nfws.split(';')
  else
    [ target_framework ]
  end
end

#to_sObject

Gets the path of the project file



377
378
379
# File 'lib/albacore/project.rb', line 377

def to_s
  path
end

#try_outputs(conf, fw) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/albacore/project.rb', line 252

def try_outputs conf, fw
  outputs = []
  if netcore? then
    outputs << OutputArtifact.new("bin/#{conf}/#{fw}/#{asmname}#{output_file_ext}", output_type)
    outputs << OutputArtifact.new("bin/#{conf}/#{fw}/#{asmname}.xml", OutputArtifact::XMLDOC) if xmldoc?
  else
    path = read_property 'OutputPath', conf, default_platform
    if path != ''
      full_path = Albacore::Paths.join(path, "#{asmname}#{output_file_ext}").to_s
      outputs << OutputArtifact.new(full_path, output_type)
    end

    if xmldoc? conf, default_platform
      xml_full_path = read_property 'DocumentationFile', conf
      outputs << OutputArtifact.new(xml_full_path, OutputArtifact::XMLDOC)
    end

    if symbols? conf, default_platform
      pdb_full_path = Albacore::Paths.join(path, "#{asmname}.pdb").to_s
      outputs << OutputArtifact.new(pdb_full_path, OutputArtifact::SYMBOLS)
    end
  end
  outputs
end

#versionObject

gets the version from the project file



138
139
140
# File 'lib/albacore/project.rb', line 138

def version
  read_property 'Version'
end

#xmldoc?(conf = 'Debug', platform = 'AnyCPU') ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
# File 'lib/albacore/project.rb', line 156

def xmldoc? conf='Debug', platform='AnyCPU'
  if netcore?
    gdf = read_property('GenerateDocumentationFile')
    !gdf.nil? && gdf == 'true'
  else
    ! read_property('DocumentationFile', conf, platform).nil?
  end
end