From 03c979c18014c555f023ee3e93757be95858753b Mon Sep 17 00:00:00 2001 From: Mathias Lang Date: Fri, 22 Dec 2023 18:12:02 +0100 Subject: [PATCH] Allow to load dub.selections.json before Project instantiation 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`. --- source/dub/dub.d | 3 ++- source/dub/project.d | 40 +++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/source/dub/dub.d b/source/dub/dub.d index 7d509ca3f..3940dbb3b 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -447,7 +447,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. diff --git a/source/dub/project.d b/source/dub/project.d index 5abfd4571..99eecc16d 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -71,23 +71,41 @@ 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(); + } - 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; + /// + static package SelectedVersions loadSelections(Package pack) + { + auto selverfile = (pack.path ~ SelectedVersions.defaultFile).toNativeString(); - reinit(); + // No file exists + if (!existsFile(selverfile)) + return new SelectedVersions(); + + // 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.