New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rake_dependencies

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rake_dependencies

3.10.0.pre.2
Rubygems
Version published
Maintainers
1
Created
Source

RakeDependencies

Rake tasks for managing binary dependencies used within a build.

Installation

Add this line to your application's Gemfile:

gem 'rake_dependencies'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rake_dependencies

Usage

RakeDependencies provides a suite of tasklibs for downloading and extracting a distribution of some dependency. The simplest way to configure all of these tasks is via the RakeDependencies::Tasks::All tasklib. The following provides an example usage with terraform as the target dependency:

RakeDependencies::Tasks::All.new do |t|
  t.namespace = :terraform
  t.dependency = 'terraform'
  t.version = '0.9.0'
  t.path = File.join('vendor', 'terraform')
  t.type = :zip

  t.platform_os_names = { darwin: 'darwin', linux: 'linux' }
  t.platform_cpu_names = { x86_64: 'amd64', arm64: 'arm64' }

  t.uri_template =
    'https://releases.hashicorp.com/terraform/<%= @version %>/' +
      'terraform_<%= @version %>_' +
      '<%= @platform_os_name %>_<%= @platform_cpu_name %><%= @ext %>'
  t.file_name_template =
    'terraform_<%= @version %>_' +
      '<%= @platform_os_name %>_<%= @platform_cpu_name %><%= @ext %>'

  t.needs_fetch = lambda do |parameters|
    terraform_binary = File.join(
      parameters[:path], parameters[:binary_directory], 'terraform')

    !(File.exist?(terraform_binary) &&
      `#{terraform_binary} -version`.lines.first =~ /#{parameters[:version]}/)
  end
end

With this in place, a number of tasks will be defined:

> rake -T
rake terraform:clean     # Clean vendored terraform
rake terraform:download  # Download terraform distribution
rake terraform:ensure    # Ensure terraform present
rake terraform:extract   # Extract terraform archive
rake terraform:fetch     # Fetch terraform

The tasks perform the following:

  • <ns>:clean - recursively deletes the directory containing the dependency
  • <ns>:download - downloads the distribution from the provided path into the dependency directory
  • <ns>:extract - extracts, in the case of a compressed archive, or copies, in the case of an uncompressed distribution, the binaries into the binary directory under the dependency directory
  • <ns>:fetch - downloads then extracts
  • <ns>:ensure - checks whether the dependency needs to be fetched and cleans and fetches if necessary

With these tasks defined, any task that requires the dependency to be present should depend on <ns>:ensure. Continuing the terraform example:

task :provision_database => ['terraform:ensure'] do
  sh('vendor/terraform/bin/terraform apply infra/database')
end

If the installation_directory attribute is supplied, an additional install task will be defined:

 RakeDependencies::Tasks::All.new do |t|
  t.namespace = :terraform
  t.dependency = 'terraform'

  # ...
  t.installation_directory = "#{ENV['HOME']}/bin"
  # ...
end

Then:

> rake -T
rake terraform:clean     # Clean vendored terraform
rake terraform:download  # Download terraform distribution
rake terraform:ensure    # Ensure terraform present
rake terraform:extract   # Extract terraform archive
rake terraform:fetch     # Fetch terraform
rake terraform:install   # Install terraform

The <ns>:install task copies the binary into the defined installation directory which can be anywhere on the filesystem.

The RakeDependencies::Tasks::All tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
namespaceThe namespace in which to define the tasks-no
dependencyThe name of the dependency, used in status reporting and as the default binary name-yes
versionThe version of the dependency to manage, only required if used in templates or needs_fetch-no
pathThe path in which to install the dependency-yes
typeThe archive type of the distribution, one of :zip, :tar_gz, :tgz or :uncompressed:zipyes
platform_os_namesA map of platform OS identifiers to OS names to use in templatesRakeDependencies::PlatformNames::OSyes
platform_cpu_namesA map of platform CPU identifiers to CPU names to use in templatesRakeDependencies::PlatformNames::CPUyes
distribution_directoryThe name of the directory under the supplied path into which to download the distribution'dist'yes
binary_directoryThe name of the directory under the supplied path into which to extract/copy the binaries'bin'yes
installation_directoryThe name of the directory into which to install the binaries, anywhere on the file system-no
uri_templateA template for the URI of the distribution-yes
file_name_templateA template for the name of the downloaded file-yes
source_binary_name_templateA template for the name of the binary before rename after extraction/copying-no
target_binary_name_templateA template for the name to rename the binary to after extraction/copying-no
strip_path_templateA template for the path to strip within an archive before extracting-no
needs_fetchA lambda taking a parameter map that should return true if the dependency needs to be fetched, false otherwiseWill always return trueno
clean_task_nameThe name of the clean task, required if it should be different from the default:cleanyes
download_task_nameThe name of the download task, required if it should be different from the default:downloadyes
extract_task_nameThe name of the extract task, required if it should be different from the default:extractyes
install_task_nameThe name of the install task, required if it should be different from the default:installno
fetch_task_nameThe name of the fetch task, required if it should be different from the default:fetchyes
ensure_task_nameThe name of the ensure task, required if it should be different from the default:ensureyes

Notes:

  • Each of the templates will have the following instance variables in scope when rendered:
    • @version: the supplied version string
    • @platform: the Gem::Platform on which the task is executing
    • @platform_os_name: the OS name derived from the platform on which the task is executing and the provided platform_os_names map
    • @platform_cpu_name: the CPU name derived from the platform on which the task is executing and the provided platform_cpu_names map
    • @ext: the file extension corresponding to the provided type, one of .zip, .tar.gz, .tgz or empty string for uncompressed files
  • The needs_fetch lambda will receive a map with the following entries:
    • path: the supplied path
    • version: the supplied version string
    • binary_directory: the supplied or default binary directory

The RakeDependencies::Tasks::All tasklib uses each of the following tasklibs in its definition:

  • RakeDependencies::Tasks::Clean
  • RakeDependencies::Tasks::Download
  • RakeDependencies::Tasks::Extract
  • RakeDependencies::Tasks::Install
  • RakeDependencies::Tasks::Fetch
  • RakeDependencies::Tasks::Ensure

RakeDependencies::Tasks::Clean

The RakeDependencies::Tasks::Clean tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:cleanyes
pathThe path in which the dependency is installed-yes
dependencyThe name of the dependency, used in status reporting-yes

RakeDependencies::Tasks::Download

The RakeDependencies::Tasks::Download tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:downloadyes
dependencyThe name of the dependency, used in status reporting-yes
versionThe version of the dependency to manage, only required if used in templates-no
pathThe path in which to install the dependency-yes
typeThe archive type of the distribution, one of :zip, :tar_gz, :tgz or :uncompressed:zipyes
platform_os_namesA map of platform OS identifiers to OS names to use in templatesRakeDependencies::PlatformNames::OSyes
platform_cpu_namesA map of platform CPU identifiers to CPU names to use in templatesRakeDependencies::PlatformNames::CPUyes
distribution_directoryThe name of the directory under the supplied path into which to download the distribution'dist'yes
uri_templateA template for the URI of the distribution-yes
file_name_templateA template for the name of the downloaded file-yes

Notes:

  • The templates have the same instance variables in scope when rendered as mentioned above.

RakeDependencies::Tasks::Extract

The RakeDependencies::Tasks::Extract tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:extractyes
dependencyThe name of the dependency, used in status reporting-yes
versionThe version of the dependency to manage, only required if used in templates-no
pathThe path in which to install the dependency-yes
typeThe archive type of the distribution, one of :zip, :tar_gz, :tgz or :uncompressed:zipyes
platform_os_namesA map of platform OS identifiers to OS names to use in templatesRakeDependencies::PlatformNames::OSyes
platform_cpu_namesA map of platform CPU identifiers to CPU names to use in templatesRakeDependencies::PlatformNames::CPUyes
extractorsA map of archive types to extractor classes, see notes for further detailsExtractors for all supported typesyes
distribution_directoryThe name of the directory under the supplied path into which the distribution was downloaded'dist'yes
binary_directoryThe name of the directory under the supplied path into which to extract/copy the binaries'bin'yes
file_name_templateA template for the name of the downloaded file-yes
source_binary_name_templateA template for the name of the binary before rename after extraction/copying-no
target_binary_name_templateA template for the name to rename the binary to after extraction/copying-no
strip_path_templateA template for the path to strip within an archive before extracting-no

Notes:

  • The templates have the same instance variables in scope when rendered as mentioned above.
  • The extractors map has entries for the following keys:
    • :zip: An extractor class for zip files
    • :tar_gz: An extractor class for tar.gz files
    • :tgz: An alias for :tar_gz using the same extractor class
    • :uncompressed: An extractor class that copies the source to the destination
  • The extractor map can be overridden but should include entries for all of the above.

RakeDependencies::Tasks::Install

The RakeDependencies::Tasks::Install tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:installyes
dependencyThe name of the dependency, used in status reporting-yes
versionThe version of the dependency to manage, only required if used in templates-no
pathThe path in which to install the dependency-yes
typeThe archive type of the original distribution, one of :zip, :tar_gz, :tgz or :uncompressed:zipyes
platform_os_namesA map of platform OS identifiers to OS names to use in templatesRakeDependencies::PlatformNames::OSyes
platform_cpu_namesA map of platform CPU identifiers to CPU names to use in templatesRakeDependencies::PlatformNames::CPUyes
binary_directoryThe name of the directory under the supplied path into which to extract/copy the binaries'bin'yes
installation_directoryThe name of the directory into which the binary should be installed-yes
binary_name_templateA template for the name of the binary-yes

Notes:

  • The templates have the same instance variables in scope when rendered as mentioned above.

RakeDependencies::Tasks::Fetch

The RakeDependencies::Tasks::Fetch tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:fetchyes
dependencyThe name of the dependency, used in status reporting-yes
download_taskThe full name including namespaces of the download task<current-namespace>:downloadyes
extract_taskThe full name including namespaces of the extract task<current-namespace>:extractyes

RakeDependencies::Tasks::Ensure

The RakeDependencies::Tasks::Fetch tasklib supports the following configuration parameters:

NameDescriptionDefaultRequired
nameThe name of the task, required if it should be different from the default:fetchyes
dependencyThe name of the dependency, used in status reporting-yes
versionThe version of the dependency to manage, only required if used in templates-no
pathThe path in which to install the dependency-yes
binary_directoryThe name of the directory under the supplied path into which to extract/copy the binaries'bin'yes
needs_fetchA lambda taking a parameter map that should return true if the dependency needs to be fetched, false otherwiseWill always return trueno
clean_taskThe full name including namespaces of the clean task<current-namespace>:cleanyes
download_taskThe full name including namespaces of the download task<current-namespace>:downloadyes
extract_taskThe full name including namespaces of the extract task<current-namespace>:extractyes
install_taskThe full name including namespaces of the install task<current-namespace>:installyes

Notes:

  • The templates have the same instance variables in scope when rendered as mentioned above.
  • The needs_fetch method receives the same parameter map as mentioned above.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Managing CircleCI keys

To encrypt a GPG key for use by CircleCI:

openssl aes-256-cbc \
  -e \
  -md sha1 \
  -in ./config/secrets/ci/gpg.private \
  -out ./.circleci/gpg.private.enc \
  -k "<passphrase>"

To check decryption is working correctly:

openssl aes-256-cbc \
  -d \
  -md sha1 \
  -in ./.circleci/gpg.private.enc \
  -k "<passphrase>"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/infrablocks/rake_dependencies. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

FAQs

Package last updated on 26 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts