Class: Pod::Transfer

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-marstransfer/transfer.rb

Instance Method Summary collapse

Constructor Details

#initialize(lockfile, pod_repo, tos_key, tos_domain, tos_bucket) ⇒ Transfer

Returns a new instance of Transfer.



9
10
11
12
13
14
15
# File 'lib/cocoapods-marstransfer/transfer.rb', line 9

def initialize(lockfile,pod_repo,tos_key,tos_domain,tos_bucket)
    @lockfile = lockfile
    @tos_key = tos_key
    @tos_domain = tos_domain
    @pod_repo = pod_repo
    @tos_bucket = tos_bucket
end

Instance Method Details

#transfer_demoObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cocoapods-marstransfer/transfer.rb', line 44

def transfer_demo()
    download_url = "https://github.com/volcengine/ve_Template_iOS/archive/refs/heads/main.zip"
    ve_template_dir = File.join(Dir.pwd, ["ve_Template_iOS.zip"])
    File.open(ve_template_dir, "wb") do |file|
        file.write URI.open(download_url).read
    end
    demo_url = upload_file(ve_template_dir)
    if !demo_url.nil?
        puts "[MARS] Transfer Demo Success: #{demo_url}"
    else
        puts "[MARS] Transfer Demo Failed"
    end
end

#transfer_pod(pod) ⇒ Object



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
# File 'lib/cocoapods-marstransfer/transfer.rb', line 59

def transfer_pod(pod)
    puts "[MARS] =========== Start Transfer #{pod} =============="
    version = @lockfile.version(pod)
    checksum = @lockfile.checksum(pod)
    md5 = Digest::MD5.hexdigest(pod)
    pod_spec_url = "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/#{md5[0,1]}/#{md5[1,1]}/#{md5[2,1]}/#{pod}/#{version}/#{pod}.podspec.json"
    puts "[MARS] send podspec request:#{pod_spec_url}"
    uri = URI(pod_spec_url)
    response = Net::HTTP.get(uri)
    json = JSON.parse(response)
    source = json["source"]
    source_url = source["git"] unless !source.has_key?("git")
    source_tag = source["tag"] unless !source.has_key?("tag")
    zip_url = nil
    if !source_url.nil? && !source_tag.nil?
        Dir.chdir("source") do
            cmd = "git clone #{source_url} #{pod} --branch #{source_tag} --config advice.detachedHead=false"
            puts "[MARS] clone repo :#{source_url}"
            system(cmd)

            archive_zip_path = "#{pod}.zip"
            archive_directory_path = "#{pod}"
            Zip::File.open( archive_zip_path, Zip::File::CREATE ) do |zip_file|
                Dir[ File.join( archive_directory_path, "**", "**" ) ].each do |file|
                    zip_file.add( file.sub( "#{ archive_directory_path }/", "" ), file )
                end
            end
            zip_url = upload_file(archive_zip_path)
        end

        if !zip_url.nil?
            puts "[MARS] Upload Success: url #{zip_url}"
            json["source"] = {"http" => zip_url}
            pod_spec_url = Dir.pwd + "/podspec/" + "#{pod}.podspec.json"
            File.open(pod_spec_url, 'w') { |file|
                file.write(JSON.pretty_generate(json))
            }
            ctr = "pod repo push #{@pod_repo} #{pod_spec_url} --skip-tests --skip-import-validation --allow-warnings"
            puts "[MARS] push podspec #{pod_spec_url} to #{@pod_repo}"
            system(ctr)
        end

    end
end

#transfer_pod_tos(pod) ⇒ Object



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cocoapods-marstransfer/transfer.rb', line 104

