static public bool TryCreateCompilation(Options options, out Task<Compilation> cu, out MSBuildWorkspace workspace, out Project project) { Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out cu) != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out workspace) != null); Output.WriteLine("Opening the solution {0}", options.Solution); workspace = MSBuildWorkspace.Create(); cu = null; project = null; try { if (options.Project != null && options.Solution != null) { return CreateCompilationFromSolution(options, workspace, out cu, out project); } else { Output.WriteError("Failed to parse either the Project or Solution"); // not implemented; return false; } } catch(Exception e) { Output.WriteError("Error while parsing the .csproj file. Exception from Roslyn: {0}", e.ToString()); cu = null; return false; } }
private static bool CreateCompilationFromSolution(Options options, MSBuildWorkspace workspace, out Task<Compilation> compilationAsync, out Project project) { var solution = workspace.OpenSolutionAsync(options.Solution).Result; var projects = solution.Projects.Where(proj => proj.FilePath.Equals(options.Project)); if (projects.Any()) { project = projects.First(); } else { Output.WriteError("Unable to find the specified project in solution. Project {0}", options.Project); project = null; compilationAsync = null; return false; } compilationAsync = project.GetCompilationAsync(); return true; }
/// <summary> /// Parse the args into an Option object /// </summary> /// <param name="args">the args param to Main</param> /// <param name="options">the parsed arguements, valid if return is ture</param> /// <param name="why">an error message if parsing fails and the return is false</param> /// <returns>true if parsing succeeded, false otherwise</returns> internal static bool TryParseOptions(string[] args, out Options options, out string why) { #region CodeContracts Contract.Requires(Contract.ForAll(args, arg => arg != null)); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options) != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Project != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Solution != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).ClousotXML != null); Contract.Ensures(!Contract.Result<bool>() || Contract.ValueAtReturn(out options).Output != OutputOption.unintialized); Contract.Ensures(Contract.Result<bool>() || Contract.ValueAtReturn(out why) != null); // there should be a contract for the output option, but it's complicated //Contract.Ensures( // !Contract.Result<bool>() // || ((Contract.ValueAtReturn(out options).Output == OutputOption.git && Contract.ValueAtReturn(out options).GitRoot != null) // ^ (Contract.ValueAtReturn(out options).Output == OutputOption.inplace)) //); #endregion CodeContracts options = new Options(); why = null; for (var i = 0; i < args.Length; i++) { var arg = args[i]; if (IsOption(arg, out arg)) { switch (arg) { case "project": if (i == args.Length - 1) { why = "The last argument can't be a keyword"; return false; } options.Project = args[++i]; break; case "source": if (i == args.Length - 1) { why = "The last argument can't be a keyword"; return false; } options.SourceFile = args[++i]; break; case "break": System.Diagnostics.Debugger.Launch(); break; case "solution": if (i == args.Length - 1) { why = "The last argument can't be a keyword"; return false; } options.Solution = args[++i]; break; case "output": if (i == args.Length - 1) { why = "The last argument can't be a keyword"; return false; } OutputOption oo; if (Enum.TryParse(args[++i], true, out oo)) { options.Output = oo; } else { why = "Unrecognized output option: " + args[i]; return false; } break; case "gitroot": if (i == args.Length - 1) { why = "The last argument can't be a keyword"; return false; } options.GitRoot = args[++i]; if (!Directory.Exists(options.GitRoot)) { why = "git root directory must exist"; return false; } break; default: options = null; why = "Unrecognized option " + arg; RBLogger.Error("Invalid option {0}", arg); return false; } } else { if (options.ClousotXML != null) { why = "Cannot express two (or more) .xml files"; return false; } else { options.ClousotXML = args[0]; } } } return options.CheckRequiredArguments(ref why); }