Skip to content

Commit

Permalink
create default pdf target for standalone builds
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin committed Nov 11, 2024
1 parent 0151fa0 commit 2a4fc48
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
32 changes: 27 additions & 5 deletions pretext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,11 @@ def init(refresh: bool, files: List[str]) -> None:
help="Build all targets configured to be deployed.",
)
@click.option(
"-i", "--input", "source_file", type=click.Path(), help="Override the source file from the manifest by providing a path to the input."
"-i",
"--input",
"source_file",
type=click.Path(),
help="Override the source file from the manifest by providing a path to the input.",
)
@click.pass_context
@nice_errors
Expand Down Expand Up @@ -453,26 +457,44 @@ def build(

# Supply help if not in project subfolder
# NOTE: we no longer need the following since we have added support for building without a manifest.
#if utils.cannot_find_project(task="build"):
# if utils.cannot_find_project(task="build"):
# return
# Create a new project, apply overlay, and get target. Note, the CLI always finds changes to the root folder of the project, so we don't need to specify a path to the project.ptx file.
# Use the project discovered in the main command.
project = ctx.obj["project"]

# Check to see whether target_name is a path to a file:
if (target_name and Path(target_name).is_file()):
log.debug(f"target is a source file {Path(target_name).resolve()}. Using this to override input.")
if target_name and Path(target_name).is_file():
log.debug(
f"target is a source file {Path(target_name).resolve()}. Using this to override input."
)
# set the source_file to that target_name and reset target_name to None
source_file = target_name
target_name = None


# Now create the target if the target_name is not missing.
try:
# deploys flag asks to build multiple targets: all that have deploy set.
if deploys and len(project.deploy_targets()) > 0:
targets = project.deploy_targets()
elif target_name is None and source_file is not None:
# We are in the case where we are building a standalone document, so we build a default target if there are no standalone targets or find the first target with standalone="yes".
if len(project.standalone_targets()) > 0:
targets = [project.standalone_targets()[0]]
else:
target = project.new_target(
name="standalone",
format="pdf",
standalone="yes",
output_dir=Path(source_file).resolve().parent,
)
targets = [target]
log.debug(f"Building standalone document with target {target.name}")
log.debug(target)
else:
targets = [project.get_target(name=target_name)]
except AssertionError as e:
log.warning("Assertion error in getting target.")
utils.show_target_hints(target_name, project, task="build")
log.critical("Exiting without completing build.")
log.debug(e, exc_info=True)
Expand Down
13 changes: 13 additions & 0 deletions pretext/project/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ def to_deploy(self) -> bool:
# specified `deploy` attr, so deploy iff choice isn't "no"
return deploy.lower() != "no"

# Specify whether this target is intended for standalone building
standalone: t.Optional[str] = pxml.attr(name="standalone", default=None)

# Check whether a target is standalone
def is_standalone(self) -> bool:
standalone = self.standalone
if standalone is None:
return False
return standalone.lower() != "no"

# These attributes have complex validators.
# Note that in each case, since we may not have validated the properties we refer to in values, we should use `values.get` instead of `values[]`.
#
Expand Down Expand Up @@ -1490,6 +1500,9 @@ def init_core(self) -> None:
def deploy_targets(self) -> t.List[Target]:
return [tgt for tgt in self.targets if tgt.to_deploy()]

def standalone_targets(self) -> t.List[Target]:
return [tgt for tgt in self.targets if tgt.is_standalone()]

def stage_deployment(self) -> None:
# First empty the stage directory (as long as it is safely in the project directory).
if (
Expand Down
5 changes: 5 additions & 0 deletions pretext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ def show_target_hints(
if project.has_target(name=target_format):
return
# Otherwise continue with hints:
if target_format is None:
log.critical(
f"No viable targets found in project.ptx. The available targets are named: {project.target_names()}."
)
return
log.critical(
f'There is not a target named "{target_format}" for this project.ptx manifest.'
)
Expand Down
22 changes: 15 additions & 7 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ def test_build(tmp_path: Path, script_runner: ScriptRunner) -> None:


def test_build_no_manifest(tmp_path: Path, script_runner: ScriptRunner) -> None:
assert script_runner.run([PTX_CMD, "-v", "debug", "new", "-d", "."], cwd=tmp_path).success
assert script_runner.run(
[PTX_CMD, "-v", "debug", "new", "-d", "."], cwd=tmp_path
).success
os.remove(tmp_path / "project.ptx")
assert (tmp_path / "project.ptx").exists() is False
assert script_runner.run([PTX_CMD, "-v", "debug", "build"], cwd=tmp_path).success
Expand All @@ -131,13 +133,19 @@ def test_override_source(tmp_path: Path, script_runner: ScriptRunner) -> None:
assert script_runner.run(
[PTX_CMD, "-v", "debug", "build", "-i", "source/main.ptx"], cwd=tmp_path
).success
assert (
script_runner.run([PTX_CMD, "build", "-i", "main.ptx"], cwd=tmp_path).success
is False
)
assert script_runner.run(
[PTX_CMD, "build", "-i", "main.ptx"], cwd=tmp_path
).success is False
assert script_runner.run(
[PTX_CMD, "-v", "debug", "build", "source/main.ptx"], cwd=tmp_path).success
assert script_runner.run(
[PTX_CMD, "-v", "debug", "build", "main.ptx"], cwd=tmp_path).success is False
[PTX_CMD, "-v", "debug", "build", "source/main.ptx"], cwd=tmp_path
).success
assert (
script_runner.run(
[PTX_CMD, "-v", "debug", "build", "main.ptx"], cwd=tmp_path
).success
is False
)


def test_init(tmp_path: Path, script_runner: ScriptRunner) -> None:
Expand Down

0 comments on commit 2a4fc48

Please sign in to comment.