Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move dmd version parser to packagerecipe #2745

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions source/dub/compilers/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -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__";

Expand Down
50 changes: 48 additions & 2 deletions source/dub/recipe/packagerecipe.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
Loading