Class: Fastlane::Actions::UploadToGoogleDriveAction

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

Class Method Summary collapse

Class Method Details

.authorsObject



133
134
135
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 133

def self.authors
  ['SpQuyt (@spquyt)']
end

.available_optionsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 67

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :drive_keyfile,
                                env_name: 'GDRIVE_KEY_FILE',
                                description: 'Json config file',
                                type: String,
                                default_value: 'drive_key.json',
                                verify_block: proc do |value|
                                  UI.user_error!("Couldn't find config keyfile at path '#{value}'") unless File.exist?(value)
                                end),
    FastlaneCore::ConfigItem.new(key: :service_account,
                                env_name: 'GDRIVE_SERVICE_ACCOUNT',
                                description: 'Credential is service account',
                                optional: true,
                                is_string: false,
                                default_value: false),
    FastlaneCore::ConfigItem.new(key: :folder_id,
                                env_name: "GDRIVE_UPLOAD_FOLDER_ID",
                                description: "Upload target folder id",
                                optional: false,
                                type: String,
                                verify_block: proc do |value|
                                  UI.user_error!("No target folder id given, pass using `folder_id: 'some_id'`") unless value and !value.empty?
                                end),
    FastlaneCore::ConfigItem.new(key: :upload_files,
                                env_name: "GDRIVE_UPLOAD_FILES",
                                description: "Path to files to be uploaded",
                                optional: false,
                                is_string: false,
                                verify_block: proc do |value|
                                  UI.user_error!("upload_files must be an Array of paths to files") unless value.kind_of?(Array)
                                  UI.user_error!("No upload file is given, pass using `upload_files: ['a', 'b']`") unless value and !value.empty?
                                  value.each do |path|
                                    UI.user_error!("Couldn't find upload file at path '#{path}'") unless File.exist?(path)
                                  end
                                end),
    FastlaneCore::ConfigItem.new(key: :public_links,
                                 env_name: 'GDRIVE_PUBLIC_LINKS',
                                 description: 'Uploaded file links should be public',
                                 optional: true,
                                 default_value: false,
                                 is_string: false),
    FastlaneCore::ConfigItem.new(key: :keyword,
                                env_name: "GDRIVE_KEYWORD_LIST_FILES",
                                description: "A keyword to filter list files",
                                optional: true,
                                default_value: ""),
    FastlaneCore::ConfigItem.new(key: :delete_alias_keyword,
                                env_name: "GDRIVE_DELETE_ALIAS_WHEN_UPLOADING_KEYWORD",
                                description: "A keyword to delete alias files when uploading a new file",
                                optional: true,
                                default_value: ""),
  ]
end

.descriptionObject



56
57
58
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 56

def self.description
  'Upload files to Google Drive'
end

.detailsObject



60
61
62
63
64
65
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 60

def self.details
  [
    'Upload files to Google Drive',
    'See https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md to get a keyfile'
  ].join("\n")
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 137

def self.is_supported?(platform)
  true
end

.outputObject



122
123
124
125
126
127
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 122

def self.output
  [
    ['GDRIVE_UPLOADED_FILE_NAMES', 'The array of names of uploaded files'],
    ['GDRIVE_UPLOADED_FILE_URLS', 'The array of links to uploaded files']
  ]
end

.return_valueObject



129
130
131
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 129

def self.return_value
  # nothing
end

.run(params) ⇒ Object



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
46
47
48
49
50
51
52
53
54
# File 'lib/fastlane/plugin/googledrive_spquyt/actions/upload_to_google_drive_action.rb', line 11

def self.run(params)
  UI.message("Using config file: #{params[:drive_keyfile]}")
  session = Helper::GoogledriveSpquytHelper.setup(
    keyfile: params[:drive_keyfile],
    service_account: params[:service_account]
  )
  folder = Helper::GoogledriveSpquytHelper.file_by_id(
    session: session, fid: params[:folder_id]
  )

  # Begin of Delete alias function
  if params[:delete_alias_keyword]
    params[:keyword] = params[:delete_alias_keyword]
  end
  Actions::ListFilesGoogleDriveAction.run(params)
  files_alias_to_delete = Actions.lane_context[SharedValues::GDRIVE_FOLDER_FILES]
  files_alias_to_delete.each do |file_to_delete_item|
    UI.message("Deleting file #{file_to_delete_item.title}...")
    file_to_delete_item.delete(true)
  end
  # End of Delete alias function

  uploaded_files = []
  assets = params[:upload_files]
  generate_public_links = params[:public_links]

  UI.abort_with_message!("No files to upload") if assets.nil? or assets.empty?

  assets.each do |asset|
    UI.message('------------------')
    UI.important("Uploading #{asset}")
    uploaded_files.push(Helper::GoogledriveSpquytHelper.upload_file(file: folder, file_name: asset))
    UI.success('Success')
    UI.message('------------------')
  end

  Actions.lane_context[SharedValues::GDRIVE_UPLOADED_FILE_NAMES] = uploaded_files.map(&:title)
  Actions.lane_context[SharedValues::GDRIVE_UPLOADED_FILE_URLS] =
    if generate_public_links
      uploaded_files.map(&Helper::GoogledriveSpquytHelper.method(:create_public_url))
    else
      uploaded_files.map(&:human_url)
    end
end