-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b42dda7
commit 74fdc61
Showing
3 changed files
with
136 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,57 @@ | ||
namespace Actions.Commands | ||
namespace Actions.Commands; | ||
|
||
public class CreateDummyCommand | ||
{ | ||
public class CreateDummyCommand | ||
public void CreateDummyFiles(string basePath) | ||
{ | ||
public void CreateDummyFiles(string basePath) | ||
{ | ||
var upm = ("package.json", """ | ||
{ | ||
"name": "com.unity.plugin.example", | ||
"version": "1.2.310", | ||
"displayName": "Package Example Plugin", | ||
"description": "This is an example package", | ||
"unity": "2019.1", | ||
"unityRelease": "0b5", | ||
"dependencies": { | ||
"com.unity.example": "1.0.0" | ||
}, | ||
"keywords": [ | ||
"keyword1", | ||
"keyword2", | ||
"keyword3" | ||
], | ||
"author": { | ||
"name": "Unity", | ||
"email": "[email protected]", | ||
"url": "https://www.unity3d.com" | ||
} | ||
var upm = ("package.json", """ | ||
{ | ||
"name": "com.unity.plugin.example", | ||
"version": "1.2.310", | ||
"displayName": "Package Example Plugin", | ||
"description": "This is an example package", | ||
"unity": "2019.1", | ||
"unityRelease": "0b5", | ||
"dependencies": { | ||
"com.unity.example": "1.0.0" | ||
}, | ||
"keywords": [ | ||
"keyword1", | ||
"keyword2", | ||
"keyword3" | ||
], | ||
"author": { | ||
"name": "Unity", | ||
"email": "[email protected]", | ||
"url": "https://www.unity3d.com" | ||
} | ||
"""); | ||
var godot = ("plugin.cfg", """ | ||
[plugin] | ||
name="Sandbox.Godot" | ||
description="Sample." | ||
author="Cysharp" | ||
version="1.2.310" | ||
language="C-sharp" | ||
script="GodotPlugin.cs" | ||
"""); | ||
var directoryBuildProps = ("Directory.Build.props", """ | ||
<Project> | ||
<PropertyGroup> | ||
<VersionPrefix>1.2.310</VersionPrefix> | ||
</PropertyGroup> | ||
</Project> | ||
"""); | ||
} | ||
"""); | ||
var godot = ("plugin.cfg", """ | ||
[plugin] | ||
name="Sandbox.Godot" | ||
description="Sample." | ||
author="Cysharp" | ||
version="1.2.310" | ||
language="C-sharp" | ||
script="GodotPlugin.cs" | ||
"""); | ||
var directoryBuildProps = ("Directory.Build.props", """ | ||
<Project> | ||
<PropertyGroup> | ||
<VersionPrefix>1.2.310</VersionPrefix> | ||
</PropertyGroup> | ||
</Project> | ||
"""); | ||
|
||
foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps }) | ||
{ | ||
var path = Path.Combine(basePath, file); | ||
if (!Directory.Exists(basePath)) | ||
Directory.CreateDirectory(basePath); | ||
foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps }) | ||
{ | ||
var path = Path.Combine(basePath, file); | ||
if (!Directory.Exists(basePath)) | ||
Directory.CreateDirectory(basePath); | ||
|
||
Console.WriteLine($"- {path} ..."); | ||
File.WriteAllText(path, contents); | ||
} | ||
Console.WriteLine($"- {path} ..."); | ||
File.WriteAllText(path, contents); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,110 @@ | ||
using Actions.Utils; | ||
using Actions.Utils; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Actions.Commands | ||
namespace Actions.Commands; | ||
|
||
public record struct UpdateVersionCommandResult(string Before, string After); | ||
public class UpdateVersionCommand(string version, string path) | ||
{ | ||
public record struct UpdateVersionCommandResult(string Before, string After); | ||
public class UpdateVersionCommand(string version, string path) | ||
public UpdateVersionCommandResult UpdateVersion(bool dryRun) | ||
{ | ||
public UpdateVersionCommandResult UpdateVersion(bool dryRun) | ||
{ | ||
if (!File.Exists(path)) throw new FileNotFoundException(path); | ||
if (!File.Exists(path)) throw new FileNotFoundException(path); | ||
|
||
var writeBack = !dryRun; | ||
var fileName = Path.GetFileName(path); | ||
return fileName switch | ||
{ | ||
// UPM | ||
"package.json" => HandleUpm(writeBack), | ||
// Godot | ||
"plugin.cfg" => HandleGodot(writeBack), | ||
// .NET | ||
"Directory.Build.props" => HandleDirectoryBuildProps(writeBack), | ||
// Other | ||
_ => throw new NotImplementedException(fileName), | ||
}; | ||
} | ||
|
||
private UpdateVersionCommandResult HandleUpm(bool writeBack) | ||
var writeBack = !dryRun; | ||
var fileName = Path.GetFileName(path); | ||
return fileName switch | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack); | ||
// UPM | ||
"package.json" => HandleUpm(writeBack), | ||
// Godot | ||
"plugin.cfg" => HandleGodot(writeBack), | ||
// .NET | ||
"Directory.Build.props" => HandleDirectoryBuildProps(writeBack), | ||
// Other | ||
_ => throw new NotImplementedException(fileName), | ||
}; | ||
} | ||
|
||
// validate | ||
Validate(after, version); | ||
private UpdateVersionCommandResult HandleUpm(bool writeBack) | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack); | ||
|
||
return new UpdateVersionCommandResult(before, after); | ||
// validate | ||
Validate(after, version); | ||
|
||
static void Validate(string contents, string version) | ||
{ | ||
var packageJson = JsonSerializer.Deserialize<UpmPackageJson>(contents) ?? throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}"); | ||
if (packageJson.Version != version) | ||
throw new ActionCommandException($"UPM package.json updated, but version miss-match. actual {packageJson?.Version}, expected {version}"); | ||
} | ||
} | ||
return new UpdateVersionCommandResult(before, after); | ||
|
||
private UpdateVersionCommandResult HandleGodot(bool writeBack) | ||
static void Validate(string contents, string version) | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack); | ||
var packageJson = JsonSerializer.Deserialize<UpmPackageJson>(contents) ?? throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}"); | ||
if (packageJson.Version != version) | ||
throw new ActionCommandException($"UPM package.json updated, but version miss-match. actual {packageJson?.Version}, expected {version}"); | ||
} | ||
} | ||
|
||
// validate | ||
Validate(after, version); | ||
private UpdateVersionCommandResult HandleGodot(bool writeBack) | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack); | ||
|
||
return new UpdateVersionCommandResult(before, after); | ||
// validate | ||
Validate(after, version); | ||
|
||
static void Validate(string contents, string version) | ||
return new UpdateVersionCommandResult(before, after); | ||
|
||
static void Validate(string contents, string version) | ||
{ | ||
var lines = contents.Split("\n"); | ||
Span<Range> destination = stackalloc Range[2]; | ||
foreach (var line in lines) | ||
{ | ||
var lines = contents.Split("\n"); | ||
Span<Range> destination = stackalloc Range[2]; | ||
foreach (var line in lines) | ||
// find the line befin with "version=", then split with = to get version | ||
if (!line.StartsWith("version=")) | ||
continue; | ||
|
||
var span = line.AsSpan(); | ||
var range = span.Split(destination, '=', StringSplitOptions.TrimEntries); | ||
if (range != 2) | ||
continue; | ||
|
||
// validate version is expceted | ||
var versionValue = span[destination[1]].ToString(); | ||
if (versionValue != $"\"{version}\"") | ||
{ | ||
// find the line befin with "version=", then split with = to get version | ||
if (!line.StartsWith("version=")) | ||
continue; | ||
|
||
var span = line.AsSpan(); | ||
var range = span.Split(destination, '=', StringSplitOptions.TrimEntries); | ||
if (range != 2) | ||
continue; | ||
|
||
// validate version is expceted | ||
var versionValue = span[destination[1]].ToString(); | ||
if (versionValue != $"\"{version}\"") | ||
{ | ||
throw new ActionCommandException($"Godot plugin.cfg updated, but version miss-match. actual {versionValue}, expected {version}"); | ||
} | ||
return; | ||
throw new ActionCommandException($"Godot plugin.cfg updated, but version miss-match. actual {versionValue}, expected {version}"); | ||
} | ||
throw new ActionCommandException($"Godot plugin.cfg updated, but version key not found."); | ||
return; | ||
} | ||
throw new ActionCommandException($"Godot plugin.cfg updated, but version key not found."); | ||
} | ||
} | ||
|
||
private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack) | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", writeBack); | ||
private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack) | ||
{ | ||
// replace | ||
var (before, after) = Sed.Replace(path, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", writeBack); | ||
|
||
// validate | ||
Validate(after, version); | ||
// validate | ||
Validate(after, version); | ||
|
||
return new UpdateVersionCommandResult(before, after); | ||
return new UpdateVersionCommandResult(before, after); | ||
|
||
static void Validate(string contents, string version) | ||
{ | ||
var xmlDoc = new System.Xml.XmlDocument(); | ||
xmlDoc.LoadXml(contents); | ||
var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix") ?? throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found."); | ||
if (versionPrefixNode.InnerText != version) | ||
throw new ActionCommandException($"Directory.Build.props updated, but version miss-match. actual {versionPrefixNode.InnerText}, expected {version}"); | ||
static void Validate(string contents, string version) | ||
{ | ||
var xmlDoc = new System.Xml.XmlDocument(); | ||
xmlDoc.LoadXml(contents); | ||
var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix") ?? throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found."); | ||
if (versionPrefixNode.InnerText != version) | ||
throw new ActionCommandException($"Directory.Build.props updated, but version miss-match. actual {versionPrefixNode.InnerText}, expected {version}"); | ||
|
||
} | ||
} | ||
} | ||
|
||
private record UpmPackageJson | ||
{ | ||
[JsonPropertyName("version")] | ||
public required string Version { get; set; } | ||
} | ||
private record UpmPackageJson | ||
{ | ||
[JsonPropertyName("version")] | ||
public required string Version { get; set; } | ||
} | ||
} |