class Pod::Command::TuyaOssPublish

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.

@todo Create a PR to add your plugin to CocoaPods/cocoapods.org

in the `plugins.json` file, once your plugin is released.

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# 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
options() click to toggle source
# 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

Public Instance Methods

archive() click to toggle source
# 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) click to toggle source
# 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
package() click to toggle source
# 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) click to toggle source
# 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"

  FileUtils.mkdir_p(repo_spec_dir)

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

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

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

end
run() click to toggle source
# 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) click to toggle source
# 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!() click to toggle source
Calls superclass method
# 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