33using UnityEditor ;
44using 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}
0 commit comments