Rake

repository·master·Indexed 25 days ago

https://github.com/ruby/rake

A Make-like build tool implemented in Ruby that allows developers to define tasks and dependencies using standard Ruby syntax. It includes features for parallel task execution via ThreadPool, a lazy-evaluating FileList for file pattern matching, and the Rake::Application class for building custom command-line tools.

Tokens
5.3K
Snippets
14
Records
47
Agent score
81%

What's inside Rake

  1. How Rake handles task execution and threading

    master

    Rake uses a ThreadPool to manage parallel task execution. When running tasks, the top_level method executes the requested tasks within a run_with_threads block.

    If the --job-stats option is used, Rake will display statistics about the thread pool after execution, such as:

    • Maximum active threads
    • Total threads in play

    If --job-stats history is specified, Rake will also display a complete history of the jobs executed using ThreadHistoryDisplay.

  2. Understand the Rake::Task abstraction

    master

    A Rake::Task is the fundamental unit of work in a Rakefile. It consists of:

    • Actions: One or more blocks of code to execute.
    • Prerequisites: A list of tasks that must be completed before this task runs.
    • Order-only Prerequisites: Tasks that must run before this task, but whose completion status (e.g., timestamp) does not affect whether this task is considered "needed".

    When a task is invoked, Rake ensures all prerequisites are executed first, then runs the task's own actions. Tasks are typically created using the task or file convenience methods rather than calling Rake::Task.new directly.

  3. Use Rake::FileList for lazy file pattern matching

    master

    A Rake::FileList is a specialized collection used for manipulating file paths and patterns. It behaves like an Array but is lazy: when you provide glob patterns, it does not search the file system immediately. Instead, it holds the patterns and only resolves them into actual file names the first time an element is requested (e.g., when calling to_a, each, or an array method).

    This allows you to define complex file sets with multiple includes and excludes without the performance penalty of immediate disk scanning.

  4. Run tasks with the rake command

    master

    Once a Rakefile is defined, you can execute tasks from your terminal:

    • Run the default task: Execute rake without any arguments. This will trigger the task named default and any of its dependencies.
    • Run a specific task: Execute rake <task_name> (e.g., rake test).
    • View available options: Run rake --help to see all command-line options.
  5. Use Rake::PackageTask to define packaging tasks

    master

    The Rake::PackageTask class is a task library used to automate the creation of redistributable package files (such as .zip, .tar.gz, or .tgz archives). When initialized, it automatically defines several Rake tasks for your project.

    Generated Tasks

    • :package: Creates all requested package files.
    • :repackage: Rebuilds package files from scratch, even if they are not out of date (runs :clobber_package first).
    • :clobber_package: Deletes all generated package files. This is automatically added to the main :clobber task.

    Configuration Options

    When creating a PackageTask, you can configure the following attributes:

    • name: The name of the package.
    • version: The version string (e.g., '1.2.3'). Use :noversion to build a package without a version in the filename.
    • package_dir: The directory where packages are stored (defaults to 'pkg').
    • package_files: A Rake::FileList of files to include in the archive.
    • need_tar, need_tar_gz, need_tar_bz2, need_tar_xz, need_zip: Boolean flags to enable specific archive formats.
    • tar_command: The command used for tar archives (defaults to 'tar').
    • zip_command: The command used for zip archives (defaults to 'zip').
    • without_parent_dir: If true, the archive will not contain the top-level directory named after the package (the files will be at the root of the archive).
    Rake::PackageTask.new("rake", "1.2.3") do |p|
      p.need_tar = true
      p.package_files.include("lib/**/*.rb")
    end
  6. Run Rake tasks

    master

    Rake is a make-like build utility for Ruby where tasks and dependencies are specified using standard Ruby syntax. You can execute specific tasks by providing the task names as arguments.

    Synopsis:

    rake [options] [rakefile] [targets ...]
  7. Create a simple Rakefile

    master

    To use Rake, you must create a file named Rakefile in your project directory. This file contains build rules defined using standard Ruby syntax.

    In a Rakefile, you can define:

    • Tasks: Named units of work.
    • Prerequisites (Dependencies): Other tasks that must run before the current task.
    • Default Task: A task named default that runs automatically when you execute the rake command without arguments.
    task default: %w[test]
    
    task :test do
        ruby "test/unittest.rb"
      end
  8. Build a custom Rake command with Rake::Application

    master

    If you want to create your own custom command-line tool that behaves like rake, you can use the Rake::Application class. To do this, follow these three steps:

    1. Initialize: Call init(app_name, argv) to parse command-line options and set the application name.
    2. Define Tasks: Load your Rakefile or manually define tasks within the application context.
    3. Run Tasks: Call top_level to execute the tasks specified on the command line.

    Alternatively, you can use the run(argv) method, which performs all three steps (init, load_rakefile, and top_level) in one call.

  9. Configure Rake::PackageTask archive formats

    master

    You can specify which archive formats Rake::PackageTask should generate by setting the corresponding boolean attributes. The resulting filenames follow the pattern <package_dir>/<name>-<version>.<extension>.

    AttributeExtensionDescription
    need_tar.tgzGzipped tar package
    need_tar_gz.tar.gzGzipped tar package
    need_tar_bz2.tar.bz2Bzip2'd tar package
    need_tar_xz.tar.xzXZ'd tar package
    need_zip.zipZip package archive
  10. Use Rake top-level constants FileList and RakeFileUtils

    master

    Rake provides two convenient top-level constants for common tasks: FileList and RakeFileUtils.

    • FileList is a specialized collection for managing sets of files, often used in build scripts to handle patterns and exclusions.
    • RakeFileUtils (aliased as RakeFileUtils) extends standard Ruby FileUtils with additional build-oriented methods like cp_r, mkdir_p, etc., optimized for Rake workflows.
    FileList = Rake::FileList
    RakeFileUtils = Rake::FileUtilsExt