Skip to content

Commit

Permalink
Refactor (#1377) a27473d
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Apr 9, 2024
1 parent c9138ed commit aa63be3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 36 deletions.
84 changes: 49 additions & 35 deletions dev/plugins/environment/reference/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,56 @@
<span class=n>requires</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=p>[</span>
<span class=w> </span><span class=s2>&quot;...&quot;</span><span class=p>,</span>
<span class=p>]</span>
</code></pre></div> </div> </div> </div> <h2 id=life-cycle>Life cycle<a class=headerlink href=#life-cycle title="Permanent link">&para;</a></h2> <p>Whenever an environment is used, the following logic is performed:</p> <div class="doc doc-object doc-function"> <div class="doc doc-contents first"> <details class=quote> <summary>Source code in <code>src/hatch/cli/application.py</code></summary> <div class=highlight><pre><span></span><code><span class=linenos data-linenos=" 89 "></span><span class=k>def</span> <span class=nf>prepare_environment</span><span class=p>(</span><span class=bp>self</span><span class=p>,</span> <span class=n>environment</span><span class=p>:</span> <span class=n>EnvironmentInterface</span><span class=p>):</span>
<span class=linenos data-linenos=" 90 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>environment</span><span class=o>.</span><span class=n>exists</span><span class=p>():</span>
<span class=linenos data-linenos=" 91 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>reset</span><span class=p>(</span><span class=n>environment</span><span class=p>)</span>
<span class=linenos data-linenos=" 92 "></span>
<span class=linenos data-linenos=" 93 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_creation</span><span class=p>():</span>
<span class=linenos data-linenos=" 94 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>create</span><span class=p>()</span>
<span class=linenos data-linenos=" 95 "></span>
<span class=linenos data-linenos=" 96 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>environment</span><span class=o>.</span><span class=n>skip_install</span><span class=p>:</span>
<span class=linenos data-linenos=" 97 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>pre_install_commands</span><span class=p>:</span>
<span class=linenos data-linenos=" 98 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_pre_installation</span><span class=p>():</span>
<span class=linenos data-linenos=" 99 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>run_shell_commands</span><span class=p>(</span><span class=n>environment</span><span class=p>,</span> <span class=n>environment</span><span class=o>.</span><span class=n>pre_install_commands</span><span class=p>,</span> <span class=n>source</span><span class=o>=</span><span class=s1>&#39;pre-install&#39;</span><span class=p>)</span>
<span class=linenos data-linenos="100 "></span>
<span class=linenos data-linenos="101 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_project_installation</span><span class=p>():</span>
<span class=linenos data-linenos="102 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>dev_mode</span><span class=p>:</span>
<span class=linenos data-linenos="103 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>install_project_dev_mode</span><span class=p>()</span>
<span class=linenos data-linenos="104 "></span> <span class=k>else</span><span class=p>:</span>
<span class=linenos data-linenos="105 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>install_project</span><span class=p>()</span>
<span class=linenos data-linenos="106 "></span>
<span class=linenos data-linenos="107 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>post_install_commands</span><span class=p>:</span>
<span class=linenos data-linenos="108 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_post_installation</span><span class=p>():</span>
<span class=linenos data-linenos="109 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>run_shell_commands</span><span class=p>(</span><span class=n>environment</span><span class=p>,</span> <span class=n>environment</span><span class=o>.</span><span class=n>post_install_commands</span><span class=p>,</span> <span class=n>source</span><span class=o>=</span><span class=s1>&#39;post-install&#39;</span><span class=p>)</span>
</code></pre></div> </div> </div> </div> <h2 id=life-cycle>Life cycle<a class=headerlink href=#life-cycle title="Permanent link">&para;</a></h2> <p>Whenever an environment is used, the following logic is performed:</p> <div class="doc doc-object doc-function"> <div class="doc doc-contents first"> <details class=quote> <summary>Source code in <code>src/hatch/cli/application.py</code></summary> <div class=highlight><pre><span></span><code><span class=linenos data-linenos="104 "></span><span class=k>def</span> <span class=nf>prepare_environment</span><span class=p>(</span><span class=bp>self</span><span class=p>,</span> <span class=n>environment</span><span class=p>:</span> <span class=n>EnvironmentInterface</span><span class=p>):</span>
<span class=linenos data-linenos="105 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>environment</span><span class=o>.</span><span class=n>exists</span><span class=p>():</span>
<span class=linenos data-linenos="106 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>reset</span><span class=p>(</span><span class=n>environment</span><span class=p>)</span>
<span class=linenos data-linenos="107 "></span>
<span class=linenos data-linenos="108 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_creation</span><span class=p>():</span>
<span class=linenos data-linenos="109 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>create</span><span class=p>()</span>
<span class=linenos data-linenos="110 "></span>
<span class=linenos data-linenos="111 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_state_check</span><span class=p>():</span>
<span class=linenos data-linenos="112 "></span> <span class=n>new_dep_hash</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>()</span>
<span class=linenos data-linenos="113 "></span>
<span class=linenos data-linenos="114 "></span> <span class=n>current_dep_hash</span> <span class=o>=</span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>(</span><span class=n>environment</span><span class=p>)</span>
<span class=linenos data-linenos="115 "></span> <span class=k>if</span> <span class=n>new_dep_hash</span> <span class=o>!=</span> <span class=n>current_dep_hash</span><span class=p>:</span>
<span class=linenos data-linenos="116 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_installation_check</span><span class=p>():</span>
<span class=linenos data-linenos="117 "></span> <span class=n>dependencies_in_sync</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependencies_in_sync</span><span class=p>()</span>
<span class=linenos data-linenos="118 "></span>
<span class=linenos data-linenos="119 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>dependencies_in_sync</span><span class=p>:</span>
<span class=linenos data-linenos="120 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_synchronization</span><span class=p>():</span>
<span class=linenos data-linenos="121 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>sync_dependencies</span><span class=p>()</span>
<span class=linenos data-linenos="122 "></span> <span class=n>new_dep_hash</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>()</span>
<span class=linenos data-linenos="123 "></span>
<span class=linenos data-linenos="124 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>update_dependency_hash</span><span class=p>(</span><span class=n>environment</span><span class=p>,</span> <span class=n>new_dep_hash</span><span class=p>)</span>
<span class=linenos data-linenos="111 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>environment</span><span class=o>.</span><span class=n>skip_install</span><span class=p>:</span>
<span class=linenos data-linenos="112 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>pre_install_commands</span><span class=p>:</span>
<span class=linenos data-linenos="113 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_pre_installation</span><span class=p>():</span>
<span class=linenos data-linenos="114 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>run_shell_commands</span><span class=p>(</span>
<span class=linenos data-linenos="115 "></span> <span class=n>ExecutionContext</span><span class=p>(</span>
<span class=linenos data-linenos="116 "></span> <span class=n>environment</span><span class=p>,</span>
<span class=linenos data-linenos="117 "></span> <span class=n>shell_commands</span><span class=o>=</span><span class=n>environment</span><span class=o>.</span><span class=n>pre_install_commands</span><span class=p>,</span>
<span class=linenos data-linenos="118 "></span> <span class=n>source</span><span class=o>=</span><span class=s1>&#39;pre-install&#39;</span><span class=p>,</span>
<span class=linenos data-linenos="119 "></span> <span class=n>show_code_on_error</span><span class=o>=</span><span class=kc>True</span><span class=p>,</span>
<span class=linenos data-linenos="120 "></span> <span class=p>)</span>
<span class=linenos data-linenos="121 "></span> <span class=p>)</span>
<span class=linenos data-linenos="122 "></span>
<span class=linenos data-linenos="123 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_project_installation</span><span class=p>():</span>
<span class=linenos data-linenos="124 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>dev_mode</span><span class=p>:</span>
<span class=linenos data-linenos="125 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>install_project_dev_mode</span><span class=p>()</span>
<span class=linenos data-linenos="126 "></span> <span class=k>else</span><span class=p>:</span>
<span class=linenos data-linenos="127 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>install_project</span><span class=p>()</span>
<span class=linenos data-linenos="128 "></span>
<span class=linenos data-linenos="129 "></span> <span class=k>if</span> <span class=n>environment</span><span class=o>.</span><span class=n>post_install_commands</span><span class=p>:</span>
<span class=linenos data-linenos="130 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_post_installation</span><span class=p>():</span>
<span class=linenos data-linenos="131 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>run_shell_commands</span><span class=p>(</span>
<span class=linenos data-linenos="132 "></span> <span class=n>ExecutionContext</span><span class=p>(</span>
<span class=linenos data-linenos="133 "></span> <span class=n>environment</span><span class=p>,</span>
<span class=linenos data-linenos="134 "></span> <span class=n>shell_commands</span><span class=o>=</span><span class=n>environment</span><span class=o>.</span><span class=n>post_install_commands</span><span class=p>,</span>
<span class=linenos data-linenos="135 "></span> <span class=n>source</span><span class=o>=</span><span class=s1>&#39;post-install&#39;</span><span class=p>,</span>
<span class=linenos data-linenos="136 "></span> <span class=n>show_code_on_error</span><span class=o>=</span><span class=kc>True</span><span class=p>,</span>
<span class=linenos data-linenos="137 "></span> <span class=p>)</span>
<span class=linenos data-linenos="138 "></span> <span class=p>)</span>
<span class=linenos data-linenos="139 "></span>
<span class=linenos data-linenos="140 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_state_check</span><span class=p>():</span>
<span class=linenos data-linenos="141 "></span> <span class=n>new_dep_hash</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>()</span>
<span class=linenos data-linenos="142 "></span>
<span class=linenos data-linenos="143 "></span> <span class=n>current_dep_hash</span> <span class=o>=</span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>(</span><span class=n>environment</span><span class=p>)</span>
<span class=linenos data-linenos="144 "></span> <span class=k>if</span> <span class=n>new_dep_hash</span> <span class=o>!=</span> <span class=n>current_dep_hash</span><span class=p>:</span>
<span class=linenos data-linenos="145 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_installation_check</span><span class=p>():</span>
<span class=linenos data-linenos="146 "></span> <span class=n>dependencies_in_sync</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependencies_in_sync</span><span class=p>()</span>
<span class=linenos data-linenos="147 "></span>
<span class=linenos data-linenos="148 "></span> <span class=k>if</span> <span class=ow>not</span> <span class=n>dependencies_in_sync</span><span class=p>:</span>
<span class=linenos data-linenos="149 "></span> <span class=k>with</span> <span class=n>environment</span><span class=o>.</span><span class=n>app_status_dependency_synchronization</span><span class=p>():</span>
<span class=linenos data-linenos="150 "></span> <span class=n>environment</span><span class=o>.</span><span class=n>sync_dependencies</span><span class=p>()</span>
<span class=linenos data-linenos="151 "></span> <span class=n>new_dep_hash</span> <span class=o>=</span> <span class=n>environment</span><span class=o>.</span><span class=n>dependency_hash</span><span class=p>()</span>
<span class=linenos data-linenos="152 "></span>
<span class=linenos data-linenos="153 "></span> <span class=bp>self</span><span class=o>.</span><span class=n>env_metadata</span><span class=o>.</span><span class=n>update_dependency_hash</span><span class=p>(</span><span class=n>environment</span><span class=p>,</span> <span class=n>new_dep_hash</span><span class=p>)</span>
</code></pre></div> </details> </div> </div><h2 id=build-environments>Build environments<a class=headerlink href=#build-environments title="Permanent link">&para;</a></h2> <p>All environment types should <a href=#hatch.env.plugin.interface.EnvironmentInterface.build_environment>offer support</a> for a special sub-environment in which projects can be built. This environment is used in the following scenarios:</p> <ul> <li>the <a href=../../../cli/reference/#hatch-build><code>build</code></a> command</li> <li>commands that read dependencies, like <a href=../../../cli/reference/#hatch-dep-hash><code>dep hash</code></a>, if any <a href=../../../config/metadata/#dependencies>project dependencies</a> are <a href=../../../config/metadata/#dynamic>set dynamically</a></li> </ul> <div class="doc doc-object doc-class"> <h2 id=hatch.env.plugin.interface.EnvironmentInterface class="doc doc-heading"> <code>EnvironmentInterface</code> <a href=#hatch.env.plugin.interface.EnvironmentInterface class=headerlink title="Permanent link">&para;</a></h2> <div class="doc doc-contents first"> <p>Example usage:</p> <div class="tabbed-set tabbed-alternate" data-tabs=1:2><input checked=checked id=hatch.env.plugin.interface.EnvironmentInterface--pluginpy name=hatch.env.plugin.interface.EnvironmentInterface--__tabbed_1 type=radio><input id=hatch.env.plugin.interface.EnvironmentInterface--hookspy name=hatch.env.plugin.interface.EnvironmentInterface--__tabbed_1 type=radio><div class=tabbed-labels><label for=hatch.env.plugin.interface.EnvironmentInterface--pluginpy><span class=twemoji><svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 16 16"><path d="M4 1.75C4 .784 4.784 0 5.75 0h5.586c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v8.586A1.75 1.75 0 0 1 14.25 15h-9a.75.75 0 0 1 0-1.5h9a.25.25 0 0 0 .25-.25V6h-2.75A1.75 1.75 0 0 1 10 4.25V1.5H5.75a.25.25 0 0 0-.25.25v2.5a.75.75 0 0 1-1.5 0Zm1.72 4.97a.75.75 0 0 1 1.06 0l2 2a.75.75 0 0 1 0 1.06l-2 2a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734l1.47-1.47-1.47-1.47a.75.75 0 0 1 0-1.06ZM3.28 7.78 1.81 9.25l1.47 1.47a.751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018l-2-2a.75.75 0 0 1 0-1.06l2-2a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042Zm8.22-6.218V4.25c0 .138.112.25.25.25h2.688l-.011-.013-2.914-2.914-.013-.011Z"/></svg></span> plugin.py</label><label for=hatch.env.plugin.interface.EnvironmentInterface--hookspy><span class=twemoji><svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 16 16"><path d="M4 1.75C4 .784 4.784 0 5.75 0h5.586c.464 0 .909.184 1.237.513l2.914 2.914c.329.328.513.773.513 1.237v8.586A1.75 1.75 0 0 1 14.25 15h-9a.75.75 0 0 1 0-1.5h9a.25.25 0 0 0 .25-.25V6h-2.75A1.75 1.75 0 0 1 10 4.25V1.5H5.75a.25.25 0 0 0-.25.25v2.5a.75.75 0 0 1-1.5 0Zm1.72 4.97a.75.75 0 0 1 1.06 0l2 2a.75.75 0 0 1 0 1.06l-2 2a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734l1.47-1.47-1.47-1.47a.75.75 0 0 1 0-1.06ZM3.28 7.78 1.81 9.25l1.47 1.47a.751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018l-2-2a.75.75 0 0 1 0-1.06l2-2a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042Zm8.22-6.218V4.25c0 .138.112.25.25.25h2.688l-.011-.013-2.914-2.914-.013-.011Z"/></svg></span> hooks.py</label></div> <div class=tabbed-content> <div class=tabbed-block> <div class=highlight><pre><span></span><code> <span class=kn>from</span> <span class=nn>hatch.env.plugin.interface</span> <span class=kn>import</span> <span class=n>EnvironmentInterface</span>


Expand Down
2 changes: 1 addition & 1 deletion dev/search/search_index.json

Large diffs are not rendered by default.

0 comments on commit aa63be3

Please sign in to comment.