-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
FileTools.cs
276 lines (244 loc) · 10.2 KB
/
FileTools.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
266
267
268
269
270
271
272
273
274
275
276
/*
MIT License
Copyright (c) 2020 Jeff Campbell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
namespace JCMG.PackageTools.Editor
{
/// <summary>
/// Helper methods for dealing with files/directories
/// </summary>
internal static class FileTools
{
/// <summary>
/// Converts <paramref name="fullFilePath"/> into a relative file path from <paramref name="referencePath"/>.
/// </summary>
/// <param name="fullFilePath"></param>
/// <param name="referencePath"></param>
/// <returns></returns>
public static string ConvertToRelativePath(string fullFilePath, string referencePath)
{
var fileUri = new Uri(fullFilePath);
var referenceUri = new Uri(referencePath);
return referenceUri.MakeRelativeUri(fileUri).ToString();
}
/// <summary>
/// Creates or updates the existing package contents of <see cref="PackageManifestConfig"/>
/// <paramref name="packageManifest"/>.
/// </summary>
/// <param name="packageManifest"></param>
public static void CreateOrUpdatePackageSource(PackageManifestConfig packageManifest)
{
if (!Application.isBatchMode)
{
EditorUtility.DisplayProgressBar(EditorConstants.PROGRESS_BAR_TITLE, string.Empty, 0f);
}
try
{
// Created the folders up to the package json path and then create/import the package.json file.
// Its important to have the package.json in the Unity project so that it will have a meta file
// Packages without meta files cause warnings/errors when imported.
var packageManifestAssetPath = AssetDatabase.GetAssetPath(packageManifest);
var parentManifestParentFolderAssetPath = packageManifestAssetPath
.Replace(packageManifest.name, string.Empty)
.Replace(EditorConstants.ASSET_EXTENSION, string.Empty);
var generatedFolderAssetPath = Path.Combine(parentManifestParentFolderAssetPath, EditorConstants.GENERATED_FOLDER_NAME);
var fullGeneratedFolderAssetPath = Path.GetFullPath(generatedFolderAssetPath);
if (!Directory.Exists(fullGeneratedFolderAssetPath))
{
Directory.CreateDirectory(fullGeneratedFolderAssetPath);
}
var packageJsonFolderAssetPath = Path.Combine(generatedFolderAssetPath, packageManifest.Id);
var fullPackageJsonFolderAssetPath = Path.GetFullPath(packageJsonFolderAssetPath);
if (!Directory.Exists(fullPackageJsonFolderAssetPath))
{
Directory.CreateDirectory(fullPackageJsonFolderAssetPath);
}
var packageJsonAssetPath = Path.Combine(packageJsonFolderAssetPath, EditorConstants.PACKAGE_JSON_FILENAME);
var fullPackageJsonAssetPath = Path.GetFullPath(packageJsonAssetPath);
File.WriteAllText(fullPackageJsonAssetPath, packageManifest.GenerateJson());
AssetDatabase.ImportAsset(packageJsonAssetPath, ImportAssetOptions.ForceUpdate);
// If the directory exists, delete its contents or make the directory.
if (Directory.Exists(packageManifest.packageDestinationPath))
{
RecursivelyDeleteDirectoryContents(new DirectoryInfo(packageManifest.packageDestinationPath));
}
else
{
Directory.CreateDirectory(packageManifest.packageDestinationPath);
}
// Copy over the package json and meta file
var destinationPackageJsonPath =
Path.Combine(packageManifest.packageDestinationPath, EditorConstants.PACKAGE_JSON_FILENAME);
File.Copy(fullPackageJsonAssetPath, destinationPackageJsonPath);
var packageJsonMetaPath = string.Format(EditorConstants.META_FORMAT, fullPackageJsonAssetPath);
File.Copy(packageJsonMetaPath,
Path.Combine(packageManifest.packageDestinationPath,
string.Format(EditorConstants.META_FORMAT, EditorConstants.PACKAGE_JSON_FILENAME)));
// Copy over all directory and file content from source to destination.
var normalizedDestinationPath = Path.GetFullPath(packageManifest.packageDestinationPath);
foreach (var sourcePath in packageManifest.packageSourcePaths)
{
// If its a file, copy over it and its meta file if it exists.
var normalizedSourcePath = Path.GetFullPath(sourcePath);
if (IsFile(normalizedSourcePath) && File.Exists(normalizedSourcePath))
{
var fileInfo = new FileInfo(normalizedSourcePath);
if (fileInfo.Directory == null)
{
continue;
}
var parentDirectoryPath = fileInfo.Directory.FullName;
var newPath = normalizedSourcePath.Replace(parentDirectoryPath, normalizedDestinationPath);
File.Copy(normalizedSourcePath, newPath);
var sourceMetaPath = string.Format(EditorConstants.META_FORMAT, normalizedSourcePath);
if (File.Exists(sourceMetaPath))
{
var newMetaPath = sourceMetaPath.Replace(parentDirectoryPath, normalizedDestinationPath);
File.Copy(sourceMetaPath, newMetaPath);
}
}
// Otherwise if this is a folder, copy it and all the contents over to the destination folder.
else
{
RecursivelyCopyDirectoriesAndFiles(
packageManifest,
new DirectoryInfo(normalizedSourcePath),
normalizedSourcePath,
normalizedDestinationPath);
}
}
Debug.LogFormat(EditorConstants.PACKAGE_UPDATE_SUCCESS_FORMAT, packageManifest.packageName);
if (!Application.isBatchMode)
{
EditorUtility.RevealInFinder(destinationPackageJsonPath);
}
}
catch (Exception ex)
{
Debug.LogErrorFormat(EditorConstants.PACKAGE_UPDATE_ERROR_FORMAT, packageManifest.packageName);
Debug.LogErrorFormat(packageManifest, ex.ToString());
}
finally
{
if (!Application.isBatchMode)
{
EditorUtility.DisplayProgressBar(EditorConstants.PROGRESS_BAR_TITLE, string.Empty, 1f);
EditorUtility.ClearProgressBar();
}
}
}
/// <summary>
/// Returns true if the <paramref name="path"/> is for a file, otherwise false.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static bool IsFile(string path)
{
var attr = File.GetAttributes(path);
return (attr & FileAttributes.Directory) != FileAttributes.Directory;
}
/// <summary>
/// Recursively copies all sub-folders and files in <see cref="DirectoryInfo"/> <paramref name="directoryInfo"/>
/// from parent folder <see cref="sourcePath"/> to <paramref name="destinationPath"/>.
/// </summary>
/// <param name="packageManifest"></param>
/// <param name="directoryInfo"></param>
/// <param name="sourcePath"></param>
/// <param name="destinationPath"></param>
private static void RecursivelyCopyDirectoriesAndFiles(
PackageManifestConfig packageManifest,
DirectoryInfo directoryInfo,
string sourcePath,
string destinationPath)
{
var normalizedSourcePath = Path.GetFullPath(sourcePath);
var normalizedDestinationPath = Path.GetFullPath(destinationPath);
var subDirectoryInfo = directoryInfo.GetDirectories(EditorConstants.WILDCARD_FILTER);
foreach (var sdi in subDirectoryInfo)
{
// If any of the paths we're looking at match the ignore paths from the user, skip them
if (packageManifest.packageIgnorePaths.Any(x =>
sdi.FullName.Contains(Path.GetFullPath(Path.Combine(EditorConstants.PROJECT_PATH, x)))))
{
continue;
}
Directory.CreateDirectory(sdi.FullName.Replace(normalizedSourcePath, normalizedDestinationPath));
RecursivelyCopyDirectoriesAndFiles(packageManifest, sdi, normalizedSourcePath, normalizedDestinationPath);
}
var fileInfo = directoryInfo.GetFiles(EditorConstants.WILDCARD_FILTER);
foreach (var fi in fileInfo)
{
// If any of the paths we're looking at match the ignore paths from the user, skip them
if (packageManifest.packageIgnorePaths.Any(x =>
fi.FullName.Contains(Path.GetFullPath(Path.Combine(EditorConstants.PROJECT_PATH, x)))))
{
continue;
}
var newPath = Path.GetFullPath(fi.FullName).Replace(normalizedSourcePath, normalizedDestinationPath);
File.Copy(fi.FullName, newPath);
}
}
/// <summary>
/// Recursively deletes all sub-folders (excluding hidden folders) and files in <see cref="DirectoryInfo"/>
/// <paramref name="directoryInfo"/>.
/// </summary>
/// <param name="directoryInfo"></param>
private static void RecursivelyDeleteDirectoryContents(DirectoryInfo directoryInfo)
{
var subDirectoryInfo = directoryInfo.GetDirectories(EditorConstants.WILDCARD_FILTER);
foreach (var sdi in subDirectoryInfo)
{
if ((sdi.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
{
sdi.Delete(true);
}
}
var fileInfo = directoryInfo.GetFiles(EditorConstants.WILDCARD_FILTER);
foreach (var fi in fileInfo)
{
fi.Delete();
}
}
/// <summary>
/// Recursive find all files starting at root folder at path <paramref name="folderPath"/>
/// and return a list of absolute paths to those files.
/// </summary>
/// <param name="folderPath"></param>
/// <returns></returns>
internal static IEnumerable<string> GetAllFilesRecursively(string folderPath)
{
var filePaths = new List<string>();
var fullPath = Path.GetFullPath(folderPath);
filePaths.AddRange(Directory.GetFiles(
fullPath,
EditorConstants.WILDCARD_FILTER,
SearchOption.AllDirectories));
filePaths.AddRange(Directory.GetDirectories(
folderPath,
EditorConstants.WILDCARD_FILTER,
SearchOption.AllDirectories));
return filePaths.Distinct().OrderBy(x => x);
}
}
}