summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Editor/ImportSampleMenu.cs
blob: c53fc9b229c4169b7f2fdfe737c9301ff462da09 (plain)
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
#if !UNITY_2019_1_OR_NEWER
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEditor;

namespace Coffee.UIEffects
{
    public static class ImportSampleMenu
    {
        private const string jsonGuid = "546af75b6221c4768be79d67c9cea1fb";

        [MenuItem("Assets/Samples/UIEffect/Import Demo")]
        private static void ImportDemo()
        {
            ImportSample(jsonGuid, "Demo");
        }

        private static void ImportSample(string jsonGuid, string sampleName)
        {
            var jsonPath = AssetDatabase.GUIDToAssetPath(jsonGuid);
            var json = File.ReadAllText(jsonPath);
            var version = Regex.Match(json, "\"version\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value;
            var displayName = Regex.Match(json, "\"displayName\"\\s*:\\s*\"([^\"]+)\"").Groups[1].Value;
            var src = string.Format("{0}/Samples~/{1}", Path.GetDirectoryName(jsonPath), sampleName);
            var srcAlt = string.Format("{0}/Samples/{1}", Path.GetDirectoryName(jsonPath), sampleName);
            var dst = string.Format("Assets/Samples/{0}/{1}/{2}", displayName, version, sampleName);
            var previousPath = GetPreviousSamplePath(displayName, sampleName);

            // Remove the previous sample directory.
            if (!string.IsNullOrEmpty(previousPath))
            {
                var msg = "A different version of the sample is already imported at\n\n"
                          + previousPath
                          + "\n\nIt will be deleted when you update. Are you sure you want to continue?";
                if (!EditorUtility.DisplayDialog("Sample Importer", msg, "OK", "Cancel"))
                    return;

                FileUtil.DeleteFileOrDirectory(previousPath);

                var metaFile = previousPath + ".meta";
                if (File.Exists(metaFile))
                    FileUtil.DeleteFileOrDirectory(metaFile);
            }

            if (!Directory.Exists(dst))
                FileUtil.DeleteFileOrDirectory(dst);

            var dstDir = Path.GetDirectoryName(dst);
            if (!Directory.Exists(dstDir))
                Directory.CreateDirectory(dstDir);

            if (Directory.Exists(src))
                FileUtil.CopyFileOrDirectory(src, dst);
            else if (Directory.Exists(srcAlt))
                FileUtil.CopyFileOrDirectory(srcAlt, dst);
            else
                throw new DirectoryNotFoundException(src);

            AssetDatabase.Refresh(ImportAssetOptions.ImportRecursive);
        }

        private static string GetPreviousSamplePath(string displayName, string sampleName)
        {
            var sampleRoot = string.Format("Assets/Samples/{0}", displayName);
            var sampleRootInfo = new DirectoryInfo(sampleRoot);
            if (!sampleRootInfo.Exists) return null;

            return sampleRootInfo.GetDirectories()
                .Select(versionDir => Path.Combine(versionDir.ToString(), sampleName))
                .FirstOrDefault(Directory.Exists);
        }
    }
}
#endif