Exemplo n.º 1
0
        // Returns output path of assembly
        public static string CompileAssemblyFromSourceDir(string fixtureName, string assemblyName, string sourceDir, bool generateExecutable = false, params string[] references)
        {
            var sourceFiles = SourceFilesIn(sourceDir);
            var cacheInfo   = new CachedAssemblyInfo(sourceFiles, references);

            var cacheKey       = AssemblyCache.GetCacheKey(fixtureName, assemblyName, cacheInfo);
            var outputAssembly = cacheKey + (generateExecutable ? ".exe" : ".dll");

            if (AssemblyCache.CheckCachedAssembly(fixtureName, assemblyName, cacheInfo))
            {
                var cachedCompilerOutput = File.ReadAllText(cacheKey + ".compilerOutput");
                Console.WriteLine("// Using cached assembly '{0}'. Original compiler output follows:", Path.GetFileName(Path.GetDirectoryName(cacheKey)));
                Console.WriteLine(cachedCompilerOutput);
                return(outputAssembly);
            }

            var compilerOutput = Compile(outputAssembly, sourceFiles, generateExecutable, references);

            File.WriteAllText(cacheKey + ".compilerOutput", compilerOutput);
            Console.WriteLine(compilerOutput);

            AssemblyCache.SaveCachedAssembly(fixtureName, assemblyName, cacheInfo);

            return(outputAssembly);
        }
Exemplo n.º 2
0
        private static void XmlWrite(string path, CachedAssemblyInfo info)
        {
            var serializer = new XmlSerializer(info.GetType());

            using (var s = File.Open(path, FileMode.Create, FileAccess.Write))
                serializer.Serialize(s, info);
        }
Exemplo n.º 3
0
        // Returns output path of assembly
        public static string CompileAssemblyFromSourceDir(string fixtureName, string assemblyName, string sourceDir, bool generateExecutable = false, params string[] references)
        {
            var sourceFiles = SourceFilesIn(sourceDir);
            var cacheInfo = new CachedAssemblyInfo(sourceFiles, references);

            var cacheKey = AssemblyCache.GetCacheKey(fixtureName, assemblyName, cacheInfo);
            var outputAssembly = cacheKey + (generateExecutable ? ".exe" : ".dll");

            if (AssemblyCache.CheckCachedAssembly(fixtureName, assemblyName, cacheInfo))
            {
                var cachedCompilerOutput = File.ReadAllText(cacheKey + ".compilerOutput");
                Console.WriteLine("// Using cached assembly '{0}'. Original compiler output follows:", Path.GetFileName(Path.GetDirectoryName(cacheKey)));
                Console.WriteLine(cachedCompilerOutput);
                return outputAssembly;
            }

            var compilerOutput = Compile(outputAssembly, sourceFiles, generateExecutable, references);

            File.WriteAllText(cacheKey + ".compilerOutput", compilerOutput);
            Console.WriteLine(compilerOutput);

            AssemblyCache.SaveCachedAssembly(fixtureName, assemblyName, cacheInfo);

            return outputAssembly;
        }
Exemplo n.º 4
0
 public static bool CheckCachedAssembly(string fixtureName, string assemblyName, CachedAssemblyInfo info)
 {
     var xmlPath = XmlCachePathFor(fixtureName, assemblyName, info);
     if (!File.Exists(xmlPath))
         return false;
     var cached = XmlRead(xmlPath);
     return info.Equals(cached);
 }
Exemplo n.º 5
0
        public static string GetCacheKey(string fixtureName, string assemblyName, CachedAssemblyInfo info)
        {
            var cacheFolder = GetCacheFolder(fixtureName);

            var infoText = info.ToString();
            var infoMD5  = MD5String(infoText);

            var subFolder = Path.Combine(cacheFolder, infoMD5);

            Directory.CreateDirectory(subFolder);

            var assemblyNameShort = Path.GetFileNameWithoutExtension(assemblyName);

            return(Path.Combine(subFolder, assemblyNameShort));
        }
