Exemplo n.º 1
0
        public CompileResult CompileFiles(IEnumerable <string> files, CompileOptions options)
        {
            if (!files.Any())
            {
                throw new ArgumentException("No source file provided.");
            }
            if (!Directory.Exists(options.OutputDir))
            {
                throw new DirectoryNotFoundException($"Directory {options.OutputDir} not found.");
            }
            var result = new CompileResult();

            string outputPath  = Path.Combine(options.OutputDir, $"{options.AssemblyName}{options.OutputType.AsExtension()}");
            string references  = options.ReferenceLocations.Aggregate((res, item) => res + "," + item);
            string sourceFiles = files.Aggregate((res, item) => res + " " + item);
            string cmdArgs     = $"-target:{options.OutputType.AsString()} -out:{outputPath} -r:{references}";

            if (options.Recurse)
            {
                cmdArgs = $"{cmdArgs} /recurse:{sourceFiles}\\*.cs";
            }
            else
            {
                cmdArgs = $"{sourceFiles} {cmdArgs}";
            }
            var psi = new ProcessStartInfo(cscPath, cmdArgs);

            psi.CreateNoWindow         = false;
            psi.WindowStyle            = ProcessWindowStyle.Hidden;
            psi.UseShellExecute        = false;
            psi.RedirectStandardInput  = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;
            Process p      = Process.Start(psi);
            string  output = string.Empty;
            string  error  = string.Empty;

            p.InputAndOutputToEnd(cmdArgs, ref output, ref error);
            p.WaitForExit();
            p.Close();
            if (!string.IsNullOrEmpty(error))
            {
                error = $"{cscPath} {cmdArgs}{Environment.NewLine}{error}";
                result.Errors.Add(error);
                result.Success = false;
            }
            else
            {
                bool hasCompileErrors = output.ContainsCase("error CS");
                if (hasCompileErrors)
                {
                    error = $"{cscPath} {cmdArgs}{Environment.NewLine}{output}";
                    result.Errors.Add(error);
                    result.Success = false;
                }
                else
                {
                    result.Assembly = Assembly.LoadFile(outputPath);
                    result.Success  = true;
                }
            }
            return(result);
        }
Exemplo n.º 2
0
 public CompileResult CompileCode(string code, CompileOptions options)
 {
     throw new NotImplementedException();
 }