Skip to content

Commit

Permalink
Added text margins percentages config item and corresponding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
revolter committed Apr 1, 2018
1 parent 4053a2a commit f1c5594
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/fastlane/plugin/icon_versioning/actions/version_icon_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ def self.available_options
description: 'The text to overlay over the icon images',
type: String
),
FastlaneCore::ConfigItem.new(
key: :text_margins_percentages,
env_name: 'VERSION_ICON_TEXT_MARGINS_PERCENTAGES',
description: 'The percentages of the text margins relative to the image\'s size. The array must have all four margins: `text_margins_percentages: [top, right, bottom, left]`, two values: `text_margins_percentages: [vertical, horizontal]` or one value for all of them `text_margins: [all]`',
default_value: [0.06],
verify_block: proc do |value|
UI.user_error!('The number of margins is not equal to 1, 2 or 4') unless value.length == 1 || value.length == 2 || value.length == 4
UI.user_error!('At least one margin percentage is less than 0') if value.any? { |percentage| percentage < 0 }
UI.user_error!('At least one margin percentage is greater than 1') if value.any? { |percentage| percentage > 1 }
end,
optional: true,
type: Array
),
FastlaneCore::ConfigItem.new(
key: :band_height_percentage,
env_name: 'VERSION_ICON_BAND_HEIGHT_PERCENTAGE',
Expand Down
19 changes: 17 additions & 2 deletions lib/fastlane/plugin/icon_versioning/helper/version_icon_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ def initialize(params)
@appiconset_path = File.expand_path(params[:appiconset_path])
@text = params[:text]

text_margins_percentages = params[:text_margins_percentages]

text_margins_percentages *= 4 if text_margins_percentages.length == 1
text_margins_percentages *= 2 if text_margins_percentages.length == 2

@text_top_margin_percentage = text_margins_percentages[0]
@text_right_margin_percentage = text_margins_percentages[1]
@text_bottom_margin_percentage = text_margins_percentages[2]
@text_left_margin_percentage = text_margins_percentages[3]

@band_height_percentage = params[:band_height_percentage]
@band_blur_radius_percentage = params[:band_blur_radius_percentage]
@band_blur_sigma_percentage = params[:band_blur_sigma_percentage]
Expand Down Expand Up @@ -88,6 +98,11 @@ def version_icon(original_icon_path, versioned_icon_path)

band_top_position = image_height - band_height

text_top_margin = image_height * @text_top_margin_percentage
text_right_margin = image_width * @text_right_margin_percentage
text_bottom_margin = image_height * @text_bottom_margin_percentage
text_left_margin = image_width * @text_left_margin_percentage

blurred_icon_path = suffix(versioned_icon_path, 'blurred')
mask_icon_path = suffix(versioned_icon_path, 'mask')
text_base_icon_path = suffix(versioned_icon_path, 'text_base')
Expand Down Expand Up @@ -118,7 +133,7 @@ def version_icon(original_icon_path, versioned_icon_path)

MiniMagick::Tool::Convert.new do |convert|
convert << '-background' << 'none'
convert << '-size' << "#{image_width}x#{band_height}"
convert << '-size' << "#{image_width - (text_left_margin + text_right_margin)}x#{band_height - (text_top_margin + text_bottom_margin)}"
convert << '-fill' << 'white'
convert << '-gravity' << 'center'
# using label instead of caption prevents wrapping long lines
Expand All @@ -142,7 +157,7 @@ def version_icon(original_icon_path, versioned_icon_path)
convert << '-geometry' << "+0+#{band_top_position}"
convert << '-composite'
convert << text_icon_path
convert << '-geometry' << "+0+#{band_top_position}"
convert << '-geometry' << "+#{text_left_margin}+#{band_top_position + text_top_margin}"
convert << '-composite'
convert << versioned_icon_path
end
Expand Down
58 changes: 58 additions & 0 deletions spec/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,64 @@
end
end

context 'when passing the text margins percentages' do
it 'sets the value when it is 1' do
options = { text_margins_percentages: [0.1] }

config = configuration.create(action.available_options, options)

expect(config[:text_margins_percentages]).to eq(options[:text_margins_percentages])
end

it 'sets the value when they are 2' do
options = { text_margins_percentages: [0.1, 0.2] }

config = configuration.create(action.available_options, options)

expect(config[:text_margins_percentages]).to eq(options[:text_margins_percentages])
end

it 'sets the value when they are 4' do
options = { text_margins_percentages: [0.1, 0.2, 0.3, 0.4] }

config = configuration.create(action.available_options, options)

expect(config[:text_margins_percentages]).to eq(options[:text_margins_percentages])
end

it 'raises an exception when they are less than 1' do
options = { text_margins_percentages: [] }

expect do
configuration.create(action.available_options, options)
end.to raise_error('The number of margins is not equal to 1, 2 or 4')
end

it 'raises an exception when they are greater than 4' do
options = { text_margins_percentages: [0.1, 0.2, 0.3, 0.4, 0.5] }

expect do
configuration.create(action.available_options, options)
end.to raise_error('The number of margins is not equal to 1, 2 or 4')
end

it 'raises an exception when any of them is less than 0' do
options = { text_margins_percentages: [0.1, -1.2] }

expect do
configuration.create(action.available_options, options)
end.to raise_error('At least one margin percentage is less than 0')
end

it 'raises an exception when any of them is greater than 1' do
options = { text_margins_percentages: [0.1, 0.2, 1.3, 0.4] }

expect do
configuration.create(action.available_options, options)
end.to raise_error('At least one margin percentage is greater than 1')
end
end

context 'when passing the band height percentage' do
it 'sets the value when it is valid' do
options = { band_height_percentage: 0.42 }
Expand Down

0 comments on commit f1c5594

Please sign in to comment.