【问题标题】:Specific many gems from the same github account来自同一个 github 帐户的特定许多 gem
【发布时间】:2018-05-06 01:32:16
【问题描述】:

喜欢pathsource 我想这样做:

git 'https://github.com/my_company', branch: 'rm_2422-rails5-upgrade' do
  gem 'foo'
  gem 'bar'
  # many more gems...
end

这样做的想法是使用rm_2422-rails5-upgrade 分支获取带有URL 的https://github.com/my_company/foo.gitfoo gem。

我可以从bundler docs 看到它不是这样工作的,我知道我可以做到:

git 'foo', git: 'https://github.com/my_company/foo.git', branch: 'rm_2422-rails5-upgrade'

但我有 很多 需要从所述分支中提取的宝石。

我也查看了git_source,但这似乎也不适用于这种情况。

【问题讨论】:

    标签: ruby github bundler


    【解决方案1】:

    您可以在Gemfile 中定义一个新的git_source

    git_source(:your_source_name) do |repo_name|
      repo_name = "company/#{repo_name}"
      "https://github.com/#{repo_name}.git"
    end
    

    然后使用它:

    gem "gem_name", your_company: "gem_repo_name"
    

    这是一个非常简单的示例,但您可以向块传递更多选项。我们使用这种方法重新定义原始 github 源,以便能够为私有存储库传递身份验证令牌:

    git_source(:github) do |(repo_name, auth_token)|
      repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
      auth_data = !!auth_token ? "#{auth_token}:x-oauth-basic@" : ""
      "https://#{auth_data}github.com/#{repo_name}.git"
    end
    

    更新:我找到了一种方法来满足您的需求,但恕我直言,这是一个相当肮脏的黑客攻击。由于 Gemfile 是一个纯 ruby​​ 文件,您可以在那里定义自己的函数:

    def my_gem(name, *args)
      options = args.last.is_a?(Hash) ? args.pop.dup : {}
      version = args || [">= 0"]
    
      options[:branch] = "develop"
    
      gem(name, version, options)
    end
    

    然后使用它,而不是原来的gem方法:

    my_gem "gem_name"
    

    【讨论】:

    • 关于块的好提示。
    • 感谢更新的答案,但我想避免在实际行中出现除 gem 'foo' 之外的任何内容,否则我可能会写出完整的 gitbranch 键。
    【解决方案2】:

    所以我使用了一个解决方案:

    ['foo', 'bar'].each do |gem_name|
      gem gem_name, git: "https://github.com/my_company/#{gem_name}.git", branch: 'rm_2422-rails5-upgrade'
    end
    

    【讨论】:

      猜你喜欢
      • 2017-06-14
      • 2021-12-20
      • 1970-01-01
      • 2011-04-21
      • 2021-04-29
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多