Skip to content

Commit

Permalink
Allow to load dub.selections.json before Project instantiation
Browse files Browse the repository at this point in the history
The aim of this commit is to have at least one constructor
for Project that does not perform IO. By moving the IO to the
`Dub` class, we can keep the dependency injection in `Dub`
and `PackageManager` only, and don't have to specialize `Project`.
  • Loading branch information
Geod24 authored and dlang-bot committed Dec 26, 2023
1 parent 9fd2ab8 commit b3350d1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
3 changes: 2 additions & 1 deletion source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ class Dub {
/// Loads a specific package as the main project package (can be a sub package)
void loadPackage(Package pack)
{
m_project = new Project(m_packageManager, pack);
auto selections = Project.loadSelections(pack);
m_project = new Project(m_packageManager, pack, selections);
}

/** Loads a single file package.
Expand Down
53 changes: 42 additions & 11 deletions source/dub/project.d
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,54 @@ class Project {
this(package_manager, pack);
}

/// ditto
/// Ditto
this(PackageManager package_manager, Package pack)
{
auto selections = Project.loadSelections(pack);
this(package_manager, pack, selections);
}

/// ditto
this(PackageManager package_manager, Package pack, SelectedVersions selections)
{
m_packageManager = package_manager;
m_rootPackage = pack;
m_selections = selections;
reinit();
}

/**
* Loads a project's `dub.selections.json` and returns it
*
* This function will load `dub.selections.json` from the path at which
* `pack` is located, and returned the resulting `SelectedVersions`.
* If no `dub.selections.json` is found, an empty `SelectedVersions`
* is returned.
*
* Params:
* pack = Package to load the selection file from.
*
* Returns:
* Always a non-null instance.
*/
static package SelectedVersions loadSelections(in Package pack)
{
auto selverfile = (pack.path ~ SelectedVersions.defaultFile).toNativeString();

auto selverfile = (m_rootPackage.path ~ SelectedVersions.defaultFile).toNativeString();
if (existsFile(selverfile)) {
// TODO: Remove `StrictMode.Warn` after v1.40 release
// The default is to error, but as the previous parser wasn't
// complaining, we should first warn the user.
auto selected = parseConfigFileSimple!Selected(selverfile, StrictMode.Warn);
m_selections = !selected.isNull() ?
new SelectedVersions(selected.get()) : new SelectedVersions();
} else m_selections = new SelectedVersions;
// No file exists
if (!existsFile(selverfile))
return new SelectedVersions();

reinit();
// TODO: Remove `StrictMode.Warn` after v1.40 release
// The default is to error, but as the previous parser wasn't
// complaining, we should first warn the user.
auto selected = parseConfigFileSimple!Selected(selverfile, StrictMode.Warn);

// Parsing error, it will be displayed to the user
if (selected.isNull())
return new SelectedVersions();

return new SelectedVersions(selected.get());
}

/** List of all resolved dependencies.
Expand Down

0 comments on commit b3350d1

Please sign in to comment.