Skip to content

Commit 6579cab

Browse files
committed
Split up some larger functions into smaller ones to satisfy rubocop.
Signed-off-by: Kyle Hammond <[email protected]>
1 parent c1c82f1 commit 6579cab

File tree

3 files changed

+102
-60
lines changed

3 files changed

+102
-60
lines changed

.rubocop.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ AllCops:
2929
Exclude:
3030
- 'spec/fixtures/**/*'
3131

32-
# Allow RSpec files to have long blocks for the tests.
32+
# Allow RSpec files to have long blocks for the unit tests.
3333
Metrics/BlockLength:
3434
AllowedMethods: ['describe', 'context', 'shared_examples']
35+
36+
# Allow some long methods because breaking them up doesn't help anything.
37+
Metrics/MethodLength:
38+
AllowedMethods: ['parse_options', 'add_to_bom']
39+
Metrics/AbcSize:
40+
AllowedMethods: ['parse_options', 'add_to_bom']

lib/cyclonedx/cocoapods/bom_builder.rb

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -59,36 +59,54 @@ class Pod
5959
CHECKSUM_ALGORITHM = 'SHA-1'
6060
HOMEPAGE_REFERENCE_TYPE = 'website'
6161

62+
def source_qualifier
63+
return '' if source.nil? || source.source_qualifier.empty?
64+
65+
"?#{source.source_qualifier.map do |key, value|
66+
"#{key}=#{CGI.escape(value)}"
67+
end.join('&')}"
68+
end
69+
70+
def purl_subpath
71+
return '' unless name.split('/').length > 1
72+
73+
"##{name.split('/').drop(1).map do |component|
74+
CGI.escape(component)
75+
end.join('/')}"
76+
end
77+
6278
def purl
6379
purl_name = CGI.escape(name.split('/').first)
64-
source_qualifier = if source.nil? || source.source_qualifier.empty?
65-
''
66-
else
67-
"?#{source.source_qualifier.map do |key, value|
68-
"#{key}=#{CGI.escape(value)}"
69-
end.join('&')}"
70-
end
71-
purl_subpath = if name.split('/').length > 1
72-
"##{name.split('/').drop(1).map do |component|
73-
CGI.escape(component)
74-
end.join('/')}"
75-
else
76-
''
77-
end
78-
"pkg:cocoapods/#{purl_name}@#{CGI.escape(version.to_s)}#{source_qualifier}#{purl_subpath}"
80+
src_qualifier = source_qualifier
81+
subpath = purl_subpath
82+
"pkg:cocoapods/#{purl_name}@#{CGI.escape(version.to_s)}#{src_qualifier}#{subpath}"
83+
end
84+
85+
def xml_add_author(xml, trim_strings_length)
86+
return if author.nil?
87+
88+
if trim_strings_length.zero?
89+
xml.author author
90+
xml.publisher author
91+
else
92+
xml.author author.slice(0, trim_strings_length)
93+
xml.publisher author.slice(0, trim_strings_length)
94+
end
95+
end
96+
97+
def xml_add_homepage(xml)
98+
return if homepage.nil?
99+
100+
xml.externalReferences do
101+
xml.reference(type: HOMEPAGE_REFERENCE_TYPE) do
102+
xml.url homepage
103+
end
104+
end
79105
end
80106

81107
def add_to_bom(xml, trim_strings_length = 0)
82108
xml.component(type: 'library') do
83-
unless author.nil?
84-
if trim_strings_length.zero?
85-
xml.author author
86-
xml.publisher author
87-
else
88-
xml.author author.slice(0, trim_strings_length)
89-
xml.publisher author.slice(0, trim_strings_length)
90-
end
91-
end
109+
xml_add_author(xml, trim_strings_length)
92110
xml.name name
93111
xml.version version.to_s
94112
xml.description { xml.cdata description } unless description.nil?
@@ -108,13 +126,7 @@ def add_to_bom(xml, trim_strings_length = 0)
108126
xml.purl purl.slice(0, trim_strings_length)
109127
end
110128
xml.bomRef purl
111-
unless homepage.nil?
112-
xml.externalReferences do
113-
xml.reference(type: HOMEPAGE_REFERENCE_TYPE) do
114-
xml.url homepage
115-
end
116-
end
117-
end
129+
xml_add_homepage(xml)
118130
end
119131
end
120132

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

