Repository navigation
chore: Isolate skill file normalization from layout handling #1617
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
Merged
hatayama
merged 2 commits into
v3-beta
from
refactor/hatayama/extract-skill-content-normalization
Jul 8, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| using System.Linq; | ||
| using System.Text; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| using io.github.hatayama.UnityCliLoop.Infrastructure; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.Tests.Editor | ||
| { | ||
| /// <summary> | ||
| /// Verifies deterministic line-ending normalization for generated skill files. | ||
| /// </summary> | ||
| [TestFixture] | ||
| public class SkillFileContentNormalizerTests | ||
| { | ||
| // Tests that CRLF and lone CR line endings become LF in single-byte text files. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenSingleByteTextUsesCarriageReturns_ReturnsLfContent() | ||
| { | ||
| byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\r\nline2\rline3\n"); | ||
| byte[] expectedBytes = Encoding.UTF8.GetBytes("line1\nline2\nline3\n"); | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(expectedBytes)); | ||
| } | ||
|
|
||
| // Tests that UTF-16 little-endian text keeps its BOM and encoding while line endings become LF. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenUtf16LittleEndianTextUsesCrlf_PreservesEncoding() | ||
| { | ||
| byte[] sourceBytes = Encoding.Unicode.GetPreamble() | ||
| .Concat(Encoding.Unicode.GetBytes("line1\r\nline2\r\n")) | ||
| .ToArray(); | ||
| byte[] expectedBytes = Encoding.Unicode.GetPreamble() | ||
| .Concat(Encoding.Unicode.GetBytes("line1\nline2\n")) | ||
| .ToArray(); | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("install.ps1", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(expectedBytes)); | ||
| } | ||
|
|
||
| // Tests that UTF-16 big-endian text keeps its BOM and encoding while line endings become LF. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenUtf16BigEndianTextUsesCrlf_PreservesEncoding() | ||
| { | ||
| byte[] sourceBytes = Encoding.BigEndianUnicode.GetPreamble() | ||
| .Concat(Encoding.BigEndianUnicode.GetBytes("line1\r\nline2\r\n")) | ||
| .ToArray(); | ||
| byte[] expectedBytes = Encoding.BigEndianUnicode.GetPreamble() | ||
| .Concat(Encoding.BigEndianUnicode.GetBytes("line1\nline2\n")) | ||
| .ToArray(); | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("install.ps1", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(expectedBytes)); | ||
| } | ||
|
|
||
| // Tests that files outside the text extension allowlist keep their original bytes. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenExtensionIsNotText_ReturnsOriginalContent() | ||
| { | ||
| byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\r\nline2\r\n"); | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("image.png", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(sourceBytes)); | ||
| } | ||
|
|
||
| // Tests that NUL-containing binary data is not rewritten even when its extension is textual. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenTextExtensionContainsBinaryData_ReturnsOriginalContent() | ||
| { | ||
| byte[] sourceBytes = { 0x41, 0x42, 0x00, 0x43, 0x0D, 0x44 }; | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(sourceBytes)); | ||
| } | ||
|
|
||
| // Tests that LF-only text content remains byte-identical. | ||
| [Test] | ||
| public void NormalizeSkillFileContent_WhenTextAlreadyUsesLf_ReturnsOriginalContent() | ||
| { | ||
| byte[] sourceBytes = Encoding.UTF8.GetBytes("line1\nline2\n"); | ||
|
|
||
| byte[] actualBytes = SkillFileContentNormalizer.NormalizeSkillFileContent("reference.md", sourceBytes); | ||
|
|
||
| Assert.That(actualBytes, Is.EqualTo(sourceBytes)); | ||
| } | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
Assets/Tests/Editor/SkillFileContentNormalizerTests.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
193 changes: 193 additions & 0 deletions
193
Packages/src/Editor/Infrastructure/SkillSetup/SkillFileContentNormalizer.cs
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
|
|
||
| using UnityEngine; | ||
|
|
||
| namespace io.github.hatayama.UnityCliLoop.Infrastructure | ||
| { | ||
| /// <summary> | ||
| /// Normalizes line endings in generated text skill files without changing their encoding. | ||
| /// </summary> | ||
| internal static class SkillFileContentNormalizer | ||
| { | ||
| private const byte Utf16LittleEndianBomFirstByte = 0xFF; | ||
| private const byte Utf16LittleEndianBomSecondByte = 0xFE; | ||
| private const byte Utf16BigEndianBomFirstByte = 0xFE; | ||
| private const byte Utf16BigEndianBomSecondByte = 0xFF; | ||
| private const int Utf16CodeUnitByteCount = 2; | ||
| private const ushort CarriageReturnCodeUnit = 0x000D; | ||
| private const ushort LineFeedCodeUnit = 0x000A; | ||
| private static readonly HashSet<string> TextSkillFileExtensions = new(StringComparer.OrdinalIgnoreCase) | ||
| { | ||
| ".json", | ||
| ".md", | ||
| ".ps1", | ||
| ".sh", | ||
| ".txt", | ||
| ".yaml", | ||
| ".yml" | ||
| }; | ||
|
|
||
| internal static byte[] NormalizeSkillFileContent(string relativePath, byte[] content) | ||
| { | ||
| Debug.Assert(!string.IsNullOrEmpty(relativePath), "relativePath must not be null or empty"); | ||
| Debug.Assert(content != null, "content must not be null"); | ||
|
|
||
| if (!ShouldNormalizeLineEndings(relativePath) || Array.IndexOf(content, (byte)'\r') < 0) | ||
| { | ||
| return content; | ||
| } | ||
|
|
||
| if (HasUtf16LittleEndianBom(content) || HasUtf16LittleEndianLineEnding(content)) | ||
| { | ||
| return NormalizeUtf16LineEndings(content, isLittleEndian: true); | ||
| } | ||
|
|
||
| if (HasUtf16BigEndianBom(content) || HasUtf16BigEndianLineEnding(content)) | ||
| { | ||
| return NormalizeUtf16LineEndings(content, isLittleEndian: false); | ||
| } | ||
|
|
||
| if (Array.IndexOf(content, (byte)0) >= 0) | ||
| { | ||
| return content; | ||
| } | ||
|
|
||
| return NormalizeSingleByteLineEndings(content); | ||
| } | ||
|
|
||
| private static bool ShouldNormalizeLineEndings(string relativePath) | ||
| { | ||
| string extension = Path.GetExtension(relativePath); | ||
| return TextSkillFileExtensions.Contains(extension); | ||
| } | ||
|
|
||
| private static byte[] NormalizeSingleByteLineEndings(byte[] content) | ||
| { | ||
| List<byte> normalized = new(content.Length); | ||
| for (int index = 0; index < content.Length; index++) | ||
| { | ||
| byte current = content[index]; | ||
| if (current != (byte)'\r') | ||
| { | ||
| normalized.Add(current); | ||
| continue; | ||
| } | ||
|
|
||
| normalized.Add((byte)'\n'); | ||
| if (index + 1 < content.Length && content[index + 1] == (byte)'\n') | ||
| { | ||
| index++; | ||
| } | ||
| } | ||
|
|
||
| return normalized.ToArray(); | ||
| } | ||
|
|
||
| private static byte[] NormalizeUtf16LineEndings(byte[] content, bool isLittleEndian) | ||
| { | ||
| List<byte> normalized = new(content.Length); | ||
| int index = 0; | ||
| if (HasMatchingUtf16Bom(content, isLittleEndian)) | ||
| { | ||
| normalized.Add(content[0]); | ||
| normalized.Add(content[1]); | ||
| index = Utf16CodeUnitByteCount; | ||
| } | ||
|
|
||
| while (index + 1 < content.Length) | ||
| { | ||
| ushort codeUnit = ReadUtf16CodeUnit(content, index, isLittleEndian); | ||
| if (codeUnit == CarriageReturnCodeUnit) | ||
| { | ||
| WriteUtf16CodeUnit(normalized, LineFeedCodeUnit, isLittleEndian); | ||
| int nextIndex = index + Utf16CodeUnitByteCount; | ||
| if (nextIndex + 1 < content.Length && | ||
| ReadUtf16CodeUnit(content, nextIndex, isLittleEndian) == LineFeedCodeUnit) | ||
| { | ||
| index += Utf16CodeUnitByteCount * 2; | ||
| continue; | ||
| } | ||
|
|
||
| index += Utf16CodeUnitByteCount; | ||
| continue; | ||
| } | ||
|
|
||
| WriteUtf16CodeUnit(normalized, codeUnit, isLittleEndian); | ||
| index += Utf16CodeUnitByteCount; | ||
| } | ||
|
|
||
| if (index < content.Length) | ||
| { | ||
| normalized.Add(content[index]); | ||
| } | ||
|
|
||
| return normalized.ToArray(); | ||
| } | ||
|
|
||
| private static bool HasUtf16LittleEndianBom(byte[] content) | ||
| { | ||
| return content.Length >= Utf16CodeUnitByteCount && | ||
| content[0] == Utf16LittleEndianBomFirstByte && | ||
| content[1] == Utf16LittleEndianBomSecondByte; | ||
| } | ||
|
|
||
| private static bool HasUtf16BigEndianBom(byte[] content) | ||
| { | ||
| return content.Length >= Utf16CodeUnitByteCount && | ||
| content[0] == Utf16BigEndianBomFirstByte && | ||
| content[1] == Utf16BigEndianBomSecondByte; | ||
| } | ||
|
|
||
| private static bool HasMatchingUtf16Bom(byte[] content, bool isLittleEndian) | ||
| { | ||
| return isLittleEndian ? HasUtf16LittleEndianBom(content) : HasUtf16BigEndianBom(content); | ||
| } | ||
|
|
||
| private static bool HasUtf16LittleEndianLineEnding(byte[] content) | ||
| { | ||
| return HasUtf16LineEnding(content, isLittleEndian: true); | ||
| } | ||
|
|
||
| private static bool HasUtf16BigEndianLineEnding(byte[] content) | ||
| { | ||
| return HasUtf16LineEnding(content, isLittleEndian: false); | ||
| } | ||
|
|
||
| private static bool HasUtf16LineEnding(byte[] content, bool isLittleEndian) | ||
| { | ||
| int startIndex = HasMatchingUtf16Bom(content, isLittleEndian) ? Utf16CodeUnitByteCount : 0; | ||
| for (int index = startIndex; index + 1 < content.Length; index += Utf16CodeUnitByteCount) | ||
| { | ||
| ushort codeUnit = ReadUtf16CodeUnit(content, index, isLittleEndian); | ||
| if (codeUnit == CarriageReturnCodeUnit || codeUnit == LineFeedCodeUnit) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static ushort ReadUtf16CodeUnit(byte[] content, int index, bool isLittleEndian) | ||
| { | ||
| return isLittleEndian | ||
| ? (ushort)(content[index] | (content[index + 1] << 8)) | ||
| : (ushort)((content[index] << 8) | content[index + 1]); | ||
| } | ||
|
|
||
| private static void WriteUtf16CodeUnit(List<byte> output, ushort codeUnit, bool isLittleEndian) | ||
| { | ||
| if (isLittleEndian) | ||
| { | ||
| output.Add((byte)(codeUnit & 0xFF)); | ||
| output.Add((byte)(codeUnit >> 8)); | ||
| return; | ||
| } | ||
|
|
||
| output.Add((byte)(codeUnit >> 8)); | ||
| output.Add((byte)(codeUnit & 0xFF)); | ||
| } | ||
| } | ||
| } | ||
11 changes: 11 additions & 0 deletions
11
Packages/src/Editor/Infrastructure/SkillSetup/SkillFileContentNormalizer.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.