While executing gem ... ["extconf.rb", ...] are not files - ruby

I'm trying to build a ruby gem around a C extension. The C extension compiles fine using the ruby extconf.rb; make; sudo make install routine, but when I try to build a gem using rake, the process terminates with the error at the bottom of this trace.
I'm using the post here, with the same directory structure, to create the gem.
What's wrong with my configuration? My gemspec and Rakefile are below the trace (the gem is called netconf).
// Trace
** Execute copy:netconf:i686-linux:1.9.2
install -c tmp/i686-linux/netconf/1.9.2/netconf.so lib/netconf/netconf.so
** Execute compile:netconf:i686-linux
** Execute compile:i686-linux
** Execute compile
** Invoke chmod (first_time)
** Execute chmod
** Execute build
rake aborted!
ERROR: While executing gem ... (Gem::InvalidSpecificationException)
["extconf.rb", "netconf.o", "netconf.so"] are not files
// netconf.gemspec
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "netconf/version"
Gem::Specification.new do |s|
s.name = "netconf"
s.version = Netconf::VERSION
s.authors = ["..."]
s.email = ["..."]
s.homepage = "..."
s.summary = %q{A tool to access and write Ubuntu network configuration}
s.description = %q{Uses ifconfig and other C system calls to access network configurations on a Ubuntu install.}
s.rubyforge_project = "netconf"
s.files = `git ls-files`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.extensions = ["ext/netconf/extconf.rb"]
end
// Rakefile
require 'rake'
require 'rake/extensiontask'
require 'bundler'
Rake::ExtensionTask.new("netconf") do |extension|
extension.lib_dir = "lib/netconf"
end
task :chmod do
File.chmod(0775, 'lib/netconf/netconf.so')
end
task :build => [:clean, :compile, :chmod]
Bundler::GemHelper.install_tasks

I got this error because I hadn't commited my updates with git yet.
s.files = `git ls-files`.split("\n")
That line is directly using git, and probably causing this error. Just do
git add .
git commit -a -m "init"

FWIW, I had a similar issue where
s.files = `git ls-files`.split("\n")
was
s.files = `git ls-files`.split('\n')
, where the single quotes were preventing the file list from splitting properly. Changing to double quotes fixed the issue for me.

Related

Sample ''Hola'" Rubygem push error on windows

I am following the very basic tutorial found here: http://guides.rubygems.org/make-your-own-gem/
hola_username.rb:
class Hola
def self.hi
puts "Hello world!"
end
end
hola_username.gemspec:
Gem::Specification.new do |s|
s.name = 'hola_username'
s.version = '0.0.0'
s.date = '2010-04-28'
s.summary = "Hola!"
s.description = "A simple hello world gem"
s.authors = ["Surname Lastname"]
s.email = 'me.me#gmail.com'
s.files = ["lib/hola_username.rb"]
s.homepage =
'http://rubygems.org/gems/hola_username'
s.license = 'MIT'
end
That really is all there is to the project.
I can build my gem with
gem build .\hola_username.gemspec
I have also tested it by importing and executing the hi function of the Hola class and it works:
PS E:\hola_username> gem install .\hola_username-0.0.0.gem
Successfully installed hola_username-0.0.0
Parsing documentation for hola_username-0.0.0
Done installing documentation for hola_username after 0 seconds
1 gem installed
&
irb(main):001:0> require 'hola_username'
=> true
irb(main):002:0> Hola.hi
Hello world!
=> nil
irb(main):003:0>
But when I try to
gem push .\hola_username-0.0.0.gem
I get:
ERROR: While executing gem ... (Psych::SyntaxError)
(): control characters are not allowed at line 1 column 1
Any ideas?
Edit: I am on a windows 10 machine using ruby 2.0.0p598
Edit v01: Anything I put after gem push will result in the above error, doesn't seem to be a problem with the sample rubygem.
Edit v02: My credentials file that was generated in the .gem folder however stars with hex characters: fffe2d002d00.. Which might be the ones causing trouble?
My credentials file in .gem folder was encoded with UCS2 - Little Endian and converting it to UTF without BOM did the trick.
Although I have absolutey no idea why..

Accessing text file in ruby gem

