Skip to content

Commit

Permalink
Merge pull request #1 from mrdaios/feature-init
Browse files Browse the repository at this point in the history
Feature init
  • Loading branch information
mrdaios authored Sep 4, 2020
2 parents 838c620 + c32226d commit 4243712
Show file tree
Hide file tree
Showing 24 changed files with 819 additions and 18 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: CI
on:
push:
tags:
- v*
jobs:
build:
runs-on: macos-latest
steps:
- name: Publish gem
uses: dawidd6/[email protected]
with:
api_key: ${{secrets.RUBYGEMS_API_KEY}}
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source 'https://rubygems.org'

gemspec

group :development do
gem 'cocoapods'
gem 'cocoapods-core'
gem 'cocoapods-downloader'
gem 'claide'
end
35 changes: 18 additions & 17 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
MIT License
Copyright (c) 2020 mrdaios <[email protected]>

Copyright (c) 2020 Mrdaios
MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# cocoapods-nexus
a plugin for nexus

A description of cocoapods-nexus.

## Installation

$ gem install cocoapods-nexus

## Usage

$ pod spec nexus POD_NAME
13 changes: 13 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'bundler/gem_tasks'

def specs(dir)
FileList["spec/#{dir}/*_spec.rb"].shuffle.join(' ')
end

desc 'Runs all the specs'
task :specs do
sh "bundle exec bacon #{specs('**')}"
end

task :default => :specs

26 changes: 26 additions & 0 deletions cocoapods-nexus.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = 'cocoapods-nexus'
spec.version = "0.0.1"
spec.authors = ['mrdaios']
spec.email = ['[email protected]']
spec.description = 'a cocoapods plugin for nexus.'
spec.summary = '运行时修改spec的source为nexus中的预编译的zip.'
spec.homepage = 'https://github.com/mrdaios/cocoapods-nexus'
spec.license = 'MIT'

spec.files = `git ls-files`.split($/)
# spec.files = Dir['lib/**/*']
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency 'cocoapods', '>= 1.9.3'
spec.add_runtime_dependency 'rest-client', '~> 2.1.0'

spec.add_development_dependency 'bundler', '~> 1.3'
spec.add_development_dependency 'rake', '~> 13.0'
end
3 changes: 3 additions & 0 deletions lib/cocoapods-nexus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require 'cocoapods-nexus/command'
require 'cocoapods-nexus/hook'
require 'cocoapods-nexus/nexus_source'
16 changes: 16 additions & 0 deletions lib/cocoapods-nexus/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'cocoapods-nexus/api/connection'
require 'cocoapods-nexus/api/components'

module CocoapodsNexus
# https://github.com/Cisco-AMP/nexus_api
class API
attr_accessor :connection
attr_accessor :repo

# 用于管理nexus服务器
def initialize(hostname:, repo:)
@connection = CocoapodsNexus::API::NexusConnection.new(hostname: hostname)
@repo = repo
end
end
end
29 changes: 29 additions & 0 deletions lib/cocoapods-nexus/api/components.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module CocoapodsNexus
class API
# 搜索组件
def search_maven_component(artifact_id:)
@connection.get_response(endpoint: "search?repository=#{@repo}&name=#{artifact_id}")
end

# 上传组件
def upload_maven_component(artifact_id:, version:, group_id:, podspec:, artifact:, 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(".")
}
upload_files = []
upload_files << podspec unless podspec.nil?
upload_files << artifact unless artifact.nil?
upload_files | files unless files.nil?

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(".")
end
@connection.post(endpoint: "components?repository=#{@repo}", parameters: parameters, headers: {})
end
end
end
158 changes: 158 additions & 0 deletions lib/cocoapods-nexus/api/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
require 'base64'
require 'json'
require 'rest-client'
require 'uri'

module CocoapodsNexus
class API
class NexusConnection
VALID_RESPONSE_CODES = [200, 201, 204].freeze

attr_accessor :continuation_token

