From 3627b0aa103f1c58309b3eb9edf3333430e23b3d Mon Sep 17 00:00:00 2001 From: Mathias Lang Date: Wed, 20 Dec 2023 01:12:23 +0100 Subject: [PATCH] Move dmd version parser to packagerecipe This module is the only one that uses it, so instead of having it in a place that looks correct but where it is not used, move it to packagerecipe and make it private. --- source/dub/compilers/utils.d | 48 ----------------------------- source/dub/recipe/packagerecipe.d | 50 +++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 50 deletions(-) diff --git a/source/dub/compilers/utils.d b/source/dub/compilers/utils.d index aac768a84..b3bb1e64a 100644 --- a/source/dub/compilers/utils.d +++ b/source/dub/compilers/utils.d @@ -258,54 +258,6 @@ void warnOnSpecialCompilerFlags(string[] compiler_flags, Flags!BuildOption optio if (got_preamble) logWarn(""); } -/** - Turn a DMD-like version (e.g. 2.082.1) into a SemVer-like version (e.g. 2.82.1). - The function accepts a dependency operator prefix and some text postfix. - Prefix and postfix are returned verbatim. - Params: - ver = version string, possibly with a dependency operator prefix and some - test postfix. - Returns: - A Semver compliant string -*/ -package(dub) string dmdLikeVersionToSemverLike(string ver) -{ - import std.algorithm : countUntil, joiner, map, skipOver, splitter; - import std.array : join, split; - import std.ascii : isDigit; - import std.conv : text; - import std.exception : enforce; - import std.functional : not; - import std.range : padRight; - - const start = ver.countUntil!isDigit; - enforce(start != -1, "Invalid semver: "~ver); - const prefix = ver[0 .. start]; - ver = ver[start .. $]; - - const end = ver.countUntil!(c => !c.isDigit && c != '.'); - const postfix = end == -1 ? null : ver[end .. $]; - auto verStr = ver[0 .. $-postfix.length]; - - auto comps = verStr - .splitter(".") - .map!((a) { if (a.length > 1) a.skipOver("0"); return a;}) - .padRight("0", 3); - - return text(prefix, comps.joiner("."), postfix); -} - -/// -unittest { - assert(dmdLikeVersionToSemverLike("2.082.1") == "2.82.1"); - assert(dmdLikeVersionToSemverLike("2.082.0") == "2.82.0"); - assert(dmdLikeVersionToSemverLike("2.082") == "2.82.0"); - assert(dmdLikeVersionToSemverLike("~>2.082") == "~>2.82.0"); - assert(dmdLikeVersionToSemverLike("~>2.082-beta1") == "~>2.82.0-beta1"); - assert(dmdLikeVersionToSemverLike("2.4.6") == "2.4.6"); - assert(dmdLikeVersionToSemverLike("2.4.6-alpha12") == "2.4.6-alpha12"); -} - private enum probeBeginMark = "__dub_probe_begin__"; private enum probeEndMark = "__dub_probe_end__"; diff --git a/source/dub/recipe/packagerecipe.d b/source/dub/recipe/packagerecipe.d index 30445d1a6..1ec1e59d6 100644 --- a/source/dub/recipe/packagerecipe.d +++ b/source/dub/recipe/packagerecipe.d @@ -615,7 +615,6 @@ struct BuildSettingsTemplate { package(dub) void checkPlatform(const scope ref ToolchainRequirements tr, BuildPlatform platform, string package_name) { - import dub.compilers.utils : dmdLikeVersionToSemverLike; import std.algorithm.iteration : map; import std.format : format; @@ -697,7 +696,6 @@ private static Dependency parseDependency(string dep) private static Dependency parseDMDDependency(string dep) { - import dub.compilers.utils : dmdLikeVersionToSemverLike; import dub.dependency : Dependency; import std.algorithm : map, splitter; import std.array : join; @@ -785,3 +783,51 @@ package void fixDependenciesNames (T) (string root, ref T aggr) nothrow fixDependenciesNames(root, aggr.tupleof[idx]); } } + +/** + Turn a DMD-like version (e.g. 2.082.1) into a SemVer-like version (e.g. 2.82.1). + The function accepts a dependency operator prefix and some text postfix. + Prefix and postfix are returned verbatim. + Params: + ver = version string, possibly with a dependency operator prefix and some + test postfix. + Returns: + A Semver compliant string +*/ +private string dmdLikeVersionToSemverLike(string ver) +{ + import std.algorithm : countUntil, joiner, map, skipOver, splitter; + import std.array : join, split; + import std.ascii : isDigit; + import std.conv : text; + import std.exception : enforce; + import std.functional : not; + import std.range : padRight; + + const start = ver.countUntil!isDigit; + enforce(start != -1, "Invalid semver: "~ver); + const prefix = ver[0 .. start]; + ver = ver[start .. $]; + + const end = ver.countUntil!(c => !c.isDigit && c != '.'); + const postfix = end == -1 ? null : ver[end .. $]; + auto verStr = ver[0 .. $-postfix.length]; + + auto comps = verStr + .splitter(".") + .map!((a) { if (a.length > 1) a.skipOver("0"); return a;}) + .padRight("0", 3); + + return text(prefix, comps.joiner("."), postfix); +} + +/// +unittest { + assert(dmdLikeVersionToSemverLike("2.082.1") == "2.82.1"); + assert(dmdLikeVersionToSemverLike("2.082.0") == "2.82.0"); + assert(dmdLikeVersionToSemverLike("2.082") == "2.82.0"); + assert(dmdLikeVersionToSemverLike("~>2.082") == "~>2.82.0"); + assert(dmdLikeVersionToSemverLike("~>2.082-beta1") == "~>2.82.0-beta1"); + assert(dmdLikeVersionToSemverLike("2.4.6") == "2.4.6"); + assert(dmdLikeVersionToSemverLike("2.4.6-alpha12") == "2.4.6-alpha12"); +}