Skip to content

Commit

Permalink
Allow build hooks to access builder configuration (#108)
Browse files Browse the repository at this point in the history
* Allow build hooks to access builder configuration

* Update validate_history.py

* Update history.md
  • Loading branch information
ofek authored Dec 31, 2021
1 parent 60d671d commit 7b4eeab
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
10 changes: 9 additions & 1 deletion backend/hatchling/builders/hooks/plugin/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ def hatch_register_build_hook():
PLUGIN_NAME = ''
"""The name used for selection."""

def __init__(self, root, config, directory, target_name, app=None):
def __init__(self, root, config, build_config, directory, target_name, app=None):
self.__root = root
self.__config = config
self.__build_config = build_config
self.__directory = directory
self.__target_name = target_name
self.__app = app
Expand Down Expand Up @@ -77,6 +78,13 @@ def config(self):
"""
return self.__config

@property
def build_config(self):
"""
An instance of [BuilderConfig](utilities.md#hatchling.builders.config.BuilderConfig).
"""
return self.__build_config

@property
def directory(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion backend/hatchling/builders/plugin/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ def get_build_hooks(self, directory):
if build_hook is None:
raise ValueError('Unknown build hook: {}'.format(hook_name))

configured_build_hooks[hook_name] = build_hook(self.root, config, directory, self.PLUGIN_NAME, self.app)
configured_build_hooks[hook_name] = build_hook(
self.root, config, self.config, directory, self.PLUGIN_NAME, self.app
)

return configured_build_hooks

Expand Down
11 changes: 9 additions & 2 deletions docs/meta/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

-----

## Hatch
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## **Hatch**

### Unreleased

### [1rc2](https://github.com/ofek/hatch/releases/tag/hatch-v1rc2) - 2021-12-29 ### {: #hatch-v1rc2 }

This is the first release candidate for Hatch v1, a complete rewrite.

## Hatchling
-----

## **Hatchling**

### Unreleased

Expand All @@ -21,6 +27,7 @@ This is the first release candidate for Hatch v1, a complete rewrite.
***Added:***

- Refactor builder config handling into its own class
- Allow build hooks to access builder configuration

### [0.3.1](https://github.com/ofek/hatch/releases/tag/hatchling-v0.3.1) - 2021-12-30 ### {: #hatchling-v0.3.1 }

Expand Down
1 change: 1 addition & 0 deletions docs/plugins/build-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ If multiple subclasses are found, you must define a function named `get_build_ho
- app
- root
- config
- build_config
- target_name
- directory
- clean
Expand Down
4 changes: 3 additions & 1 deletion scripts/validate_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ def main():
continue
elif line.startswith('## '):
_, _, package = line.partition(' ')
# Remove any emphasis
package = package.strip('*_')
current_pattern = HEADER_PATTERN.format(package=package.lower())
elif line.startswith('### '):
_, _, header = line.partition(' ')
if header == 'Unreleased':
if header.strip('*_') == 'Unreleased':
continue
elif not re.search(current_pattern, header):
print('Invalid header:')
Expand Down
12 changes: 6 additions & 6 deletions tests/backend/builders/hooks/test_custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ def test_no_path(isolation):
config = {'path': ''}

with pytest.raises(ValueError, match='Option `path` for build hook `custom` must not be empty if defined'):
CustomBuildHook(str(isolation), config, '', '')
CustomBuildHook(str(isolation), config, None, '', '')


def test_path_not_string(isolation):
config = {'path': 3}

with pytest.raises(TypeError, match='Option `path` for build hook `custom` must be a string'):
CustomBuildHook(str(isolation), config, '', '')
CustomBuildHook(str(isolation), config, None, '', '')


def test_nonexistent(isolation):
config = {'path': 'test.py'}

with pytest.raises(OSError, match='Build script does not exist: test.py'):
CustomBuildHook(str(isolation), config, '', '')
CustomBuildHook(str(isolation), config, None, '', '')


def test_default(temp_dir, helpers):
Expand All @@ -43,7 +43,7 @@ def foo(self):
)

with temp_dir.as_cwd():
hook = CustomBuildHook(str(temp_dir), config, '', '')
hook = CustomBuildHook(str(temp_dir), config, None, '', '')

assert hook.foo() == ('custom', str(temp_dir))

Expand All @@ -66,7 +66,7 @@ def foo(self):
)

with temp_dir.as_cwd():
hook = CustomBuildHook(str(temp_dir), config, '', '')
hook = CustomBuildHook(str(temp_dir), config, None, '', '')

assert hook.foo() == ('custom', str(temp_dir))

Expand Down Expand Up @@ -94,4 +94,4 @@ class CustomHook:
ValueError, match=re.escape(f'Unable to find a subclass of `BuildHookInterface` in `foo/build.py`: {temp_dir}')
):
with temp_dir.as_cwd():
CustomBuildHook(str(temp_dir), config, '', '')
CustomBuildHook(str(temp_dir), config, None, '', '')

0 comments on commit 7b4eeab

Please sign in to comment.