Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 需要检查的 gem 以及对应的版本
requireGems = {
'psych' => '>= 2.1',
}
# cocoapods & psych
requireGems.each do |gem_name, gem_ver_reqs|
gdep = Gem::Dependency.new(gem_name, *gem_ver_reqs)
found_gspec = gdep.matching_specs.max_by(&:version)
if found_gspec
else
puts "ERROR: Requirement '#{gdep}' not satisfied; use `gem install` to install required version"
exit 1
end
end
# node
node_run_code = system "node -v"
puts node_run_code
if node_run_code != true
puts "ERROR: please install node; use `brew install node` to install"
puts "温馨提示:安装完后建议设置镜像以加速后面的过程"
puts "> npm config set registry https://registry.npm.taobao.org --global"
puts "> npm config set disturl https://npm.taobao.org/dist --global"
exit 1
end
class VersionChecker
# 依赖的名字
def self.tool_name
end
# 希望安装的版本
def self.wanted_version
end
# 是否安装了这个依赖
def self.check_has_install?
end
# 已安装的版本是否满足当前的需求
def self.is_version_satisfied?(version)
end
# 安装指定的版本
def self.install(version)
end
# 更新到指定的版本
def self.update(version)
end
# 子类无需重写
def self.check
if check_has_install?
if is_version_satisfied?(wanted_version)
puts "#{tool_name} 版本检测通过"
else
puts "⌛️ 正在更新 #{tool_name}"
update(wanted_version)
end
else
puts "⌛️ 正在安装 #{tool_name}"
install(wanted_version)
end
end
end
class KSChecker < VersionChecker
def self.tool_name
'ks'
end
def self.wanted_version
'1.3.1'
end
def self.check_has_install?
`which ks`.start_with?("/")
end
def self.is_version_satisfied?(version)
version_prefix = 'ks version'
output = `ks -v`
false unless output.start_with?(version_prefix)
current_version = output[version_prefix.length..-1].strip
Gem::Version.new(current_version) >= Gem::Version.new(version)
end
def self.install(version)
system "brew install xxx"
end
def self.update(version)
system 'ks update'
end
end
class CocoaPodsChecker < VersionChecker
def self.tool_name
'CocoaPods'
end
def self.wanted_version
'1.3.1'
end
def self.check_has_install?
`which pod`.start_with?("/")
end
def self.is_version_satisfied?(version)
output = `pod --version`
current_version = output.strip
Gem::Version.new(current_version) == Gem::Version.new(version)
end
def self.install(version)
system "gem install cocoapods -v #{version}"
end
def self.update(version)
# 卸载之前的所有版本
system "gem uninstall cocoapods --version \'!=#{version}\'"
install(version)
end
end
checkers = [KSChecker, CocoaPodsChecker]
checkers.each{ |c|
c.check
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment