Exemplo n.º 1
0
        private BuildInfo BuildAndScan(string workingDirectory, string dllFileFullName, string unitTestingFileFullName)
        {
            //Add unit testing file to .csproj file
            var csProjPath        = Directory.GetFiles(workingDirectory, "*.csproj").FirstOrDefault();
            var shortTestFileName = unitTestingFileFullName.Split('\\').Last();

            var doc = XDocument.Load(csProjPath);
            var ns  = doc.Root.GetDefaultNamespace();

            var element = new XElement(ns + "Compile", new XAttribute("Include", shortTestFileName));

            if (doc.Root.Descendants().Any(d => d.Name.LocalName == "Compile" && d.Attribute("Include") != null))
            {
                if (!doc.Root.Descendants().Any(d => d.Name.LocalName == "Compile" && d.Attribute("Include").Value == shortTestFileName))
                {
                    doc.Root.Descendants().Where(d => d.Name.LocalName == "Compile" && d.Attribute("Include") != null).FirstOrDefault().Parent.Add(element);
                }
            }

            doc.Save(csProjPath);

            //Build the assembly
            var process = ProcessFactory.BuildAndScanProcess(workingDirectory, dllFileFullName);

            try
            {
                process.Start();

                string output = process.StandardOutput.ReadToEnd();

                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    return(new BuildInfo(false, "Exit code not 0"));
                }

                if (string.IsNullOrEmpty(output))
                {
                    return(new BuildInfo(true, "Build succeeded"));
                }
                else
                {
                    return(new BuildInfo(false, output));
                }
            }

            catch (Exception ex)
            {
                return(new BuildInfo(false, $"An exception occured while trying to build and scan the .cs file \r\n {ex}"));
            }
        }