[plan-build] Split dep resolving into 3 steps around Scaffolding.
[plan-build] Split dep resolving into 3 steps around Scaffolding.

This change alters the package dependency resolving logic by splitting
up the task into 3 steps which were formerly all together in the
`_resolve_dependencies()` function:

1. `_resolve_build_dependencies()` - Downloads, resolves, and records
   all build time dependencies. The side effects are creating the
   `$pkg_build_deps_resolved` & `$pkg_build_tdeps_resolved` arrays.
2. `_resolve_run_dependencies()` - Downloads, resolves, and records are
   run time dependencies. The side effects are creating the
   `$pkg_deps_resolved` & `$pkg_tdeps_resolved` arrays.
3. `_finalize_dependencies()` -  Populates the remaining package arrays
   `$pkg_add_deps_resolved` & `$pkg_all_tdeps_resolved`.

The driving need for this change is when a Scaffolding package needs to
provide a **runtime** dependency for a Plan. For example a Python
Scaffolding will want to inject some Python package into the `pkg_deps`
of the Python app Plan. The Plan's build dependencies are satisfied by
the Scaffolding packages run deps but there was no mechanism by which to
inject any packages into the Plan's run deps. What made this even more
difficult is that the build program used the `_resolve_dependencies()`
function to download and install the Scaffolding package itself by
injecting the package identifier into the Plan's `pkg_build_deps`.

To allow a chance for a Scaffolding to inject a runtime dependency for a
Plan, the pseudo logic is changed to be roughly as follows (from the app
developer's Plan's point of view):

1. Inject the Scaffolding package into the Plan's `pkg_build_deps`
2. Download, install, and resolve all **build** dependencies. This gets
   us our Scaffolding package.
3. Load the Scaffolding by sourcing it in and invoking the internal
   `_begin_scaffolding()` function if the Scaffolding implements this
   function. A Scaffolding author can use this function to inject one or
   more **runtime** dependencies into a Plan's `pkg_deps` (for example:
   `core/python` or similar).
4. Download, install, and resolve all **runtime** dependencies. This
   would get us our Python package, for example.
5. Finalize the dependency resolving by writing the remaining package
   arrays and validating the runtime deps as before.
6. Set the `PATH` and other environment setup, as before.

In this way we don't have to run dependency resolving multiple times or
re-resolve the same set by mutating it from an initial state. The
assumption underlying this implementation is that the Plan's **build**
dependencies should not be mutated by a Scaffolding package, but rather
described statically in the Scaffolding package's Plan, captured in its
`pkg_deps`.

The changes made here changed one implementation detail in the
`_load_scaffolding()` function: the `pkg_path_for()` helper would no
longer work as the underlying package array does not yet exist. Instead,
this function now uses a new (internal) function which only inspects the
resolved build dependencies.

Given the current state of environment and specifically `PATH` loading,
the Scaffolding's optional `_begin_scaffolding()` function will not have
access to the normal `pkg_path_for()` helper, nor will build
dependencies be present on `PATH`. Future work may need to add more
support here, but in the meantime Scaffolding authors should take note
to be careful what is being run in this early stage function.

Fixes #2073
Closes #2075

/cc @rhass

Signed-off-by: Fletcher Nichol <77a0fd9e8048bbacd11af4e957bc6ff03b549f49@nichol.ca>

georgemarshall/habitat,georgemarshall/habitat,rsertelon/habitat,nathenharvey/habitat,georgemarshall/habitat,habitat-sh/habitat,nathenharvey/habitat,georgemarshall/habitat,nathenharvey/habitat,habitat-sh/habitat,rsertelon/habitat,nathenharvey/habitat,habitat-sh/habitat,georgemarshall/habitat,nathenharvey/habitat,georgemarshall/habitat,georgemarshall/habitat,rsertelon/habitat,habitat-sh/habitat,rsertelon/habitat,habitat-sh/habitat,habitat-sh/habitat,habitat-sh/habitat,habitat-sh/habitat,nathenharvey/habitat,rsertelon/habitat,georgemarshall/habitat,rsertelon/habitat,rsertelon/habitat