Class: Fastlane::Actions::UnusedImagesAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



84
85
86
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 84

def self.authors
  ['Krzysztof Piatkowski', 'Jakob Jensen']
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 47

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :dry_run,
                            env_name: "FL_IOS_IMAGE_ASSETS_UNUSED_IMAGES_DRY_RUN",
                         description: "Dry run - will not delete images",
                       default_value: false,
                            optional: true,
                           is_string: false),
    FastlaneCore::ConfigItem.new(key: :paths,
                                   env_name: 'FL_IOS_IMAGE_ASSETS_UNUSED_IMAGES_PATHS',
                                   description: 'single path or Array of paths to image assets to convert',
                                   is_string: false),
    FastlaneCore::ConfigItem.new(key: :code_path,
                                   env_name: 'FL_IOS_IMAGE_ASSETS_UNUSED_IMAGES_CODE_PATH',
                                   description: 'path to check',
                                   is_string: true),
    FastlaneCore::ConfigItem.new(key: :ignore,
                                   env_name: 'FL_IOS_IMAGE_ASSETS_UNUSED_IMAGES_CODE_IGNORE',
                                   description: 'paths to ignore',
                                   is_string: false),
    FastlaneCore::ConfigItem.new(key: :extensions,
                                   env_name: 'FL_IOS_IMAGE_ASSETS_UNUSED_IMAGES_EXTENSIONS',
                                   description: '',
                                   is_string: false)

  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 88

def self.is_supported?(platform)
  [:ios].include? platform
end

.outputObject



75
76
77
78
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 75

def self.output
  [
  ]
end

.return_valueObject



80
81
82
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 80

def self.return_value
  # If you method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fastlane/plugin/static_assets/actions/unused_images_action.rb', line 6

def self.run(params)
  params[:paths] = [params[:paths]] unless params[:paths].kind_of?(Array)
  params[:ignore] = [params[:ignore]] unless params[:ignore].kind_of?(Array)
  params[:extensions] = [params[:extensions]] unless params[:extensions].kind_of?(Array)

  ignore = params[:ignore].map do |i|
    File.expand_path("#{FastlaneCore::FastlaneFolder.path}/../#{i}")
  end

  code_path = params[:code_path]
  extensions = params[:extensions].join('|')

  image_paths = Helper::StaticAssetsHelper.fetch_images(params[:paths])

  files = Find.find("#{FastlaneCore::FastlaneFolder.path}../#{code_path}").grep(/.*#{extensions}$/)

  files = files.delete_if do |file|
    ignore.include?(File.expand_path(file))
  end

  deletable_images = image_paths.keys.reject do |image|
    r = files.inject(false) do |result, file|
      result || File.foreach(file).grep(/#{image}/).any?
    end
    r
  end

  if deletable_images.count == 0
    UI.success "No unused images present"
  else
    UI.header "#{deletable_images.count} unused images:"
    deletable_images.each do |image|
      image_path = image_paths[image]
      image_path.each do |ip|
        UI.important " - removing #{ip.sub('./fastlane/../', '')}"
        Helper::StaticAssetsHelper.remove_dir(ip) unless params[:dry_run] || !File.exist?(ip)
      end
    end
  end
end