-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCloneManager.cs
265 lines (220 loc) · 11.7 KB
/
CloneManager.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace CloneProject {
public static class CloneManager {
public static Project GetCurrentProject() {
string pathString = GetCurrentProjectPath();
var path = Path.Combine(pathString, CloneConfig.ConfigPath);
if (File.Exists(path)) {
var json = File.ReadAllText(path);
var project = JsonUtility.FromJson<Project>(json);
CheckClones(project);
return project;
}
return new Project(pathString);
}
public static void CheckClones(Project project) {
if ( project.Clones is not { Length: > 0 } ) return;
List<Project> temp = new List<Project>();
foreach ( var clone in project.Clones ) {
if ( Directory.Exists(clone.ProjectPath) ) {
temp.Add(clone);
}
}
project.Clones = temp.ToArray();
ReSaveConfig(project);
}
public static string GetCurrentProjectPath() {
return Application.dataPath.Replace("/Assets", "");
}
public static Project AddExistingClone() {
var path = EditorUtility.OpenFilePanel("Add Existing Clone", GetCurrentProjectPath(), "clone");
if ( !File.Exists(path) ) return null;
var json = File.ReadAllText(path);
return JsonUtility.FromJson<Project>(json);
}
public static Project CreateCloneFromCurrent(Project sourceProject) {
var parentProject = new DirectoryInfo(sourceProject.ProjectPath);
var name = parentProject.Name + $"_Clone{sourceProject.ClonedCount() + 1}";
var clonePath = EditorUtility.SaveFilePanel("Create Clone", parentProject.Parent.FullName, name, "");
if ( string.IsNullOrEmpty(clonePath) ) return null;
var cloneProject = CreateCloneFromPath(sourceProject.ProjectPath, clonePath);
sourceProject.AddCloneProject(cloneProject);
ReSaveConfig(sourceProject);
return cloneProject;
}
public static Project CreateCloneFromPath(string sourcePath, string clonePath) {
Project cloneProject = new Project(sourcePath, clonePath);
Directory.CreateDirectory(cloneProject.ProjectPath);
DirectoryInfo directory = new DirectoryInfo(sourcePath);
// foreach ( var info in directory.GetFiles() ) {
// var linkFile = Path.Combine(clonePath, info.Name);
// if ( CloneConfig.CopyPaths.Contains(info.Name) ) CopyDirectoryWithProgressBar(info.FullName, linkFile);
// else LinkFolders(info.FullName, linkFile);
// }
foreach ( var info in directory.GetDirectories() ) {
var linkDir = Path.Combine(clonePath, info.Name);
if ( CloneConfig.CopyPaths.Contains(info.Name) ) CopyDirectoryWithProgressBar(info.FullName, linkDir);
else LinkFolders(info.FullName, linkDir);
}
ReSaveConfig(cloneProject);
return cloneProject;
}
public static void CopyDirectoryWithProgressBar(string sourcePath, string destinationPath, string progressBarPrefix = "")
{
var source = new DirectoryInfo(sourcePath);
var destination = new DirectoryInfo(destinationPath);
long totalBytes = 0;
long copiedBytes = 0;
CopyDirectoryWithProgressBarRecursive(source, destination, ref totalBytes, ref copiedBytes, progressBarPrefix);
EditorUtility.ClearProgressBar();
}
private static void CopyDirectoryWithProgressBarRecursive(DirectoryInfo source, DirectoryInfo destination,
ref long totalBytes, ref long copiedBytes,
string progressBarPrefix = "") {
if ( source.FullName.ToLower() == destination.FullName.ToLower() ) {
UnityEngine.Debug.LogError("Source Path And Destination Path Is Same!");
return;
}
if ( totalBytes == 0 ) {
totalBytes = GetDirectorySize(source, true, progressBarPrefix);
}
if ( !Directory.Exists(destination.FullName) ) {
Directory.CreateDirectory(destination.FullName);
}
foreach ( FileInfo file in source.GetFiles() ) {
try {
file.CopyTo(Path.Combine(destination.ToString(), file.Name), true);
}
catch ( IOException ) {
}
copiedBytes += file.Length;
float progress = (float)copiedBytes / totalBytes;
bool cancelCopy = EditorUtility.DisplayCancelableProgressBar($"{progressBarPrefix} Clone:{source.FullName} To {destination.FullName}...", $"({progress * 100f:F2}%) Copying {file.Name}", progress);
if ( cancelCopy ) return;
}
foreach ( DirectoryInfo info in source.GetDirectories() ) {
DirectoryInfo directory = destination.CreateSubdirectory(info.Name);
CopyDirectoryWithProgressBarRecursive(info, directory, ref totalBytes, ref copiedBytes, progressBarPrefix);
}
}
private static long GetDirectorySize(DirectoryInfo directory, bool includeNested = false, string progressBarPrefix = "") {
EditorUtility.DisplayProgressBar(progressBarPrefix + "Calculating size of directories...", "Scanning: " + directory.FullName , 0f);
long filesSize = directory.GetFiles().Sum(file => file.Length);
if ( !includeNested ) return filesSize;
long directoriesSize = 0;
foreach ( DirectoryInfo info in directory.GetDirectories() ) {
directoriesSize += GetDirectorySize(info, true, progressBarPrefix);
}
return filesSize + directoriesSize;
}
public static void LinkFolders(string sourcePath, string destinationPath) {
if ( !Directory.Exists(sourcePath) || Directory.Exists(destinationPath) ) {
UnityEngine.Debug.LogWarning("Check SourcePath Is No Exists, Or DestinationPath Is Exists");
return;
}
switch ( Application.platform ) {
case RuntimePlatform.WindowsEditor:
CreateLinkWin(sourcePath, destinationPath);
break;
case RuntimePlatform.OSXEditor:
CreateLinkMac(sourcePath, destinationPath);
break;
default:
UnityEngine.Debug.LogWarning("Not in a known editor. Application.platform: " + Application.platform);
break;
}
}
private static void CreateLinkWin(string sourcePath, string destinationPath)
{
string cmd = $"/C mklink /J \"{destinationPath}\" \"{sourcePath}\"";
StartHiddenConsoleProcess("cmd.exe", cmd);
}
private static void CreateLinkMac(string sourcePath, string destinationPath)
{
sourcePath = sourcePath.Replace(" ", "\\ ");
destinationPath = destinationPath.Replace(" ", "\\ ");
var command = $"ln -s {sourcePath} {destinationPath}";
ExecuteBashCommand(command);
}
private static void ExecuteBashCommand(string command) {
command = command.Replace("\"", "\"\"");
var proc = new System.Diagnostics.Process {
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "/bin/bash",
Arguments = "-c \"" + command + "\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
using ( proc ) {
proc.Start();
proc.WaitForExit();
if ( !proc.StandardError.EndOfStream ) {
UnityEngine.Debug.LogError(proc.StandardError.ReadToEnd());
}
}
}
/// <summary>
/// Registers a clone by placing an identifying ".clone" file in its root directory.
/// </summary>
/// <param name="cloneProject"></param>
public static void ReSaveConfig(Project cloneProject) {
string configFile = Path.Combine(cloneProject.ProjectPath, CloneConfig.ConfigPath);
File.WriteAllText(configFile, JsonUtility.ToJson(cloneProject));
}
public static bool CheckProjectIsOpen(string projectPath) {
return File.Exists(Path.Combine(projectPath, CloneConfig.LockFilePath));
}
public static void OpenProjectInFileExplorer(string projectPath) {
System.Diagnostics.Process.Start(projectPath);
}
public static void OpenProject(string projectPath) {
if ( !Directory.Exists(projectPath) ) {
UnityEngine.Debug.LogError("Path Is No Exists: "+ projectPath);
return;
}
if ( CheckProjectIsOpen(projectPath) ) {
UnityEngine.Debug.LogError("Project Is Opening: "+ projectPath);
return;
}
string fileName = GetApplicationPath();
string args = "-projectPath \"" + projectPath + "\"";
StartHiddenConsoleProcess(fileName, args);
}
private static string GetApplicationPath() {
switch ( Application.platform ) {
case RuntimePlatform.WindowsEditor: return EditorApplication.applicationPath;
case RuntimePlatform.OSXEditor: return EditorApplication.applicationPath + "/Contents/MacOS/Unity";
default: throw new System.NotImplementedException("Platform has not supported yet ;(");
}
}
private static void StartHiddenConsoleProcess(string fileName, string args) {
Debug.Log("Process.Start: " + fileName + " -> " +args);
System.Diagnostics.Process.Start(fileName, args);
}
public static void DeleteClone(string projectPath) {
if ( string.IsNullOrEmpty(projectPath) ) return;
File.Delete(Path.Combine(projectPath, CloneConfig.ConfigPath));
switch ( Application.platform ) {
case RuntimePlatform.WindowsEditor:
string args = $"/c rmdir /s/q \"{projectPath}\"";
StartHiddenConsoleProcess("cmd.exe", args);
UnityEngine.Debug.Log("Attempting to delete folder \"" + projectPath + "\"");
break;
case RuntimePlatform.OSXEditor:
FileUtil.DeleteFileOrDirectory(projectPath);
UnityEngine.Debug.Log("Attempting to delete folder \"" + projectPath + "\"");
break;
default:
UnityEngine.Debug.LogWarning("Not in a known editor. Where are you!?");
break;
}
}
}
}