Skip to content

Commit

Permalink
use template for output_path
Browse files Browse the repository at this point in the history
  • Loading branch information
tdyas committed Jul 15, 2024
1 parent 7452afe commit 66594db
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/notes/2.23.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ for inspiration. To opt into the feature set the flag

### Goals

#### Package

The `output_path` field present on targets which can be packaged by `pants package` is now based on a template so that you can use parts of `output_path`'s default behavior when overriding it on a target. For example, you can use the template replacement `{{normalized_spec_path}}` to obtain the default output directory for the target (i.e., the directory in which the target lives with slashes replaced by dots).

### Backends

#### BUILD
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class PyOxidizerOutputPathField(OutputPathField):

class PyOxidizerBinaryNameField(OutputPathField):
alias = "binary_name"
default = None
help = help_text(
"""
The name of the binary that will be output by PyOxidizer. If not set, this will default
Expand Down
41 changes: 30 additions & 11 deletions src/python/pants/core/goals/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,37 +66,56 @@ class BuiltPackage:


class OutputPathField(StringField, AsyncFieldMixin):
DEFAULT = "{normalized_spec_path}/{normalized_address}{file_suffix}"

alias = "output_path"
default = DEFAULT

help = help_text(
f"""
Where the built asset should be located.
This field supports the following template replacements:
- `{{normalized_spec_path}}`: The path to the target's directory ("spec path") with forward slashes replaced by dots.
- `{{normalized_address}}`: The target's name with paramaterizations escaped byreplacing dots with underscores.
- `{{file_suffix}}`: For target's which produce single file artifacts, this is the file type suffix to use with a leading dot,
and is empty otherwise when not applicable.
If undefined, this will use the path to the BUILD file, followed by the target name.
For example, `src/python/project:app` would be `src.python.project/app.ext`.
For example, `src/python/project:app` would be `src.python.project/app.ext`. This behavior corresponds to
the default template: `{DEFAULT}`
When running `{bin_name()} package`, this path will be prefixed by `--distdir` (e.g. `dist/`).
Warning: setting this value risks naming collisions with other package targets you may have.
"""
)

def value_or_default(self, *, file_ending: str | None) -> str:
if self.value:
return self.value
def parameters(self, *, file_ending: str | None) -> dict[str, str]:
target_name_part = (
self.address.generated_name.replace(".", "_")
if self.address.generated_name
else self.address.target_name
)
params_sanitized = self.address.parameters_repr.replace(".", "_")
file_prefix = f"{target_name_part}{params_sanitized}"
target_params_sanitized = self.address.parameters_repr.replace(".", "_")
normalized_address = f"{target_name_part}{target_params_sanitized}"

if file_ending is None:
file_name = file_prefix
else:
file_suffix = ""
if file_ending:
assert not file_ending.startswith("."), "`file_ending` should not start with `.`"
file_name = f"{file_prefix}.{file_ending}"
return os.path.join(self.address.spec_path.replace(os.sep, "."), file_name)
file_suffix = f".{file_ending}"

return dict(
normalized_spec_path=self.address.spec_path.replace(os.sep, "."),
normalized_address=normalized_address,
file_suffix=file_suffix,
)

def value_or_default(self, *, file_ending: str | None) -> str:
template = self.value
assert template is not None
params = self.parameters(file_ending=file_ending)
return template.format(**params)


@dataclass(frozen=True)
Expand Down

0 comments on commit 66594db

Please sign in to comment.