Skip to content

Commit

Permalink
[webui] Refactor Kiwi:Image.build_from_xml.
Browse files Browse the repository at this point in the history
Move the contents of the build_from_xml method into its own class
using instance methods to seperate indiviudal units of work.
  • Loading branch information
Evan Rolfe committed Oct 25, 2017
1 parent b03a2f0 commit 1e29b7c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 48 deletions.
47 changes: 2 additions & 45 deletions src/api/app/models/kiwi/image.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO: Please overwrite this comment with something explaining the model target
class Kiwi::Image < ApplicationRecord
#### Includes and extends

Expand Down Expand Up @@ -44,53 +43,11 @@ class Kiwi::Image < ApplicationRecord

#### Alias of methods
def self.build_from_xml(xml_string, md5)
xml = Xmlhash.parse(xml_string)
return new(name: 'New Image: Please provide a name', md5_last_revision: md5) if xml.blank?
new_image = new(name: xml['name'], md5_last_revision: md5)

repositories = [xml["repository"]].flatten.compact
new_image.use_project_repositories = repositories.any? { |repository| repository['source']['path'] == 'obsrepositories:/' }
repositories.reject{ |repository| repository['source']['path'] == 'obsrepositories:/' }.each.with_index(1) do |repository, index|
attributes = {
repo_type: repository['type'],
source_path: repository['source']['path'],
priority: repository['priority'],
order: index,
alias: repository['alias'],
replaceable: repository['status'] == 'replaceable',
username: repository['username'],
password: repository['password']
}
attributes['imageinclude'] = repository['imageinclude'] == 'true' if repository.key?('imageinclude')
attributes['prefer_license'] = repository['prefer-license'] == 'true' if repository.key?('prefer-license')

new_image.repositories.build(attributes)
end

[xml["packages"]].flatten.compact.each do |package_group_xml|
attributes = {
kiwi_type: package_group_xml['type'],
profiles: package_group_xml['profiles '],
pattern_type: package_group_xml['patternType']
}
package_group = Kiwi::PackageGroup.new(attributes)
[package_group_xml['package']].flatten.compact.each do |package|
attributes = {
name: package['name'],
arch: package['arch'],
replaces: package['replaces']
}
attributes['bootinclude'] = package['bootinclude'] == 'true' if package.key?('bootinclude')
attributes['bootdelete'] = package['bootdelete'] == 'true' if package.key?('bootdelete')
package_group.packages.build(attributes)
end
new_image.package_groups << package_group
end
new_image
Kiwi::Image::XmlParser.new(xml_string, md5).parse
end

def to_xml
Kiwi::Image::Xml.new(self).to_xml
Kiwi::Image::XmlBuilder.new(self).build
end

def write_to_backend
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# TODO: Please overwrite this comment with something explaining the model target
module Kiwi
class Image
class Xml
class XmlBuilder
def initialize(image)
@image = image
end

def to_xml
def build
doc = Nokogiri::XML::DocumentFragment.parse(kiwi_body)
image = doc.at_css('image')

Expand Down
91 changes: 91 additions & 0 deletions src/api/app/models/kiwi/image/xml_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
module Kiwi
class Image
class XmlParser
def initialize(xml_string, md5)
@xml_string = xml_string
@md5 = md5
end

def parse
return blank_image if xml_hash.blank?

new_image = Kiwi::Image.new(name: xml_hash['name'], md5_last_revision: @md5)

new_image.use_project_repositories = use_project_repositories?
new_image.repositories = repositories
new_image.package_groups = package_groups

new_image
end

private

def xml_hash
@xml_hash ||= Xmlhash.parse(@xml_string)
end

def blank_image
Kiwi::Image.new(name: 'New Image: Please provide a name', md5_last_revision: @md5)
end

def repositories_from_xml
@repositories_from_xml ||= [xml_hash["repository"]].flatten.compact
end

def use_project_repositories?
repositories_from_xml.any? do |repository|
repository['source']['path'] == 'obsrepositories:/'
end
end

# Return an array of Kiwi::Repository models from the parsed xml
def repositories
repositories_from_xml.reject{ |repository| repository['source']['path'] == 'obsrepositories:/' }.map.with_index(1) do |repository, index|
attributes = {
repo_type: repository['type'],
source_path: repository['source']['path'],
priority: repository['priority'],
order: index,
alias: repository['alias'],
replaceable: repository['status'] == 'replaceable',
username: repository['username'],
password: repository['password']
}
attributes['imageinclude'] = repository['imageinclude'] == 'true' if repository.key?('imageinclude')
attributes['prefer_license'] = repository['prefer-license'] == 'true' if repository.key?('prefer-license')

Repository.new(attributes)
end
end

# Return an array of Kiwi::PackageGroup models, including their related Kiwi::Package models, from the parsed xml
def package_groups
package_groups = []

[xml_hash["packages"]].flatten.compact.each do |package_group_xml|
package_group = Kiwi::PackageGroup.new(
kiwi_type: package_group_xml['type'],
profiles: package_group_xml['profiles '],
pattern_type: package_group_xml['patternType']
)

[package_group_xml['package']].flatten.compact.each do |package_xml|
attributes = {
name: package_xml['name'],
arch: package_xml['arch'],
replaces: package_xml['replaces']
}
attributes['bootinclude'] = package_xml['bootinclude'] == 'true' if package_xml.key?('bootinclude')
attributes['bootdelete'] = package_xml['bootdelete'] == 'true' if package_xml.key?('bootdelete')

package_group.packages.build(attributes)
end

package_groups << package_group
end

package_groups
end
end
end
end

0 comments on commit 1e29b7c

Please sign in to comment.