Exemplo n.º 6
0
        // Checks to see if the output path and source files all match exactly.
        // Note that the actual output isn't compared.
        public bool Equals(CachedAssemblyInfo rhs)
        {
            if ((References == null) || (rhs.References == null))
            {
                return(false);
            }
            if ((SourceFiles == null) || (rhs.SourceFiles == null))
            {
                return(false);
            }

            return(SourceFiles.Length == rhs.SourceFiles.Length &&
                   SourceFiles.OrderBy(sf => sf.Path)
                   .SequenceEqual(rhs.SourceFiles.OrderBy(sf => sf.Path)) &&
                   References.Length == rhs.References.Length &&
                   References.OrderBy(r => r)
                   .SequenceEqual(rhs.References.OrderBy(r => r)));
        }
Exemplo n.º 7
0
        public static string GetCacheKey(string fixtureName, string assemblyName, CachedAssemblyInfo info)
        {
            var cacheFolder = GetCacheFolder(fixtureName);

            var infoText = info.ToString();
            var infoMD5 = MD5String(infoText);

            var subFolder = Path.Combine(cacheFolder, infoMD5);
            Directory.CreateDirectory(subFolder);

            var assemblyNameShort = Path.GetFileNameWithoutExtension(assemblyName);
            return Path.Combine(subFolder, assemblyNameShort);
        }
Exemplo n.º 8
0
        // Checks to see if the output path and source files all match exactly.
        // Note that the actual output isn't compared.
        public bool Equals(CachedAssemblyInfo rhs)
        {
            if ((References == null) || (rhs.References == null))
                return false;
            if ((SourceFiles == null) || (rhs.SourceFiles == null))
                return false;

            return SourceFiles.Length == rhs.SourceFiles.Length
                && SourceFiles.OrderBy(sf => sf.Path)
                    .SequenceEqual(rhs.SourceFiles.OrderBy(sf => sf.Path))
                && References.Length == rhs.References.Length
                && References.OrderBy(r => r)
                    .SequenceEqual(rhs.References.OrderBy(r => r));
        }
Exemplo n.º 9
0
 private static void XmlWrite(string path, CachedAssemblyInfo info)
 {
     var serializer = new XmlSerializer(info.GetType());
     using (var s = File.Open(path, FileMode.Create, FileAccess.Write))
         serializer.Serialize(s, info);
 }
Exemplo n.º 10
0
 private static string XmlCachePathFor(string fixtureName, string assemblyName, CachedAssemblyInfo info)
 {
     return GetCacheKey(fixtureName, assemblyName, info) + ".xml";
 }
Exemplo n.º 11
0
 public static void SaveCachedAssembly(string fixtureName, string assemblyName, CachedAssemblyInfo info)
 {
     var xmlPath = XmlCachePathFor(fixtureName, assemblyName, info);
     XmlWrite(xmlPath, info);
 }
Exemplo n.º 12
0
 private static string XmlCachePathFor(string fixtureName, string assemblyName, CachedAssemblyInfo info)
 {
     return(GetCacheKey(fixtureName, assemblyName, info) + ".xml");
 }
Exemplo n.º 13
0
        public static void SaveCachedAssembly(string fixtureName, string assemblyName, CachedAssemblyInfo info)
        {
            var xmlPath = XmlCachePathFor(fixtureName, assemblyName, info);

            XmlWrite(xmlPath, info);
        }
Exemplo n.º 14
0
        public static bool CheckCachedAssembly(string fixtureName, string assemblyName, CachedAssemblyInfo info)
        {
            var xmlPath = XmlCachePathFor(fixtureName, assemblyName, info);

            if (!File.Exists(xmlPath))
            {
                return(false);
            }
            var cached = XmlRead(xmlPath);

            return(info.Equals(cached));
        }