Skip to content

Commit

Permalink
Split up some larger functions into smaller ones to satisfy rubocop.
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Hammond <[email protected]>
  • Loading branch information
macblazer committed Feb 1, 2024
1 parent c1c82f1 commit 6579cab
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 60 deletions.
8 changes: 7 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ AllCops:
Exclude:
- 'spec/fixtures/**/*'

# Allow RSpec files to have long blocks for the tests.
# Allow RSpec files to have long blocks for the unit tests.
Metrics/BlockLength:
AllowedMethods: ['describe', 'context', 'shared_examples']

# Allow some long methods because breaking them up doesn't help anything.
Metrics/MethodLength:
AllowedMethods: ['parse_options', 'add_to_bom']
Metrics/AbcSize:
AllowedMethods: ['parse_options', 'add_to_bom']
128 changes: 77 additions & 51 deletions lib/cyclonedx/cocoapods/bom_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,54 @@ class Pod
CHECKSUM_ALGORITHM = 'SHA-1'
HOMEPAGE_REFERENCE_TYPE = 'website'

def source_qualifier
return '' if source.nil? || source.source_qualifier.empty?

"?#{source.source_qualifier.map do |key, value|
"#{key}=#{CGI.escape(value)}"
end.join('&')}"
end

def purl_subpath
return '' unless name.split('/').length > 1

"##{name.split('/').drop(1).map do |component|
CGI.escape(component)
end.join('/')}"
end

def purl
purl_name = CGI.escape(name.split('/').first)
source_qualifier = if source.nil? || source.source_qualifier.empty?
''
else
"?#{source.source_qualifier.map do |key, value|
"#{key}=#{CGI.escape(value)}"
end.join('&')}"
end
purl_subpath = if name.split('/').length > 1
"##{name.split('/').drop(1).map do |component|
CGI.escape(component)
end.join('/')}"
else
''
end
"pkg:cocoapods/#{purl_name}@#{CGI.escape(version.to_s)}#{source_qualifier}#{purl_subpath}"
src_qualifier = source_qualifier
subpath = purl_subpath
"pkg:cocoapods/#{purl_name}@#{CGI.escape(version.to_s)}#{src_qualifier}#{subpath}"
end

def xml_add_author(xml, trim_strings_length)
return if author.nil?

if trim_strings_length.zero?
xml.author author
xml.publisher author
else
xml.author author.slice(0, trim_strings_length)
xml.publisher author.slice(0, trim_strings_length)
end
end

def xml_add_homepage(xml)
return if homepage.nil?

xml.externalReferences do
xml.reference(type: HOMEPAGE_REFERENCE_TYPE) do
xml.url homepage
end
end
end

def add_to_bom(xml, trim_strings_length = 0)
xml.component(type: 'library') do
unless author.nil?
if trim_strings_length.zero?
xml.author author
xml.publisher author
else
xml.author author.slice(0, trim_strings_length)
xml.publisher author.slice(0, trim_strings_length)
end
end
xml_add_author(xml, trim_strings_length)
xml.name name
xml.version version.to_s
xml.description { xml.cdata description } unless description.nil?
Expand All @@ -108,13 +126,7 @@ def add_to_bom(xml, trim_strings_length = 0)
xml.purl purl.slice(0, trim_strings_length)
end
xml.bomRef purl
unless homepage.nil?
xml.externalReferences do
xml.reference(type: HOMEPAGE_REFERENCE_TYPE) do
xml.url homepage
end
end
end
xml_add_homepage(xml)
end
end

Expand Down Expand Up @@ -162,29 +174,39 @@ def bom(version: 1, trim_strings_length: 0)
"Incorrect string length: #{trim_strings_length} should be an integer greater than 0"
end

unchecked_bom(version: version, trim_strings_length: trim_strings_length)
end

private

# does not verify parameters because the public method does that.
def unchecked_bom(version: 1, trim_strings_length: 0)
Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
xml.bom(xmlns: NAMESPACE, version: version.to_i.to_s, serialNumber: "urn:uuid:#{SecureRandom.uuid}") do
bom_metadata(xml)
xml.components do
pods.each do |pod|
pod.add_to_bom(xml, trim_strings_length)
end
end

xml.dependencies do
bom_dependencies(xml, dependencies)
end
bom_components(xml, pods, trim_strings_length)

bom_dependencies(xml, dependencies)
end
end.to_xml
end

private
def bom_components(xml, pods, trim_strings_length)
xml.components do
pods.each do |pod|
pod.add_to_bom(xml, trim_strings_length)
end
end
end

def bom_dependencies(xml, dependencies)
dependencies&.each do |key, array|
xml.dependency(ref: key) do
array.sort.each do |value|
xml.dependency(ref: value)
xml.dependencies do
dependencies&.each do |key, array|
xml.dependency(ref: key) do
array.sort.each do |value|
xml.dependency(ref: value)
end
end
end
end
Expand All @@ -193,16 +215,20 @@ def bom_dependencies(xml, dependencies)
def bom_metadata(xml)
xml.metadata do
xml.timestamp Time.now.getutc.strftime('%Y-%m-%dT%H:%M:%SZ')
xml.tools do
xml.tool do
xml.vendor 'CycloneDX'
xml.name 'cyclonedx-cocoapods'
xml.version VERSION
end
end
bom_tools(xml)
component&.add_to_bom(xml)
end
end

def bom_tools(xml)
xml.tools do
xml.tool do
xml.vendor 'CycloneDX'
xml.name 'cyclonedx-cocoapods'
xml.version VERSION
end
end
end
end
end
end
26 changes: 18 additions & 8 deletions lib/cyclonedx/cocoapods/cli_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,9 @@ def run
setup_logger(verbose: options[:verbose])
@logger.debug "Running cyclonedx-cocoapods with options: #{options}"

analyzer = PodfileAnalyzer.new(logger: @logger, exclude_test_targets: options[:exclude_test_targets])
podfile, lockfile = analyzer.ensure_podfile_and_lock_are_present(options)
pods, dependencies = analyzer.parse_pods(podfile, lockfile)
analyzer.populate_pods_with_additional_info(pods)
pods, dependencies = analyze(options)

builder = BOMBuilder.new(pods: pods, component: component_from_options(options), dependencies: dependencies)
bom = builder.bom(version: options[:bom_version] || 1,
trim_strings_length: options[:trim_strings_length] || 0)
write_bom_to_file(bom: bom, options: options)
build_and_write_bom(options, pods, dependencies)
rescue StandardError => e
@logger.error ([e.message] + e.backtrace).join($INPUT_RECORD_SEPARATOR)
exit 1
Expand Down Expand Up @@ -136,6 +130,22 @@ def parse_options
parsed_options
end

def analyze(options)
analyzer = PodfileAnalyzer.new(logger: @logger, exclude_test_targets: options[:exclude_test_targets])
podfile, lockfile = analyzer.ensure_podfile_and_lock_are_present(options)
pods, dependencies = analyzer.parse_pods(podfile, lockfile)
analyzer.populate_pods_with_additional_info(pods)

[pods, dependencies]
end

def build_and_write_bom(options, pods, dependencies)
builder = BOMBuilder.new(pods: pods, component: component_from_options(options), dependencies: dependencies)
bom = builder.bom(version: options[:bom_version] || 1,
trim_strings_length: options[:trim_strings_length] || 0)
write_bom_to_file(bom: bom, options: options)
end

def component_from_options(options)
return unless options[:name]

Expand Down

0 comments on commit 6579cab

Please sign in to comment.