22

我正在尝试构建我编写的自定义 gem,client_package但它失败了。

我的目录结构如下所示:

client_package
    Gemfile
    Gemfile.lock
    client_package.gemspec
    Rakefile
    Readme.md
    .gitignore
    .git
        ...git files...
    lib
        client_package.rb
        client_package
            version.rb
            api.rb
            ...more...

我的client_package.gemspec样子是这样的:

# encoding: UTF-8
require File.expand_path('../lib/client_package/version', __FILE__)

Gem::Specification.new do |s|
    s.name = 'client_package'
    s.version = ClientPackage::VERSION
    s.platform = Gem::Platform::RUBY

    s.files = `git ls-files`.split('\n')
    s.executables = `git ls-files -- bin/*`.split('\n').map{ |f| File.basename(f) }
    s.require_paths = ['lib']

    # also have s.authors, s.email, s.homepage, s.summary, s.description

    s.add_dependency 'httparty'
    s.add_dependency 'json'
end

我所有的文件都已提交,git 状态是干净的。

从顶级client_package目录中,我运行gem build client_package.gemspec并收到此错误:

ERROR:  While executing gem ... (Gem::InvalidSpecificationException)
    [".gitignore
Gemfile
Rakefile
Readme.md
client_package.gemspec
lib/client_package.rb
lib/client_package/api.rb
lib/client_package/version.rb
lib/client_package/...more...
"] are not files

这让我感到困惑,因为这些对我来说似乎是文件。无论如何,我认为如果它没有看到这些文件,则存在一些路径问题,并且只是做了一些试验和错误,我发现如果我进入一个目录(顶级目录之上的一个client_package)然后运行gem build client_package/client_package.gemspec似乎首先工作,创建文件client_package-1.0.0.gem。但还是有问题。如果我然后安装那个gem,gem install client_package-1.0.0.gem它似乎也可以工作。但后来这个:

require 'rubygems'
require 'client_package'

退货LoadError: no such file to load -- client_package

我觉得我一定错过了一些小而重要的东西。有任何想法吗?

4

2 回答 2

55

Excuses for resurrecting this old thread, but I found another cause: if you did not check in git, some old (deleted files) might interfere: on disk they do not exist, but git ls-files migh report them as being included in the gem.

Check in the files and this exact error is over.

于 2014-11-15T11:21:05.077 回答
15

这很小但很重要:

应该split('\n')split("\n")

因为看起来像

[".gitignore
Gemfile
Rakefile
Readme.md
client_package.gemspec
lib/client_package.rb
lib/client_package/api.rb
lib/client_package/version.rb
lib/client_package/...more...
"]

可以是包含单个多行字符串的数组,而不是包含多个字符串的数组。

于 2011-10-19T00:23:34.280 回答