Building newsfiles for your project.
The build command combines individual news fragments into a comprehensive changelog entry. It supports both command-line and programmatic usage with extensive customization options.
The primary entry point for building changelogs from news fragments.
def __main(
draft: bool,
directory: str | None,
config_file: str | None,
project_name: str | None,
project_version: str | None,
project_date: str | None,
answer_yes: bool,
answer_keep: bool
) -> None:
"""
Build a combined news file from news fragments.
Args:
draft: If True, render to stdout without writing files or checking versions
directory: Directory to build in (None for current directory)
config_file: Path to custom configuration file (None for auto-detection)
project_name: Custom project name (None to use config or auto-detect)
project_version: Custom version string (None to use config or auto-detect)
project_date: Custom date string (None for current date)
answer_yes: If True, automatically confirm fragment removal
answer_keep: If True, keep fragments instead of removing them
"""The Click-decorated command-line interface for the build command.
def _main(
draft: bool,
directory: str | None,
config_file: str | None,
project_name: str | None,
project_version: str | None,
project_date: str | None,
answer_yes: bool,
answer_keep: bool
) -> None:
"""
Build a combined news file from news fragment.
This is the Click command entry point that wraps __main with error handling.
"""Helper functions for date handling and validation.
def _get_date() -> str:
"""Get current date in ISO format (YYYY-MM-DD)."""
def _validate_answer(ctx: Context, param: Option, value: bool) -> bool:
"""
Validate that --yes and --keep options are not used together.
Args:
ctx: Click context
param: Click option parameter
value: Option value
Returns:
bool: The validated value
Raises:
click.Abort: If both --yes and --keep are specified
"""
def should_remove_fragment_files(
fragment_filenames: list[str],
answer_yes: bool,
answer_keep: bool
) -> bool:
"""
Determine if fragment files should be removed based on user options.
Args:
fragment_filenames: List of fragment file paths to potentially remove
answer_yes: Whether user confirmed removal
answer_keep: Whether user wants to keep fragments
Returns:
bool: True if fragments should be removed
"""from towncrier.build import __main as build_main
# Build with defaults - removes fragments after build
build_main(
draft=False,
directory=None,
config_file=None,
project_name=None,
project_version=None,
project_date=None,
answer_yes=True,
answer_keep=False
)# Generate draft output to stdout without modifying files
build_main(
draft=True, # Output to stdout only
directory=None,
config_file=None,
project_name="My Project",
project_version="1.2.0",
project_date="2024-01-15",
answer_yes=False, # Not relevant for draft
answer_keep=False # Not relevant for draft
)# Build using custom config and keeping fragments
build_main(
draft=False,
directory="/path/to/project",
config_file="/path/to/custom-towncrier.toml",
project_name="Custom Project Name",
project_version="2.0.0-beta",
project_date="2024-12-25",
answer_yes=False, # Will prompt user
answer_keep=True # Keep fragments after build
)# Basic build
towncrier build
# Draft build to preview output
towncrier build --draft
# Build with custom version and date
towncrier build --version 1.5.0 --date 2024-01-01
# Build and keep fragments
towncrier build --keep
# Build with auto-confirmation
towncrier build --yes
# Build in specific directory with custom config
towncrier build --dir /path/to/project --config custom.tomlThe build command uses these configuration values:
template: Jinja2 template for rendering the changelogstart_string: Marker in the news file where new content is insertedfilename: Name of the news/changelog filepackage: Package name for version detectionpackage_dir: Directory containing the packageversion: Static version stringname: Project name for the changelogtitle_format: Format string for section titlesissue_format: Format string for issue referencesThe build command handles these error scenarios:
Install with Tessl CLI
npx tessl i tessl/pypi-towncrier