2022-04-18 23:36:09 +00:00
|
|
|
package funkin.util;
|
|
|
|
|
|
|
|
import thx.semver.Version;
|
|
|
|
import thx.semver.VersionRule;
|
|
|
|
|
|
|
|
/**
|
2023-11-07 09:04:22 +00:00
|
|
|
* Utility functions for operating on semantic versions.
|
|
|
|
*
|
2022-04-18 23:36:09 +00:00
|
|
|
* Remember, increment the patch version (1.0.x) if you make a bugfix,
|
|
|
|
* increment the minor version (1.x.0) if you make a new feature (but previous content is still compatible),
|
|
|
|
* and increment the major version (x.0.0) if you make a breaking change (e.g. new API or reorganized file format).
|
|
|
|
*/
|
|
|
|
class VersionUtil
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
/**
|
|
|
|
* Checks that a given verison number satisisfies a given version rule.
|
|
|
|
* Version rule can be complex, e.g. "1.0.x" or ">=1.0.0,<1.1.0", or anything NPM supports.
|
2024-03-17 02:20:22 +00:00
|
|
|
* @param version The semantic version to validate.
|
|
|
|
* @param versionRule The version rule to validate against.
|
|
|
|
* @return `true` if the version satisfies the rule, `false` otherwise.
|
2023-01-23 03:25:45 +00:00
|
|
|
*/
|
2023-08-22 08:27:30 +00:00
|
|
|
public static function validateVersion(version:thx.semver.Version, versionRule:thx.semver.VersionRule):Bool
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-06-04 23:44:00 +00:00
|
|
|
var versionRaw:thx.semver.Version.SemVer = version;
|
|
|
|
trace('${versionRaw} satisfies (${versionRule})? ${version.satisfies(versionRule)}');
|
2023-08-22 08:27:30 +00:00
|
|
|
return version.satisfies(versionRule);
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
trace('[VERSIONUTIL] Invalid semantic version: ${version}');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 08:27:30 +00:00
|
|
|
|
2024-05-21 06:23:21 +00:00
|
|
|
public static function repairVersion(version:thx.semver.Version):thx.semver.Version
|
|
|
|
{
|
|
|
|
var versionData:thx.semver.Version.SemVer = version;
|
|
|
|
|
2024-05-21 08:02:53 +00:00
|
|
|
if (thx.Types.isAnonymousObject(versionData.version))
|
2024-05-21 06:23:21 +00:00
|
|
|
{
|
|
|
|
// This is bad! versionData.version should be an array!
|
2024-06-04 18:26:24 +00:00
|
|
|
trace('[SAVE] Version data repair required! (got ${versionData.version})');
|
2024-06-04 23:44:00 +00:00
|
|
|
// Turn the objects back into arrays.
|
|
|
|
// I'd use DynamicsT.values but IDK if it maintains order
|
|
|
|
versionData.version = [versionData.version[0], versionData.version[1], versionData.version[2]];
|
|
|
|
|
|
|
|
// This is so jank but it should work.
|
|
|
|
var buildData:Dynamic<String> = cast versionData.build;
|
|
|
|
var buildDataFixed:Array<thx.semver.Version.Identifier> = thx.Dynamics.DynamicsT.values(buildData)
|
|
|
|
.map(function(d:Dynamic) return StringId(d.toString()));
|
|
|
|
versionData.build = buildDataFixed;
|
|
|
|
|
|
|
|
var preData:Dynamic<String> = cast versionData.pre;
|
|
|
|
var preDataFixed:Array<thx.semver.Version.Identifier> = thx.Dynamics.DynamicsT.values(preData).map(function(d:Dynamic) return StringId(d.toString()));
|
|
|
|
versionData.pre = preDataFixed;
|
2024-05-21 06:23:21 +00:00
|
|
|
|
|
|
|
var fixedVersion:thx.semver.Version = versionData;
|
2024-06-04 23:44:00 +00:00
|
|
|
trace('[SAVE] Fixed version: ${fixedVersion}');
|
2024-05-21 06:23:21 +00:00
|
|
|
return fixedVersion;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-06-04 18:26:24 +00:00
|
|
|
trace('[SAVE] Version data repair not required (got ${version})');
|
2024-05-21 06:23:21 +00:00
|
|
|
// No need for repair.
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-22 08:27:30 +00:00
|
|
|
/**
|
|
|
|
* Checks that a given verison number satisisfies a given version rule.
|
|
|
|
* Version rule can be complex, e.g. "1.0.x" or ">=1.0.0,<1.1.0", or anything NPM supports.
|
2024-03-17 02:20:22 +00:00
|
|
|
* @param version The semantic version to validate.
|
|
|
|
* @param versionRule The version rule to validate against.
|
|
|
|
* @return `true` if the version satisfies the rule, `false` otherwise.
|
2023-08-22 08:27:30 +00:00
|
|
|
*/
|
|
|
|
public static function validateVersionStr(version:String, versionRule:String):Bool
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var version:thx.semver.Version = version;
|
|
|
|
var versionRule:thx.semver.VersionRule = versionRule;
|
|
|
|
return version.satisfies(versionRule);
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
|
|
|
trace('[VERSIONUTIL] Invalid semantic version: ${version}');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get and parse the semantic version from a JSON string.
|
|
|
|
* @param input The JSON string to parse.
|
|
|
|
* @return The semantic version, or null if it could not be parsed.
|
|
|
|
*/
|
2023-09-18 21:59:55 +00:00
|
|
|
public static function getVersionFromJSON(input:Null<String>):Null<thx.semver.Version>
|
2023-08-22 08:27:30 +00:00
|
|
|
{
|
2023-09-18 21:59:55 +00:00
|
|
|
if (input == null) return null;
|
2024-03-17 02:20:22 +00:00
|
|
|
var parsed:Dynamic = SerializerUtil.fromJSON(input);
|
2023-08-22 08:27:30 +00:00
|
|
|
if (parsed == null) return null;
|
|
|
|
if (parsed.version == null) return null;
|
|
|
|
var versionStr:String = parsed.version; // Dynamic -> String cast
|
|
|
|
var version:thx.semver.Version = versionStr; // Implicit, not explicit, cast.
|
|
|
|
return version;
|
|
|
|
}
|
2023-10-21 05:04:50 +00:00
|
|
|
|
2024-03-17 02:20:22 +00:00
|
|
|
/**
|
|
|
|
* Get and parse the semantic version from a JSON string.
|
|
|
|
* @param input The JSON string to parse.
|
|
|
|
* @return The semantic version, or null if it could not be parsed.
|
|
|
|
*/
|
2023-10-21 05:04:50 +00:00
|
|
|
public static function parseVersion(input:Dynamic):Null<thx.semver.Version>
|
|
|
|
{
|
|
|
|
if (input == null) return null;
|
|
|
|
|
|
|
|
if (Std.isOfType(input, String))
|
|
|
|
{
|
|
|
|
var inputStr:String = input;
|
|
|
|
var version:thx.semver.Version = inputStr;
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var semVer:thx.semver.Version.SemVer = input;
|
|
|
|
var version:thx.semver.Version = semVer;
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
2022-04-18 23:36:09 +00:00
|
|
|
}
|