Improve DSL method documentation - close #164

This commit is contained in:
Josh Holtrop 2024-03-17 19:23:20 -04:00
parent c2277bc0a1
commit 150960246b
3 changed files with 108 additions and 9 deletions

View File

@ -1308,10 +1308,10 @@ It iterates through each enabled variant and calls the given block.
In this example, the block would be called twice, once with the "kde" variant In this example, the block would be called twice, once with the "kde" variant
active, and the second time with the "gnome" variant active. active, and the second time with the "gnome" variant active.
Each `env()` call creates an Environment, so two environments are created. Each `env` method call creates an Environment, so two environments are created.
When an Environment is created within a `with_variants` block, the When an Environment is created within a `with_variants` block, the
Environment's name has the active variant(s) appended to the given Environment Environment's build directory name has the active variant(s) keys appended to
name (if any), and separated by a "-". the given Environment name, and separated by a "-".
In this example, a "prog-kde" Environment would be created with build root In this example, a "prog-kde" Environment would be created with build root
build/prog-kde and -DKDE would be passed to the compiler when compiling each build/prog-kde and -DKDE would be passed to the compiler when compiling each
@ -1320,6 +1320,29 @@ Next a "prog-gnome" Environment would be created with build root
build/prog-gnome and -DGNOME would be passed to the compiler when compiling build/prog-gnome and -DGNOME would be passed to the compiler when compiling
the sources. the sources.
The key for a variant is the variant's name by default but can be overridden by
passing a `:key` value to the `variant` method.
A `nil` value for the `:key` parameter will omit that variant from appearing
in the environment's build directory name.
For example:
```ruby
variant "debug"
variant "release", key: nil
with_variants do
env "prog" do |env|
end
end
```
In this example, one "prog" environment will be created for the "debug" variant
with the build directory "prog-debug", and another "prog" environment will be
created for the "release" variant with the build directory "prog".
The build directory for the release variant of the "prog" environment is just
"prog" instead of "prog-release" because the key for the "release" variant is
set to `nil`.
Variants are enabled by default, but can be disabled by passing a `false` value Variants are enabled by default, but can be disabled by passing a `false` value
to the `:default` option of the `variant` method. to the `:default` option of the `variant` method.
For example: For example:

View File

@ -258,6 +258,14 @@ module Rscons
# #
# @param name [String] # @param name [String]
# Variant name. # Variant name.
# @param options [Hash]
# Optional parameters.
# @option options [String] :default
# Whether the variant is enabled by default (default: true).
# @option options [String] :key
# Variant key, used to name an Environment's build directory. If nil,
# this variant will not contribute to the Environment's build directory
# name.
def variant(name, options = {}) def variant(name, options = {})
if @active_variants if @active_variants
!!@active_variants.find {|variant| variant[:name] == name} !!@active_variants.find {|variant| variant[:name] == name}
@ -294,6 +302,15 @@ module Rscons
end end
# Create a variant group. # Create a variant group.
#
# @overload variant_group(name, options = {})
# @param name [String]
# Variant group name (optional).
# @param options [Hash]
# Optional variant group parameters.
# @overload variant_group(options = {})
# @param options [Hash]
# Optional variant group parameters.
def variant_group(*args, &block) def variant_group(*args, &block)
if args.first.is_a?(String) if args.first.is_a?(String)
name = args.slice!(0) name = args.slice!(0)

View File

@ -289,27 +289,80 @@ module Rscons
end end
# Create or modify a task. # Create or modify a task.
#
# @overload task(name, options = {}, &block)
# @param name [String]
# Task name.
# @param options [Hash]
# Optional task attributes.
# @option options [Boolean] :autoconf
# Whether to automatically configure before running this task
# (default: true).
# @option options [String] :desc
# Description for the task.
# @option options [Array<String>] :depends
# Dependencies of the task.
# @option options [Array<Task::Param>] :params
# Task parameter definitions.
#
# The action block given will be invoked by Rscons when the task
# executes. It will be passed two arguments:
# 1) The Task object.
# 2) A Hash of task parameter names and values.
def task(*args, &block) def task(*args, &block)
Util.task(*args, &block) Util.task(*args, &block)
end end
# Define a variant, or within a with_variants block, query if it is # Define a variant, or within a with_variants block, query if it is
# active. # active.
def variant(*args) #
Rscons.application.variant(*args) # @param name [String]
# Variant name.
# @param options [Hash]
# Optional parameters.
# @option options [String] :default
# Whether the variant is enabled by default (default: true).
# @option options [String] :key
# Variant key, used to name an Environment's build directory. If nil,
# this variant will not contribute to the Environment's build directory
# name.
def variant(name, options = {})
Rscons.application.variant(name, options)
end end
# Check if a variant is enabled. # Check if a variant is enabled.
def variant_enabled?(*args) #
Rscons.application.variant_enabled?(*args) # This can be used, for example, in a configuration block to omit or
# include configuration checks based on which variants have been
# configured.
#
# @param variant_name [String]
# Variant name.
#
# @return [Boolean]
# Whether the requested variant is enabled.
def variant_enabled?(variant_name)
Rscons.application.variant_enabled?(variant_name)
end end
# Create a variant group. # Create a variant group.
#
# @overload variant_group(name, options = {})
# @param name [String]
# Variant group name (optional).
# @param options [Hash]
# Optional variant group parameters.
# @overload variant_group(options = {})
# @param options [Hash]
# Optional variant group parameters.
def variant_group(*args, &block) def variant_group(*args, &block)
Rscons.application.variant_group(*args, &block) Rscons.application.variant_group(*args, &block)
end end
# Iterate through variants. # Iterate through enabled variants.
#
# The given block is called for each combination of enabled variants
# across the defined variant groups.
def with_variants(&block) def with_variants(&block)
Rscons.application.enable_variants Rscons.application.enable_variants
Rscons.application.with_variants(&block) Rscons.application.with_variants(&block)
@ -349,11 +402,17 @@ module Rscons
# Top-level DSL available to the Rsconscript. # Top-level DSL available to the Rsconscript.
class TopLevelDsl < GlobalDsl class TopLevelDsl < GlobalDsl
# Set the project name. # Set the project name.
#
# @param project_name [String]
# Project name.
def project_name(project_name) def project_name(project_name)
@script.project_name = project_name @script.project_name = project_name
end end
# Whether to automatically configure (default true). # Set whether to automatically configure (default true).
#
# @param autoconf [Boolean]
# Whether to automatically configure.
def autoconf(autoconf) def autoconf(autoconf)
@script.autoconf = autoconf @script.autoconf = autoconf
end end