def transfer_pod_tos(pod)
    puts "[MARS] =========== Start Transfer #{pod} =============="
    version = @lockfile.version(pod)
    checksum = @lockfile.checksum(pod)
    md5 = Digest::MD5.hexdigest(pod)
    pod_spec_url = "https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/#{md5[0,1]}/#{md5[1,1]}/#{md5[2,1]}/#{pod}/#{version}/#{pod}.podspec.json"
    puts "[MARS] send podspec request:#{pod_spec_url}"
    uri = URI(pod_spec_url)
    response = Net::HTTP.get(uri)
    json = JSON.parse(response)
    source = json["source"]
    source_url = source["git"] unless !source.has_key?("git")
    source_tag = source["tag"] unless !source.has_key?("tag")
    response_code = ""
    if !source_url.nil? && !source_tag.nil?
        Dir.chdir("source") do
            cmd = "git clone #{source_url} #{pod} --branch #{source_tag} --config advice.detachedHead=false"
            puts "[MARS] clone repo :#{source_url}"
            system(cmd)

            archive_zip_path = "#{pod}.zip"
            archive_directory_path = "#{pod}"
            Zip::File.open( archive_zip_path, Zip::File::CREATE ) do |zip_file|
                Dir[ File.join( archive_directory_path, "**", "**" ) ].each do |file|
                    zip_file.add( file.sub( "#{ archive_directory_path }/", "" ), file )
                end
            end

            url = "http://#{@tos_bucket}.#{@tos_domain}/#{@tos_bucket}/#{pod}/#{version}/#{pod}.zip"
            uri = URI(url)
            req = Net::HTTP::Put.new(uri)
            req['Host'] = uri.hostname
            req['User-Agent'] = "tos-sdk-go"
            req['X-Tos-Access'] = @tos_key
            req['Content-Length'] = File.size(archive_zip_path)
            # puts "filesize:#{File.size(archive_zip_path)}"
            req.body_stream = File.open(archive_zip_path)
            response = Net::HTTP.start(uri.hostname, uri.port) do |http|
                http.request(req)
            end
            puts "[MARS] Upload file to TOS: Sending PUT #{url} with #{@tos_key}"
            puts "[MARS] Response #{response.code} #{response.message}"
            # puts "Headers: #{response.to_hash.inspect}"
            response_code = response.code
        end

        if response_code == "200"
            zip_url = "http://#{@tos_bucket}.#{@tos_domain}/#{@tos_bucket}/#{pod}/#{version}/#{pod}.zip"
            puts "[MARS] Upload Success: url #{zip_url}"
            json["source"] = {"http" => zip_url}
            pod_spec_url = Dir.pwd + "/podspec/" + "#{pod}.podspec.json"
            File.open(pod_spec_url, 'w') { |file|
                file.write(JSON.pretty_generate(json))
            }
            ctr = "pod repo push #{@pod_repo} #{pod_spec_url} --skip-tests --skip-import-validation --allow-warnings --silent"
            puts "[MARS] push podspec #{pod_spec_url} to #{@pod_repo}"
            system(ctr)
        end

    end
end

#upload_file(file_path) ⇒ Object



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
# File 'lib/cocoapods-marstransfer/transfer.rb', line 18

def upload_file(file_path)
    url = ""
    if @tos_domain.include?("http")
        url = "#{@tos_domain}/open_oss/upload_file"
    else
        url = "http://#{@tos_domain}/open_oss/upload_file"
    end
    uri = URI(url)
    req = Net::HTTP::Post.new(uri)
    req['Host'] = uri.hostname
    req['Content-Type'] = "multipart/form-data"
    req['Content-Length'] = File.size(file_path)
    form_data = [['file', File.open(file_path)],['file_path',@tos_bucket],['token',@tos_key]] # or File.open() in case of local file
    req.set_form(form_data, 'multipart/form-data')
    response = Net::HTTP.start(uri.hostname, uri.port) do |http|
        http.request(req)
    end
    puts "[MARS] Upload file to TOS: Sending PUT #{url} with key #{@tos_key} bucket #{@tos_bucket}"
    puts "[MARS] Response #{response.code} #{response.message}"
    # puts "Headers: #{response.to_hash.inspect}"
    data = JSON.parse(response.body)
    puts "[MARS] Response #{response.body}"
    zip_url = data["data"][0]
    zip_url
end