Skip to content

Commit

Permalink
add podspec_hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
代岭池 committed Sep 8, 2020
1 parent 06847e1 commit cae8963
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
41 changes: 34 additions & 7 deletions lib/cocoapods-nexus/api/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,49 @@ def search_maven_component(artifact_id:)
end

# 上传组件
def upload_maven_component(artifact_id:, version:, group_id:, podspec:, artifact:, files:)
def upload_maven_component(artifact_id:, version:, group_id:, podspec:, artifact:, podspec_hook:, files:)
parameters = {
'maven2.artifactId' => artifact_id,
'maven2.version' => version,
'maven2.groupId' => group_id,
'maven2.generate-pom' => true,
'maven2.packaging' => artifact.nil? ? 'podspec' : File.extname(artifact).delete(".")
'maven2.packaging' => artifact.nil? ? 'podspec' : File.extname(artifact).delete('.')
}
upload_files = []
upload_files << podspec unless podspec.nil?
upload_files << artifact unless artifact.nil?
upload_files | files unless files.nil?
unless podspec.nil?
upload_files << {
'file' => podspec,
'extension' => 'podspec',
# 'classifier' => 'podspec'
}
end
unless artifact.nil?
upload_files << {
'file' => artifact,
'extension' => File.extname(artifact).delete('.')
}
end
unless podspec_hook.nil?
upload_files << {
'file' => podspec_hook,
'extension' => 'rb',
'classifier' => 'podspec_hook'
}
end

unless files.nil?
upload_files |= files.map do |file|
{
'file' => file,
'extension' => File.extname(file).delete('.'),
}
end
end

upload_files.each_index do |index|
parameters["maven2.asset#{index + 1}"] = File.open(upload_files[index], 'r:utf-8')
parameters["maven2.asset#{index + 1}.extension"] = File.extname(upload_files[index]).delete(".")
parameters["maven2.asset#{index + 1}"] = File.open(upload_files[index]['file'], 'r:utf-8')
parameters["maven2.asset#{index + 1}.extension"] = upload_files[index]['extension'] unless upload_files[index]['extension'].nil?
parameters["maven2.asset#{index + 1}.classifier"] = upload_files[index]['classifier'] unless upload_files[index]['classifier'].nil?
end
@connection.post(endpoint: "components?repository=#{@repo}", parameters: parameters, headers: {})
end
Expand Down
15 changes: 10 additions & 5 deletions lib/cocoapods-nexus/command/nexus/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ class Push < Nexus

def self.options
[
['--url=url', 'a nexus hostname'],
['--repo=repo', 'a nexus repo'],
['--artifact=artifact', 'a nexus artifact']
%w[--url=url nexus服务器地址(http://ip:port)],
%w[--repo=repo nexus的仓库名称],
%w[--artifact=artifact 制品文件],
%w[--podspec_hook=podspec_hook. podspec动态修改脚本文件]
].concat(super)
end

Expand All @@ -27,6 +28,7 @@ def initialize(argv)
@url = argv.option('url')
@repo = argv.option('repo')
@artifact = argv.option('artifact')
@podspec_hook = argv.option('podspec_hook')
super
end

Expand All @@ -40,8 +42,10 @@ def validate!
def run
podspec_path = File.expand_path(@podspec)
artifact_path = File.expand_path(@artifact) unless @artifact.nil?
podspec_hook_path = File.expand_path(@podspec_hook) unless @podspec_hook.nil?

UI.section("开始发布 #{File.basename(@podspec)} -> #{@url}/nexus/#browse/browse:#{@repo}") do

UI.section("开始发布 #{File.basename(@podspec)} -> #{File.join(@url,"/nexus/#browse/browse:",@repo)}") do
spec = Specification.from_file(podspec_path)
artifact_id = spec.attributes_hash['name']
version = spec.attributes_hash['version']
Expand All @@ -52,7 +56,8 @@ def run
group_id: group_id,
podspec: podspec_path,
artifact: artifact_path,
files: [])
podspec_hook: podspec_hook_path,
files: nil)
UI.puts "成功发布 #{artifact_id}(#{version})"
else
raise Informative, "发布失败 #{artifact_id}(#{version}),请检查~/.netrc文件或#{@repo}类型"
Expand Down
2 changes: 1 addition & 1 deletion lib/cocoapods-nexus/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def self.options
executable :curl

def download!
@filename = "#{options[:name]}.#{'podspec'.to_sym}"
@filename = "#{options[:name]}.#{options[:type]}"
@download_path = @target_path + @filename
download_file(@download_path)
end
Expand Down
19 changes: 19 additions & 0 deletions lib/cocoapods-nexus/hook/specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'cocoapods-core'

module Pod
class Specification
class << Specification
def _eval_nexus_podspec(nexus_podspec, parent_podspec)
podspec_string = nexus_podspec.gsub('Pod::Spec.new', 'Pod::Spec.nexus(parent_podspec)')
.gsub('Pod::Specification.new', 'Pod::Spec.nexus(parent_podspec)')
# rubocop:disable Eval
eval(podspec_string, binding)
# rubocop:enable Eval
end

def nexus(parent_podspec)
yield parent_podspec if block_given?
end
end
end
end
24 changes: 21 additions & 3 deletions lib/cocoapods-nexus/nexus_source.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'cocoapods-nexus/api'
require 'cocoapods-nexus/downloader'
require 'versionomy'
require 'cocoapods-nexus/hook/specification'

module Pod
class NexusSource < Source
Expand Down Expand Up @@ -39,7 +40,7 @@ def search(query)
# 暂时这样处理
spec_version = query.requirement.requirements.last.last.to_s
artifacte = nexus_find_artifacte(spec_name: query.root_name, spec_version: spec_version)
unless artifacte.empty?
if artifacte
download_url = parse_artifacte_asset_url(artifacte, 'podspec')
if download_url
target_path = "#{@repo}/#{query.root_name}/#{spec_version}"
Expand All @@ -66,7 +67,24 @@ def specification(name, version)
specification.attributes_hash['source'] = {
'http' => download_url
}
specification.attributes_hash['vendored_frameworks'] = "#{name}.framework"

# 执行自定义脚本
podspec_rb_url = parse_artifacte_asset_url(artifacte, 'podspec_hook.rb')
if podspec_rb_url
tmpdir = Dir.tmpdir
downloader = Pod::Downloader::NexusHttp.new(tmpdir, podspec_rb_url, {:type => 'rb', :name => name})
downloader.download

path = File.join(tmpdir, "#{name}.rb")
if File.exist?(path)
string = File.open(path, 'r:utf-8', &:read)
if string
Pod::Specification._eval_nexus_podspec(string, specification)
end
end
else
specification.attributes_hash['vendored_frameworks'] = "#{name}.framework"
end
end
specification
end
Expand All @@ -88,7 +106,7 @@ def find_local_podspec(query)
def parse_artifacte_asset_url(artifacte, asset_type)
asset = artifacte['assets'].select { |asset| asset['path'].end_with?(asset_type) }.first
asset['downloadUrl'] if asset && asset['downloadUrl']
end
end

def nexus_find_artifacte(spec_name:, spec_version:)
artifactes = nexus_api.search_maven_component(artifact_id: spec_name)
Expand Down

0 comments on commit cae8963

Please sign in to comment.