Class: Fastlane::Actions::TinifymeAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



34
35
36
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 34

def self.authors
  ["Danilo Becke"]
end

.available_optionsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 45

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :api_key,
      description: "Required TinyPNG API key (https://tinypng.com/developers)",
      env_name: "TINYPNG_API_KEY",
      optional: false,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :file_path,
      description: "If present, the action will compress the given image. If not, the action will act as a pre-commit hook and look for staged added or modified images",
      optional: true,
      type: String
    ),
    FastlaneCore::ConfigItem.new(
      key: :image_formats,
      description: "Allowed image extensions to be compressed",
      default_value: [".jpg", ".png", ".webp", ".jpeg"],
      optional: false,
      type: Array
    ),
    FastlaneCore::ConfigItem.new(
      key: :abort_commit_without_internet_connection,
      description: "Decide whether the commit should be aborted when there are images to be compressed and the internet connection is not reachable (thus, the compression won't be possible)",
      default_value: true,
      optional: false,
      type: Boolean
    )
  ]
end

.descriptionObject



30
31
32
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 30

def self.description
  "Compress assets using TinyPNG."
end

.detailsObject



41
42
43
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 41

def self.details
  "fastlane plugin to automate image compression in your project via precommit hook or one-off runs."
end

.is_supported?(platform) ⇒ Boolean



77
78
79
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 77

def self.is_supported?(platform)
  true
end

.return_valueObject



38
39
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 38

def self.return_value
end

.run(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb', line 8

def self.run(params)
  key = params[:api_key]
  file_path = params[:file_path]
  helper = Helper::TinifymeHelper.new
  if file_path
    compress([file_path], key)
  else
    UI.message(helper.format_output('Checking staged files...', is_step: true))
    modified_images = helper.get_modified_images(params[:image_formats])
    length = modified_images.length
    return UI.success(helper.format_output('No images found!')) unless length > 0

    UI.success(helper.format_output("Found #{length} #{length > 1 ? 'images' : 'image'}."))
    compressed = compress(modified_images, key)
    return UI.abort_with_message!("The commit was aborted.") unless compressed || params[:abort_commit_without_internet_connection] == false

    UI.message(helper.format_output('Adding to commit...', is_step: true))
    helper.add_to_commit(modified_images)
    UI.success(helper.format_output("#{compressed ? 'Compressed images' : 'Images'} added to the current commit."))
  end
end