Example #1
0
 public CompileResult (Assembly assembly, Metacomment[] metacomments) {
     Assembly = assembly;
     Metacomments = metacomments;
 }
Example #2
0
        private static CompileResult Compile(
            Func <CodeDomProvider> getProvider, IEnumerable <string> _filenames, string assemblyName, string compilerOptions
            )
        {
            var filenames = _filenames.ToArray();
            var tempPath  = Path.Combine(TempPath, assemblyName);

            Directory.CreateDirectory(tempPath);

            var warningTextPath = Path.Combine(
                tempPath, "warnings.txt"
                );

            var references = new List <string> {
                "System.dll",
                "System.Core.dll", "System.Xml.dll",
                "Microsoft.CSharp.dll",
                typeof(JSIL.Meta.JSIgnore).Assembly.Location
            };

            bool generateExecutable = false;

            var metacomments = new List <Metacomment>();

            foreach (var sourceFile in filenames)
            {
                var sourceText = File.ReadAllText(sourceFile);

                var localMetacomments = Metacomment.FromText(sourceText);
                foreach (var metacomment in localMetacomments)
                {
                    switch (metacomment.Command.ToLower())
                    {
                    case "reference":
                        references.Add(metacomment.Arguments);
                        break;

                    case "compileroption":
                        compilerOptions += " " + metacomment.Arguments;
                        break;

                    case "generateexecutable":
                        generateExecutable = true;
                        break;
                    }
                }

                metacomments.AddRange(localMetacomments);
            }

            var outputAssembly = Path.Combine(
                tempPath,
                Path.GetFileNameWithoutExtension(assemblyName) +
                (generateExecutable ? ".exe" : ".dll")
                );

            if (
                File.Exists(outputAssembly) &&
                CheckCompileManifest(filenames, tempPath)
                )
            {
                if (File.Exists(warningTextPath))
                {
                    Console.Error.WriteLine(File.ReadAllText(warningTextPath));
                }

                return(new CompileResult(
                           Assembly.LoadFile(outputAssembly),
                           metacomments.ToArray()
                           ));
            }

            var files = Directory.GetFiles(tempPath);

            foreach (var file in files)
            {
                try {
                    File.Delete(file);
                } catch {
                }
            }

            var parameters = new CompilerParameters(references.ToArray())
            {
                CompilerOptions         = compilerOptions,
                GenerateExecutable      = generateExecutable,
                GenerateInMemory        = false,
                IncludeDebugInformation = true,
                TempFiles             = new TempFileCollection(tempPath, true),
                OutputAssembly        = outputAssembly,
                WarningLevel          = 4,
                TreatWarningsAsErrors = false
            };

            CompilerResults results;

            using (var provider = getProvider()) {
                results = provider.CompileAssemblyFromFile(
                    parameters,
                    filenames.ToArray()
                    );
            }

            var compileErrorsAndWarnings = results.Errors.Cast <CompilerError>().ToArray();
            var compileWarnings          = (from ce in compileErrorsAndWarnings where ce.IsWarning select ce).ToArray();
            var compileErrors            = (from ce in compileErrorsAndWarnings where !ce.IsWarning select ce).ToArray();

            if (compileWarnings.Length > 0)
            {
                var warningText = String.Format(
                    "// C# Compiler warning(s) follow //\r\n{0}\r\n// End of C# compiler warning(s) //",
                    String.Join(Environment.NewLine, compileWarnings.Select(Convert.ToString).ToArray())
                    );
                Console.Error.WriteLine(
                    warningText
                    );
                File.WriteAllText(
                    warningTextPath, warningText
                    );
            }
            else
            {
                if (File.Exists(warningTextPath))
                {
                    File.Delete(warningTextPath);
                }
            }

            if (compileErrors.Length > 0)
            {
                throw new Exception(
                          String.Join(Environment.NewLine, compileErrors.Select(Convert.ToString).ToArray())
                          );
            }

            WriteCompileManifest(filenames, tempPath);

            return(new CompileResult(
                       results.CompiledAssembly,
                       metacomments.ToArray()
                       ));
        }
Example #3
0
 public CompileResult (Assembly assembly, Metacomment[] metacomments, bool wasCached) {
     Assembly = assembly;
     Metacomments = metacomments;
     WasCached = wasCached;
 }