Class: IronHammer::Utils::IvyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/iron_hammer/utils/ivy_builder.rb

Constant Summary collapse

VERSION_PATTERN =
/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
VERSION_REPLACE =
{
  'latest' => '+',
  'latest.major' => '+',
  'latest.minor' => '\\1.+',
  'latest.revision' => '\\1.\\2.+',
  'latest.build' => '\\1.\\2.\\3.+',
  'specific' => '\\1.\\2.\\3.\\4'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ IvyBuilder

Returns a new instance of IvyBuilder.



17
18
19
20
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 17

def initialize params={}
  @project = params[:project] || raise('You must specify a project')
  @config = params[:config] || raise('You must specify a config')
end

Class Method Details

.rename_artifactsObject



94
95
96
97
98
99
100
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 94

def self.rename_artifacts
  Dir["Libraries/*.{dll,exe}"].each do |file|
    file.scan(/Libraries\/(.*)-([\d\.]*)\.(.*)/) do |name, version, extension|
      FileUtils.mv(file, "Libraries\\#{name}.#{extension}")
    end
  end
end

Instance Method Details

#generate_xml(dependencies = []) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 26

def generate_xml dependencies=[]
  xml = Builder::XmlMarkup.new(:indent => 2)
  xml.tag! 'ivy-module', :version => '2.0' do
    name = @project.assembly_name
    xml.info :organisation => @config.organisation, :module => name
    xml.publications do
      @project.artifacts.each do |artifact|
        names = artifact.split '.'
        extension = names.pop
        name = names.join '.'
        xml.artifact :name => name, :type => extension
      end
    end if @project.is_a? DllProject

    xml.dependencies do
      dependencies.each do |dependency|
        rev = dependency.version
        rev = rev.gsub VERSION_PATTERN, VERSION_REPLACE[@config.retrieve_version] unless dependency.specific
        xml.dependency :org => @config.organisation, :name => dependency.name, :rev => rev do
          xml.artifact :type => dependency.extension, :name => dependency.name
        end
      end
    end unless dependencies.empty?
  end
end

#modify_csprojObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 73

def modify_csproj
  xml = FileSystem.read_file @project.path, @project.csproj
  doc = REXML::Document.new xml
  references = doc.get_elements(ProjectFile::REFERENCE_PATH)
  references.each do |reference|
    artifact = artifact_for reference
    artifact.scan(/Libraries\/(.*)-([\d\.]*)\.(.*)/) do |name, version, extension|
      includes = reference.attribute('Include').value
      includes.gsub!(/Version=(.*?)(,|$)/, "Version=#{version}\\2")
      reference.add_attribute('Include', includes)

      artifact = "Libraries\\#{name}.#{extension}"
    end
    reference.elements['SpecificVersion'] = REXML::Element.new('SpecificVersion').add_text('false') unless reference.elements['SpecificVersion']
    reference.elements['HintPath'] = REXML::Element.new('HintPath').
      add_text([relative, "#{artifact}"].flatten.patheticalize)
  end

  FileSystem.write! :path => @project.path, :name => @project.csproj, :content => doc.to_s
end

#publish(ivy_file) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 59

def publish ivy_file
  "java -jar #{@config.ivy_jar}
    -ivy #{ivy_file}
    -settings #{@config.ivy_settings}
    -publish default
    -publishpattern #{@project.path_to_binaries}/[artifact].[ext]
    -revision #{@project.version}
    -overwrite true".gsub(/\s+/, ' ')
end

#relativeObject



102
103
104
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 102

def relative
  ['..'] * @project.path.split(/\/|\\/).size
end

#retrieve(ivy_file) ⇒ Object



52
53
54
55
56
57
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 52

def retrieve ivy_file
  "java -jar #{@config.ivy_jar}
    -ivy #{ivy_file}
    -settings #{@config.ivy_settings}
    -retrieve Libraries/[artifact]-[revision].[ext]".gsub(/\s+/, ' ')
end

#to_sObject



22
23
24
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 22

def to_s
  generate_xml @project.dependencies
end

#write_to(file) ⇒ Object



69
70
71
# File 'lib/iron_hammer/utils/ivy_builder.rb', line 69

def write_to file
  File.open(file, "w") { |f| f.write to_s }
end