File size: 3,027 Bytes
1ae2e8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
namespace RobloxCS
{
    internal static class FileManager
    {
        public static IEnumerable<string> GetSourceFiles(string sourceDirectory)
        {
            try
            {
                return Directory.GetFiles(sourceDirectory, "*.cs", SearchOption.AllDirectories)
                    .Where(file => !Utility.FixPathSep(file).StartsWith(Utility.FixPathSep(sourceDirectory) + "/obj"))
                    .Select(Utility.FixPathSep);
            }
            catch (Exception e)
            {
                Logger.Error($"Failed to read source files: {e.Message}");
                return [];
            }
        }

        public static void CopyDirectory(string sourceDirectory, string destinationDirectory)
        {
            var directory = new DirectoryInfo(sourceDirectory);
            if (!directory.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourceDirectory);
            }

            if (!Directory.Exists(destinationDirectory))
            {
                Directory.CreateDirectory(destinationDirectory);
            }

            var files = directory.GetFiles();
            foreach (var file in files)
            {
                var tempPath = Path.Combine(destinationDirectory, file.Name);
                file.CopyTo(tempPath, true);
            }

            var directories = directory.GetDirectories();
            foreach (var subdirectory in directories)
            {
                var tempPath = Path.Combine(destinationDirectory, subdirectory.Name);
                CopyDirectory(subdirectory.FullName, tempPath);
            }
        }

        public static void WriteCompiledFiles(string outDirectory, List<CompiledFile> compiledFiles)
        {
            Logger.Info($"Compiling {compiledFiles.Count} files...");
            TryCreateDirectory(outDirectory);

            foreach (var compiledFile in compiledFiles)
            {
                var directoryParts = compiledFile.Path.Split('/').ToList();
                directoryParts.Remove(directoryParts.Last());
                var parentDirectory = string.Join('/', directoryParts);
                TryCreateDirectory(parentDirectory);

                try
                {
                    File.WriteAllText(compiledFile.Path, compiledFile.LuaSource);
                }
                catch (Exception e)
                {
                    Logger.Error($"Failed to write to \"{compiledFile.Path}\": {e.Message}");
                }
                Logger.Info($"Successfully wrote \"{compiledFile.Path}\"!");
            }
        }

        private static void TryCreateDirectory(string path)
        {
            try
            {
                Directory.CreateDirectory(path);
            }
            catch (Exception e)
            {
                Logger.Error($"Failed to create directory \"{path}\": {e.Message}");
            }
        }
    }
}