class Echoe

Echoe includes some optional accessors for more advanced gem configuration.

For example, a simple Rakefile might look like this:

require 'echoe'

Echoe.new("uncapitalizer") do |p|
  p.author = "Evan Weaver"
  p.summary = "A library that uncapitalizes strings."
  p.runtime_dependencies = ["string_tools >=1.4.0"]
end

See below for the full list.

Signing gems

Echoe supports signing gems. First, create yourself a public and private key:

gem cert --build you@yourmail.com

Move them somewhere secret, and add the following environment variables in your .bash_profile or similar:

export GEM_PRIVATE_KEY='/secret/path/to/gem-private_key.pem'
export GEM_CERTIFICATE_CHAIN='/secret/path/to/gem-public_cert.pem'

Make sure your environment is up-to-date:

source ~/.bash_profile

Upload your public_cert.pem file to your website or Rubyforge project, and tell your users to add that certificate to their system via:

gem cert --add /path/to/public_cert.pem

Finally, package and release your project as normal. Now users can install your gem via:

sudo gem install gemname -P HighSecurity

Note that you can also set the key and certificate locations in the Rakefile itself. Finally, you can add p.require_signed = true to your Rakefile so that you don’t accidentally release an unsigned gem if your key is missing.

Metadependencies

Echoe does not force packages to depend on Echoe itself. Instead, it generates a gemspec from your Rakefile and includes that. Downstream repackagers can use the gemspec as-is to build new versions of your gem even without Echoe.

Cross-packaging

Echoe supports platform Rake targets to allow you to cross-package your gems. Just write the spec assuming RUBY_PLATFORM will be what you need it to be for each architecture, and then invoke Rake with the platform name when you’re cross-packaging.

For example, on JRuby, rake package will build a generic -ruby type gem. But if you want to include a Java-specific extension, you can do one of two things. You can package from within JRuby by checking if RUBY_PLATFORM =~ /java/ and setting p.platform = jruby, or you can run rake java package, which will set RUBY_PLATFORM and p.platform for you.

This way you can run rake java package, rake aix install, or whatever task you need and Echoe will behave just like you’re packaging from within the target platform.

Test environment setup and teardown

For some applications, you may need to setup and teardown environment state for the entire test suite. This is especially common for integration tests that may need to spawn an external daemon. To support this, you can add a file tests/setup.rb and it will be silently executed before the entire suite runs. Add a similar file tests/teardown.rb in your app to be executed at the end of the entire run.

Note; these files will only get executed if you run the tests via rake. Also, you can set the environment variable VERBOSE=1 to not hide the setup/teardown output.

Accessor options

Descriptive options:

<tt>author</tt> - Your name.
<tt>email</tt> - Your email address.
<tt>description</tt> - A more detailed description of the library.
<tt>summary</tt> - A shorter description of the library.
<tt>url</tt> - A url for the library. Defaults to generated RDoc on GitHub pages for the project.f
<tt>install_message</tt> - A message to display after the gem is installed.

Versioning options:

<tt>version</tt> - A string for the version number. Parsed from CHANGELOG otherwise.
<tt>changes</tt> - A string describing the most recent changes. Parsed from CHANGELOG otherwise.

Common packaging options:

<tt>runtime_dependencies</tt> - An array of runtime dependencies for this gem. For example, <tt>['mongrel', 'activesupport >= 2.0.2']</tt>.
<tt>development_dependencies</tt> - An array of development dependencies for this gem. For example, <tt>['rake >=0.7.1']</tt>.
<tt>extension_pattern</tt> - A filename array, glob array, or regex for extension files that need to be run at install time. Defaults to <tt>"ext   /extconf.rb"</tt>.

Testing options:

<tt>clean_pattern</tt> - A filename array, glob array, or regex for files that should be removed when <tt>rake clean</tt> is run.
<tt>test_pattern</tt> - A filename array, glob array, or regex for test runners. Overridden by <tt>"test/test_all.rb"</tt>, if it exists.

<tt>spec_pattern</tt> - A filename array, glob array, or regex for test runners.
<tt>rcov_options</tt> - Any extra flags to pass to RCov when coverage reports are run.

Uncommon packaging options:

