Skip to content

Commit 4987676

Browse files
committed
EOL Conversion now automatically processes files.
Namespace Processor respects this and will convert too.
1 parent ea13be2 commit 4987676

File tree

4 files changed

+82
-22
lines changed

4 files changed

+82
-22
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
exclude: [ ]
3030
- name: Auto Namespace
3131
package: lachee-utilities-only-auto-namespace.unitypackage
32-
assets: [ "Editor/Tools/Namespace*.cs", "Editor/Icons/Namespace*.png" ]
32+
assets: [ "Editor/Tools/Namespace*.cs", "Editor/Icons/Namespace*.png", "Editor/Tools/EOLConversion.cs" ]
3333
- name: End Of Line Converter
3434
package: lachee-utilities-only-eol-converter.unitypackage
3535
assets: [ "Editor/Tools/EOLConversion.cs" ]

Editor/Tools/EOLConversion.cs

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,104 @@
33
using UnityEditor;
44
using System.IO;
55

6-
namespace Lachee.Tools.Editor
6+
namespace Lachee.Tools.Editor
77
{
88
/// <summary>
99
/// Provides tools to convert line endings
1010
/// </summary>
11-
public class EOLConversion
11+
public class EOLConversion : UnityEditor.AssetModificationProcessor
1212
{
13+
public const string PREFS_PREFERED = "prefered_eol";
14+
public const string PREFS_PROCESS = "process_eol";
15+
1316
[MenuItem("Tools/EOL Conversion/Windows")]
1417
private static void ConvertToWindows()
1518
{
19+
EditorPrefs.SetString(PREFS_PREFERED, "\r\n");
1620
Convert("\r\n");
1721
}
1822

19-
23+
2024
[MenuItem("Tools/EOL Conversion/Unix")]
2125
private static void ConvertToUnix()
2226
{
27+
EditorPrefs.SetString(PREFS_PREFERED, "\n");
2328
Convert("\n");
2429
}
2530

31+
[MenuItem("Tools/EOL Conversion/Automaticly Process")]
32+
private static void ToggleProcessing()
33+
{
34+
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
35+
EditorPrefs.SetBool(PREFS_PROCESS, !auto);
36+
}
37+
38+
[MenuItem("Tools/EOL Conversion/Automaticly Process", true)]
39+
private static bool ToogleProcessingValidation()
40+
{
41+
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
42+
Menu.SetChecked("Tools/EOL Conversion/Automaticly Process", auto);
43+
return true;
44+
}
45+
46+
/// <summary>
47+
/// This gets called for every .meta file created by the Editor.
48+
/// </summary>
49+
public static void OnWillCreateAsset(string path)
50+
{
51+
bool auto = EditorPrefs.GetBool(PREFS_PROCESS, true);
52+
if (!auto) return;
53+
54+
path = path.Replace(".meta", string.Empty);
55+
if (!path.EndsWith(".cs"))
56+
return;
57+
58+
if (!EditorPrefs.HasKey(PREFS_PREFERED))
59+
return;
60+
61+
string lineEnding = EditorPrefs.GetString(PREFS_PREFERED);
62+
if (ConvertFile(path, lineEnding))
63+
AssetDatabase.Refresh();
64+
}
65+
2666
/// <summary> Converts all the assets and returns a list of files that were modified </summary>
27-
public static string[] Convert(string lineEnding) {
67+
public static string[] Convert(string lineEnding)
68+
{
2869
List<string> assetsConverted = new List<string>();
2970
string[] assetPaths = AssetDatabase.GetAllAssetPaths();
3071
int progress = 0;
3172

32-
foreach(string assetPath in assetPaths)
73+
foreach (string assetPath in assetPaths)
3374
{
3475
EditorUtility.DisplayProgressBar("Converting Line Ending", assetPath, (progress++ / (float)assetPaths.Length));
76+
if (ConvertFile(assetPath, lineEnding))
77+
assetsConverted.Add(assetPath);
78+
}
79+
80+
EditorUtility.ClearProgressBar();
81+
return assetsConverted.ToArray();
82+
}
3583

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

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

40-
string content = File.ReadAllText(assetPath);
41-
string contentNew = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);
94+
string content = File.ReadAllText(path);
95+
string contentNew = Regex.Replace(content, @"\r\n|\n\r|\n|\r", lineEnding);
4296

43-
if (content != contentNew) {
44-
File.WriteAllText(assetPath, contentNew);
45-
assetsConverted.Add(assetPath);
46-
}
97+
if (content != contentNew)
98+
{
99+
File.WriteAllText(path, contentNew);
100+
return true;
47101
}
48-
49-
EditorUtility.ClearProgressBar();
50-
return assetsConverted.ToArray();
102+
103+
return false;
51104
}
52105
}
53-
54106
}

Editor/Tools/NamespaceProcessor.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ namespace Lachee.Tools.Editor
1515
/// </summary>
1616
public class NamespaceProcessor : UnityEditor.AssetModificationProcessor
1717
{
18-
18+
private static string EOL => EditorPrefs.GetString("prefered_eol", "\r\n");
1919
private readonly static Regex _namespaceRegex = new Regex(@"namespace\s(\s?[a-zA-Z]+[0-9]*\.?)*", RegexOptions.Compiled);
2020

21+
static NamespaceProcessor()
22+
{
23+
if (!EditorPrefs.HasKey(EOLConversion.PREFS_PROCESS))
24+
EditorPrefs.SetBool(EOLConversion.PREFS_PROCESS, false);
25+
}
26+
2127
/// <summary>
2228
/// This gets called for every .meta file created by the Editor.
2329
/// </summary>
@@ -36,6 +42,7 @@ public static void OnWillCreateAsset(string path)
3642
if (configuration.FormatDocument)
3743
FormatScripts(path);
3844

45+
EOLConversion.ConvertFile(path);
3946
AssetDatabase.Refresh();
4047
}
4148

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

55+
string eol = EOL;
4856
string contents = File.ReadAllText(asset);
4957
if (contents.Contains("namespace"))
5058
{
@@ -54,8 +62,8 @@ public static void SetNamespace(string asset, string @namespace)
5462
else
5563
{
5664
int index = FindIndexOfLastImport(contents);
57-
contents = contents.Insert(index, "\nnamespace " + @namespace + " {");
58-
contents += "\n}";
65+
contents = contents.Insert(index, eol + "namespace " + @namespace + " {");
66+
contents += eol + "}";
5967
}
6068
File.WriteAllText(asset, contents);
6169
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)