ios环境第三方库

认识 Ruby 与 Gem

2022-04-22  本文已影响0人  _涼城

什么是 Ruby

Ruby

    Ruby 是一种面向对象、指令式、函数式、动态的通用编程语言。Mac 内部默认是有 Ruby 的,其路径可以通过 which ruby获取,默认为/usr/bin/ruby,可以通过 ruby -v 命令查看当前的版本。

RVM

    有些时候系统默认的 Ruby 版本满足不了我们的需求,就会使用到 RVMRVM,全名 Ruby Version Manager,是一个命令行工具,可安装、管理和使用多个 Ruby 环境。

RVM 安装方式

rvm 有两种安装方式

RVM 的使用

RVM 的移除

rvm implode

Gem

     GemRuby 中的包,其中包含包信息,以及用于安装的文件。Gem 通常是依照 .gemspec 文件构建的,其为 YAML 文件。然而,Ruby 代码也可以直接创建 Gem,这种情况下通常利用Rake 来进行。

.gemspec

    .gemspec 文件其中包含 Gem 有关的信息,例如版本号、作者、联系邮件以及依赖等,例如 cocoapods.gemspec 文件如下:

# encoding: UTF-8
require File.expand_path('../lib/cocoapods/gem_version', __FILE__)
require 'date'

Gem::Specification.new do |s|
  s.name     = "cocoapods"
  s.version  = Pod::VERSION
  s.date     = Date.today
  s.license  = "MIT"
  s.email    = ["eloy.de.enige@gmail.com", "fabiopelosin@gmail.com", "kyle@fuller.li", "segiddins@segiddins.me"]
  s.homepage = "https://github.com/CocoaPods/CocoaPods"
  s.authors  = ["Eloy Duran", "Fabio Pelosin", "Kyle Fuller", "Samuel Giddins"]

  s.summary     = "The Cocoa library package manager."
  s.description = "CocoaPods manages library dependencies for your Xcode project.\n\n"     \
                  "You specify the dependencies for your project in one easy text file. "  \
                  "CocoaPods resolves dependencies between libraries, fetches source "     \
                  "code for the dependencies, and creates and maintains an Xcode "         \
                  "workspace to build your project.\n\n"                                   \
                  "Ultimately, the goal is to improve discoverability of, and engagement " \
                  "in, third party open-source libraries, by creating a more centralized " \
                  "ecosystem."

  s.files = Dir["lib/**/*.rb"] + %w{ bin/pod bin/sandbox-pod README.md LICENSE CHANGELOG.md }

  s.executables   = %w{ pod sandbox-pod }
  s.require_paths = %w{ lib }

  # Link with the version of CocoaPods-Core
  s.add_runtime_dependency 'cocoapods-core',        "= #{Pod::VERSION}"

  s.add_runtime_dependency 'claide',                '>= 1.0.2', '< 2.0'
  s.add_runtime_dependency 'cocoapods-deintegrate', '>= 1.0.3', '< 2.0'
  s.add_runtime_dependency 'cocoapods-downloader',  '>= 1.6.0', '< 2.0'
  s.add_runtime_dependency 'cocoapods-plugins',     '>= 1.0.0', '< 2.0'
  s.add_runtime_dependency 'cocoapods-search',      '>= 1.0.0', '< 2.0'
  s.add_runtime_dependency 'cocoapods-trunk',       '>= 1.6.0', '< 2.0'
  s.add_runtime_dependency 'cocoapods-try',         '>= 1.1.0', '< 2.0'
  s.add_runtime_dependency 'molinillo',             '~> 0.8.0'
  s.add_runtime_dependency 'xcodeproj',             '>= 1.21.0', '< 2.0'

  s.add_runtime_dependency 'colored2',       '~> 3.1'
  s.add_runtime_dependency 'escape',        '~> 0.0.4'
  s.add_runtime_dependency 'fourflusher',   '>= 2.3.0', '< 3.0'
  s.add_runtime_dependency 'gh_inspector',  '~> 1.0'
  s.add_runtime_dependency 'nap',           '~> 1.0'
  s.add_runtime_dependency 'ruby-macho',    '>= 2.3.0', '< 3.0'

  s.add_runtime_dependency 'addressable', '~> 2.8'

  s.add_development_dependency 'bacon', '~> 1.1'
  s.add_development_dependency 'bundler', '~> 2.0'
  s.add_development_dependency 'rake', '~> 12.3'

  s.required_ruby_version = '>= 2.6'

什么是 RubyGems

    RubyGems 是 Ruby 的一个包管理器源,提供了分发 Ruby 程序和库的标准格式 gem,旨在方便地管理 gem 安装的工具,以及用于分发 gem服务器源。这类似于 Pythonpip

Gem 包的安装和卸载

gem 命令文档

GEM_HOME & GEM_PATH

    一般地,可以通过 gem env 查看 gem 的位置 GEM_HOME 和其执行路径 GEM_PATH,除了上述外可以通过覆写 GEM_HOME & GEM_PATH 从而不需要管理员权限(sudo)。

export GEM_HOME=$HOME/.gem
export PATH=$GEM_HOME/bin:$PATH

GEM 源

    gem 命令行的默认源是 RubyGems,也就是 https://rubygems.org/,以下是对源的操作

#查看源
gem source -l 
#镜像源
gem source -a https://gems.ruby-china.com/  
#移除源
gem source --remove https://rubygems.org/

Bundler

Bundler 能够跟踪并安装所需的特定版本的 gem,以此来为 Ruby 项目提供一致的运行环境。定义一个 Gemfile,说明想要包含哪些库,并且可以选择指定版本或范围。 运行bundle install,它会生成一个 Gemfile.lock,说明所有库的确切版本,然后bundle install使用该项目运行的任何其他人都会获得完全相同的版本。

安装

gem install bundler

使用

在项目根目录下新建 Gemfile 文件并指定所需的依赖:

source 'https://gems.ruby-china.com/'
gem 'fastlane'

配置后使用 bundle install 安装

执行

bundle exec pod [command]
上一篇 下一篇

猜你喜欢

热点阅读