Class: Pod::Command::TuyaOssPublish

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-tuya-oss-publish/command/publish.rb,
lib/cocoapods-tuya-oss-publish/command/upload.rb

Overview

TODO:

Create a PR to add your plugin to CocoaPods/cocoapods.org in the ‘plugins.json` file, once your plugin is released.

This is an example of a cocoapods plugin adding a top-level subcommand to the ‘pod’ command.

You can also create subcommands of existing or new commands. Say you wanted to add a subcommand to ‘list` to show newly deprecated pods, (e.g. `pod list deprecated`), there are a few things that would need to change.

  • move this file to ‘lib/pod/command/list/deprecated.rb` and update the class to exist in the the Pod::Command::List namespace

  • change this class to extend from ‘List` instead of `Command`. This tells the plugin system that it is a subcommand of `list`.

  • edit ‘lib/cocoapods_plugins.rb` to require this file

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ TuyaOssPublish

Returns a new instance of TuyaOssPublish.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 58

def initialize(argv)
  @repo = argv.shift_argument
  @local_repo_path = "#{ENV["HOME"]}/.cocoapods/repos/#{@repo}"

  @podspec_path = argv.shift_argument
  @library = argv.flag?('library') # framework/library
  @spec_sources = argv.option('spec-sources')

  @upload_to = argv.option('upload-to', 'oss')
  @force = argv.flag?('force', false)

  @oss_endpoint = argv.option('oss-endpoint', 'https://oss-cn-hangzhou.aliyuncs.com')
  @oss_access_key_id = argv.option('oss-access-key-id')
  @oss_access_key_secret = argv.option('oss-access-key-secret')
  @oss_bucket_name = argv.option('oss-bucket-name')
  @oss_base_path = argv.option('oss-base-path', '')

  super
end

Class Method Details

.optionsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 34

def self.options
  [
    ['--library', 'Generate static libraries.'],
    ['--spec-sources=private,https://github.com/CocoaPods/Specs.git', 'The sources to pull dependant ' \
      'pods from (defaults to https://github.com/CocoaPods/Specs.git)'],

    ['--upload-to', 'The place where zip archive upload to, Defaults to oss. (oss/maven/bintray)'],
    ['--force', 'Override existing remote archive.'],

    ['--oss-endpoint', 'Aliyun oss endpoint, defaults to https://oss-cn-hangzhou.aliyuncs.com.'],
    ['--oss-access-key-id', 'Aliyun oss access key id.'],
    ['--oss-access-key-secret', 'Aliyun oss access key secret.'],
    ['--oss-bucket-name', 'Aliyun oss bucket name.'],
    ['--oss-base-path', 'Aliyun oss base path, defaults to /.'],

    ['--maven-auth', 'Maven auth (USER[:PASSWORD]).'],
    ['--maven-group-id', 'Maven group id, defaults to com.tuya.ios.'],

    ['--bintray-auth', 'Bintray auth (USER[:PASSWORD]).'],
    ['--bintray-subject', 'Bintray subject.'],
    ['--bintray-repo', 'Bintray repo.'],
  ]
end

Instance Method Details

#archiveObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/cocoapods-tuya-oss-publish/command/upload.rb', line 7

def archive
  pod_package_path = "#{@podspec.name}-#{@podspec.version}"
  file_path = "#{pod_package_path}.zip"
  UI.puts("Archiving #{file_path}")

  Dir.chdir(pod_package_path) do
    `rm -rf #{file_path}`
    `zip -ry #{file_path} Headers #{@podspec.available_platforms.map{|p| p.name}.join(" ")}`
  end

  return "#{pod_package_path}/#{file_path}"
end

#create_binary_podspec(source_spec, archive_url) ⇒ Object



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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 140

def create_binary_podspec(source_spec, archive_url)
  keys_to_be_removed = [
    "source",
    "prefix_header_contents",
    "prefix_header_file",
    "source_files",
    "public_header_files",
    "private_header_files",
    "vendored_frameworks",
    "vendored_libraries",
    "subspecs",
    "default_subspecs",
    # TODO copy resources
    # "resource_bundles",
    # "resources",
  ]

  # Remove source-related keys
  binary_spec = source_spec.to_hash
  for key in keys_to_be_removed do
    if binary_spec.key?(key)
      binary_spec.delete(key)
    end
    for platform in source_spec.available_platforms
      if binary_spec.key?(platform.name)
        if binary_spec[platform.name].key?(key)
          binary_spec[platform.name].delete(key)
        end
      else
        binary_spec[platform.name] = {}
      end
    end
  end

  binary_spec['static_framework'] = true

  # set source url
  binary_spec['source'] = {
    'type': 'zip',
    'http': archive_url,
  }

  # set vendored_libraries/vendored_frameworks
  for platform in source_spec.available_platforms
    if @library
      binary_spec[platform.name]['vendored_libraries'] = "#{platform.name}/*.a"
    else
      binary_spec[platform.name]['vendored_frameworks'] = "#{platform.name}/*.framework"
    end
  end

  # set headers
  if @library
    binary_spec['public_header_files'] = "Headers/**/*.h"
  end

  binary_spec = Specification.from_hash(binary_spec)
  UI.puts(binary_spec.to_pretty_json)

  binary_spec