I am using ruby version 2.0.0 , I made some custom logo in text file named logo.txt like this:
_____
| |
|_____|
|
|
|
Now i made a gem with name of "custom" and placed this file under lib/logo.txt . Now i wants to print this file in my script under ruby gem so i wrote in this way.
file = File.open("lib/logo.txt")
contents = file.read
puts "#{contents}"
But above code produce errors, like:
/usr/local/rvm/rubies/ruby-2.0.0-p451/lib/ruby/gems/2.0.0/gems/custom-0.0.1/lib/custom/custom.rb:1551:in `initialize': No such file or directory - lib/logo.txt (Errno::ENOENT)
I include this logo.txt file in gemspec as per below:
Gem::Specification.new do |s|
s.name = "custom"
s.version = VERSION
s.author = "Custom Wear"
s.email = "custom#custom.com"
s.homepage = "http://custom.com"
s.summary = "custom wera"
s.description = File.read(File.join(File.dirname(__FILE__), 'README'))
s.license = 'ALL RIGHTS RESERVED'
s.files = [""lib/custom.rb", "lib/custom/custom.rb", "lib/custom /version.rb","lib/logo.txt"]
s.test_files = Dir["spec/**/*"]
s.executables = [ 'custom' ]
s.require_paths << 'lib/'
The file is opened relative to the current working directory, unless you specify the full path.
In order to avoid hard-coding full paths, you can get the full path of the current file from Ruby using __FILE__. In fact you can see in the custom.gemspec file something very similar going on:
File.join( File.dirname(__FILE__), 'README')
I think you can get to your logo file like this:
logo_path = File.join( File.dirname(__FILE__), '../logo.txt' )
file = File.open( logo_path )
In Ruby 2.0, you also have __dir__ (which can replace File.dirname(__FILE__)) but that would not be compatible with Ruby 1.9. Generally you are safer using backward-compatible syntax in gems in case you are not sure what someone has when they run your library.

Unable to activate faraday_middleware-0.9.0 // Gem::LoadError // trying to run the pocket-ruby gem

I am trying to run the pocket-ruby gem and after I clone the repo and bundle, it raises this error:
Unable to activate faraday_middleware-0.9.0, because faraday-0.9.0 conflicts with faraday
(< 0.9, >= 0.7.4)
The gemspecfile for the pocket-ruby gem looks like this:
require File.expand_path('../lib/pocket/version', __FILE__)
Gem::Specification.new do |s|
s.add_development_dependency('sinatra', '~> 1.3.3')
s.add_development_dependency('multi_xml')
s.add_runtime_dependency('faraday', ['>= 0.7', '< 0.9'])
s.add_runtime_dependency('faraday_middleware', '~> 0.8')
s.add_runtime_dependency('multi_json', '>= 1.0.3', '~> 1.0')
s.add_runtime_dependency('hashie', '>= 0.4.0')
s.authors = ["Turadg Aleahmad","Jason Ng PT"]
s.description = %q{A Ruby wrapper for the Pocket API v3 (Add, Modify and Retrieve)}
s.email = ['turadg#aleahmad.net',"me#jasonngpt.com"]
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.files = `git ls-files`.split("\n")
s.homepage = 'https://github.com/turadg/pocket-ruby'
s.name = 'pocket-ruby'
s.platform = Gem::Platform::RUBY
s.require_paths = ['lib']
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.6')
if s.respond_to? :required_rubygems_version=
s.rubyforge_project = s.name
s.summary = %q{Ruby wrapper for the Pocket API v3}
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.version = Pocket::VERSION
end
I messed around with changing the versions on the two dependencies in question but didn't have any luck, since I guess I am not entirely sure what the error is saying. Thoughts?
There's a conflict between the two version specifiers - ~> 0.8 means "any version of the form 0.x", but < 0.9 means that 0.9 is not actually allowed.
You'll need to determine which of these need to change - maybe ~> 0.8 should really be ~> 0.8.0, which would specify "any version of the form 0.8.x".
More info on the pessimistic constraint operator (~>) here:
Meaning of tilde-greater-than (~>) in version requirement?

Determining the gem's list of files for the specification

I've always used git to determine which files should go into the gem package:
gem.files = `git ls-files`.split "\n"
Unfortunately, this approach has recently proved to be inappropriate. I need a self-contained, pure-Ruby solution.
My first idea was to simply glob the entire directory, but that alone is likely to include unwanted files. So, after researching the problem, I came up with this:
# example.gemspec
directory = File.dirname File.expand_path __FILE__
dotfiles = %w(.gitignore .rvmrc)
ignore_file = '.gitignore'
file_list = []
Dir.chdir directory do
ignored = File.readlines(ignore_file).map(&:chomp).reject { |glob| glob =~ /\A(#|\s*\z)/ }
file_list.replace Dir['**/**'] + dotfiles
file_list.delete_if do |file|
File.directory?(file) or ignored.any? { |glob| File.fnmatch? glob, file }
end
end
# Later...
gem.files = file_list
That seems a bit complex for a gemspec. It also does not fully support gitignore's pattern format. It currently seems to work but I'd rather not run into problems later.
Is there a simpler but robust way to compute the gem's list of files? Most gems apparently use git ls-files, and the ones that don't either use a solution similar to mine or specify the files manually.
Hi,
You can list all files of your project with pure Ruby:
gem.files = Dir['**/*'].keep_if { |file| File.file?(file) }
Or you can do it manually, this solution is used by Ruby on Rails gems:
gem.files = Dir['lib/**/*'] + %w(.yardopts Gemfile LICENSE README.md Rakefile my_gem.gemspec)
With Rake
The easiest solution depending on rake to list all files from a directory, but exclude everything in the .gitignore file:
require 'rake/file_list'
Rake::FileList['**/*'].exclude(*File.read('.gitignore').split)
RubyGems
Official rubygems solution, list and exclude manually:
require 'rake'
spec.files = FileList['lib/*.rb',
'bin/*',
'[A-Z]*',
'test/*'].to_a
# or without Rake...
spec.files = Dir['lib/*.rb'] + Dir['bin/*']
spec.files += Dir['[A-Z]*'] + Dir['test/**/*']
spec.files.reject! { |fn| fn.include? "CVS" }
Bundler
Bundler solution, list manually:
s.files = Dir.glob("{lib,exe}/**/*", File::FNM_DOTMATCH).reject {|f| File.directory?(f) }
Note: rejecting directories is useless as gem will ignore them by default.
Vagrant
Vagrant solution to mimic git ls-files and taking care of .gitignore in pure ruby:
# The following block of code determines the files that should be included
# in the gem. It does this by reading all the files in the directory where
# this gemspec is, and parsing out the ignored files from the gitignore.
# Note that the entire gitignore(5) syntax is not supported, specifically
# the "!" syntax, but it should mostly work correctly.
root_path = File.dirname(__FILE__)
all_files = Dir.chdir(root_path) { Dir.glob("**/{*,.*}") }
all_files.reject! { |file| [".", ".."].include?(File.basename(file)) }
all_files.reject! { |file| file.start_with?("website/") }
all_files.reject! { |file| file.start_with?("test/") }
gitignore_path = File.join(root_path, ".gitignore")
gitignore = File.readlines(gitignore_path)
gitignore.map! { |line| line.chomp.strip }
gitignore.reject! { |line| line.empty? || line =~ /^(#|!)/ }
unignored_files = all_files.reject do |file|
# Ignore any directories, the gemspec only cares about files
next true if File.directory?(file)
# Ignore any paths that match anything in the gitignore. We do
# two tests here:
#
# - First, test to see if the entire path matches the gitignore.
# - Second, match if the basename does, this makes it so that things
# like '.DS_Store' will match sub-directories too (same behavior
# as git).
#
gitignore.any? do |ignore|
File.fnmatch(ignore, file, File::FNM_PATHNAME) ||
File.fnmatch(ignore, File.basename(file), File::FNM_PATHNAME)
end
end
Pathspec
Using pathspec gem Match Path Specifications, such as .gitignore, in Ruby!
See https://github.com/highb/pathspec-ruby
References
Ref:
Bundler
Vagrant
RubyGems
Rake easy solution

How do you find out what files where changed between commits using Grit

I am having trouble trying to find out what files changed between two different commits. Here is the setup, version of Ruby and the Gem Grit, and what happens when I run the program:
> cd /temp
> mkdir tt
> cd tt
> git init
Initialized empty Git repository in C:/temp/tt/.git/
> ruby --version
ruby 1.8.7 (2010-01-10 patchlevel 249) [i386-mingw32]
> gem list grit
*** LOCAL GEMS ***
grit (2.0.0)
> ruby ..\git_diff_test.rb
Commit #1 succeeded.
Commit #2 succeeded.
c:/apps/ruby/lib/ruby/gems/1.8/gems/grit-2.0.0/lib/grit/repo.rb:290:in `diff': wrong number of arguments (4 for 3) (ArgumentError)
from c:/apps/ruby/lib/ruby/gems/1.8/gems/grit-2.0.0/lib/grit/repo.rb:290:in `diff'
from ../git_diff_test.rb:13
Here is the ruby program:
require 'rubygems'
require 'grit'
include Grit
File.open('t1.txt', 'w') { |f| f.write('hi/bye') }
repo = Repo.new('.')
repo.add('.')
puts "Commit #1 #{repo.commit_all('1') ? 'succeeded' : 'failed'}."
File.open('t1.txt', 'w') { |f| f.write('hi/bye twice') }
File.open('t2.txt', 'w') { |f| f.write('hi/bye 2') }
repo.add('.')
puts "Commit #2 #{repo.commit_all('2') ? 'succeeded' : 'failed'}."
commits = repo.commits
the_diff = repo.diff(commits[0], commits[1]) # line 13 with the error
p the_diff
you can use
grit.commits_between('HASH1', 'HASH2')
to get list of the two commit

Resources