<tt>platform</tt> - What platform this gem is for.
<tt>manifest_name</tt> - The name of the manifest file. Defaults to <tt>Manifest</tt>.
<tt>need_gem</tt> - Whether to generate a gem package. Defaults to <tt>true</tt>.
<tt>need_tar_gz</tt> - Whether to generate a <tt>.tar.gz</tt> package. Defaults to <tt>true</tt>.
<tt>need_tgz</tt> - Whether to generate a <tt>.tgz</tt> package. Defaults to <tt>false</tt>.
<tt>need_zip</tt> - Whether to generate a <tt>.zip</tt> package. Defaults to <tt>false</tt>.
<tt>include_rakefile</tt> - Include the Rakefile directly within the package. Defaults to <tt>true</tt>.
<tt>include_gemspec</tt> - Include the generated gemspec file within the package. Defaults to <tt>true</tt>.
<tt>ruby_version</tt> - Version string for which Ruby to require (for example, <tt>'>= 1.8.4'</tt>.
<tt>eval</tt> - Accepts a proc to be evaluated in the context of the Gem::Specification object. This allows you to set more unusual gemspec options.
<tt>ignore_pattern</tt> - A filename array, glob array, or regex for pathnames that should be ignored when building the manifest.
<tt>require_paths</tt> - Non-standard requirement paths to add to the gem definition.
<tt>executable_pattern</tt> - A filename array, glob array, or regex for files that should be installed as wrapped executables.

Security options:

<tt>private_key</tt> - The path to your gem private key. Defaults to ENV['GEM_PRIVATE_KEY'], if available. This accessor is not published in the resulting gemspec.
<tt>certificate_chain</tt> - An array representing your certificate authorization chain. If no one else has signed your certificate, just set it to your own cert. Defaults to ENV['GEM_CERTIFICATE_CHAIN'], if available. This accessor is not published in the resulting gemspec.
<tt>require_signed</tt> - Force Echoe to refuse to package your gem if it's not properly signed. Default false.

Publishing options:

<tt>project</tt> - The name of the Rubyforge project. Defaults to the name of the gem.
<tt>docs_host</tt> - A host and filesystem path to publish the documentation to. Defaults to GitHub pages for the project. SSH upload to an accessible static file host also works.

Documentation options:

<tt>rdoc_pattern</tt> - A filename array, glob array, or regex for filenames that should be passed to RDoc.
<tt>rdoc_template</tt> - A path to an RDoc template. Defaults to the generic template.

Attributes

author[RW]

user-configurable

bin_files[RW]

best left alone

certificate_chain[RW]

user-configurable

changelog[RW]

user-configurable

changelog_patterns[RW]

best left alone

changes[RW]

user-configurable

clean_pattern[RW]

user-configurable

dependencies[RW]

legacy

description[RW]

user-configurable

development_dependencies[RW]

user-configurable

docs_host[RW]

user-configurable

email[RW]

user-configurable

eval[RW]

best left alone

executable_pattern[RW]

user-configurable

extension_pattern[RW]

user-configurable

extensions[RW]

legacy

extra_deps[RW]

legacy

files[RW]

best left alone

gem_bin[RW]

best left alone

gemspec_format[RW]

user-configurable

gemspec_name[RW]

best left alone

ignore_pattern[RW]

user-configurable

include_gemspec[RW]

best left alone

include_rakefile[RW]

best left alone

install_message[RW]

user-configurable

lib_files[RW]

best left alone

licenses[RW]

user-configurable

manifest_name[RW]

user-configurable

name[RW]

best left alone

need_gem[RW]

user-configurable

need_tar_gz[RW]

user-configurable

need_tgz[RW]

user-configurable

need_zip[RW]

user-configurable

platform[RW]

user-configurable

private_key[RW]

user-configurable

project[RW]

user-configurable

rakefile_name[RW]

best left alone

rcov_options[RW]

user-configurable

rdoc_files[RW]

legacy

rdoc_options[RW]

best left alone

rdoc_pattern[RW]

user-configurable

rdoc_template[RW]

user-configurable

require_paths[RW]

user-configurable

require_signed[RW]

user-configurable

retain_gemspec[RW]

best left alone

ruby_version[RW]

user-configurable

rubygems_version[RW]

best left alone

runtime_dependencies[RW]

user-configurable

spec[RW]

best left alone

spec_pattern[RW]

user-configurable

summary[RW]

user-configurable

test_files[RW]

best left alone

test_pattern[RW]

user-configurable

url[RW]

user-configurable

use_sudo[RW]

best left alone

version[RW]

user-configurable

Public Class Methods

new(name, _version = nil) { |self| ... } click to toggle source
# File lib/echoe.rb, line 159
def initialize(name, _version = nil)
  # Defaults

  self.name = name
  self.project = name.downcase
  self.changelog = "CHANGELOG"
  self.author = ""
  self.licenses = []
  self.email = ""
  self.clean_pattern = ["pkg", "doc", 'build/*', '**/coverage', '**/*.o', '**/*.so', '**/*.a', '**/*.log', "{ext,lib}/*.{bundle,so,obj,pdb,lib,def,exp}", "ext/Makefile", "{ext,lib}/**/*.{bundle,so,obj,pdb,lib,def,exp}", "ext/**/Makefile", "pkg", "*.gem", ".config"]
  self.test_pattern = File.exist?("test/test_all.rb") ? "test/test_all.rb" : ['test/**/test_*.rb', 'test/**/*_test.rb']
  self.spec_pattern = "spec/**/*_spec.rb"

  self.ignore_pattern = /^(pkg|doc)|(\.svn|CVS|\.bzr|\.DS|\.git)$/

  self.changelog_patterns = {
      :version => [
          /^\s*v([\d\w\.]+)(\.|\s|$)/,
          /\s*\*\s*([\d\w\.]+)\s*\*\s*$/
        ],
      :changes => [
        /^\s*v([\d\.]+\. .*)/,
        /\*\s*[\d\.]+\s*\*\s*(.*)\*\s*[\d\.]+\s*\*$/
      ]
    }

  self.description = ""
  self.summary = ""
  self.install_message = nil
  self.executable_pattern = /^bin\//
  self.require_paths = nil
  self.use_sudo = !(Platform.windows? or ENV['GEM_HOME'].to_s.include?(ENV['USER'].to_s))
  self.gem_bin = "gem#{Platform.suffix}"
  self.rcov_options = []
  self.rdoc_pattern = /^(lib|bin|tasks|ext)|^README|^CHANGELOG|^TODO|^LICENSE|^COPYING$/

  self.gemspec_format = :ruby

  title = (name.downcase == name ? name.capitalize : name)
  self.rdoc_options = ['--line-numbers', '--title', title]

  readme = Dir['*'].detect { |filename| filename =~ /^readme/ }
  self.rdoc_options += ['--main', readme] if readme

  self.runtime_dependencies = []
  self.development_dependencies = [] # These appear to not work at all
  self.manifest_name = "Manifest"
  self.extension_pattern = ["ext/**/extconf.rb", "ext/extconf.rb"]
  self.private_key = ENV['GEM_PRIVATE_KEY']
  self.require_signed = false
  self.certificate_chain = ENV['GEM_CERTIFICATE_CHAIN'].to_s.split(/\,\s*/).compact

  self.need_gem = true
  self.need_tar_gz = true
  self.need_tgz = false
  self.need_zip = false
  self.platform = $platform

  self.include_rakefile = true
  self.include_gemspec = true
  self.gemspec_name = "#{name}.gemspec"
  self.retain_gemspec = false
  self.rakefile_name = "Rakefile"
  self.rubygems_version = ">= 1.2"

  yield self if block_given?

  self.docs_host ||= "git@github.com:#{self.project}/#{self.project}.github.com"
  if self.docs_host =~ /git@github.com/
    self.url ||= "http://#{self.project}.github.com/#{self.project}/#{(self.name + '/') if project != name}"
  end

  # legacy compatibility
  self.runtime_dependencies = dependencies if dependencies and runtime_dependencies.empty?
  self.runtime_dependencies = extra_deps if extra_deps and runtime_dependencies.empty?
  self.rdoc_pattern = rdoc_files if rdoc_files
  self.extension_pattern = extensions if extensions

  # read manifest
  begin
    self.files = File.readlines(manifest_name).map { |x| x.strip } +
      [(gemspec_name if include_gemspec)] +
      [(rakefile_name if include_rakefile)]
    self.files = files.compact.uniq
  rescue Errno::ENOENT
    unless ARGV.include? "manifest"
      puts "Missing manifest. You can build one with 'rake manifest'."
      exit 1
    else
      self.files = []
    end
  end

  # snag version and changeset
  self.version ||= _version
  unless version
    if File.exist? changelog
      parsed = Array(changelog_patterns[:version]).map do |pattern|
        open(changelog) do |log|
          log.read[pattern, 1]
        end
      end.compact.first
      raise "Could not parse version from #{changelog}" unless parsed
      self.version = parsed.chomp(".").strip
    else
      raise "No #{changelog} found, and no version supplied in Rakefile."
    end
  end

  unless self.changes
    self.changes = if File.exist? changelog
      Array(changelog_patterns[:changes]).map do |pattern|
        open(changelog) do |log|
          log.read[pattern, 1]
        end
      end.compact.first or ""
    else
      ""
    end
  end

  # set some post-defaults
  self.certificate_chain = Array(certificate_chain).map {|file| File.expand_path(file)}
  self.private_key = File.expand_path(private_key) if private_key
  self.description = summary if description.empty?
  self.summary = description if summary.empty?
  self.clean_pattern = apply_pattern(clean_pattern)
  self.extension_pattern = apply_pattern(extension_pattern, files)

  self.ignore_pattern = apply_pattern(ignore_pattern)
  honor_gitignore! if File.exist?(".git")

  self.rdoc_pattern = apply_pattern(rdoc_pattern, files) - [manifest_name]
  self.executable_pattern = apply_pattern(executable_pattern, files)
  self.test_pattern = apply_pattern(test_pattern)
  self.spec_pattern = apply_pattern(spec_pattern)

  define_tasks
end
silence() { || ... } click to toggle source
# File lib/echoe/extensions.rb, line 18
def self.silence
  if !ENV['VERBOSE']
    stdout, stderr = $stdout.clone, $stderr.clone
    $stdout.reopen(File.new("#{Dir.tmpdir}/stdout.echoe", 'w'))
    $stderr.reopen(File.new("#{Dir.tmpdir}/stderr.echoe", 'w'))
    begin
      yield
    ensure
      $stdout.reopen(stdout)
      $stderr.reopen(stderr)
    end
  else
    yield
  end
end