private void Save(Bridge.Translator.Translator translator) { Dictionary <string, LuaFileInfo> luaFiles = translator.ParsedSourceFiles.ToDictionary(i => i.FileName, i => new LuaFileInfo(i.FileName)); var usings = Bridge.Translator.Lua.EmitBlock.UsingNamespaces; foreach (var pair in usings) { string fileName = GetFileName(pair.Key); luaFiles[fileName].AddUsingDeclaration(pair.Value); } var types = GetEnableTypeInfos(translator.Types); foreach (var type in types) { string content = translator.Outputs[type.Key]; string fileName = GetFileName(type); luaFiles[fileName].AddType(type, content); } foreach (LuaFileInfo luaFileInfo in luaFiles.Values) { luaFileInfo.Save(folder_, output_); } SaveManifests(luaFiles.Values.ToList(), types); }
public static void ExtractCore(Translator translatorInstance, string outputPath, bool nodebug = false) { var clrPath = translatorInstance.BridgeLocation; var assembly = System.Reflection.Assembly.UnsafeLoadFrom(clrPath); // We can only have Beautified, Minified or Both, so this test has inverted logic: // output beautified if not minified only == (output beautified or output both) if (translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Minified) { ExtractResourceAndWriteToFile(outputPath, assembly, "Bridge.Resources.bridge.js", "bridge.js"); } if (translatorInstance.AssemblyInfo.GenerateTypeScript) { ExtractResourceAndWriteToFile(outputPath, assembly, "Bridge.Resources.bridge.d.ts", "bridge.d.ts"); } // Like above test: output minified if not beautified only == (out minified or out both) if (translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Formatted) { if (!nodebug) { ExtractResourceAndWriteToFile(outputPath, assembly, "Bridge.Resources.bridge.js", "bridge.min.js", (reader) => { var minifier = new Minifier(); return minifier.MinifyJavaScript(reader.ReadToEnd(), new CodeSettings { TermSemicolons = true }); }); } } }
public static void ExtractCore(Translator translatorInstance, string outputPath, bool nodebug = false) { foreach (var reference in translatorInstance.References) { var listRes = reference.MainModule.Resources.FirstOrDefault(r => r.Name == Translator.BridgeResourcesList); if (listRes != null) { string resourcesStr = null; using (var resourcesStream = ((EmbeddedResource) listRes).GetResourceStream()) { using (StreamReader reader = new StreamReader(resourcesStream)) { resourcesStr = reader.ReadToEnd(); } } //var resourcesStr = enc.GetString(((EmbeddedResource) listRes).GetResourceData()); var resources = resourcesStr.Split('+'); foreach (var res in resources) { var parts = res.Split(':'); var fileName = parts[0].Trim(); var resName = parts[1].Trim(); bool isTs = resName.EndsWith(".d.ts"); bool isJs = resName.EndsWith(".js"); if (!isTs && translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Minified) { ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName); } if (isTs && translatorInstance.AssemblyInfo.GenerateTypeScript) { ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName); } if (isJs && translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Formatted) { if (!nodebug) { ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName.ReplaceLastInstanceOf(".js", ".min.js"), (content) => { var minifier = new Minifier(); return minifier.MinifyJavaScript(content, MinifierCodeSettings); }); } } } } } }
public LiveTranslator(string folder, string source, bool recursive, string lib, HttpContext context) { this.sessionId = context.Session.SessionID; this.bridgeFolder = folder + @"\Bridge\Builder\"; this.csFolder = folder + @"\UserCode\"; this.BuildSourceFile(source); lib = (this.RebuildStub(source)) ? this.BuildAssembly() : Path.Combine(folder, lib); this.translator = new Bridge.Translator.Translator(this.csFolder, this.Source, recursive, lib); this.Rebuild = false; this.ChangeCase = true; this.Config = null; }
public static void ExtractCore(Translator translatorInstance, string outputPath, bool nodebug = false) { var clrPath = translatorInstance.BridgeLocation; var assembly = System.Reflection.Assembly.UnsafeLoadFrom(clrPath); string resourceName; // We can only have Beautified, Minified or Both, so this test has inverted logic: // output beautified if not minified only == (output beautified or output both) if (translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Minified) { resourceName = "Bridge.Resources.bridge.js"; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) { using (StreamReader reader = new StreamReader(stream)) { File.WriteAllText(Path.Combine(outputPath, "bridge.js"), reader.ReadToEnd()); } } } // Like above test: output minified if not beautified only == (out minified or out both) if (translatorInstance.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Formatted) { if (!nodebug) { resourceName = "Bridge.Resources.bridge.js"; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) { using (StreamReader reader = new StreamReader(stream)) { var code = reader.ReadToEnd(); var minifier = new Minifier(); File.WriteAllText(Path.Combine(outputPath, "bridge.min.js"), minifier.MinifyJavaScript(code), System.Text.UTF8Encoding.UTF8); } } } } }
public void ExtractCore(string outputPath, bool nodebug = false) { foreach (var reference in this.References) { var listRes = reference.MainModule.Resources.FirstOrDefault(r => r.Name == Translator.BridgeResourcesList); if (listRes != null) { string resourcesStr = null; using (var resourcesStream = ((EmbeddedResource)listRes).GetResourceStream()) { using (StreamReader reader = new StreamReader(resourcesStream)) { resourcesStr = reader.ReadToEnd(); } } var resources = resourcesStr.Split('+'); foreach (var res in resources) { var parts = res.Split(':'); var fileName = parts[0].Trim(); var resName = parts[1].Trim(); bool isTs = resName.EndsWith(".d.ts"); bool isJs = resName.EndsWith(".js"); if (!isTs && this.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Minified) { this.ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName); } if (isTs && this.AssemblyInfo.GenerateTypeScript) { this.ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName); } if (isJs && this.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Formatted) { if (!nodebug) { this.ExtractResourceAndWriteToFile(outputPath, reference, resName, fileName.ReplaceLastInstanceOf(".js", ".min.js"), (content) => { var minifier = new Minifier(); return(minifier.MinifyJavaScript(content, Translator.GetMinifierSettings(fileName))); }); } } } } } if (!string.IsNullOrWhiteSpace(this.AssemblyInfo.Locales)) { StringBuilder bufferjs = null; StringBuilder bufferjsmin = null; if (this.AssemblyInfo.CombineLocales && !this.AssemblyInfo.CombineScripts) { bufferjs = new StringBuilder(); bufferjsmin = new StringBuilder(); } var bridgeAssembly = this.References.FirstOrDefault(r => r.Name.Name == "Bridge"); var localesRes = bridgeAssembly.MainModule.Resources.Where(r => r.Name.StartsWith(Translator.LocalesPrefix)).Cast <EmbeddedResource>(); var locales = this.AssemblyInfo.Locales.Split(';'); foreach (var locale in locales) { if (locale == "all") { this.ExtractLocale(localesRes, outputPath, nodebug, bufferjs, bufferjsmin); break; } else if (locale.Contains("*")) { var name = Translator.LocalesPrefix + locale.SubstringUpToFirst('*'); this.ExtractLocale(localesRes.Where(r => r.Name.StartsWith(name)), outputPath, nodebug, bufferjs, bufferjsmin); } else { var name = Translator.LocalesPrefix + locale + ".js"; this.ExtractLocale(localesRes.First(r => r.Name == name), outputPath, nodebug, bufferjs, bufferjsmin); } } if ((bufferjs != null && bufferjs.Length > 0) || (bufferjsmin != null && bufferjsmin.Length > 0)) { if (!string.IsNullOrWhiteSpace(this.AssemblyInfo.LocalesOutput)) { outputPath = Path.Combine(outputPath, this.AssemblyInfo.LocalesOutput); } var defaultFileName = this.AssemblyInfo.LocalesFileName ?? "Bridge.Locales.js"; var fileName = defaultFileName.Replace(":", "_"); var oldFNlen = fileName.Length; while (Path.IsPathRooted(fileName)) { fileName = fileName.TrimStart(Path.DirectorySeparatorChar, '/', '\\'); if (fileName.Length == oldFNlen) { break; } oldFNlen = fileName.Length; } var file = CreateFileDirectory(outputPath, fileName); if (bufferjs != null && bufferjs.Length > 0) { File.WriteAllText(file.FullName, bufferjs.ToString(), OutputEncoding); } if (bufferjsmin != null && bufferjsmin.Length > 0) { File.WriteAllText(file.FullName.ReplaceLastInstanceOf(".js", ".min.js"), bufferjsmin.ToString(), OutputEncoding); } } } }
public virtual void SaveTo(string path, string defaultFileName) { var minifier = new Minifier(); var files = new Dictionary <string, string>(); foreach (var item in this.Outputs) { string fileName = item.Key; string code = item.Value; if (fileName.Contains(Bridge.Translator.AssemblyInfo.DEFAULT_FILENAME)) { fileName = fileName.Replace(Bridge.Translator.AssemblyInfo.DEFAULT_FILENAME, defaultFileName); } // Ensure filename contains no ":". It could be used like "c:/absolute/path" fileName = fileName.Replace(":", "_"); // Trim heading slash/backslash off file names until it does not start with slash. var oldFNlen = fileName.Length; while (Path.IsPathRooted(fileName)) { fileName = fileName.TrimStart(Path.DirectorySeparatorChar, '/', '\\'); // Trimming didn't change the path. This way, it will just loop indefinitely. // Also, this means the absolute path specifies a fully-qualified DOS PathName with drive letter. if (fileName.Length == oldFNlen) { break; } oldFNlen = fileName.Length; } // If 'fileName' is an absolute path, Path.Combine will ignore the 'path' prefix. string filePath = Path.Combine(path, fileName); string extension = Path.GetExtension(fileName); bool isJs = extension == ('.' + Bridge.Translator.AssemblyInfo.JAVASCRIPT_EXTENSION); // We can only have Beautified, Minified or Both, so this test has inverted logic: // output beautified if not minified only == (output beautified or output both) // Check by @vladsch: Output anyway if the class is not a JavaScript file. if (this.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Minified || !isJs) { string header = GetOutputHeader(isJs, isJs); var file = CreateFileDirectory(filePath); this.SaveToFile(file.FullName, string.IsNullOrWhiteSpace(header) ? code : header + code); files.Add(fileName, file.FullName); } // Like above test: output minified if not beautified only == (out minified or out both) // Check by @vladsch: Output minified is allowed only and only if it is a JavaScript being output. if (this.AssemblyInfo.OutputFormatting != JavaScriptOutputType.Formatted && isJs) { var fileNameMin = Path.GetFileNameWithoutExtension(filePath) + ".min" + extension; var file = CreateFileDirectory(Path.GetDirectoryName(filePath), fileNameMin); this.SaveToFile(file.FullName, minifier.MinifyJavaScript(code, Translator.GetMinifierSettings(fileNameMin))); } } if (this.AssemblyInfo.InjectScriptToAssembly) { this.InjectResources(files); } if (!string.IsNullOrWhiteSpace(this.AssemblyInfo.AfterBuild)) { try { this.RunEvent(this.AssemblyInfo.AfterBuild); } catch (Exception exc) { throw new Bridge.Translator.Exception("Error: Unable to run afterBuild event command: " + exc.Message + "\nStack trace:\n" + exc.StackTrace); } } }