class CocoapodsHd::SpecSourceCreator

Attributes

code_spec[RW]
filepath[RW]
name[RW]
spec[RW]

Public Class Methods

new(code_spec, name, filepath) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 13
def initialize(code_spec, name, filepath)
  @code_spec = code_spec
  @name = name
  @platforms = ['ios']
  @filepath = filepath
  validate!
end

Public Instance Methods

binary_source() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 25
def binary_source
  {
    http: "http://172.16.43.173/binary/#{@name}/#{@code_spec.version}/#{@name}.zip",
    type: "zip"
  }
end
create() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 36
def create
  spec = create_from_code_spec
  spec
end
create_from_code_spec() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 53
def create_from_code_spec
  @spec = code_spec.dup
  # vendored_frameworks | resources | source | source_files | public_header_files
  # license | resource_bundles | vendored_libraries

  # vendored_frameworks
  results = vendored_frameworks(@spec, @name, @filepath)
  @spec.vendored_frameworks = results
  @spec.description = "打包版本的commit:#{`git rev-parse HEAD`.strip}"
  @spec.license = "MIT"

  # source_files && public_header_files
  @spec.source_files = framework_contents(results, '/Headers/*')
  @spec.public_header_files = framework_contents(results, '/Headers/*')

  # Source Location
  @spec.source = binary_source
  # Unused for binary
  spec_hash = @spec.to_hash
  # resources || resource_bundles
  if @spec.to_hash.has_key?('resource') || @spec.to_hash.has_key?('resources')
    spec_hash['resources'] = resources(@name)
  elsif @spec.to_hash.has_key?('resource_bundles')
    spec_hash['resources'] = resource_bundles
  end

  spec_hash.delete('exclude_files')
  spec_hash.delete('preserve_paths')
  # 这里不确定 vendored_libraries 指定的是动态/静态库
  # 如果是静态库的话,需要移除,否则就不移除
  # 最好是静态库都独立成 Pod ,cocoapods-package 打静态库去 collect 目标文件时好做过滤
  # 这里统一只对命名后缀 .a 文件做处理
  # spec_hash.delete('vendored_libraries')
  # libraries 只能假设为动态库不做处理了,如果有例外,需要开发者自行处理
  result_vendored_libraries = vendored_libraries(@spec, @filepath)
  if result_vendored_libraries.empty?
    spec_hash.delete('vendored_libraries')
  else
    spec_hash['vendored_libraries'] = result_vendored_libraries
  end

  # Filter platforms
  platforms = spec_hash['platforms']
  selected_platforms = platforms.select { |k, _v| @platforms.include?(k) }
  spec_hash['platforms'] = selected_platforms.empty? ? platforms : selected_platforms

  # Subspecs
  if spec_hash.has_key?('subspecs')
    subspecs = spec_hash['subspecs']
    subspecs.each do |value|
      # 删除resource_bundles
      value.delete('resource_bundles')

      # 添加public_header_files、vendored_frameworks、source_files
      result_vendored_frameworks = subspec_vendored_frameworks(value, @name, @filepath)
      value["vendored_frameworks"] = result_vendored_frameworks
      value["public_header_files"] = subspec_public_header_files(@name)
      value["source_files"] = subspec_source_files(@name)

      result_vendored_libraries = subspec_vendored_libraries(value, @filepath)
      if !result_vendored_libraries.empty?
        value['vendored_libraries'] = result_vendored_libraries
      end
    end
  end

  @spec = Pod::Specification.from_hash(spec_hash)
  @spec
end
filename() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 32
def filename
  @filename ||= "#{@name}.podspec.json"
end
framework_contents(frameworks, name) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 237
def framework_contents(frameworks, name)
  results = []
  frameworks.each do |value|
    results.push(value + name)
  end
  results
end
getFileNameByFilePath(path) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 232
def getFileNameByFilePath(path)
  result = path.split('/')[-1]
  result
end
resource_bundles() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 257
def resource_bundles
  ["*.bundle"]
