Skip to content

Commit

Permalink
Add life cycle messaging methods to the environment interface (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Dec 26, 2023
1 parent a4ceb97 commit f60a90e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 23 deletions.
15 changes: 15 additions & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

***Added:***

- Add the following methods to the `environment` interface for complete control over output during life cycle management:
- app_status_creation
- app_status_pre_installation
- app_status_post_installation
- app_status_project_installation
- app_status_dependency_state_check
- app_status_dependency_installation_check
- app_status_dependency_synchronization

***Fixed:***

- When projects derive dependencies from metadata hooks, there is now by default a status indicator for when the hooks are executed for better responsiveness

## [1.9.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.9.1) - 2023-12-25 ## {: #hatch-v1.9.1 }

***Fixed:***
Expand Down
33 changes: 20 additions & 13 deletions docs/plugins/environment/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ All environment types should [offer support](#hatch.env.plugin.interface.Environ
options:
members:
- PLUGIN_NAME
- find
- create
- remove
- exists
- install_project
- install_project_dev_mode
- dependencies_in_sync
- sync_dependencies
- dependency_hash
- build_environment
- build_environment_exists
- activate
- deactivate
- app_status_creation
- app_status_pre_installation
- app_status_post_installation
- app_status_project_installation
- app_status_dependency_state_check
- app_status_dependency_installation_check
- app_status_dependency_synchronization
- app
- root
- name
Expand All @@ -58,19 +78,6 @@ All environment types should [offer support](#hatch.env.plugin.interface.Environ
- skip_install
- dev_mode
- description
- activate
- deactivate
- find
- create
- remove
- exists
- install_project
- install_project_dev_mode
- dependencies_in_sync
- sync_dependencies
- dependency_hash
- build_environment
- build_environment_exists
- run_builder
- construct_build_command
- command_context
Expand Down
21 changes: 11 additions & 10 deletions src/hatch/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,33 +85,34 @@ def prepare_environment(self, environment: EnvironmentInterface):
if not environment.exists():
self.env_metadata.reset(environment)

with self.status(f'Creating environment: {environment.name}'):
with environment.app_status_creation():
environment.create()

if not environment.skip_install:
if environment.pre_install_commands:
with self.status('Running pre-installation commands'):
with environment.app_status_pre_installation():
self.run_shell_commands(environment, environment.pre_install_commands, source='pre-install')

if environment.dev_mode:
with self.status('Installing project in development mode'):
with environment.app_status_project_installation():
if environment.dev_mode:
environment.install_project_dev_mode()
else:
with self.status('Installing project'):
else:
environment.install_project()

if environment.post_install_commands:
with self.status('Running post-installation commands'):
with environment.app_status_post_installation():
self.run_shell_commands(environment, environment.post_install_commands, source='post-install')

new_dep_hash = environment.dependency_hash()
with environment.app_status_dependency_state_check():
new_dep_hash = environment.dependency_hash()

current_dep_hash = self.env_metadata.dependency_hash(environment)
if new_dep_hash != current_dep_hash:
with self.status('Checking dependencies'):
with environment.app_status_dependency_installation_check():
dependencies_in_sync = environment.dependencies_in_sync()

if not dependencies_in_sync:
with self.status('Syncing dependencies'):
with environment.app_status_dependency_synchronization():
environment.sync_dependencies()
new_dep_hash = environment.dependency_hash()

Expand Down
65 changes: 65 additions & 0 deletions src/hatch/env/plugin/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,71 @@ def dependency_hash(self):

return hash_dependencies(self.dependencies_complex)

@contextmanager
def app_status_creation(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
with self.app.status(f'Creating environment: {self.name}'):
yield

@contextmanager
def app_status_pre_installation(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
with self.app.status('Running pre-installation commands'):
yield

@contextmanager
def app_status_post_installation(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
with self.app.status('Running post-installation commands'):
yield

@contextmanager
def app_status_project_installation(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
if self.dev_mode:
with self.app.status('Installing project in development mode'):
yield
else:
with self.app.status('Installing project'):
yield

@contextmanager
def app_status_dependency_state_check(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
if not self.skip_install and (
'dependencies' in self.metadata.dynamic or 'optional-dependencies' in self.metadata.dynamic
):
with self.app.status('Polling dependency state'):
yield
else:
yield

@contextmanager
def app_status_dependency_installation_check(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
with self.app.status('Checking dependencies'):
yield

@contextmanager
def app_status_dependency_synchronization(self):
"""
See the [life cycle of environments](reference.md#life-cycle).
"""
with self.app.status('Syncing dependencies'):
yield

@contextmanager
def build_environment(
self,
Expand Down
1 change: 1 addition & 0 deletions tests/cli/env/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,7 @@ def update(self, metadata):
Creating environment: test
Installing project in development mode
Running post-installation commands
Polling dependency state
Checking dependencies
Syncing dependencies
"""
Expand Down

0 comments on commit f60a90e

Please sign in to comment.