private void LoadArchive(BackgroundWorker worker, string pathToMappingFile, out SrcMLArchive archive, out AbstractWorkingSet data, out string projectName) { throw new NotImplementedException(); //string pathToArchive = FilePath.GetDirectoryName(pathToMappingFile); //string archiveDirectoryName = FilePath.GetFileName(pathToArchive); //string baseDirectory = FilePath.GetFullPath(FilePath.GetDirectoryName(pathToArchive)); //projectName = FilePath.GetFileName(baseDirectory); //worker.ReportProgress(0, String.Format("Loading {0}", projectName)); //archive = new SrcMLArchive(baseDirectory, archiveDirectoryName); //int numberOfFiles = archive.FileUnits.Count(); //worker.ReportProgress(0, String.Format("Loading {0} ({1} files)", projectName, numberOfFiles)); //data = new DataRepository(archive); //int i = 0; //foreach(var unit in archive.FileUnits) { // try { // data.AddFile(unit); // } catch(Exception) { // } // if(++i % 25 == 0) { // int percentComplete = (int) (100 * (double) i / (double) numberOfFiles); // worker.ReportProgress(percentComplete, String.Format("Loading {0} ({1} / {2} files)", projectName, i, numberOfFiles)); // } //} //worker.ReportProgress(100, String.Format("Loaded {0} ({1} files)", baseDirectory, i)); }
/// <summary> /// Creates a new source monitor /// </summary> /// <param name="solution">The solution to monitor</param> /// <param name="foldersToMonitor">A list of folders to monitor</param> /// <param name="scanInterval">The interval at which to scan the folders (in /// seconds) </param> /// <param name="baseDirectory">The base directory for this monitor</param> /// <param name="defaultArchive">The default archive to route files to</param> /// <param name="otherArchives">Other archives to route files to</param> public SourceMonitor(Solution solution, double scanInterval, TaskScheduler scheduler, string baseDirectory, AbstractArchive defaultArchive, SrcMLArchive sourceArchive, params AbstractArchive[] otherArchives) : base(DirectoryScanningMonitor.MONITOR_LIST_FILENAME, scanInterval, scheduler, baseDirectory, defaultArchive, otherArchives) { if(null != sourceArchive) { RegisterArchive(sourceArchive, false); } this.MonitoredSolution = solution; }
public static XElement GetElement(SrcMLArchive archive, SrcMLLocation location) { string fileName = location.SourceFileName; string query = location.XPath; var unit = archive.GetXElementForSourceFile(fileName); var element = unit.XPathSelectElement(query, SrcMLNamespaces.Manager); return(element); }
public void TestEmptyArchive() { var archive = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation))); Assert.That(archive.IsEmpty); var foo_c = Path.Combine(SourceDirectory, "foo.c"); File.WriteAllText(foo_c, String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine)); archive.AddOrUpdateFile(foo_c); Assert.That(archive.IsEmpty, Is.False); }
public void TestFindMethodCalls_Simple() { using(var sa = new SrcMLArchive("DataRepositoryTests")) { sa.AddOrUpdateFile(@"..\..\TestInputs\function_def.cpp"); var da = new DataRepository(sa); var expected = da.GlobalScope.ChildScopes.OfType<MethodDefinition>().First(md => md.Name == "main").MethodCalls.First(); var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\function_def.cpp", 12, 20)); Assert.IsNotNull(actual); Assert.AreEqual(1, actual.Count); Assert.AreEqual(expected, actual[0]); } }
public void TestEmptyArchive() { var archive = new SrcMLArchive(ArchiveDirectory); Assert.That(archive.IsEmpty); var foo_c = Path.Combine(SourceDirectory, "foo.c"); File.WriteAllText(foo_c, String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine)); archive.AddOrUpdateFile(foo_c); Assert.That(archive.IsEmpty, Is.False); }
public void TestRoundtrip() { var archive = new SrcMLArchive("DataRepositoryTests"); archive.AddOrUpdateFile(@"..\..\TestInputs\A.h"); archive.AddOrUpdateFile(@"..\..\TestInputs\A.cpp"); var data = new DataRepository(archive); data.Save(@"DataRepositoryTests\saved.dar"); var newData = new DataRepository(archive, @"DataRepositoryTests\saved.dar"); Assert.IsTrue(TestHelper.ScopesAreEqual(data.GlobalScope, newData.GlobalScope)); }
public void TestRoundtrip_Self() { var archive = new SrcMLArchive("DataRepositoryTests"); foreach(var csFile in Directory.GetFiles(@"..\..\ABB.SrcML", "*.cs", SearchOption.AllDirectories)) { archive.AddOrUpdateFile(csFile); } var data = new DataRepository(archive); data.Save(@"DataRepositoryTests\saved.dar"); var newData = new DataRepository(archive, @"DataRepositoryTests\saved.dar"); Assert.IsTrue(TestHelper.ScopesAreEqual(data.GlobalScope, newData.GlobalScope)); }
public static XElement GetElement(SrcMLArchive archive, SrcMLLocation location) { string fileName = location.SourceFileName; string query = location.XPath; var unit = archive.GetXElementForSourceFile(fileName); var startingLength = fileName.Length + 23; var path = query.Substring(startingLength); var element = unit.XPathSelectElement(path, SrcMLNamespaces.Manager); return element; }
public static XElement GetElement(SrcMLArchive archive, SrcMLLocation location) { string fileName = location.SourceFileName; string query = location.XPath; var unit = archive.GetXElementForSourceFile(fileName); var startingLength = fileName.Length + 23; var path = query.Substring(startingLength); var element = unit.XPathSelectElement(path, SrcMLNamespaces.Manager); return(element); }
/// <summary> /// Sets up the <see cref="CurrentDataArchive"/> and <see cref="CurrentWorkingSet"/> to respond to events from the srcML service /// </summary> protected override void Setup() { string storagePath = _srcMLService.CurrentMonitor.MonitorStoragePath; SrcMLArchive sourceArchive = _srcMLService.CurrentSrcMLArchive; CurrentDataArchive = new DataArchive(storagePath, sourceArchive, GlobalScheduler); CurrentDataArchive.Generator.ErrorLog = sourceArchive.Generator.ErrorLog; CurrentDataArchive.Generator.IsLoggingErrors = true; _srcMonitor = new ArchiveMonitor <SrcMLArchive>(GlobalScheduler, storagePath, sourceArchive, CurrentDataArchive); CurrentWorkingSet = _workingSetFactories.Default.CreateWorkingSet(storagePath, CurrentDataArchive, GlobalTaskFactory); CurrentWorkingSet.UseAsynchronousMethods = true; }
public void TestQueryDuringUpdate() { var sourceFolder = "TestQueryDuringUpdate"; var dataFolder = "TestQueryDuringUpdate_Data"; Directory.CreateDirectory(sourceFolder); string fooSourcePath = Path.Combine(sourceFolder, "foo.cpp"); string barSourcePath = Path.Combine(sourceFolder, "bar.cpp"); var fooRevisions = new string[] { "void foo() { }", "void foo() { bar(); }" }; File.WriteAllText(fooSourcePath, fooRevisions[1]); File.WriteAllText(barSourcePath, "void bar() { }"); int iterations = 1000; using (var archive = new SrcMLArchive(dataFolder)) { using (var data = new DataRepository(archive)) { data.InitializeData(); archive.AddOrUpdateFile(Path.Combine(sourceFolder, "foo.cpp")); archive.AddOrUpdateFile(Path.Combine(sourceFolder, "bar.cpp")); var developer = new Task(() => { for (int i = 0; i < iterations; i++) { File.WriteAllText(fooSourcePath, fooRevisions[i % 2]); archive.AddOrUpdateFile(fooSourcePath); } }); developer.Start(); Assert.DoesNotThrow(() => { for (int i = 0; i < iterations; i++) { var foo = GetMethodWithName(data, 500, "foo"); Thread.Sleep(1); var bar = GetMethodWithName(data, 500, "bar"); foo.ContainsCallTo(bar); if (i % 10 == 0 && i > 0) { Console.WriteLine("Finished {0} iterations", i); } } }); developer.Wait(); } } }
public MethodDefinition(SrcMLArchive archive, MethodData data, MethodCall fromCall) { this.Archive = archive; this.SourceCall = fromCall; this.Data = data; this.isValid = false; this.Location = data.PrimaryLocation; this.FullName = Data.GetFullName(); this.Id = DataHelpers.GetLocation(Location); this.Path = Location.SourceFileName; this.Signature = GetMethodSignature(); }
public void TestFindScopeForAdjacentMethods() { using (var sa = new SrcMLArchive("DataRepositoryTests")) { sa.AddOrUpdateFile(@"..\..\TestInputs\adjacent_methods.cpp"); using (var da = new DataRepository(sa)) { da.InitializeData(); IScope globalScope; Assert.That(da.TryLockGlobalScope(Timeout.Infinite, out globalScope)); try { var mainMethod = globalScope.GetDescendantScopes <IMethodDefinition>().FirstOrDefault(); var fooMethod = globalScope.GetDescendantScopes <IMethodDefinition>().LastOrDefault(); Assert.IsNotNull(mainMethod, "could not find main()"); Assert.IsNotNull(fooMethod, "could not find foo()"); Assert.AreEqual("main", mainMethod.Name); Assert.AreEqual("Foo", fooMethod.Name); var fileName = @"TestInputs\adjacent_methods.cpp"; var startOfMain = new SourceLocation(fileName, 1, 1); var locationInMain = new SourceLocation(fileName, 1, 11); Assert.That(mainMethod.PrimaryLocation.Contains(startOfMain)); Assert.That(mainMethod.PrimaryLocation.Contains(locationInMain)); Assert.AreEqual(mainMethod, da.FindScope(startOfMain)); Assert.AreEqual(mainMethod, da.FindScope(locationInMain)); var startOfFoo = new SourceLocation(fileName, 3, 1); var locationInFoo = new SourceLocation(fileName, 3, 11); Assert.That(fooMethod.PrimaryLocation.Contains(startOfFoo)); Assert.That(fooMethod.PrimaryLocation.Contains(locationInFoo)); Assert.AreEqual(fooMethod, da.FindScope(startOfFoo)); Assert.AreEqual(fooMethod, da.FindScope(locationInFoo)); var lineBetweenMethods = new SourceLocation(fileName, 2, 1); Assert.That(mainMethod.PrimaryLocation.Contains(lineBetweenMethods)); Assert.IsFalse(fooMethod.PrimaryLocation.Contains(lineBetweenMethods)); Assert.AreEqual(mainMethod, da.FindScope(lineBetweenMethods)); } finally { da.ReleaseGlobalScopeLock(); } } } }
/// <summary> /// Gets the XElement referred to by <see cref="XPath"/>. /// </summary> /// <param name="archive">The archive for this location</param> /// <returns>The XElement referred to by <see cref="XPath"/></returns> public XElement GetXElement(SrcMLArchive archive) { if (null == archive) { throw new ArgumentNullException("archive"); } var unit = archive.GetXElementForSourceFile(this.SourceFileName); if (unit != null) { return(unit.XPathSelectElement(this.XPath, SrcMLNamespaces.Manager)); } return(null); }
public MethodCallSample(SrcMLArchive archive, AbstractWorkingSet data, int sampleSize) { this.Archive = archive; this.Data = data; this.Date = DateTime.Now; this._projectName = string.Empty; this.SampleSize = sampleSize; FirstMatchIsValid = new Statistic("FirstMatchIsValid", SampleSize); HasMatches = new Statistic("HasMatches", SampleSize); HasNoMatches = new Statistic("HasNoMatches", SampleSize); IsExternal = new Statistic("IsExternal", SampleSize); FirstMatchIsValid.PropertyChanged += StatisticChanged; HasMatches.PropertyChanged += StatisticChanged; HasNoMatches.PropertyChanged += StatisticChanged; IsExternal.PropertyChanged += StatisticChanged; }
public void CheckSrcMLFilesForNewCPPProject(bool flag) { SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string addedCPPProjectFolder = Path.Combine(testFileTemplateFolder, @"ConsoleApplication1\ConsoleApplication1"); string sourcePath1 = Path.Combine(addedCPPProjectFolder, "stdafx.cpp"); string sourcePath2 = Path.Combine(addedCPPProjectFolder, "stdafx.h"); string sourcePath3 = Path.Combine(addedCPPProjectFolder, "targetver.h"); string sourcePath4 = Path.Combine(addedCPPProjectFolder, "ConsoleApplication1.cpp"); string srcMLPath1 = archive.GetXmlPathForSourcePath(sourcePath1); string srcMLPath2 = archive.GetXmlPathForSourcePath(sourcePath2); string srcMLPath3 = archive.GetXmlPathForSourcePath(sourcePath3); string srcMLPath4 = archive.GetXmlPathForSourcePath(sourcePath4); if (flag) //add { Assert.IsTrue(File.Exists(sourcePath1), "The source file [" + sourcePath1 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath2), "The source file [" + sourcePath2 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath3), "The source file [" + sourcePath3 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath4), "The source file [" + sourcePath4 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath1), "The srcML file [" + srcMLPath1 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath2), "The srcML file [" + srcMLPath2 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath3), "The srcML file [" + srcMLPath3 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath4), "The srcML file [" + srcMLPath4 + "] does not exist."); Assert.AreEqual(new FileInfo(sourcePath1).LastWriteTime, new FileInfo(srcMLPath1).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath2).LastWriteTime, new FileInfo(srcMLPath2).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath3).LastWriteTime, new FileInfo(srcMLPath3).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath4).LastWriteTime, new FileInfo(srcMLPath4).LastWriteTime); XElement xelement1 = srcMLService.GetXElementForSourceFile(sourcePath1); XElement xelement2 = srcMLService.GetXElementForSourceFile(sourcePath2); XElement xelement3 = srcMLService.GetXElementForSourceFile(sourcePath3); XElement xelement4 = srcMLService.GetXElementForSourceFile(sourcePath4); Assert.IsNotNull(xelement1, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement2, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement3, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement4, "GetXElementForSourceFile returned null."); } else //remove { Assert.IsFalse(File.Exists(srcMLPath1), "The srcML file [" + srcMLPath1 + "] still exists."); Assert.IsFalse(File.Exists(srcMLPath2), "The srcML file [" + srcMLPath2 + "] still exists."); Assert.IsFalse(File.Exists(srcMLPath3), "The srcML file [" + srcMLPath3 + "] still exists."); Assert.IsFalse(File.Exists(srcMLPath4), "The srcML file [" + srcMLPath4 + "] still exists."); } }
public void GenerateXmlAndStringForSourceTest() { IFileMonitor watchedFolder = Substitute.For <IFileMonitor>(); var archive = new SrcMLArchive(watchedFolder, ".srcml"); archive.XmlGenerator.ApplicationDirectory = TestConstants.SrcmlPath; var xmlDirectory = new DirectoryInfo(archive.ArchivePath); File.WriteAllText(SOURCEDIRECTORY + "\\foo.c", String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\bar.c", String.Format(@"int bar() {{{0} printf(""goodbye, world!"");{0}}}", Environment.NewLine)); string fooXML = archive.GenerateXmlAndStringForSource(SOURCEDIRECTORY + "\\foo.c"); string barXML = archive.GenerateXmlAndStringForSource(SOURCEDIRECTORY + "\\bar.c"); Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4X88LX7RR.xml"))); Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4Z4UW37RR.xml"))); }
public void GenerateXmlForSourceTest() { IFileMonitor watchedFolder = Substitute.For <IFileMonitor>(); var archive = new SrcMLArchive(watchedFolder, ".srcml"); archive.XmlGenerator.ApplicationDirectory = TestConstants.SrcmlPath; var xmlDirectory = new DirectoryInfo(archive.ArchivePath); File.WriteAllText(SOURCEDIRECTORY + "\\foo.c", String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\bar.c", String.Format(@"int bar() {{{0} printf(""goodbye, world!"");{0}}}", Environment.NewLine)); archive.GenerateXmlForSource(SOURCEDIRECTORY + "\\foo.c"); archive.GenerateXmlForSource(SOURCEDIRECTORY + "\\bar.c"); Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "foo.c.xml"))); Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "bar.c.xml"))); }
public void CheckCSharpSolutionStartup() { SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string sourcePath = Path.Combine(testCSharpProjectFolder, "Class1.cs"); string srcMLPath = archive.GetXmlPathForSourcePath(sourcePath); Assert.IsTrue(File.Exists(sourcePath), "The source file [" + sourcePath + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath), "The srcML file [" + srcMLPath + "] does not exist."); Assert.AreEqual(new FileInfo(sourcePath).LastWriteTime, new FileInfo(srcMLPath).LastWriteTime); XElement xelement = srcMLService.GetXElementForSourceFile(sourcePath); Assert.IsNotNull(xelement, "GetXElementForSourceFile returned null."); string sourcePathX = Path.Combine(testCSharpProjectFolder, "AlreadyDeletedClass1.cs"); XElement xelementX = srcMLService.GetXElementForSourceFile(sourcePathX); Assert.IsNull(xelementX, "GetXElementForSourceFile returned not null."); }
public MethodCall(SrcMLArchive archive, CallData data) { this.Archive = archive; this.Data = data; this.firstMatchIsValid = false; this.hasMatches = false; this.hasNoMatches = false; this.isExternal = false; this.Location = data.Location; //TODO fix this once new type hierarchy is in place //this.FullName = data.ParentScope.GetParentScopesAndSelf<NamedScope>().First().GetFullName(); this.Id = DataHelpers.GetLocation(data.Location); this.Path = this.Location.SourceFileName; this.numberOfValidMatches = 0; this.SourceCode = GetSourceCode(); PossibleMatches = new ObservableCollection<MethodDefinition>(GetMatches()); StartMonitoringDefinitions(); }
public void TestFindMethodCalls_Nested() { using(var sa = new SrcMLArchive("DataRepositoryTests")) { sa.AddOrUpdateFile(@"..\..\TestInputs\nested_method_calls.cpp"); var da = new DataRepository(sa); var foo = da.GlobalScope.ChildScopes.OfType<MethodDefinition>().First(md => md.Name == "Foo"); var expected = new[] { foo.MethodCalls.First(mc => mc.Name == "ToString"), foo.MethodCalls.First(mc => mc.Name == "SomeMethodCall"), foo.MethodCalls.First(mc => mc.Name == "printf") }; var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\nested_method_calls.cpp", 4, 41)); Assert.IsNotNull(actual); Assert.AreEqual(expected.Length, actual.Count); for(int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } } }
public void TestGetXElement() { var archive = new SrcMLArchive("SrcMLLocationTest", false, new SrcMLGenerator("SrcML")); var sourcePath = Path.GetFullPath(@"..\..\TestInputs\class_test.h"); archive.AddOrUpdateFile(sourcePath); var unit = archive.GetXElementForSourceFile(sourcePath); Assert.IsNotNull(unit); var classElement = unit.Descendants(SRC.Class).FirstOrDefault(); Assert.IsNotNull(classElement); var parser = new CPlusPlusCodeParser(); var globalScope = parser.ParseFileUnit(unit); var typeDefinition = globalScope.ChildStatements.OfType<TypeDefinition>().FirstOrDefault(); Assert.IsNotNull(typeDefinition); var element = typeDefinition.PrimaryLocation.GetXElement(archive); Assert.IsNotNull(element); Assert.AreEqual(classElement.GetSrcLineNumber(), element.GetSrcLineNumber()); Assert.AreEqual(classElement.GetSrcLinePosition(), element.GetSrcLinePosition()); Assert.AreEqual(classElement.GetXPath(), element.GetXPath()); }
public void TestFindMethodCalls_Simple() { using (var sa = new SrcMLArchive("DataRepositoryTests")) { sa.AddOrUpdateFile(@"..\..\TestInputs\function_def.cpp"); using (var da = new DataRepository(sa)) { da.InitializeData(); IScope globalScope; Assert.That(da.TryLockGlobalScope(Timeout.Infinite, out globalScope)); try { var expected = globalScope.ChildScopes.OfType <IMethodDefinition>().First(md => md.Name == "main").MethodCalls.First(); var actual = da.FindMethodCalls(new SourceLocation(@"TestInputs\function_def.cpp", 12, 20)); Assert.IsNotNull(actual); Assert.AreEqual(1, actual.Count); Assert.AreEqual(expected, actual[0]); } finally { da.ReleaseGlobalScopeLock(); } } } }
public MethodCall(SrcMLArchive archive, CallData data) { this.Archive = archive; this.Data = data; this.firstMatchIsValid = false; this.hasMatches = false; this.hasNoMatches = false; this.isExternal = false; this.Location = data.Location; //TODO fix this once new type hierarchy is in place //this.FullName = data.ParentScope.GetParentScopesAndSelf<NamedScope>().First().GetFullName(); this.Id = DataHelpers.GetLocation(data.Location); this.Path = this.Location.SourceFileName; this.numberOfValidMatches = 0; this.SourceCode = GetSourceCode(); PossibleMatches = new ObservableCollection <MethodDefinition>(GetMatches()); StartMonitoringDefinitions(); }
public void CheckCPPSolutionStartup() { SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string sourcePath1 = Path.Combine(testCPPProjectFolder, "stdafx.cpp"); string sourcePath2 = Path.Combine(testCPPProjectFolder, "stdafx.h"); string sourcePath3 = Path.Combine(testCPPProjectFolder, "targetver.h"); string sourcePath4 = Path.Combine(testCPPProjectFolder, "TestCPPSolution.cpp"); string srcMLPath1 = archive.GetXmlPathForSourcePath(sourcePath1); string srcMLPath2 = archive.GetXmlPathForSourcePath(sourcePath2); string srcMLPath3 = archive.GetXmlPathForSourcePath(sourcePath3); string srcMLPath4 = archive.GetXmlPathForSourcePath(sourcePath4); Assert.IsTrue(File.Exists(sourcePath1), "The source file [" + sourcePath1 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath2), "The source file [" + sourcePath2 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath3), "The source file [" + sourcePath3 + "] does not exist."); Assert.IsTrue(File.Exists(sourcePath4), "The source file [" + sourcePath4 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath1), "The srcML file [" + srcMLPath1 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath2), "The srcML file [" + srcMLPath2 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath3), "The srcML file [" + srcMLPath3 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath4), "The srcML file [" + srcMLPath4 + "] does not exist."); Assert.AreEqual(new FileInfo(sourcePath1).LastWriteTime, new FileInfo(srcMLPath1).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath2).LastWriteTime, new FileInfo(srcMLPath2).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath3).LastWriteTime, new FileInfo(srcMLPath3).LastWriteTime); Assert.AreEqual(new FileInfo(sourcePath4).LastWriteTime, new FileInfo(srcMLPath4).LastWriteTime); XElement xelement1 = srcMLService.GetXElementForSourceFile(sourcePath1); XElement xelement2 = srcMLService.GetXElementForSourceFile(sourcePath2); XElement xelement3 = srcMLService.GetXElementForSourceFile(sourcePath3); XElement xelement4 = srcMLService.GetXElementForSourceFile(sourcePath4); Assert.IsNotNull(xelement1, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement2, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement3, "GetXElementForSourceFile returned null."); Assert.IsNotNull(xelement4, "GetXElementForSourceFile returned null."); string sourcePathX = Path.Combine(testCSharpProjectFolder, "AlreadyDeletedClass1.cpp"); XElement xelementX = srcMLService.GetXElementForSourceFile(sourcePathX); Assert.IsNull(xelementX, "GetXElementForSourceFile returned not null."); }
public void TestFindScopeForAdjacentMethods() { using(var sa = new SrcMLArchive("DataRepositoryTests")) { sa.AddOrUpdateFile(@"..\..\TestInputs\adjacent_methods.cpp"); var da = new DataRepository(sa); var mainMethod = da.GlobalScope.GetDescendantScopes<MethodDefinition>().FirstOrDefault(); var fooMethod = da.GlobalScope.GetDescendantScopes<MethodDefinition>().LastOrDefault(); Assert.IsNotNull(mainMethod, "could not find main()"); Assert.IsNotNull(fooMethod, "could not find foo()"); Assert.AreEqual("main", mainMethod.Name); Assert.AreEqual("Foo", fooMethod.Name); var fileName = @"TestInputs\adjacent_methods.cpp"; var startOfMain = new SourceLocation(fileName, 1, 1); var locationInMain = new SourceLocation(fileName, 1, 11); Assert.That(mainMethod.PrimaryLocation.Contains(startOfMain)); Assert.That(mainMethod.PrimaryLocation.Contains(locationInMain)); Assert.AreEqual(mainMethod, da.FindScope(startOfMain)); Assert.AreEqual(mainMethod, da.FindScope(locationInMain)); var startOfFoo = new SourceLocation(fileName, 3, 1); var locationInFoo = new SourceLocation(fileName, 3, 11); Assert.That(fooMethod.PrimaryLocation.Contains(startOfFoo)); Assert.That(fooMethod.PrimaryLocation.Contains(locationInFoo)); Assert.AreEqual(fooMethod, da.FindScope(startOfFoo)); Assert.AreEqual(fooMethod, da.FindScope(locationInFoo)); var lineBetweenMethods = new SourceLocation(fileName, 2, 1); Assert.That(mainMethod.PrimaryLocation.Contains(lineBetweenMethods)); Assert.IsFalse(fooMethod.PrimaryLocation.Contains(lineBetweenMethods)); Assert.AreEqual(mainMethod, da.FindScope(lineBetweenMethods)); } }
public void CheckSrcMLFilesForNewCSharpProject(bool flag) { SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string addedCSharpProjectFolder = Path.Combine(testFileTemplateFolder, @"ClassLibrary1\ClassLibrary1"); string sourcePath1 = Path.Combine(addedCSharpProjectFolder, "Class1.cs"); string srcMLPath1 = archive.GetXmlPathForSourcePath(sourcePath1); if (flag) //add { Assert.IsTrue(File.Exists(sourcePath1), "The source file [" + sourcePath1 + "] does not exist."); Assert.IsTrue(File.Exists(srcMLPath1), "The srcML file [" + srcMLPath1 + "] does not exist."); Assert.AreEqual(new FileInfo(sourcePath1).LastWriteTime, new FileInfo(srcMLPath1).LastWriteTime); XElement xelement1 = srcMLService.GetXElementForSourceFile(sourcePath1); Assert.IsNotNull(xelement1, "GetXElementForSourceFile returned null."); } else //remove { Assert.IsFalse(File.Exists(srcMLPath1), "The srcML file [" + srcMLPath1 + "] still exists."); } }
public void CheckSrcMLFiles() { string sourcePath = fera.FilePath; string oldSourcePath = fera.OldFilePath; FileEventType type = fera.EventType; bool hasSrcML = fera.HasSrcML; if (type == FileEventType.FileAdded || type == FileEventType.FileChanged) { Assert.IsTrue((receivedFileAdded || receivedFileUpdated)); if (hasSrcML) { SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string srcMLPath = archive.GetXmlPathForSourcePath(sourcePath); ////WriteLog(logFilePath, "Adding/Updating srcMLPath = " + srcMLPath); Assert.IsTrue(File.Exists(srcMLPath), "The srcML file [" + srcMLPath + "] does not exist."); Assert.AreEqual(new FileInfo(sourcePath).LastWriteTime, new FileInfo(srcMLPath).LastWriteTime); XElement xelement = srcMLService.GetXElementForSourceFile(sourcePath); Assert.IsNotNull(xelement, "GetXElementForSourceFile returned null."); } } else if (type == FileEventType.FileDeleted) { Assert.IsTrue(receivedFileDeleted); SrcMLArchive archive = srcMLService.GetSrcMLArchive(); Assert.IsNotNull(archive, "GetSrcMLArchive returned null."); string srcMLPath = archive.GetXmlPathForSourcePath(sourcePath); ////WriteLog(logFilePath, "Deleting srcMLPath = " + srcMLPath); Assert.IsFalse(File.Exists(srcMLPath), "The srcML file [" + srcMLPath + "] still exists."); XElement xelementX = srcMLService.GetXElementForSourceFile(sourcePath); Assert.IsNull(xelementX, "GetXElementForSourceFile returned not null."); } receivedFileAdded = receivedFileUpdated = receivedFileDeleted = false; fera = null; }
/// <summary> /// Gets the XElement referred to by <see cref="XPath"/>. /// </summary> /// <param name="archive">The archive for this location</param> /// <returns>The XElement referred to by <see cref="XPath"/></returns> public XElement GetXElement(SrcMLArchive archive) { if(null == archive) throw new ArgumentNullException("archive"); var unit = archive.GetXElementForSourceFile(this.SourceFileName); if(unit != null) { return unit.XPathSelectElement(this.XPath, SrcMLNamespaces.Manager); } return null; }
public void GenerateXmlForDirectoryTest() { IFileMonitor watchedFolder = Substitute.For <IFileMonitor>(); var archive = new SrcMLArchive(watchedFolder, ".srcml"); archive.XmlGenerator.ApplicationDirectory = TestConstants.SrcmlPath; var xmlDirectory = new DirectoryInfo(archive.ArchivePath); File.WriteAllText(SOURCEDIRECTORY + "\\foo.c", String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\bar.c", String.Format(@"int bar() {{{0} printf(""goodbye, world!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir1")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\foo1.c", String.Format(@"int foo1() {{{0}printf(""hello world 1!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\bar1.c", String.Format(@"int bar1() {{{0} printf(""goodbye, world 1!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir2")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\foo2.c", String.Format(@"int foo2() {{{0}printf(""hello world 2!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\bar2.c", String.Format(@"int bar2() {{{0} printf(""goodbye, world 2!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir1\\subdir11")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\subdir11\\foo11.c", String.Format(@"int foo11() {{{0}printf(""hello world 11!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\subdir11\\bar11.c", String.Format(@"int bar11() {{{0} printf(""goodbye, world 11!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir1\\subdir12")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\subdir12\\foo12.c", String.Format(@"int foo12() {{{0}printf(""hello world 12!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir1\\subdir12\\bar12.c", String.Format(@"int bar12() {{{0} printf(""goodbye, world 12!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir2\\subdir21")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\subdir21\\foo21.c", String.Format(@"int foo21() {{{0}printf(""hello world 21!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\subdir21\\bar21.c", String.Format(@"int bar21() {{{0} printf(""goodbye, world 21!"");{0}}}", Environment.NewLine)); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir2\\subdir22")); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\subdir22\\foo22.c", String.Format(@"int foo22() {{{0}printf(""hello world 22!"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\subdir22\\bar22.c", String.Format(@"int bar22() {{{0} printf(""goodbye, world 22!"");{0}}}", Environment.NewLine)); System.Threading.Thread.Sleep(5000); ////archive.GenerateXmlForDirectory(SOURCEDIRECTORY); /* * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "foo.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "bar.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\foo1.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\bar1.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\foo2.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\bar2.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir11\\foo11.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir11\\bar11.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir12\\foo12.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir12\\bar12.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir21\\foo21.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir21\\bar21.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir22\\foo22.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir22\\bar22.c.xml"))); * Assert.That(archive.FileUnits.Count(), Is.EqualTo(14)); */ File.WriteAllText(SOURCEDIRECTORY + "\\foo.c", String.Format(@"int foo() {{{0}printf(""hello world! changed"");{0}}}", Environment.NewLine)); File.WriteAllText(SOURCEDIRECTORY + "\\subdir2\\subdir21\\bar21.c", String.Format(@"int bar21() {{{0} printf(""goodbye, world 21! changed"");{0}}}", Environment.NewLine)); File.Delete("C:\\Users\\USJIZHE\\Documents\\GitHub\\SrcML.NET\\Build\\Debug\\testSourceDir\\subdir1\\subdir12\\bar12.c"); File.Move("C:\\Users\\USJIZHE\\Documents\\GitHub\\SrcML.NET\\Build\\Debug\\testSourceDir\\subdir1\\subdir11\\foo11.c", "C:\\Users\\USJIZHE\\Documents\\GitHub\\SrcML.NET\\Build\\Debug\\testSourceDir\\subdir1\\subdir11\\foo1111111.c"); System.Threading.Thread.Sleep(5000); ////archive.GenerateXmlForDirectory(SOURCEDIRECTORY); /* * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "foo.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "bar.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\foo1.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\bar1.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\foo2.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\bar2.c.xml"))); * Assert.That(!File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir11\\foo11.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir11\\foo1111111.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir11\\bar11.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir12\\foo12.c.xml"))); * Assert.That(!File.Exists(Path.Combine(xmlDirectory.FullName, "subdir1\\subdir12\\bar12.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir21\\foo21.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir21\\bar21.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir22\\foo22.c.xml"))); * Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "subdir2\\subdir22\\bar22.c.xml"))); * Assert.That(archive.FileUnits.Count(), Is.EqualTo(13)); */ }
public void GenerateXmlForDirectoryTest() { ManualResetEvent resetEvent = new ManualResetEvent(false); var archive = new SrcMLArchive(ArchiveDirectory, false, new SrcMLGenerator(Path.Combine(SrcMLHelper.GetSrcMLRootDirectory(), SrcMLHelper.srcMLExecutableLocation))); FileEventType expectedEventType = FileEventType.FileAdded; FileEventType actualEventType = FileEventType.FileChanged; archive.FileChanged += (sender, e) => { actualEventType = e.EventType; bool shouldHaveSrcML = (e.EventType != FileEventType.FileDeleted); Assert.AreEqual(shouldHaveSrcML, e.HasSrcML); resetEvent.Set(); }; Dictionary <string, string> sourceFiles = new Dictionary <string, string>() { { Path.Combine(SourceDirectory, "foo.c"), String.Format(@"int foo() {{{0}printf(""hello world!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "bar.c"), String.Format(@"int bar() {{{0} printf(""goodbye, world!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "foo1.c"), String.Format(@"int foo1() {{{0}printf(""hello world 1!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "bar1.c"), String.Format(@"int bar1() {{{0} printf(""goodbye, world 1!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "foo2.c"), String.Format(@"int foo2() {{{0}printf(""hello world 2!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "bar2.c"), String.Format(@"int bar2() {{{0} printf(""goodbye, world 2!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c"), String.Format(@"int foo11() {{{0}printf(""hello world 11!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "subdir11", "bar11.c"), String.Format(@"int bar11() {{{0} printf(""goodbye, world 11!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "subdir12", "foo12.c"), String.Format(@"int foo12() {{{0}printf(""hello world 12!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c"), String.Format(@"int bar12() {{{0} printf(""goodbye, world 12!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "subdir21", "foo21.c"), String.Format(@"int foo21() {{{0}printf(""hello world 21!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "subdir21", "bar21.c"), String.Format(@"int bar21() {{{0} printf(""goodbye, world 21!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "subdir22", "foo22.c"), String.Format(@"int foo22() {{{0}printf(""hello world 22!"");{0}}}", Environment.NewLine) }, { Path.Combine(SourceDirectory, "subdir2", "subdir22", "bar22.c"), String.Format(@"int bar22() {{{0} printf(""goodbye, world 22!"");{0}}}", Environment.NewLine) }, }; foreach (var fileDataPair in sourceFiles) { var directory = Path.GetDirectoryName(fileDataPair.Key); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } File.WriteAllText(fileDataPair.Key, fileDataPair.Value); archive.AddOrUpdateFile(fileDataPair.Key); Assert.That(resetEvent.WaitOne(300)); Assert.AreEqual(expectedEventType, actualEventType); } foreach (var fileName in sourceFiles.Keys) { Assert.That(archive.ContainsFile(fileName), String.Format("Archive should contain {0}", fileName)); } var changedFileName = Path.Combine(SourceDirectory, "foo.c"); var changedFileContents = String.Format(@"int foo() {{{0}printf(""hello world! changed"");{0}}}", Environment.NewLine); expectedEventType = FileEventType.FileChanged; File.WriteAllText(changedFileName, changedFileContents); File.SetLastWriteTime(changedFileName, DateTime.Now); Assert.That(archive.ContainsFile(changedFileName)); Assert.That(archive.IsOutdated(changedFileName)); archive.AddOrUpdateFile(changedFileName); Assert.That(resetEvent.WaitOne(300)); Assert.AreEqual(expectedEventType, actualEventType); expectedEventType = FileEventType.FileDeleted; var deletedFileName = Path.Combine(SourceDirectory, "subdir1", "subdir12", "bar12.c"); File.Delete(deletedFileName); Assert.That(archive.IsOutdated(deletedFileName)); archive.DeleteFile(deletedFileName); Assert.That(resetEvent.WaitOne(300)); Assert.AreEqual(expectedEventType, actualEventType); expectedEventType = FileEventType.FileRenamed; var movedFileName = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo11.c"); var newNameForMoved = Path.Combine(SourceDirectory, "subdir1", "subdir11", "foo1111111.c"); File.Move(movedFileName, newNameForMoved); Assert.That(archive.IsOutdated(movedFileName)); archive.RenameFile(movedFileName, newNameForMoved); Assert.That(resetEvent.WaitOne(300)); Assert.AreEqual(expectedEventType, actualEventType); Assert.That(archive.ContainsFile(newNameForMoved)); Assert.IsFalse(archive.ContainsFile(movedFileName)); }
private static void GenerateData(string sourcePath, string dataPath, string csvDirectory) { Dictionary <Language, ICodeParser> CodeParser = new Dictionary <Language, ICodeParser>() { { Language.CPlusPlus, new CPlusPlusCodeParser() }, { Language.Java, new JavaCodeParser() }, { Language.CSharp, new CSharpCodeParser() } }; string fileLogPath = Path.Combine(dataPath, "parse.log"); string callLogPath = Path.Combine(dataPath, "methodcalls.log"); string csvPath = Path.Combine(csvDirectory, "timing.csv"); string jsonPath = String.Format("{0}.json", Path.Combine(@"c:\Workspace\DataVisualization", dataPath.Substring(23))); if (!Directory.Exists(sourcePath)) { Console.Error.WriteLine("{0} does not exist", sourcePath); return; } if (File.Exists(callLogPath)) { File.Delete(callLogPath); } if (File.Exists(fileLogPath)) { File.Delete(fileLogPath); } var archive = new SrcMLArchive(dataPath); archive.XmlGenerator.ExtensionMapping[".cxx"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".c"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".cc"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".hpp"] = Language.CPlusPlus; AbstractFileMonitor monitor = new FileSystemFolderMonitor(sourcePath, dataPath, new LastModifiedArchive(dataPath), archive); ManualResetEvent mre = new ManualResetEvent(false); Stopwatch timer = new Stopwatch(); bool startupCompleted = false; monitor.UpdateArchivesCompleted += (o, e) => { timer.Stop(); startupCompleted = true; mre.Set(); }; timer.Start(); var task = monitor.UpdateArchivesAsync(); string[] spinner = new string[] { "\\\r", "|\r", "/\r" }; int spinner_index = -1; while (!startupCompleted) { spinner_index = (++spinner_index) % 3; Console.Write("Updating archive for {0}... {1}", sourcePath, spinner[spinner_index]); startupCompleted = mre.WaitOne(5000); } timer.Stop(); Console.WriteLine("Updating archive for {0}... {1}", sourcePath, timer.Elapsed); IScope globalScope = null; timer.Reset(); int numberOfFailures = 0; int numberOfSuccesses = 0; int numberOfFiles = 0; Dictionary <string, List <string> > errors = new Dictionary <string, List <string> >(); if (!File.Exists(csvPath)) { File.WriteAllLines(csvPath, new string[] { String.Join(",", "Project", "Files", "Failures", "Time (s)") }); } using (StreamWriter fileLog = new StreamWriter(fileLogPath), csvFile = new StreamWriter(csvPath, true)) { timer.Start(); foreach (var unit in archive.FileUnits) { var fileName = SrcMLElement.GetFileNameForUnit(unit); var language = SrcMLElement.GetLanguageForUnit(unit); try { var scopeForUnit = CodeParser[language].ParseFileUnit(unit); if (null == globalScope) { globalScope = scopeForUnit; } else { globalScope = globalScope.Merge(scopeForUnit); } timer.Stop(); fileLog.WriteLine("Parsing {0} PASSED", fileName); numberOfSuccesses++; } catch (Exception e) { timer.Stop(); fileLog.WriteLine("Parsing {0} FAILED", fileName); fileLog.WriteLine(e.StackTrace); var key = e.StackTrace.Split('\n')[0].Trim(); if (!errors.ContainsKey(key)) { errors[key] = new List <string>(); } errors[key].Add(fileName); numberOfFailures++; } finally { if (++numberOfFiles % 50 == 0) { Console.Write("{0,5:N0} files completed in {1} with {2,5:N0} failures\r", numberOfFiles, timer.Elapsed, numberOfFailures); csvFile.WriteLine(string.Join(",", sourcePath, numberOfFiles, numberOfFailures, timer.Elapsed.TotalSeconds)); } timer.Start(); } } } timer.Stop(); Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, timer.Elapsed, numberOfFailures); Console.WriteLine("\nSummary"); Console.WriteLine("==================="); Console.WriteLine("{0,10:N0} failures ({1,8:P2})", numberOfFailures, ((float)numberOfFailures) / numberOfFiles); Console.WriteLine("{0,10:N0} successes ({1,8:P2})", numberOfSuccesses, ((float)numberOfSuccesses) / numberOfFiles); Console.WriteLine("{0} to generate data", timer.Elapsed); Console.WriteLine("See parse log at {0}", fileLogPath); OutputCallGraphByType(globalScope, jsonPath); PrintScopeReport(globalScope, sourcePath, csvDirectory); PrintMethodCallReport(globalScope, sourcePath, csvDirectory, callLogPath); }
/// <summary> /// Creates a new data archive /// </summary> /// <param name="baseDirectory">The base directory</param> /// <param name="dataDirectory">the directory to store the data files in. This will be created as a subdirectory of <paramref name="baseDirectory"/></param> /// <param name="useExistingData">If true, any existing data files in <see cref="AbstractArchive.ArchivePath"/> will be used.</param> /// <param name="monitoredArchive">The srcML archive to monitor</param> public DataArchive(string baseDirectory, string dataDirectory, bool useExistingData, SrcMLArchive monitoredArchive) : this(baseDirectory, dataDirectory, useExistingData, monitoredArchive, new DataGenerator(), new DataFileNameMapping(Path.Combine(baseDirectory, dataDirectory)), TaskScheduler.Default) { }
private void TestDataGeneration(string sourcePath, string dataPath, bool useAsyncMethods=false) { string fileLogPath = Path.Combine(dataPath, "parse.log"); string callLogPath = Path.Combine(dataPath, "methodcalls.log"); bool regenerateSrcML = shouldRegenerateSrcML; if(!Directory.Exists(sourcePath)) { Assert.Ignore("Source code for is missing"); } if(File.Exists(callLogPath)) { File.Delete(callLogPath); } if(File.Exists(fileLogPath)) { File.Delete(fileLogPath); } if(!Directory.Exists(dataPath)) { regenerateSrcML = true; } else if(shouldRegenerateSrcML) { Directory.Delete(dataPath, true); } var archive = new SrcMLArchive(dataPath, regenerateSrcML); archive.XmlGenerator.ExtensionMapping[".cxx"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".c"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".cc"] = Language.CPlusPlus; AbstractFileMonitor monitor = new FileSystemFolderMonitor(sourcePath, dataPath, new LastModifiedArchive(dataPath), archive); monitor.UseAsyncMethods = useAsyncMethods; ManualResetEvent mre = new ManualResetEvent(false); DateTime start, end = DateTime.MinValue; bool startupCompleted = false; //monitor.IsReadyChanged += (o, e) => { archive.IsReadyChanged += (o, e) => { if(e.ReadyState) { end = DateTime.Now; startupCompleted = true; mre.Set(); } }; start = DateTime.Now; monitor.Startup(); startupCompleted = mre.WaitOne(120000); if(!startupCompleted) { end = DateTime.Now; } Console.WriteLine("{0} to {1} srcML", end - start, (regenerateSrcML ? "generate" : "verify")); Assert.That(startupCompleted); Scope globalScope = null; int numberOfFailures = 0; int numberOfSuccesses = 0; int numberOfFiles = 0; Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>(); start = DateTime.Now; using(var fileLog = new StreamWriter(fileLogPath)) { foreach(var unit in archive.FileUnits) { if(++numberOfFiles % 100 == 0) { Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, DateTime.Now - start, numberOfFailures); } var fileName = SrcMLElement.GetFileNameForUnit(unit); var language = SrcMLElement.GetLanguageForUnit(unit); fileLog.Write("Parsing {0}", fileName); try { var scopeForUnit = CodeParser[language].ParseFileUnit(unit); if(null == globalScope) { globalScope = scopeForUnit; } else { globalScope = globalScope.Merge(scopeForUnit); } fileLog.WriteLine(" PASSED"); numberOfSuccesses++; } catch(Exception e) { fileLog.WriteLine(" FAILED"); fileLog.WriteLine(e.StackTrace); var key = e.StackTrace.Split('\n')[0].Trim(); if(!errors.ContainsKey(key)) { errors[key] = new List<string>(); } errors[key].Add(fileName); numberOfFailures++; } } } end = DateTime.Now; Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, end - start, numberOfFailures); Console.WriteLine("\nSummary"); Console.WriteLine("==================="); Console.WriteLine("{0,10:N0} failures ({1,8:P2})", numberOfFailures, ((float)numberOfFailures) / numberOfFiles); Console.WriteLine("{0,10:N0} successes ({1,8:P2})", numberOfSuccesses, ((float)numberOfSuccesses) / numberOfFiles); Console.WriteLine("{0} to generate data", end - start); Console.WriteLine(fileLogPath); PrintScopeReport(globalScope); PrintMethodCallReport(globalScope, callLogPath); PrintErrorReport(errors); monitor.Dispose(); //Assert.AreEqual(numberOfFailures, (from e in errors.Values select e.Count).Sum()); //Assert.AreEqual(0, numberOfFailures); }
public void TestLoad_BadFile() { var archive = new SrcMLArchive("DataRepositoryTests"); var data = new DataRepository(archive, @"..\..\TestInputs\bad_file.dar"); }
/// <summary> /// Creates a new source monitor /// </summary> /// <param name="solution">The solution to monitor</param> /// <param name="foldersToMonitor">A list of folders to monitor</param> /// <param name="baseDirectory">The base directory for this monitor</param> /// <param name="defaultArchive">The default archive to route files to</param> /// <param name="otherArchives">Other archives to route files to</param> public SourceMonitor(Solution solution, string baseDirectory, AbstractArchive defaultArchive, SrcMLArchive sourceArchive, params AbstractArchive[] otherArchives) : this(solution, DEFAULT_SCAN_INTERVAL, TaskScheduler.Default, baseDirectory, defaultArchive, sourceArchive, otherArchives) { }
/// <summary> /// Creates a new data archive /// </summary> /// <param name="baseDirectory">The base directory</param> /// <param name="monitoredArchive">The srcML archive to monitor</param> /// <param name="scheduler">The task scheduler to use for asynchronous tasks</param> public DataArchive(string baseDirectory, SrcMLArchive monitoredArchive, TaskScheduler scheduler) : this(baseDirectory, DEFAULT_ARCHIVE_DIRECTORY, true, monitoredArchive, new DataGenerator(), new DataFileNameMapping(Path.Combine(baseDirectory, DEFAULT_ARCHIVE_DIRECTORY)), scheduler) { }
public void FileAddDeleteChangeRenameTest() { int numberOfEventsRaised = 0; IFileMonitor watchedFolder = Substitute.For <IFileMonitor>(); var archive = new SrcMLArchive(watchedFolder, ".srcml"); archive.XmlGenerator.ApplicationDirectory = TestConstants.SrcmlPath; var xmlDirectory = new DirectoryInfo(archive.ArchivePath); ////archive.SourceFileChanged += (o, e) => archive.SourceFileChanged += (o, e) => { numberOfEventsRaised++; Assert.That(e.SourceFilePath, Is.Not.SamePathOrUnder(xmlDirectory.Name)); Console.WriteLine("Event Type '{0}': [{1}]", e.EventType, e.SourceFilePath); }; WriteTextAndRaiseEvent(watchedFolder, "foo.c", @"int foo(int i) { return i + 1; }"); // Base32 encoded filename foo.c.xml Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4X88LX7RR.xml"))); WriteTextAndRaiseEvent(watchedFolder, "bar.c", @"int bar(int i) { return i - 1; }"); // Base32 encoded filename bar.c.xml Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4Z4UW37RR.xml"))); Directory.CreateDirectory(Path.Combine(SOURCEDIRECTORY, "subdir")); WriteTextAndRaiseEvent(watchedFolder, Path.Combine("subdir", "component.c"), @"int are_equal(int i, int j) { return i == j; }"); // Base32 encoded filename subdir\component.c.xml Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4649WXBSFB27XN7HFT88KNBJFY64XX.xml"))); Assert.That(archive.FileUnits.Count(), Is.EqualTo(3)); Assert.That(numberOfFunctions(archive), Is.EqualTo(3)); DeleteSourceAndRaiseEvent(watchedFolder, "bar.c"); // Base32 encoded filename bar.c.xml Assert.That(!File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4Z4UW37RR.xml"))); Assert.That(archive.FileUnits.Count(), Is.EqualTo(2)); Assert.That(numberOfFunctions(archive), Is.EqualTo(2)); WriteTextAndRaiseEvent(watchedFolder, Path.Combine("subdir", "component.c"), @"struct A { int a; char b; }"); // Base32 encoded filename subdir\component.c.xml Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4649WXBSFB27XN7HFT88KNBJFY64XX.xml"))); Assert.That(archive.FileUnits.Count(), Is.EqualTo(2)); Assert.That(numberOfFunctions(archive), Is.EqualTo(1)); RenameSourceFileAndRaiseEvent(watchedFolder, "foo.c", "foo2.c"); // Base32 encoded filename foo.c.xml Assert.That(!File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4X88LX7RR.xml"))); // Base32 encoded filename foo2.c.xml Assert.That(File.Exists(Path.Combine(xmlDirectory.FullName, "2BVUHCFVS6KX3VHC6BBBWFADSZ7EY7FRH48CX7GV627VYYGVEC9WXVFCB8UMWXJSVCGESVDEH4MUXBG4WCJWNM9RKZ9CNJGV6M8C3JFRS2GBXJG4X88LXJUS22.xml"))); Assert.That(archive.FileUnits.Count(), Is.EqualTo(2)); Assert.That(numberOfFunctions(archive), Is.EqualTo(1)); Assert.That(numberOfEventsRaised, Is.EqualTo(6)); }
private void TestDataGeneration_Concurrent(string sourcePath, string dataPath) { string fileLogPath = Path.Combine(dataPath, "parse.log"); string callLogPath = Path.Combine(dataPath, "methodcalls.log"); bool regenerateSrcML = shouldRegenerateSrcML; if(!Directory.Exists(sourcePath)) { Assert.Ignore("Source code for is missing"); } if(File.Exists(callLogPath)) { File.Delete(callLogPath); } if(File.Exists(fileLogPath)) { File.Delete(fileLogPath); } if(!Directory.Exists(dataPath)) { regenerateSrcML = true; } else if(shouldRegenerateSrcML) { Directory.Delete(dataPath, true); } var archive = new SrcMLArchive(dataPath, regenerateSrcML); archive.XmlGenerator.ExtensionMapping[".cxx"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".c"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".cc"] = Language.CPlusPlus; AbstractFileMonitor monitor = new FileSystemFolderMonitor(sourcePath, dataPath, new LastModifiedArchive(dataPath), archive); ManualResetEvent mre = new ManualResetEvent(false); Stopwatch sw = new Stopwatch(); bool startupCompleted = false; monitor.IsReadyChanged += (o, e) => { if(e.ReadyState) { sw.Stop(); startupCompleted = true; mre.Set(); } }; sw.Start(); //add the concurrency test: ZL monitor.Startup_Concurrent(); startupCompleted = mre.WaitOne(60000); if(sw.IsRunning) { sw.Stop(); } Console.WriteLine("{0} to {1} srcML", sw.Elapsed, (regenerateSrcML ? "generate" : "verify")); Assert.That(startupCompleted); Scope globalScope = null; sw.Reset(); int numberOfFailures = 0; int numberOfSuccesses = 0; int numberOfFiles = 0; Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>(); sw.Start(); using(var fileLog = new StreamWriter(fileLogPath)) { using(BlockingCollection<NamespaceDefinition> bc = new BlockingCollection<NamespaceDefinition>()) { Task.Factory.StartNew(() => { Parallel.ForEach(archive.FileUnits, unit => { var currentFile = SrcMLElement.GetFileNameForUnit(unit); var language = SrcMLElement.GetLanguageForUnit(unit); try { var scopeForUnit = CodeParser[language].ParseFileUnit(unit); bc.Add(scopeForUnit); } catch(Exception e) { var key = e.StackTrace.Split('\n')[0].Trim(); if(!errors.ContainsKey(key)) { errors[key] = new List<string>(); } errors[key].Add(currentFile); } }); bc.CompleteAdding(); }); foreach(var item in bc.GetConsumingEnumerable()) { if(null == globalScope) { globalScope = item; } else { try { globalScope = globalScope.Merge(item); } catch(Exception e) { //So far, don't know how to log the error (ZL 04/2013) //Console.WriteLine("error"); } } } } } sw.Stop(); Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, sw.Elapsed, numberOfFailures); Console.WriteLine("\nSummary"); Console.WriteLine("==================="); Console.WriteLine("{0,10:N0} failures ({1,8:P2})", numberOfFailures, ((float)numberOfFailures) / numberOfFiles); Console.WriteLine("{0,10:N0} successes ({1,8:P2})", numberOfSuccesses, ((float)numberOfSuccesses) / numberOfFiles); Console.WriteLine("{0} to generate data", sw.Elapsed); Console.WriteLine(fileLogPath); PrintScopeReport(globalScope); PrintMethodCallReport(globalScope, callLogPath); PrintErrorReport(errors); monitor.Dispose(); //Assert.AreEqual(numberOfFailures, (from e in errors.Values select e.Count).Sum()); //Assert.AreEqual(0, numberOfFailures); }
private void TestDataGeneration(string sourcePath, string dataPath, bool useAsyncMethods = false) { string fileLogPath = Path.Combine(dataPath, "parse.log"); string callLogPath = Path.Combine(dataPath, "methodcalls.log"); bool regenerateSrcML = shouldRegenerateSrcML; if (!Directory.Exists(sourcePath)) { Assert.Ignore("Source code for is missing"); } if (File.Exists(callLogPath)) { File.Delete(callLogPath); } if (File.Exists(fileLogPath)) { File.Delete(fileLogPath); } if (!Directory.Exists(dataPath)) { regenerateSrcML = true; } else if (shouldRegenerateSrcML) { Directory.Delete(dataPath, true); } var archive = new SrcMLArchive(dataPath, regenerateSrcML); archive.XmlGenerator.ExtensionMapping[".cxx"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".c"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".cc"] = Language.CPlusPlus; int numberOfFailures = 0; int numberOfSuccesses = 0; int numberOfFiles = 0; Dictionary <string, List <string> > errors = new Dictionary <string, List <string> >(); using (var fileLog = new StreamWriter(fileLogPath)) { using (var monitor = new FileSystemFolderMonitor(sourcePath, dataPath, new LastModifiedArchive(dataPath), archive)) { DateTime start, end = DateTime.MinValue; bool startupCompleted = false; start = DateTime.Now; if (useAsyncMethods) { var task = monitor.UpdateArchivesAsync().ContinueWith((t) => { end = DateTime.Now; startupCompleted = true; }); task.Wait(); } else { monitor.UpdateArchives(); end = DateTime.Now; startupCompleted = true; } if (!startupCompleted) { end = DateTime.Now; } Console.WriteLine("{0} to {1} srcML", end - start, (regenerateSrcML ? "generate" : "verify")); Assert.That(startupCompleted); using (var data = new DataRepository(archive)) { start = DateTime.Now; data.FileProcessed += (o, e) => { if (e.EventType == FileEventType.FileAdded) { numberOfFiles++; numberOfSuccesses++; if (numberOfFiles % 100 == 0) { Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, DateTime.Now - start, numberOfFailures); } fileLog.WriteLine("OK {0}", e.FilePath); } }; data.ErrorRaised += (o, e) => { numberOfFiles++; numberOfFailures++; if (numberOfFiles % 100 == 0) { Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, DateTime.Now - start, numberOfFailures); } ParseException pe = e.Exception as ParseException; if (pe != null) { fileLog.WriteLine("ERROR {0}", pe.FileName); fileLog.WriteLine(e.Exception.InnerException.StackTrace); var key = e.Exception.InnerException.StackTrace.Split('\n')[0].Trim(); if (!errors.ContainsKey(key)) { errors[key] = new List <string>(); } int errorLineNumber = (pe.LineNumber < 1 ? 1 : pe.LineNumber); int errorColumnNumber = (pe.ColumnNumber < 1 ? 1 : pe.ColumnNumber); var errorLocation = String.Format("{0}({1},{2})", pe.FileName, errorLineNumber, errorColumnNumber); errors[key].Add(errorLocation); } }; if (useAsyncMethods) { data.InitializeDataAsync().Wait(); } else { data.InitializeData(); } end = DateTime.Now; Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, end - start, numberOfFailures); Console.WriteLine("{0} to generate data", end - start); Console.WriteLine("\nSummary"); Console.WriteLine("==================="); Console.WriteLine("{0,10:N0} failures ({1,8:P2})", numberOfFailures, ((float)numberOfFailures) / numberOfFiles); Console.WriteLine("{0,10:N0} successes ({1,8:P2})", numberOfSuccesses, ((float)numberOfSuccesses) / numberOfFiles); Console.WriteLine("{0} to generate data", end - start); Console.WriteLine(fileLogPath); IScope globalScope; Assert.That(data.TryLockGlobalScope(Timeout.Infinite, out globalScope)); try { PrintScopeReport(globalScope); PrintMethodCallReport(globalScope, callLogPath); PrintErrorReport(errors); } finally { data.ReleaseGlobalScopeLock(); } } } } }
private static void GenerateData(string sourcePath, string dataPath, string csvDirectory) { Dictionary<Language, AbstractCodeParser> CodeParser = new Dictionary<Language, AbstractCodeParser>() { { Language.CPlusPlus, new CPlusPlusCodeParser() }, { Language.Java, new JavaCodeParser() }, { Language.CSharp, new CSharpCodeParser() } }; string fileLogPath = Path.Combine(dataPath, "parse.log"); string callLogPath = Path.Combine(dataPath, "methodcalls.log"); string csvPath = Path.Combine(csvDirectory, "timing.csv"); string jsonPath = String.Format("{0}.json", Path.Combine(@"c:\Workspace\DataVisualization", dataPath.Substring(23))); if(!Directory.Exists(sourcePath)) { Console.Error.WriteLine("{0} does not exist", sourcePath); return; } if(File.Exists(callLogPath)) { File.Delete(callLogPath); } if(File.Exists(fileLogPath)) { File.Delete(fileLogPath); } var archive = new SrcMLArchive(dataPath); archive.XmlGenerator.ExtensionMapping[".cxx"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".c"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".cc"] = Language.CPlusPlus; archive.XmlGenerator.ExtensionMapping[".hpp"] = Language.CPlusPlus; AbstractFileMonitor monitor = new FileSystemFolderMonitor(sourcePath, dataPath, new LastModifiedArchive(dataPath), archive); ManualResetEvent mre = new ManualResetEvent(false); Stopwatch timer = new Stopwatch(); bool startupCompleted = false; monitor.IsReadyChanged += (o, e) => { if(e.ReadyState) { timer.Stop(); startupCompleted = true; mre.Set(); } }; timer.Start(); monitor.Startup(); string[] spinner = new string[] { "\\\r", "|\r", "/\r" }; int spinner_index = -1; while(!startupCompleted) { spinner_index = (++spinner_index) % 3; Console.Write("Updating archive for {0}... {1}", sourcePath, spinner[spinner_index]); startupCompleted = mre.WaitOne(5000); } timer.Stop(); Console.WriteLine("Updating archive for {0}... {1}", sourcePath, timer.Elapsed); Scope globalScope = null; timer.Reset(); int numberOfFailures = 0; int numberOfSuccesses = 0; int numberOfFiles = 0; Dictionary<string, List<string>> errors = new Dictionary<string, List<string>>(); if(!File.Exists(csvPath)) { File.WriteAllLines(csvPath, new string[] { String.Join(",", "Project", "Files", "Failures", "Time (s)") }); } using(StreamWriter fileLog = new StreamWriter(fileLogPath), csvFile = new StreamWriter(csvPath, true)) { timer.Start(); foreach(var unit in archive.FileUnits) { var fileName = SrcMLElement.GetFileNameForUnit(unit); var language = SrcMLElement.GetLanguageForUnit(unit); try { var scopeForUnit = CodeParser[language].ParseFileUnit(unit); if(null == globalScope) { globalScope = scopeForUnit; } else { globalScope = globalScope.Merge(scopeForUnit); } timer.Stop(); fileLog.WriteLine("Parsing {0} PASSED", fileName); numberOfSuccesses++; } catch(Exception e) { timer.Stop(); fileLog.WriteLine("Parsing {0} FAILED", fileName); fileLog.WriteLine(e.StackTrace); var key = e.StackTrace.Split('\n')[0].Trim(); if(!errors.ContainsKey(key)) { errors[key] = new List<string>(); } errors[key].Add(fileName); numberOfFailures++; } finally { if(++numberOfFiles % 50 == 0) { Console.Write("{0,5:N0} files completed in {1} with {2,5:N0} failures\r", numberOfFiles, timer.Elapsed, numberOfFailures); csvFile.WriteLine(string.Join(",", sourcePath, numberOfFiles, numberOfFailures, timer.Elapsed.TotalSeconds)); } timer.Start(); } } } timer.Stop(); Console.WriteLine("{0,5:N0} files completed in {1} with {2,5:N0} failures", numberOfFiles, timer.Elapsed, numberOfFailures); Console.WriteLine("\nSummary"); Console.WriteLine("==================="); Console.WriteLine("{0,10:N0} failures ({1,8:P2})", numberOfFailures, ((float)numberOfFailures) / numberOfFiles); Console.WriteLine("{0,10:N0} successes ({1,8:P2})", numberOfSuccesses, ((float)numberOfSuccesses) / numberOfFiles); Console.WriteLine("{0} to generate data", timer.Elapsed); Console.WriteLine("See parse log at {0}", fileLogPath); OutputCallGraphByType(globalScope, jsonPath); PrintScopeReport(globalScope, sourcePath, csvDirectory); PrintMethodCallReport(globalScope, sourcePath, csvDirectory, callLogPath); }
private static void Main(string[] args) { string archivePath = "srcml"; string outputPath = null; Command command = null; bool shouldShowHelp = false; Dictionary <string, Language> extensionMapping = new Dictionary <string, Language>(); OptionSet options = new OptionSet() { { "a|archive=", "the location of the srcML {ARCHIVE}", a => archivePath = a }, { "c|command=", "the {COMMAND} to use (print-methods|print-calls)", c => command = GetCommand(c) }, { "o|output=", "the location of the {OUTPUT} file", o => outputPath = o }, { "e|extension=", "an extension mapping of the form \"EXT=Language\" (for example -e cs=C#)", e => { var parts = e.Split(':'); if (parts.Length == 2) { extensionMapping[parts[0]] = SrcMLElement.GetLanguageFromString(parts[1]); } } }, { "h|?|help", o => shouldShowHelp = true } }; var sourceFolderPaths = options.Parse(args); string errorMessage = String.Empty; if (sourceFolderPaths.Count == 0) { errorMessage = "Provide a source folder"; shouldShowHelp = true; } if (command == null) { errorMessage = "Provide a command"; shouldShowHelp = true; } if (shouldShowHelp) { if (!String.IsNullOrEmpty(errorMessage)) { Console.Error.WriteLine(errorMessage); } ShowHelp(options); } else { var archive = new SrcMLArchive(Path.GetDirectoryName(archivePath), Path.GetFileName(archivePath)); foreach (var key in extensionMapping.Keys) { var finalKey = (key[0] != '.' ? "." + key : key); Console.WriteLine("Mapping {0} files to {1}", finalKey, extensionMapping[key]); archive.XmlGenerator.ExtensionMapping[finalKey] = extensionMapping[key]; } var sourceFolder = new SourceFolder(sourceFolderPaths.First(), archive); sourceFolder.UpdateArchive(); using (TextWriter output = (outputPath == null ? Console.Out : new StreamWriter(outputPath))) { command(sourceFolder, output); } } }
/// <summary> /// Creates a new data archive /// </summary> /// <param name="baseDirectory">The base directory</param> /// /// <param name="monitoredArchive">The srcML archive to monitor</param> /// <param name="useExistingData">If true, any existing data files in <see cref="AbstractArchive.ArchivePath"/> will be used.</param> public DataArchive(string baseDirectory, SrcMLArchive monitoredArchive, bool useExistingData) : this(baseDirectory, DEFAULT_ARCHIVE_DIRECTORY, useExistingData, monitoredArchive, new DataGenerator(), new DataFileNameMapping(Path.Combine(baseDirectory, DEFAULT_ARCHIVE_DIRECTORY)), TaskScheduler.Default) { }
/// <summary> /// Creates a new data archive /// </summary> /// <param name="baseDirectory">The base directory</param> /// <param name="dataDirectory">the directory to store the data files in. This will be created as a subdirectory of <paramref name="baseDirectory"/></param> /// <param name="useExistingData">If true, any existing data files in <see cref="AbstractArchive.ArchivePath"/> will be used.</param> /// <param name="generator">The data generator to use for this archive</param> /// <param name="monitoredArchive">The srcML archive to monitor</param> /// <param name="mapping">the <see cref="AbstractFileNameMapping"/> to use to map source paths to data file paths</param> /// <param name="scheduler">The task scheduler to use for asynchronous tasks</param> public DataArchive(string baseDirectory, string dataDirectory, bool useExistingData, SrcMLArchive monitoredArchive, DataGenerator generator, AbstractFileNameMapping mapping, TaskScheduler scheduler) : base(baseDirectory, dataDirectory, useExistingData, generator, mapping, scheduler) { this.MonitoredArchive = monitoredArchive; }