end
resources(name) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 253
def resources(name)
  ["#{name}.framework/*.bundle"]
end
subspec_public_header_files(name) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 245
def subspec_public_header_files(name)
  "#{name}.framework/Headers/*.h"
end
subspec_source_files(name) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 249
def subspec_source_files(name)
  ["#{name}.framework/Headers/*"]
end
subspec_vendored_frameworks(value, name, path) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 177
def subspec_vendored_frameworks(value, name, path)
  origin_framework = "#{name}.framework"
  results = [origin_framework]
  if value.has_key?('vendored_frameworks')
    hash_vendored_frameworks = value['vendored_frameworks']
    if hash_vendored_frameworks.class == String
      final_path = path + hash_vendored_frameworks
      all_path = Dir[final_path]
      all_path.each do |value|
        name = getFileNameByFilePath(value)
        results.push(name)
      end
    else
      #数组
      hash_vendored_frameworks = spec.to_hash['vendored_frameworks']
      hash_vendored_frameworks.each do |value|
        final_path = path + value
        all_path = Dir[final_path]
        all_path.each do |value|
          name = getFileNameByFilePath(value)
          results.push(name)
        end
      end
    end
  end
  results
end
subspec_vendored_libraries(value, path) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 205
def subspec_vendored_libraries(value, path)
  results = []
  if value.has_key?('vendored_libraries')
    hash_vendored_libraries = value['vendored_libraries']
    if hash_vendored_libraries.class == String
      final_path = path + hash_vendored_libraries
      all_path = Dir[final_path]
      all_path.each do |value|
        name = getFileNameByFilePath(value)
        results.push(name)
      end
    else
      #数组
      hash_vendored_libraries = spec.to_hash['vendored_libraries']
      hash_vendored_libraries.each do |value|
        final_path = path + value
        all_path = Dir[final_path]
        all_path.each do |value|
          name = getFileNameByFilePath(value)
          results.push(name)
        end
      end
    end
  end
  results
end
validate!() click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 21
def validate!
  raise Pod::Informative, '源码 podspec 不能为空 .' unless code_spec
end
vendored_frameworks(spec, name, path) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 150
def vendored_frameworks(spec, name, path)
  origin_framework = "#{name}.framework"
  results = [origin_framework]
  if spec.to_hash.has_key?('vendored_frameworks')
    hash_vendored_frameworks = spec.to_hash['vendored_frameworks']
    if hash_vendored_frameworks.class == String
      final_path = path + hash_vendored_frameworks
      all_path = Dir[final_path]
      all_path.each do |value|
        name = getFileNameByFilePath(value)
        results.push(name)
      end
    else
      #数组
      hash_vendored_frameworks.each do |value|
        final_path = path + value
        all_path = Dir[final_path]
        all_path.each do |value|
          name = getFileNameByFilePath(value)
          results.push(name)
        end
      end
    end
  end
  results
end
vendored_libraries(spec, path) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 123
def vendored_libraries(spec, path)
  results = []
  if spec.to_hash.has_key?('vendored_libraries')
    hash_vendored_libraries = spec.to_hash['vendored_libraries']
    if hash_vendored_libraries.class == String
      final_path = path + hash_vendored_libraries
      all_path = Dir[final_path]
      all_path.each do |value|
        name = getFileNameByFilePath(value)
        results.push(name)
      end
    else
      #数组
      hash_vendored_libraries = spec.to_hash['vendored_libraries']
      hash_vendored_libraries.each do |value|
        final_path = path + value
        all_path = Dir[final_path]
        all_path.each do |value|
          name = getFileNameByFilePath(value)
          results.push(name)
        end
      end
    end
  end
  results
end
write_spec_file(file = filename) click to toggle source
# File lib/cocoapods-hd/upload/spec_source_creator.rb, line 41
def write_spec_file(file = filename)

  create unless spec

  File.open(file, 'w+') do |f|
    f.write(spec.to_pretty_json)
  end

  @filename = file

end