def initialize(hostname:)
@hostname = hostname
end

def get_response(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
response = send_get(endpoint, paginate, headers, api_version)
response.nil? ? {} : jsonize(response)
end

def get(endpoint:, paginate: false, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
valid?(send_get(endpoint, paginate, headers, api_version))
end

def post(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
response = send_request(
:post,
endpoint,
parameters: parameters,
headers: headers,
api_version: api_version
)
valid?(response)
end

def put(endpoint:, parameters: '', headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
response = send_request(
:put,
endpoint,
parameters: parameters,
headers: headers,
api_version: api_version
)
valid?(response)
end

def delete(endpoint:, headers: {'Content-Type' => 'application/json'}, api_version: 'v1')
response = send_request(
:delete,
endpoint,
headers: headers,
api_version: api_version
)
valid?(response)
end

def head(asset_url:)
catch_connection_error do
RestClient.head(URI.escape(asset_url))
end
end

def content_length(asset_url:)
response = head(asset_url: asset_url)
return -1 unless response.respond_to?(:headers)
response.headers[:content_length]
end

def download(url:)
catch_connection_error do
RestClient.get(URI.escape(url), authorization_header)
end
end

def paginate?
!@continuation_token.nil?
end


private

def valid?(response)
return false if response.nil?
VALID_RESPONSE_CODES.include?(response.code) ? true : false
end

def handle(error)
puts 'ERROR: Request failed'
puts error
end

def catch_connection_error
begin
yield
rescue SocketError => error
return handle(error)
rescue RestClient::Unauthorized => error
return handle(error)
rescue RestClient::Exceptions::ReadTimeout => error
return handle(error)
rescue RestClient::ExceptionWithResponse => error
return handle(error.response)
rescue StandardError => error
return handle(error)
end
end

def authorization_header
{}
end

def send_request(connection_method, endpoint, parameters: '', headers: {}, api_version: 'v1')
parameters = parameters.to_json if headers['Content-Type'] == 'application/json'
url = "#{@hostname}/nexus/service/rest/#{api_version}/#{endpoint}"
catch_connection_error do
RestClient::Request.execute(
method: connection_method,
url: URI.escape(url),
payload: parameters,
headers: authorization_header.merge(headers)
)
end
end

def send_get(endpoint, paginate, headers, api_version)
url_marker = endpoint.include?('?') ? '&' : '?'
# paginate answers is the user requesting pagination, paginate? answers does a continuation token exist
# if an empty continuation token is included in the request we'll get an ArrayIndexOutOfBoundsException
endpoint += "#{url_marker}continuationToken=#{@continuation_token}" if paginate && paginate?
send_request(
:get,
endpoint,
headers: headers,
api_version: api_version
)
end

# That's right, nexus has inconsistent null values for its api
def continuation_token_for(json)
return nil if json['continuationToken'].nil?
return nil if json['continuationToken'] == 'nil'
json['continuationToken']
end

def jsonize(response)
json = JSON.parse(response.body)
if json.class == Hash
@continuation_token = continuation_token_for(json)
json = json['items'] if json['items']
end
json
rescue JSON::ParserError
response.body
end
end
end
end
1 change: 1 addition & 0 deletions lib/cocoapods-nexus/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require 'cocoapods-nexus/command/nexus'
24 changes: 24 additions & 0 deletions lib/cocoapods-nexus/command/nexus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'cocoapods-nexus/nexus_source'

module Pod
class Command
class Nexus < Command
require 'cocoapods-nexus/command/nexus/add'
require 'cocoapods-nexus/command/nexus/list'
require 'cocoapods-nexus/command/nexus/push'

self.abstract_command = true

self.summary = 'a cocoapods plugin for nexus'

self.description = <<-DESC
a cocoapods plugin for nexus.
DESC

def initialize(argv)
@repos_nexus_dir = Pod::Config.instance.repos_dir
super
end
end
end
end
Loading

0 comments on commit 4243712

Please sign in to comment.