Skip to content

Instantly share code, notes, and snippets.

@ju2wheels
Last active August 29, 2015 14:21
Show Gist options
  • Save ju2wheels/2a96a7b9332037040b8d to your computer and use it in GitHub Desktop.
Save ju2wheels/2a96a7b9332037040b8d to your computer and use it in GitHub Desktop.
Rubygems gemspec template
#!/usr/bin/env gem build
# -*- coding: utf-8 -*-
module Gem::Specification::SpecificationHelpers
def self.find_executables(executable_dir = 'bin', options = {})
raise ArgumentError, "options must be a Hash" unless options.kind_of?(Hash)
exclude_executables = options['exclude_executables'] || []
executable_files = []
if File.directory?(executable_dir)
Dir.new(executable_dir).each do |file_name|
full_file_name = File.join(executable_dir, file_name)
next if [ '.', '..' ].include?(file_name) || ! File.file?(full_file_name) || exclude_executables.include?(file_name)
executable_files.push(file_name)
end if File.readable?(executable_dir)
end
return executable_files
end
# All extensions are expected to follow the standard structure of <ext_dir>/<extension_name>/extconf.rb
def self.find_extensions(extension_dirs = [ 'ext' ], options = {})
raise ArgumentError, "extension_dirs must be an Array" unless extension_dirs.kind_of?(Array)
raise ArgumentError, "options must be a Hash" unless options.kind_of?(Hash)
exclude_extensions = options['exclude_extensions'] || []
extension_files = []
extension_dirs.each do |extension_dir|
next unless File.directory?(extension_dir)
Dir.new(extension_dir).each do |file_name|
full_file_name = File.join(extension_dir, file_name)
next if [ '.', '..' ].include?(file_name) || ! File.directory?(full_file_name) || exclude_extensions.include?(file_name)
if File.readable?(full_file_name) && Dir.entries(full_file_name).include?("extconf.rb")
extension_files.push(File.join(full_file_name, "extconf.rb"))
end
end if File.readable?(extension_dir)
end
return extension_files
end
def self.find_files(search_dirs = [ 'bin', 'doc', 'ext', 'lib' ], options = {})
raise ArgumentError, "search_dirs must be an Array" unless search_dirs.kind_of?(Array)
raise ArgumentError, "options must be a Hash" unless options.kind_of?(Hash)
exclude_dirs = options['exclude_dirs'] || []
exclude_files = options['exclude_files'] || []
file_listing = []
search_subdirs = []
search_dirs.each do |search_dir|
next unless File.directory?(search_dir)
Dir.new(search_dir).each do |file_name|
next if [ '.', '..' ].include?(file_name)
full_file_name = File.join(search_dir, file_name)
if ! File.directory?(full_file_name) && exclude_files.select{ |exclude_file| full_file_name.match(exclude_file) }.empty?
file_listing.push(full_file_name)
end
if File.directory?(full_file_name) && exclude_dirs.select{ |exclude_dir| full_file_name.match(exclude_dir) }.empty?
search_subdirs.push(full_file_name)
end
end if File.readable?(search_dir)
end
unless search_subdirs.empty?
file_listing.concat(find_files(search_subdirs, 'exclude_dirs' => exclude_dirs, 'exclude_files' => exclude_files))
end
return file_listing
end
end
# http://guides.rubygems.org/
Gem::Specification.new do |spec|
# REQUIRED ATTRIBUTES
#======================
# Singular writer for authors
spec.author = 'John Doe'
# Sets the list of authors, ensuring it is an array.
spec.authors = [ 'John Doe', 'Jane Doe' ]
# Files included in this gem. You cannot append to this accessor, you must assign to it.
# Only add files you can require to this list, not directories, etc.
# Directories are automatically stripped from this list when building a gem,
# other non-files cause an error.
spec.files = SpecificationHelpers::find_files(search_dirs = [ 'bin', 'doc', 'ext', 'lib' ], 'exclude_dir' => [], 'exclude_files' => [])
# This gem's name.
spec.name = 'mygem'
# The platform this gem runs on.
#
# This is usually Gem::Platform::RUBY or Gem::Platform::CURRENT.
#
# Most gems contain pure Ruby code; they should simply leave the default value in place.
# Some gems contain C (or other) code to be compiled into a Ruby “extension”. The gem
# should leave the default value in place unless the code will only compile on a certain
# type of system. Some gems consist of pre-compiled code (“binary gems”). It's especially
# important that they set the platform attribute appropriately. A shortcut is to set the
# platform to Gem::Platform::CURRENT, which will cause the gem builder to set the platform
# to the appropriate value for the system on which the build is being performed.
#
# If this attribute is set to a non-default value, it will be included in the filename of the
# gem when it is built
spec.platform = Gem::Platform::RUBY # || RUBY_PLATFORM
# Paths in the gem to add to $LOAD_PATH when this gem is activated.
#
# See also require_paths
#
# If you have an extension you do not need to add "ext" to the require path, the extension
# build process will copy the extension files into “lib” for you.
#
# The default value is "lib"
spec.require_paths = [ 'lib' ]
# The version of RubyGems used to create this gem.
#
# Do not set this, it is set automatically when the gem is packaged.
spec.rubygems_version = '0.0.0'
# A short summary of this gem's description. Displayed in `gem list -d`.
#
# The description should be more detailed than the summary.
spec.summary = 'Summary'
# This gem's version.
#
# The version string can contain numbers and periods, such as 1.0.0. A gem is a 'prerelease'
# gem if the version has a letter in it, such as 1.0.0.pre.
spec.version = '0.0.0'
# RECOMMENDED ATTRIBUTES
#=======================
# The license for this gem.
#
# The license must be no more than 64 characters.
#
# This should just be the name of your license. The full text of the license should be inside
# of the gem (at the top level) when you build it.
#
# The simplest way, is to specify the standard SPDX ID spdx.org/licenses/ for the license.
# Ideally you should pick one that is OSI (Open Source Initiative)
# opensource.org/licenses/alphabetical approved.
#
# The most commonly used OSI approved licenses are BSD-3-Clause and MIT. GitHub also provides
# a license picker at choosealicense.com/.
#
# You should specify a license for your gem so that people know how they are permitted to use it,
# and any restrictions you're placing on it. Not specifying a license means all rights are
# reserved; others have no rights to use the code for any purpose.
#
# You can set multiple licenses with licenses=
spec.license = 'GPL-2'
# The license(s) for the library.
#
# Each license must be a short name, no more than 64 characters.
#
# This should just be the name of your license. The full text of the license should be inside
# of the gem when you build it.
#
# See license= for more discussion
spec.licenses = [ 'Apache', 'GPL-2', 'MIT' ]
# OPTIONAL ATTRIBUTES
#=======================
# Adds a development dependency named gem with requirements to this gem.
#
# Development dependencies aren't installed by default and aren't activated when a gem
# is required.
spec.add_development_dependency('dependent_gem', '~> 1.1', '>= 1.1.4')
# Adds a runtime dependency named gem with requirements to this gem.
spec.add_runtime_dependency('runtime_gem', '~> 1.1', '>= 1.1.4')
# The path in the gem for executable scripts. Usually 'bin'
spec.bindir = 'bin'
# The certificate chain used to sign this gem. See Gem::Security for details.
spec.cert_chain = [ 'gem-public_cert.pem' ]
# A long description of this gem
#
# The description should be more detailed than the summary but not excessively long. A
# few paragraphs is a recommended length with no examples or formatting.
spec.description = <<-EOF
Long
Description
Yo
EOF
# A contact email address (or addresses) for this gem
spec.email = [ '[email protected]', '[email protected]' ]
# Executables included in the gem.
#
# For example, the rake gem has rake as an executable. You don’t specify the full path
# (as in bin/rake); all application-style files are expected to be found in bindir. These
# files must be executable Ruby files. Files that use bash or other interpreters will not work.
#
# Executables included may only be ruby scripts, not scripts for other languages or compiled
# binaries.
spec.executables = SpecificationHelpers::find_executables(executable_dir = spec.bindir, 'exclude_executables' => [ 'executable_name'])
# Extensions to build when installing the gem, specifically the paths to extconf.rb-style files
# used to compile extensions.
#
# These files will be run when the gem is installed, causing the C (or whatever) code to be
# compiled on the user’s machine.
spec.extensions = SpecificationHelpers::find_extensions(extension_dirs = [ 'ext' ], 'exclude_extensions' => [ 'extension_name' ])
# Extra files to add to RDoc such as README or doc/examples.txt
#
# When the user elects to generate the RDoc documentation for a gem (typically at install time),
# all the library files are sent to RDoc for processing. This option allows you to have some
# non-code files included for a more complete set of documentation.
spec.extra_rdoc_files = [ 'README', 'doc/guide.txt' ]
# The URL of this gem's home page
spec.homepage = 'http://johndoe.com'
# The metadata holds extra data for this gem that may be useful to other consumers and is settable
# by gem authors without requiring an update to the rubygems software.
#
# Metadata items have the following restrictions:
#
# - The metadata must be a Hash object
# - All keys and values must be Strings
# - Keys can be a maximum of 128 bytes and values can be a maximum of 1024 bytes
# - All strings must be UTF-8, no binary data is allowed
spec.metadata = { 'issue_tracker' => 'https://www.github.com/johndoe/missing/issues' }
# A message that gets displayed after the gem is installed.
spec.post_install_message = "Gem is now installed"
# Specifies the rdoc options to be used when generating API documentation.
spec.rdoc_options = [ '--opt' , 'opt' ]
# The version of Ruby required by this gem
spec.required_ruby_version = '>= 1.8.7'
# The RubyGems version required by this gem
spec.required_rubygems_version = '>= 0'
# Lists the external (to RubyGems) requirements that must be met for this gem to work. It's simply
# information for the user.
spec.requirements = [ 'cygwin: package_name', 'multicore cpu' ]
# The key used to sign this gem. See Gem::Security for details.
spec.signing_key = File.expand_path('~/.ssh/gem-private_key.pem')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment