<commit_before>﻿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) {
            var filePath = Path.Combine(folder, name);
            var fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.CreateNew;

            EnsureDirectoryExists(Path.GetDirectoryName(filePath));

            using (var fs = File.Open(filePath, fileMode, FileAccess.Write, FileShare.Read)) {
                fs.Write(bytes.Array, bytes.Offset, bytes.Count);
                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);
                    if (kvp.Value.SourceMapBuilder != null && kvp.Value.SourceMapBuilder.Build(path, kvp.Key))
                    {
                        using (var fs = File.Open(Path.Combine(path, kvp.Key), FileMode.Append, FileAccess.Write, FileShare.Read))
                        {
                            using (var tw = new StreamWriter(fs))
                            {
                                tw.Write("//# sourceMappingURL=" + new Uri(Path.Combine(path, (kvp.Key + ".map").Replace(" ", ""))));
                            }
                        }
                    }
                }
            }
        }
<commit_msg>SourceMap - use full local path for sourcemap.<commit_after>﻿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) {
            var filePath = Path.Combine(folder, name);
            var fileMode = File.Exists(filePath) ? FileMode.Truncate : FileMode.CreateNew;

            EnsureDirectoryExists(Path.GetDirectoryName(filePath));

            using (var fs = File.Open(filePath, fileMode, FileAccess.Write, FileShare.Read)) {
                fs.Write(bytes.Array, bytes.Offset, bytes.Count);
                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);
                    if (kvp.Value.SourceMapBuilder != null && kvp.Value.SourceMapBuilder.Build(path, kvp.Key))
                    {
                        using (var fs = File.Open(Path.Combine(path, kvp.Key), FileMode.Append, FileAccess.Write, FileShare.Read))
                        {
                            using (var tw = new StreamWriter(fs))
                            {
                                tw.Write("//# sourceMappingURL=" + new Uri(Path.GetFullPath(Path.Combine(path, (kvp.Key + ".map").Replace(" ", "")))));
                            }
                        }
                    }
                }
            }
        }

{'fraction_non_alphanumeric': 0.08372135487994156, 'fraction_numerical': 0.0010955902492467817, 'mean_word_length': 1.710715169512497, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3710596', 'n_tokens_mistral': 2612, 'n_tokens_neox': 2503, 'n_words': 634}