fastlane-plugin-tinifyme 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7c4dd33581bb6fd8b2e7c4cb681a67c2aa638d722d4e1081de9088fdfbca5232
4
+ data.tar.gz: 4c6ae1449c2f5ba1c0ebe09f1f9a5d9bcf8db6b706808f0febebe2d0a141ef59
5
+ SHA512:
6
+ metadata.gz: c3ef02ca3e17fb18ec405dfac2283b203d8847cb8bd8dcb31a91e8100b81915a29693410e57c10109546cad24f004807e87e268d17ac7039c48285cf8c2d9c34
7
+ data.tar.gz: 29a06723613c74beb77d9a717caba722716476f18c3ad2ee6aeccffcee5473d277034b8775fa384704caab7eeacfa060549fdb80581dbe24599a770ea39acecc
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Danilo Becke <danilobecke@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # tinifyme plugin
2
+
3
+ [![fastlane Plugin Badge](https://rawcdn.githack.com/fastlane/fastlane/master/fastlane/assets/plugin-badge.svg)](https://rubygems.org/gems/fastlane-plugin-tinifyme)
4
+
5
+ ## Getting Started
6
+
7
+ This project is a [_fastlane_](https://github.com/fastlane/fastlane) plugin. To get started with `fastlane-plugin-tinifyme`, add it to your project by running:
8
+
9
+ ```bash
10
+ fastlane add_plugin tinifyme
11
+ ```
12
+
13
+ ## About tinifyme
14
+
15
+ Compress assets using [TinyPNG](https://tinypng.com). This plugin was designed to automate image compression in your project via pre-commit hook or one-off runs.
16
+
17
+ You'll need a TinyPNG developer API key, which can be freely obtained on their website: [https://tinypng.com/developers](https://tinypng.com/developers).
18
+
19
+ ## Example
20
+
21
+ Check out the [example `Fastfile`](fastlane/Fastfile) to see how to use this plugin.
22
+
23
+ ### Try it
24
+
25
+ Clone the repo and run `fastlane install_plugins`.
26
+
27
+ ### Compress a specific image
28
+
29
+ ```bash
30
+ bundle exec fastlane compress_image file_path:"path/to/file" api_key:"YOUR_API_KEY"
31
+ ```
32
+
33
+ ### Use as pre-commit hook
34
+
35
+ 1. Set your TinyPNG developer API key as the value of the `TINYPNG_API_KEY` env var
36
+ 2. Call your _fastlane_ lane from your `.git/hooks/pre-commit` file. For instance:
37
+
38
+ ```sh
39
+ bundle exec fastlane compress_images_hook
40
+ ```
41
+
42
+ ## Run tests for this plugin
43
+
44
+ To run both the tests, and code style validation, run
45
+
46
+ ```
47
+ rake
48
+ ```
49
+
50
+ To automatically fix many of the styling issues, use
51
+ ```
52
+ rubocop -a
53
+ ```
54
+
55
+ ## Issues and Feedback
56
+
57
+ For any other issues and feedback about this plugin, please submit it to this repository.
58
+
59
+ ## Troubleshooting
60
+
61
+ If you have trouble using plugins, check out the [Plugins Troubleshooting](https://docs.fastlane.tools/plugins/plugins-troubleshooting/) guide.
62
+
63
+ ## Using _fastlane_ Plugins
64
+
65
+ For more information about how the `fastlane` plugin system works, check out the [Plugins documentation](https://docs.fastlane.tools/plugins/create-plugin/).
66
+
67
+ ## About _fastlane_
68
+
69
+ _fastlane_ is the easiest way to automate beta deployments and releases for your iOS and Android apps. To learn more, check out [fastlane.tools](https://fastlane.tools).
@@ -0,0 +1,104 @@
1
+ require 'fastlane_core'
2
+ require 'fastlane/action'
3
+ require_relative '../helper/tinifyme_helper'
4
+
5
+ module Fastlane
6
+ module Actions
7
+ class TinifymeAction < Action
8
+ def self.run(params)
9
+ key = params[:api_key]
10
+ file_path = params[:file_path]
11
+ helper = Helper::TinifymeHelper.new
12
+ if file_path
13
+ compress([file_path], key)
14
+ else
15
+ UI.message(helper.format_output('Checking staged files...', is_step: true))
16
+ modified_images = helper.get_modified_images(params[:image_formats])
17
+ length = modified_images.length
18
+ return UI.success(helper.format_output('No images found!')) unless length > 0
19
+
20
+ UI.success(helper.format_output("Found #{length} #{length > 1 ? 'images' : 'image'}."))
21
+ compressed = compress(modified_images, key)
22
+ return UI.abort_with_message!("The commit was aborted.") unless compressed || params[:abort_commit_without_internet_connection] == false
23
+
24
+ UI.message(helper.format_output('Adding to commit...', is_step: true))
25
+ helper.add_to_commit(modified_images)
26
+ UI.success(helper.format_output("#{compressed ? 'Compressed images' : 'Images'} added to the current commit."))
27
+ end
28
+ end
29
+
30
+ def self.description
31
+ "Compress assets using TinyPNG."
32
+ end
33
+
34
+ def self.authors
35
+ ["Danilo Becke"]
36
+ end
37
+
38
+ def self.return_value
39
+ end
40
+
41
+ def self.details
42
+ "fastlane plugin to automate image compression in your project via precommit hook or one-off runs."
43
+ end
44
+
45
+ def self.available_options
46
+ [
47
+ FastlaneCore::ConfigItem.new(
48
+ key: :api_key,
49
+ description: "Required TinyPNG API key (https://tinypng.com/developers)",
50
+ env_name: "TINYPNG_API_KEY",
51
+ optional: false,
52
+ type: String
53
+ ),
54
+ FastlaneCore::ConfigItem.new(
55
+ key: :file_path,
56
+ 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",
57
+ optional: true,
58
+ type: String
59
+ ),
60
+ FastlaneCore::ConfigItem.new(
61
+ key: :image_formats,
62
+ description: "Allowed image extensions to be compressed",
63
+ default_value: [".jpg", ".png", ".webp", ".jpeg"],
64
+ optional: false,
65
+ type: Array
66
+ ),
67
+ FastlaneCore::ConfigItem.new(
68
+ key: :abort_commit_without_internet_connection,
69
+ 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)",
70
+ default_value: true,
71
+ optional: false,
72
+ type: Boolean
73
+ )
74
+ ]
75
+ end
76
+
77
+ def self.is_supported?(platform)
78
+ true
79
+ end
80
+
81
+ class << self
82
+ private
83
+
84
+ def helper
85
+ @helper ||= Helper::TinifymeHelper.new
86
+ end
87
+
88
+ def compress(images, key)
89
+ unless helper.has_connection?
90
+ UI.important('No internet connection.')
91
+ return false
92
+ end
93
+
94
+ helper.validate_credentials!(key)
95
+ UI.message(helper.format_output('Compressing...', is_step: true))
96
+ helper.compress!(images)
97
+ length = images.length
98
+ UI.success(helper.format_output("Compressed #{length} #{length > 1 ? 'images' : 'image'}."))
99
+ true
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,53 @@
1
+ require 'fastlane_core/ui/ui'
2
+ require 'tinify'
3
+
4
+ module Fastlane
5
+ UI = FastlaneCore::UI unless Fastlane.const_defined?(:UI)
6
+
7
+ module Helper
8
+ class TinifymeHelper
9
+ def has_connection?
10
+ require 'net/http'
11
+ url = URI('https://www.google.com')
12
+ begin
13
+ http = Net::HTTP.start(url.host, url.port, use_ssl: true)
14
+ http.finish
15
+ return true
16
+ rescue SocketError
17
+ return false
18
+ end
19
+ end
20
+
21
+ def validate_credentials!(api_key)
22
+ UI.message(self.format_output('Checking TinyPNG credentials...', is_step: true))
23
+ Tinify.key = api_key
24
+ Tinify.validate!
25
+ UI.success(self.format_output('Valid credentials!'))
26
+ rescue Tinify::Error => e
27
+ UI.abort_with_message!(e)
28
+ end
29
+
30
+ def format_output(text, is_step: false)
31
+ if is_step
32
+ return " > #{text}"
33
+ else
34
+ return " #{text}"
35
+ end
36
+ end
37
+
38
+ def get_modified_images(image_extensions)
39
+ added_or_modified = `git diff --name-only --cached --diff-filter=d`
40
+ added_or_modified.split("\n").select { |file| file.downcase.end_with?(*image_extensions) }
41
+ end
42
+
43
+ def compress!(images)
44
+ images.each { |image| Tinify.from_file(image).to_file(image) }
45
+ end
46
+
47
+ def add_to_commit(images)
48
+ images_text = images.join(" ")
49
+ system("git add #{images_text}")
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Fastlane
2
+ module Tinifyme
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'fastlane/plugin/tinifyme/version'
2
+
3
+ module Fastlane
4
+ module Tinifyme
5
+ # Return all .rb files inside the "actions" and "helper" directory
6
+ def self.all_classes
7
+ Dir[File.expand_path('**/{actions,helper}/*.rb', File.dirname(__FILE__))]
8
+ end
9
+ end
10
+ end
11
+
12
+ # By default we want to import all available actions and helpers
13
+ # A plugin can contain any number of actions and plugins
14
+ Fastlane::Tinifyme.all_classes.each do |current|
15
+ require current
16
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fastlane-plugin-tinifyme
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Danilo Becke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tinify
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.0
27
+ description:
28
+ email: danilobecke@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE
34
+ - README.md
35
+ - lib/fastlane/plugin/tinifyme.rb
36
+ - lib/fastlane/plugin/tinifyme/actions/tinifyme_action.rb
37
+ - lib/fastlane/plugin/tinifyme/helper/tinifyme_helper.rb
38
+ - lib/fastlane/plugin/tinifyme/version.rb
39
+ homepage: https://github.com/danilobecke/fastlane-plugin-tinifyme
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ rubygems_mfa_required: 'true'
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '3.1'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.10
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Compress assets using TinyPNG.
63
+ test_files: []