This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
/
ftp.rb
130 lines (96 loc) · 3.83 KB
/
ftp.rb
1
2
3
4
5
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
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
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
121
122
123
124
125
126
127
128
129
130
require 'net/ftp'
module Shenzhen::Plugins
module FTP
class Client
def initialize(host, user, pass)
@host, @user, @password = host, user, pass
@connection = Net::FTP.new
@connection.passive = true
@connection.connect(@host)
end
def upload_build(ipa, options)
path = expand_path_with_substitutions_from_ipa_plist(ipa, options[:path])
begin
@connection.login(@user, @password) rescue raise "Login authentication failed"
if options[:mkdir]
components, pwd = path.split(/\//), nil
components.each do |component|
pwd = File.join(*[pwd, component].compact)
begin
@connection.mkdir pwd
rescue => exception
raise exception unless /File exists/ === exception.message
end
end
end
@connection.chdir path unless path.empty?
@connection.putbinaryfile ipa, File.basename(ipa)
if dsym = options.delete(:dsym)
@connection.putbinaryfile dsym, File.basename(dsym)
end
ensure
@connection.close
end
end
private
def expand_path_with_substitutions_from_ipa_plist(ipa, path)
components = []
substitutions = path.scan(/\{CFBundle[^}]+\}/)
return path if substitutions.empty?
Dir.mktmpdir do |dir|
system "unzip -q #{ipa} -d #{dir} 2> /dev/null"
plist = Dir["#{dir}/**/Info.plist"].last
substitutions.uniq.each do |substitution|
key = substitution[1...-1]
value = Shenzhen::PlistBuddy.print(plist, key)
path.gsub!(Regexp.new(substitution), value) if value
end
end
return path
end
end
end
end
command :'distribute:ftp' do |c|
c.syntax = "ipa distribute:ftp [options]"
c.summary = "Distribute an .ipa file over FTP"
c.description = ""
c.example '', '$ ipa distribute:ftp --host 127.0.0.1 -f ./file.ipa -u username --path "/path/to/folder/{CFBundleVersion}/" --mkdir'
c.option '-f', '--file FILE', ".ipa file for the build"
c.option '-d', '--dsym FILE', "zipped .dsym package for the build"
c.option '-h', '--host HOST', "FTP host"
c.option '-u', '--user USER', "FTP user"
c.option '-p', '--password PASS', "FTP password"
c.option '-P', '--path PATH', "FTP path. Values from Info.plist will be substituded for keys wrapped in {} \n\t\t eg. \"/path/to/folder/{CFBundleVersion}/\" could be evaluated as \"/path/to/folder/1.0.0/\""
c.option '--[no-]mkdir', "Create directories on FTP if they don't already exist"
c.action do |args, options|
determine_file! unless @file = options.file
say_error "Missing or unspecified .ipa file" and abort unless @file and File.exist?(@file)
determine_dsym! unless @dsym = options.dsym
say_error "Specified dSYM.zip file doesn't exist" unless @dsym and File.exist?(@dsym)
determine_host! unless @host = options.host
say_error "Missing FTP host" and abort unless @host
determine_user! unless @user = options.user
say_error "Missing FTP user" and abort unless @user
determine_password! unless @password = options.password
say_error "Missing FTP password" and abort unless @password
@path = options.path || ""
client = Shenzhen::Plugins::FTP::Client.new(@host, @user, @password)
begin
client.upload_build @file, {:path => @path, :dsym => @dsym, :mkdir => !!options.mkdir}
say_ok "Build successfully uploaded to FTP" unless options.quiet
rescue => exception
say_error "Error while uploading to FTP: #{exception}"
end
end
private
def determine_host!
@host ||= ask "FTP Host:"
end
def determine_user!
@user ||= ask "Username:"
end
def determine_password!
@password ||= password "Password:"
end
end