Skip to content

Commit

Permalink
EOL Conversion now automatically processes files.
Browse files Browse the repository at this point in the history
Namespace Processor respects this and will convert too.
  • Loading branch information
Lachee committed Oct 19, 2022
1 parent ea13be2 commit 4987676
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
exclude: [ ]
- name: Auto Namespace
package: lachee-utilities-only-auto-namespace.unitypackage
assets: [ "Editor/Tools/Namespace*.cs", "Editor/Icons/Namespace*.png" ]
assets: [ "Editor/Tools/Namespace*.cs", "Editor/Icons/Namespace*.png", "Editor/Tools/EOLConversion.cs" ]
- name: End Of Line Converter
package: lachee-utilities-only-eol-converter.unitypackage
assets: [ "Editor/Tools/EOLConversion.cs" ]
Expand Down
86 changes: 69 additions & 17 deletions Editor/Tools/EOLConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,104 @@
using UnityEditor;
using System.IO;

namespace Lachee.Tools.Editor
namespace Lachee.Tools.Editor
{
/// <summary>
/// Provides tools to convert line endings
/// </summary>
public class EOLConversion
public class EOLConversion : UnityEditor.AssetModificationProcessor
{
public const string PREFS_PREFERED = "prefered_eol";
public const string PREFS_PROCESS = "process_eol";

[MenuItem("Tools/EOL Conversion/Windows")]
private static void ConvertToWindows()
{
EditorPrefs.SetString(PREFS_PREFERED, "\r\n");
Convert("\r\n");
}


[MenuItem("Tools/EOL Conversion/Unix")]
private static void ConvertToUnix()
{
EditorPrefs.SetString(PREFS_PREFERED, "\n");
Convert("\n");
}

[MenuItem("Tools/EOL Conversion/Automaticly Process")]
private static void ToggleProcessing()
{
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
EditorPrefs.SetBool(PREFS_PROCESS, !auto);
}

[MenuItem("Tools/EOL Conversion/Automaticly Process", true)]
private static bool ToogleProcessingValidation()
{
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
Menu.SetChecked("Tools/EOL Conversion/Automaticly Process", auto);
return true;
}

/// <summary>
/// This gets called for every .meta file created by the Editor.
/// </summary>
public static void OnWillCreateAsset(string path)
{
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
if (!auto) return;

path = path.Replace(".meta", string.Empty);
if (!path.EndsWith(".cs"))
return;

if (!EditorPrefs.HasKey(PREFS_PREFERED))
return;

string lineEnding = EditorPrefs.GetString(PREFS_PREFERED);
if (ConvertFile(path, lineEnding))
AssetDatabase.Refresh();
}

/// <summary> Converts all the assets and returns a list of files that were modified </summary>
public static string[] Convert(string lineEnding) {
public static string[] Convert(string lineEnding)
{
List<string> assetsConverted = new List<string>();
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
int progress = 0;

foreach(string assetPath in assetPaths)
foreach (string assetPath in assetPaths)
{
EditorUtility.DisplayProgressBar("Converting Line Ending", assetPath, (progress++ / (float)assetPaths.Length));
if (ConvertFile(assetPath, lineEnding))
assetsConverted.Add(assetPath);
}

EditorUtility.ClearProgressBar();
return assetsConverted.ToArray();
}

if(!assetPath.EndsWith(".cs")) continue;
if (assetPath.StartsWith("Packages/")) continue;
/// <summary>Converts a single file's line ending</summary>
public static bool ConvertFile(string path)
=> ConvertFile(path, EditorPrefs.GetString(PREFS_PREFERED, "\r\n"));

/// <summary>Converts a single file's line ending</summary>
public static bool ConvertFile(string path, string lineEnding)
{
if (!path.EndsWith(".cs") || path.StartsWith("Packages/"))
return false;

string content = File.ReadAllText(assetPath);
string contentNew = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);
string content = File.ReadAllText(path);
string contentNew = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);

if (content != contentNew) {
File.WriteAllText(assetPath, contentNew);
assetsConverted.Add(assetPath);
}
if (content != contentNew)
{
File.WriteAllText(path, contentNew);
return true;
}

EditorUtility.ClearProgressBar();
return assetsConverted.ToArray();

return false;
}
}

}
14 changes: 11 additions & 3 deletions Editor/Tools/NamespaceProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ namespace Lachee.Tools.Editor
/// </summary>
public class NamespaceProcessor : UnityEditor.AssetModificationProcessor
{

private static string EOL => EditorPrefs.GetString("prefered_eol", "\r\n");
private readonly static Regex _namespaceRegex = new Regex(@"namespace\s(\s?[a-zA-Z]+[0-9]*\.?)*", RegexOptions.Compiled);

static NamespaceProcessor()
{
if (!EditorPrefs.HasKey(EOLConversion.PREFS_PROCESS))
EditorPrefs.SetBool(EOLConversion.PREFS_PROCESS, false);
}

/// <summary>
/// This gets called for every .meta file created by the Editor.
/// </summary>
Expand All @@ -36,6 +42,7 @@ public static void OnWillCreateAsset(string path)
if (configuration.FormatDocument)
FormatScripts(path);

EOLConversion.ConvertFile(path);
AssetDatabase.Refresh();
}

Expand All @@ -45,6 +52,7 @@ public static void SetNamespace(string asset, string @namespace)
if (Path.GetExtension(asset) != ".cs")
throw new System.ArgumentException("Asset must be a cs file", "asset");

string eol = EOL;
string contents = File.ReadAllText(asset);
if (contents.Contains("namespace"))
{
Expand All @@ -54,8 +62,8 @@ public static void SetNamespace(string asset, string @namespace)
else
{
int index = FindIndexOfLastImport(contents);
contents = contents.Insert(index, "\nnamespace " + @namespace + " {");
contents += "\n}";
contents = contents.Insert(index, eol + "namespace " + @namespace + " {");
contents += eol + "}";
}
File.WriteAllText(asset, contents);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.lachee.utilities",
"version": "1.4.0",
"version": "1.4.1",
"displayName": "Lachee's Utilities",
"description": "Bunch of utility functionality",
"unity": "2019.1",
Expand Down

0 comments on commit 4987676

Please sign in to comment.