177+
unchecked_bom(version: version, trim_strings_length: trim_strings_length)
178+
end
179+
180+
private
181+
182+
# does not verify parameters because the public method does that.
183+
def unchecked_bom(version: 1, trim_strings_length: 0)
165184
Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
166185
xml.bom(xmlns: NAMESPACE, version: version.to_i.to_s, serialNumber: "urn:uuid:#{SecureRandom.uuid}") do
167186
bom_metadata(xml)
168-
xml.components do
169-
pods.each do |pod|
170-
pod.add_to_bom(xml, trim_strings_length)
171-
end
172-
end
173187

174-
xml.dependencies do
175-
bom_dependencies(xml, dependencies)
176-
end
188+
bom_components(xml, pods, trim_strings_length)
189+
190+
bom_dependencies(xml, dependencies)
177191
end
178192
end.to_xml
179193
end
180194

181-
private
195+
def bom_components(xml, pods, trim_strings_length)
196+
xml.components do
197+
pods.each do |pod|
198+
pod.add_to_bom(xml, trim_strings_length)
199+
end
200+
end
201+
end
182202

183203
def bom_dependencies(xml, dependencies)
184-
dependencies&.each do |key, array|
185-
xml.dependency(ref: key) do
186-
array.sort.each do |value|
187-
xml.dependency(ref: value)
204+
xml.dependencies do
205+
dependencies&.each do |key, array|
206+
xml.dependency(ref: key) do
207+
array.sort.each do |value|
208+
xml.dependency(ref: value)
209+
end
188210
end
189211
end
190212
end
@@ -193,16 +215,20 @@ def bom_dependencies(xml, dependencies)
193215
def bom_metadata(xml)
194216
xml.metadata do
195217
xml.timestamp Time.now.getutc.strftime('%Y-%m-%dT%H:%M:%SZ')
196-
xml.tools do
197-
xml.tool do
198-
xml.vendor 'CycloneDX'
199-
xml.name 'cyclonedx-cocoapods'
200-
xml.version VERSION
201-
end
202-
end
218+
bom_tools(xml)
203219
component&.add_to_bom(xml)
204220
end
205221
end
222+
223+
def bom_tools(xml)
224+
xml.tools do
225+
xml.tool do
226+
xml.vendor 'CycloneDX'
227+
xml.name 'cyclonedx-cocoapods'
228+
xml.version VERSION
229+
end
230+
end
231+
end
206232
end
207233
end
208234
end

lib/cyclonedx/cocoapods/cli_runner.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,9 @@ def run
3939
setup_logger(verbose: options[:verbose])
4040
@logger.debug "Running cyclonedx-cocoapods with options: #{options}"
4141

42-
analyzer = PodfileAnalyzer.new(logger: @logger, exclude_test_targets: options[:exclude_test_targets])
43-
podfile, lockfile = analyzer.ensure_podfile_and_lock_are_present(options)
44-
pods, dependencies = analyzer.parse_pods(podfile, lockfile)
45-
analyzer.populate_pods_with_additional_info(pods)
42+
pods, dependencies = analyze(options)
4643

47-
builder = BOMBuilder.new(pods: pods, component: component_from_options(options), dependencies: dependencies)
48-
bom = builder.bom(version: options[:bom_version] || 1,
49-
trim_strings_length: options[:trim_strings_length] || 0)
50-
write_bom_to_file(bom: bom, options: options)
44+
build_and_write_bom(options, pods, dependencies)
5145
rescue StandardError => e
5246
@logger.error ([e.message] + e.backtrace).join($INPUT_RECORD_SEPARATOR)
5347
exit 1
@@ -136,6 +130,22 @@ def parse_options
136130
parsed_options
137131
end
138132

133+
def analyze(options)
134+
analyzer = PodfileAnalyzer.new(logger: @logger, exclude_test_targets: options[:exclude_test_targets])
135+
podfile, lockfile = analyzer.ensure_podfile_and_lock_are_present(options)
136+
pods, dependencies = analyzer.parse_pods(podfile, lockfile)
137+
analyzer.populate_pods_with_additional_info(pods)
138+
139+
[pods, dependencies]
140+
end
141+
142+
def build_and_write_bom(options, pods, dependencies)
143+
builder = BOMBuilder.new(pods: pods, component: component_from_options(options), dependencies: dependencies)
144+
bom = builder.bom(version: options[:bom_version] || 1,
145+
trim_strings_length: options[:trim_strings_length] || 0)
146+
write_bom_to_file(bom: bom, options: options)
147+
end
148+
139149
def component_from_options(options)
140150
return unless options[:name]
141151

0 commit comments

Comments
 (0)