private FsharpFolderComposite FindProjectFilesUsingBuildalyzer(IAnalyzerResult analyzerResult) { var inputFiles = new FsharpFolderComposite(); var projectUnderTestDir = Path.GetDirectoryName(analyzerResult.ProjectFilePath); var projectRoot = Path.GetDirectoryName(projectUnderTestDir); var rootFolderComposite = new FsharpFolderComposite() { FullPath = projectRoot, RelativePath = string.Empty }; var cache = new Dictionary <string, FsharpFolderComposite> { [string.Empty] = rootFolderComposite }; // Save cache in a singleton so we can use it in other parts of the project FolderCompositeCache <FsharpFolderComposite> .Instance.Cache = cache; inputFiles.Add(rootFolderComposite); var fSharpChecker = FSharpChecker.Create(projectCacheSize: null, keepAssemblyContents: null, keepAllBackgroundResolutions: null, legacyReferenceResolver: null, tryGetMetadataSnapshot: null, suggestNamesForErrors: null, keepAllBackgroundSymbolUses: null, enableBackgroundItemKeyStoreAndSemanticClassification: null); foreach (var sourceFile in analyzerResult.SourceFiles) { // Skip xamarin UI generated files if (sourceFile.EndsWith(".xaml.cs")) { continue; } var relativePath = Path.GetRelativePath(projectUnderTestDir, sourceFile); var folderComposite = GetOrBuildFolderComposite(cache, Path.GetDirectoryName(relativePath), projectUnderTestDir, projectRoot, inputFiles); var fileName = Path.GetFileName(sourceFile); var file = new FsharpFileLeaf() { SourceCode = FileSystem.File.ReadAllText(sourceFile), RelativePath = FileSystem.Path.Combine(folderComposite.RelativePath, fileName), FullPath = sourceFile }; // Get the syntax tree for the source file Tuple <FSharpProjectOptions, FSharpList <FSharpErrorInfo> > fSharpOptions = FSharpAsync.RunSynchronously(fSharpChecker.GetProjectOptionsFromScript(filename: file.FullPath, sourceText: SourceText.ofString(file.SourceCode), previewEnabled: null, loadedTimeStamp: null, otherFlags: null, useFsiAuxLib: null, useSdkRefs: null, assumeDotNetFramework: null, extraProjectInfo: null, optionsStamp: null, userOpName: null), timeout: null, cancellationToken: null); FSharpParseFileResults result = FSharpAsync.RunSynchronously(fSharpChecker.ParseFile(fileName, SourceText.ofString(file.SourceCode), fSharpChecker.GetParsingOptionsFromProjectOptions(fSharpOptions.Item1).Item1, userOpName: null), timeout: null, cancellationToken: null); if (result.ParseTree.Value.IsImplFile) { var syntaxTree = (ImplFile)result.ParseTree.Value; file.SyntaxTree = syntaxTree; folderComposite.Add(file); } else { var message = $"Cannot make Fsharp SyntaxTree from .fsi filetype (SyntaxTree.ParsedImplFileInput class wanted)"; throw new InputException(message); } } return(inputFiles); }
/// <summary> /// Recursively scans the given directory for files to mutate /// </summary> private FsharpFolderComposite FindInputFiles(string path, string projectUnderTestDir, string parentFolder) { var lastPathComponent = Path.GetFileName(path); var folderComposite = new FsharpFolderComposite { FullPath = Path.GetFullPath(path), RelativePath = Path.Combine(parentFolder, lastPathComponent), }; foreach (var folder in FileSystem.Directory.EnumerateDirectories(folderComposite.FullPath).Where(x => !_foldersToExclude.Contains(Path.GetFileName(x)))) { folderComposite.Add(FindInputFiles(folder, projectUnderTestDir, folderComposite.RelativePath)); } var fSharpChecker = FSharpChecker.Create(projectCacheSize: null, keepAssemblyContents: null, keepAllBackgroundResolutions: null, legacyReferenceResolver: null, tryGetMetadataSnapshot: null, suggestNamesForErrors: null, keepAllBackgroundSymbolUses: null, enableBackgroundItemKeyStoreAndSemanticClassification: null); foreach (var file in FileSystem.Directory.GetFiles(folderComposite.FullPath, "*.fs", SearchOption.TopDirectoryOnly)) { var fileName = Path.GetFileName(file); var fileLeaf = new FsharpFileLeaf() { SourceCode = FileSystem.File.ReadAllText(file), RelativePath = Path.Combine(folderComposite.RelativePath, fileName), FullPath = file, }; // Get the syntax tree for the source file Tuple <FSharpProjectOptions, FSharpList <FSharpErrorInfo> > fsharpoptions = FSharpAsync.RunSynchronously(fSharpChecker.GetProjectOptionsFromScript(fileLeaf.FullPath, SourceText.ofString(fileLeaf.SourceCode), previewEnabled: null, loadedTimeStamp: null, otherFlags: null, useFsiAuxLib: null, useSdkRefs: null, assumeDotNetFramework: null, extraProjectInfo: null, optionsStamp: null, userOpName: null), timeout: null, cancellationToken: null); FSharpParseFileResults result = FSharpAsync.RunSynchronously(fSharpChecker.ParseFile(fileLeaf.FullPath, SourceText.ofString(fileLeaf.SourceCode), fSharpChecker.GetParsingOptionsFromProjectOptions(fsharpoptions.Item1).Item1, userOpName: null), timeout: null, cancellationToken: null); if (result.ParseTree.Value.IsImplFile) { var syntaxTree = (ImplFile)result.ParseTree.Value; fileLeaf.SyntaxTree = syntaxTree; folderComposite.Add(fileLeaf); } else { var message = $"Cannot make Fsharp SyntaxTree from .fsi filetype (SyntaxTree.ParsedImplFileInput class wanted)"; throw new InputException(message); } } return(folderComposite); }
static void Main(string[] args) { FSharpChecker fSharpChecker = FSharpChecker.Create(null, null, null, null, null, null, null, null); var fullPath = Path.GetFullPath("..\\..\\..\\..\\..\\boolMutateTestApp\\boolMutateTestApp\\Program.fs"); var sourceCode = File.ReadAllText(fullPath); Tuple <FSharpProjectOptions, FSharpList <FSharpErrorInfo> > fsharpoptions = FSharpAsync.RunSynchronously(fSharpChecker.GetProjectOptionsFromScript(fullPath, SourceText.ofString(sourceCode), null, null, null, null, null, null, null, null, null), null, null); FSharpParseFileResults result = FSharpAsync.RunSynchronously(fSharpChecker.ParseFile(fullPath, SourceText.ofString(sourceCode), fSharpChecker.GetParsingOptionsFromProjectOptions(fsharpoptions.Item1).Item1, null), null, null); var syntaxTree = result.ParseTree; if (syntaxTree.Value.IsImplFile) { var test = ((ImplFile)syntaxTree.Value).Item; VisitModulesAndNamespaces(test.modules); } else { Console.WriteLine("F# Interface file (*.fsi) not supported."); } //fSharpChecker.CompileToDynamicAssembly() // FSharpAsync<Tuple<FSharpErrorInfo[], int>> Compile(FSharpList<SyntaxTree.ParsedInput> ast, string assemblyName, string outFile, FSharpList<string> dependencies, [OptionalArgument] FSharpOption<string> pdbFile, [OptionalArgument] FSharpOption<bool> executable, [OptionalArgument] FSharpOption<bool> noframework, [OptionalArgument] FSharpOption<string> userOpName); }
internal FSharpParseAndCheckResults(FSharpParseFileResults parseResults, FSharpCheckFileAnswer checkAnswer) { ParseResults = parseResults; CheckAnswer = checkAnswer; }