-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationResult.cs
More file actions
169 lines (143 loc) · 6.29 KB
/
Copy pathTranslationResult.cs
File metadata and controls
169 lines (143 loc) · 6.29 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using JSIL.Internal;
using JSIL.Translator;
using Mono.Cecil;
namespace JSIL {
public class TranslationResult {
public struct ResultFile {
public string Type;
public string Filename;
public long Size;
public ArraySegment<byte> Contents;
public Dictionary<string, object> Properties;
public SourceMapBuilder SourceMapBuilder;
}
public readonly string AssemblyPath;
public readonly AssemblyManifest AssemblyManifest;
public readonly Configuration Configuration;
public readonly List<AssemblyDefinition> Assemblies = new List<AssemblyDefinition>();
public readonly List<string> FileOrder = new List<string>();
public readonly Dictionary<string, ResultFile> Files = new Dictionary<string, ResultFile>();
public readonly StringBuilder Log = new StringBuilder();
public TimeSpan Elapsed;
public ArraySegment<byte> Manifest;
internal TranslationResult (
Configuration configuration, string assemblyPath, AssemblyManifest assemblyManifest
) {
Configuration = configuration;
AssemblyPath = assemblyPath;
AssemblyManifest = assemblyManifest;
}
public IEnumerable<ResultFile> OrderedFiles {
get {
foreach (var filename in FileOrder)
yield return Files[filename];
}
}
public void AddFile (
string type,
string filename,
ArraySegment<byte> bytes,
int? position = null,
Dictionary<string, object> properties = null,
SourceMapBuilder sourceMapBuilder = null
) {
lock (Files) {
if (position.HasValue)
FileOrder.Insert(position.Value, filename);
else
FileOrder.Add(filename);
Files.Add(filename, new ResultFile {
Type = type,
Filename = filename,
Contents = bytes,
Size = bytes.Count,
Properties = properties,
SourceMapBuilder = sourceMapBuilder
});
}
}
public void WriteToStream (Stream output) {
if (Manifest.Array == null)
throw new Exception("AssemblyTranslator.GenerateManifest must be called first");
var newline = Encoding.ASCII.GetBytes(Environment.NewLine);
output.Write(Manifest.Array, Manifest.Offset, Manifest.Count);
output.Write(newline, 0, newline.Length);
foreach (var file in Files.Values) {
if (file.Contents.Count > 0) {
output.Write(file.Contents.Array, file.Contents.Offset, file.Contents.Count);
}
output.Write(newline, 0, newline.Length);
}
output.Flush();
}
public string WriteToString () {
if (Manifest.Array == null)
throw new Exception("AssemblyTranslator.GenerateManifest must be called first");
using (var ms = new MemoryStream(AssemblyTranslator.DefaultStreamCapacity)) {
WriteToStream(ms);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
private static void WriteBytesToFile (string folder, string name, ArraySegment<byte> bytes, SourceMapBuilder sourceMapBuilder = null) {
var filePath = Path.Combine(folder, name);
var fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.CreateNew;
EnsureDirectoryExists(Path.GetDirectoryName(filePath));
bool writeMapLink = sourceMapBuilder != null && sourceMapBuilder.Build(folder, name);
using (var fs = File.Open(filePath, fileMode, FileAccess.Write, FileShare.Read))
{
fs.Write(bytes.Array, bytes.Offset, bytes.Count);
if (writeMapLink)
sourceMapBuilder.WriteSourceMapLink(fs, folder, name);
fs.Flush();
}
}
public static void EnsureDirectoryExists (string directoryName) {
if (!Directory.Exists(directoryName))
Directory.CreateDirectory(directoryName);
}
public void WriteToDirectory (string path, string manifestPrefix = "") {
if (Manifest.Array == null)
throw new Exception("AssemblyTranslator.GenerateManifest must be called first");
EnsureDirectoryExists(path);
WriteBytesToFile(path, manifestPrefix + "manifest.js", Manifest);
foreach (var kvp in Files) {
if (kvp.Value.Contents.Count > 0)
WriteBytesToFile(path, kvp.Key, kvp.Value.Contents, kvp.Value.SourceMapBuilder);
}
}
internal void AddExistingFile (string type, string filename, long fileSize, int? position = null) {
lock (Files) {
if (Files.ContainsKey(filename)) {
var existingFile = Files[filename];
if (
(existingFile.Size != fileSize) ||
(existingFile.Type != type) ||
(existingFile.Contents.Count > 0)
) {
throw new InvalidOperationException(String.Format(
"A '{0}' named '{1}' already exists in the asset manifest, and has different metadata than the file being added.",
type, filename
));
} else {
Console.Error.WriteLine("// Warning: Adding '{0}' '{1}' to manifest multiple times!", type, filename);
}
return;
}
if (position.HasValue)
FileOrder.Insert(position.Value, filename);
else
FileOrder.Add(filename);
Files.Add(filename, new ResultFile {
Type = type,
Filename = filename,
Size = fileSize
});
}
}
}
}