public SourceCodeService( ISourceFileRepository sourceFilesRepository, ILogger logger, ICalculationsRepository calculationsRepository, ISourceFileGeneratorProvider sourceFileGeneratorProvider, ICompilerFactory compilerFactory, ICodeMetadataGeneratorService codeMetadataGenerator, ICalcsResiliencePolicies resiliencePolicies) { Guard.ArgumentNotNull(sourceFilesRepository, nameof(sourceFilesRepository)); Guard.ArgumentNotNull(logger, nameof(logger)); Guard.ArgumentNotNull(calculationsRepository, nameof(calculationsRepository)); Guard.ArgumentNotNull(sourceFileGeneratorProvider, nameof(sourceFileGeneratorProvider)); Guard.ArgumentNotNull(compilerFactory, nameof(compilerFactory)); Guard.ArgumentNotNull(codeMetadataGenerator, nameof(codeMetadataGenerator)); Guard.ArgumentNotNull(resiliencePolicies, nameof(resiliencePolicies)); Guard.ArgumentNotNull(resiliencePolicies?.SourceFilesRepository, nameof(resiliencePolicies.SourceFilesRepository)); Guard.ArgumentNotNull(resiliencePolicies?.CalculationsRepository, nameof(resiliencePolicies.CalculationsRepository)); _sourceFilesRepository = sourceFilesRepository; _logger = logger; _calculationsRepository = calculationsRepository; _sourceFileGeneratorProvider = sourceFileGeneratorProvider; _compilerFactory = compilerFactory; _sourceFileGenerator = sourceFileGeneratorProvider.CreateSourceFileGenerator(TargetLanguage.VisualBasic); _codeMetadataGenerator = codeMetadataGenerator; _sourceFilesRepositoryPolicy = resiliencePolicies.SourceFilesRepository; _calculationsRepositoryPolicy = resiliencePolicies.CalculationsRepository; }
private async Task RunAsync() { await SetupAsync(); Console.WriteLine("Generating files to hash..."); ISourceFileGenerator fileGenerator = CreateSourceFileGenerator(); await fileGenerator.CleanupSourceFilesAsync(mAppConfiguration); ISourceFileRepository sourceFileRepository = await fileGenerator .GenerateSourceFilesAsync(mAppConfiguration); Console.WriteLine("Generated {0} files to hash.", sourceFileRepository.TotalFileCount); IProcessingWatcher processingWatcher = CreateProcessingWatcher(sourceFileRepository); ISourceFileScheduler sourceFileScheduler = CreateSourceFileScheduler(); IFileHashRepository fileHashRepository = CreatFileHashRepository(); ISourceFileProcessor processor = CreateSourceFileProcessor(sourceFileScheduler); Console.WriteLine("Begin processing files..."); await processor.StartProcesingFilesAsync(sourceFileRepository, fileHashRepository, processingWatcher); await processingWatcher.WaitForCompletionAsync(); Console.WriteLine("Processing files completed. Number of hashed files: {0}. Shutting down...", fileHashRepository.TotalHashCount); await processor.StopProcessingFilesAsync(); Console.WriteLine("Successfully shut down. Press any key to continue..."); Console.ReadKey(); }
public void Compile_ErrorThrown_ReturnsErrorAsCompilerMessage() { //Arrange BuildProject buildProject = new BuildProject { SpecificationId = "3456" }; IEnumerable <Calculation> calculations = new List <Calculation>(); string errorMessage = "The sky is red, I don't understand"; ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>(); sourceFileGenerator .GenerateCode(buildProject, calculations, Arg.Any <CompilerOptions>()) .Throws(new Exception(errorMessage)); ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider(); sourceFileGeneratorProvider .CreateSourceFileGenerator(TargetLanguage.VisualBasic) .Returns(sourceFileGenerator); SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider); //Act Build result = sourceCodeService.Compile(buildProject, calculations); //Assert result.CompilerMessages.Count .Should() .Be(1); result.CompilerMessages.Count(x => x.Message == errorMessage && x.Severity == Severity.Error) .Should() .Be(1); sourceFileGenerator .Received(1) .GenerateCode(buildProject, calculations, Arg.Is <CompilerOptions>(x => x.SpecificationId == buildProject.SpecificationId && !x.OptionStrictEnabled)); }
public ISourceFileGenerator CreateSourceFileGenerator(TargetLanguage targetLanguage) { ISourceFileGenerator generator = null; switch (targetLanguage) { case TargetLanguage.VisualBasic: generator = _serviceProvider.GetService <VisualBasicSourceFileGenerator>(); break; } //Shouldnt ever happen but if a new language is added but no generator if (generator == null) { _logger.Error("An invalid language type was provided"); throw new NotSupportedException("Target language not supported"); } _logger.Verbose($"Generating {targetLanguage.ToString()} source file generator"); return(generator); }
public void Compile_GivenStringCompareInCodeAndAggregatesIsEnabledAndCalculationAggregateFunctionsFound_CompilesCodeAndReturnsOk() { //Arrange string stringCompareCode = "Public Class TestClass\nPublic Property E1 As ExampleClass\nPublic Function TestFunction As String\nIf E1.ProviderType = \"goodbye\" Then\nReturn Sum(Calc1)\nElse Return \"no\"\nEnd If\nEnd Function\nEnd Class"; Calculation calculation = new Calculation { Id = calculationId, BuildProjectId = buildProjectId, Current = new CalculationVersion(), SpecificationId = specificationId, Name = "TestFunction" }; IEnumerable <Calculation> calculations = new List <Calculation>() { calculation }; BuildProject buildProject = new BuildProject { SpecificationId = specificationId }; ILogger logger = CreateLogger(); List <SourceFile> sourceFiles = new List <SourceFile> { new SourceFile { FileName = "project.vbproj", SourceCode = "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>netcoreapp2.0</TargetFramework></PropertyGroup></Project>" }, new SourceFile { FileName = "ExampleClass.vb", SourceCode = "Public Class ExampleClass\nPublic Property ProviderType() As String\nEnd Class" }, new SourceFile { FileName = "Calculation.vb", SourceCode = stringCompareCode } }; Build build = new Build { Success = true, SourceFiles = sourceFiles }; Dictionary <string, string> sourceCodes = new Dictionary <string, string>() { { "TestFunction", stringCompareCode }, { "Calc1", "return 1" } }; CompilerOptions compilerOptions = new CompilerOptions(); ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>(); sourceFileGenerator .GenerateCode(Arg.Is(buildProject), Arg.Is(calculations), compilerOptions) .Returns(sourceFiles); ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider(); sourceFileGeneratorProvider .CreateSourceFileGenerator(Arg.Any <TargetLanguage>()) .Returns(sourceFileGenerator); ICompiler compiler = CreateCompiler(); ICompilerFactory compilerFactory = CreateCompilerFactory(compiler, sourceFiles); SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider, compilerFactory: compilerFactory); //Act Build buildResult = sourceCodeService.Compile(buildProject, calculations, compilerOptions); //Assert compiler .Received(1) .GenerateCode(Arg.Is <List <SourceFile> >(m => m.Count == 3)); }
public async Task GetAssembly_GivenAssemblyDoesNotExist_CompilesNewAssembly() { //Arrange BuildProject buildProject = new BuildProject { SpecificationId = specificationId, }; Calculation calculation = new Calculation { Id = calculationId, BuildProjectId = buildProjectId, Current = new CalculationVersion(), SpecificationId = specificationId, Name = "TestFunction" }; IEnumerable <Calculation> calculations = new List <Calculation>() { calculation }; ISourceFileRepository sourceFileRepository = CreateSourceFileRepository(); sourceFileRepository .DoesAssemblyExist(Arg.Is(specificationId)) .Returns(false); List <SourceFile> sourceFiles = new List <SourceFile> { new SourceFile { FileName = "project.vbproj", SourceCode = "<Project Sdk=\"Microsoft.NET.Sdk\"><PropertyGroup><TargetFramework>netcoreapp2.0</TargetFramework></PropertyGroup></Project>" }, new SourceFile { FileName = "ExampleClass.vb", SourceCode = "Public Class ExampleClass\nPublic Property ProviderType() As String\nEnd Class" }, new SourceFile { FileName = "Calculation.vb", SourceCode = "code" } }; buildProject.Build = new Build { SourceFiles = sourceFiles, }; Build newBuild = new Build { SourceFiles = sourceFiles, Assembly = new byte[100] }; ISourceFileGenerator sourceFileGenerator = Substitute.For <ISourceFileGenerator>(); sourceFileGenerator .GenerateCode(Arg.Is(buildProject), Arg.Any <IEnumerable <Calculation> >(), Arg.Any <CompilerOptions>()) .Returns(sourceFiles); ISourceFileGeneratorProvider sourceFileGeneratorProvider = CreateSourceFileGeneratorProvider(); sourceFileGeneratorProvider .CreateSourceFileGenerator(Arg.Any <TargetLanguage>()) .Returns(sourceFileGenerator); ICompiler compiler = CreateCompiler(); compiler .GenerateCode(Arg.Any <List <SourceFile> >()) .Returns(newBuild); ICompilerFactory compilerFactory = CreateCompilerFactory(compiler, sourceFiles); SourceCodeService sourceCodeService = CreateSourceCodeService(sourceFileGeneratorProvider: sourceFileGeneratorProvider, compilerFactory: compilerFactory); //Act byte[] assembly = await sourceCodeService.GetAssembly(buildProject); //Assert assembly .Should() .NotBeNull(); assembly .Length .Should() .Be(100); }