-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathSyncSolutionUtilities.cs
92 lines (79 loc) · 2.59 KB
/
SyncSolutionUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace EditorUtilities.Solution
{
public static class SyncSolutionUtilities
{
private static Type _syncVSType;
private static MethodInfo _syncSolutionMethodInfo;
private static FieldInfo _synchronizerField;
private static object _synchronizerObject;
private static Type _synchronizerType;
private static MethodInfo _synchronizerSyncMethodInfo;
static SyncSolutionUtilities()
{
_syncVSType = Type.GetType("UnityEditor.SyncVS,UnityEditor");
_synchronizerField = _syncVSType.GetField("Synchronizer", BindingFlags.NonPublic | BindingFlags.Static);
_syncSolutionMethodInfo = _syncVSType.GetMethod("SyncSolution", BindingFlags.Public | BindingFlags.Static);
_synchronizerObject = _synchronizerField.GetValue(_syncVSType);
_synchronizerType = _synchronizerObject.GetType();
_synchronizerSyncMethodInfo = _synchronizerType.GetMethod("Sync", BindingFlags.Public | BindingFlags.Instance);
}
[MenuItem("Assets/Sync C# Solution", priority = 1000000)]
public static void Sync()
{
Sync(true);
}
public static void Sync(bool logsEnabled)
{
CleanOldFiles(logsEnabled);
Call_SyncSolution(logsEnabled);
Call_SynchronizerSync(logsEnabled);
}
private static void CleanOldFiles(bool logsEnabled)
{
DirectoryInfo assetsDirectoryInfo = new DirectoryInfo(Application.dataPath);
DirectoryInfo projectDirectoryInfo = assetsDirectoryInfo.Parent;
IEnumerable<FileInfo> files = GetFilesByExtensions(projectDirectoryInfo, "*.sln", "*.csproj");
foreach(FileInfo file in files)
{
if(logsEnabled)
{
Debug.Log($"Remove old solution file: {file.Name}");
}
file.Delete();
}
}
private static void Call_SyncSolution(bool logsEnabled)
{
if(logsEnabled)
{
Debug.Log($"Coll method: SyncVS.Sync()");
}
_syncSolutionMethodInfo.Invoke(null, null);
}
private static void Call_SynchronizerSync(bool logsEnabled)
{
if(logsEnabled)
{
Debug.Log($"Coll method: SyncVS.Synchronizer.Sync()");
}
_synchronizerSyncMethodInfo.Invoke(_synchronizerObject, null);
}
private static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
{
extensions = extensions ?? new []{"*"};
IEnumerable<FileInfo> files = Enumerable.Empty<FileInfo>();
foreach(string ext in extensions)
{
files = files.Concat(dir.GetFiles(ext));
}
return files;
}
}
}