end

#packageObject



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
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 112

def package

  # pod package
  podspec_path = "#{@podspec.name}.podspec.json"
  File.write(podspec_path, @podspec.to_pretty_json)

  argv = CLAide::ARGV.new([
    podspec_path,
    "--no-mangle",
    "--exclude-deps",
    "--spec-sources=#{@spec_sources}",
    @library ? "--library" : "",
    "--force",
    "--verbose",
  ])

  begin
    package = TYPackage.new(argv)
    package.run
  rescue => exception
    UI.puts(exception)
    raise "#{@podspec.name} (#{@podspec.version}) build failed."
  ensure
    File.delete(podspec_path)
  end

end

#repo_push_without_build(spec) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 202

def repo_push_without_build(spec)

  local_repo_path = "#{ENV["HOME"]}/.cocoapods/repos/#{@repo}"
  repo_spec_dir = "#{local_repo_path}/Specs/#{spec.name}/#{spec.version}"
  repo_spec_path = "#{repo_spec_dir}/#{spec.name}.podspec.json"

  # repo update
  exit_code = system("""cd #{repo_spec_dir} \
    && git fetch \
    && REPO_COMMIT_HASH=`git rev-parse origin/master` \
    && git reset --hard $REPO_COMMIT_HASH
  """)
  if exit_code != 0
    raise 'pod repo update failed.'
  end

  # write
  File.delete(repo_spec_path) if File.exist?(repo_spec_path)
  FileUtils.mkdir_p(repo_spec_dir)
  File.write(repo_spec_path, spec.to_pretty_json)

  # commit
  exit_code = system("""cd #{repo_spec_dir} \
    && git add . \
    && git commit -m \"⚠️ [Add] #{spec.name} (#{spec.version})\" \
    && git push
  """)
  if exit_code != 0
    raise 'pod repo push failed.'
  end

end

#runObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 96

def run
  @podspec = Pod::Specification::from_file(@podspec_path)
  UI.puts "Building #{@dynamic ? "dynamic" : "static"} #{@library ? "library" : "framework"}: #{@podspec.name} (#{@podspec.version})"

  self.package

  if @upload_to == 'oss'
    url = self.upload_to_oss(self.archive, @force)
  end

  binary_spec = self.create_binary_podspec(@podspec, url)

  self.repo_push_without_build(binary_spec)

end

#upload_to_oss(file_path, force) ⇒ Object



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
# File 'lib/cocoapods-tuya-oss-publish/command/upload.rb', line 20

def upload_to_oss(file_path, force)
  if not File.exist?(file_path)
    raise "#{file_path} not exists"
  end

  file_name = File.basename(file_path)
  object_key = "#{@oss_base_path}/#{file_name}"

  client = Aliyun::OSS::Client.new(
    :endpoint => @oss_endpoint,
    :access_key_id => @oss_access_key_id,
    :access_key_secret => @oss_access_key_secret)
  bucket = client.get_bucket(@oss_bucket_name)

  url = bucket.object_url(object_key, false)

  if bucket.object_exists?(object_key) && force == false
    # 正式版上传不覆盖
    raise "#{url} already exists" unless file_name.index(/\balpha|\bbeta|\brc|\bSNAPSHOT/)
  end

  UI.puts("Uploading #{file_path}")
  begin
    bucket.put_object(object_key, :file => file_path)
  rescue => exception
    UI.puts(exception)
    raise "#{file_name} upload failed."
  end

  puts(url)
  return url
end

#validate!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/cocoapods-tuya-oss-publish/command/publish.rb', line 78

def validate!
  super

  help! 'A spec-repo name or url is required.' unless @repo
  help! "#{@repo} repo not exist in ~/.cocoapods/repos/." unless File.directory?(@local_repo_path)

  help! 'A podspec file path is required.' unless @podspec_path

  if @upload_to == 'oss'
    help! 'A oss-access-key-id is required.' unless @oss_access_key_id
    help! 'A oss-access-key-secret is required.' unless @oss_access_key_secret
    help! 'A oss-bucket-name is required.' unless @oss_bucket_name
  else
    help! 'upload-to param is required.'
  end

end