From b3350d14152342e8d805969b1873a3325413dfa0 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 | 53 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/source/dub/dub.d b/source/dub/dub.d index 47d20a8f9..ab7f724e7 100644 --- a/source/dub/dub.d +++ b/source/dub/dub.d @@ -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. diff --git a/source/dub/project.d b/source/dub/project.d index 5abfd4571..76cb2e627 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -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.