/// <summary> /// Build assembly library. /// </summary> /// <param name="context">The build context with parameters required to build library.</param> /// <exception cref="System.IO.DirectoryNotFoundException">OutputPath is null or not found.</exception> public override void BuildLibraries(ILibraryBuilderContext context) { try { var outputFolder = Path.Combine(SilverlightOutputFolder, Constants.DynamicSubfolder); var sysLibFileName = Path.Combine(outputFolder, "Temp", Constants.SystemProcessesLibName + ".dll"); var custLibFileName = Path.Combine(outputFolder, "Temp", Constants.ProcessesLibName + ".dll"); var outputPath = Path.GetDirectoryName(Path.Combine(SilverlightOutputFolder, "Source\\")); if (!string.IsNullOrEmpty(outputPath)) { if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } } else { throw new DirectoryNotFoundException("OutputPath is null or not found."); } CleanOutputFolder(outputPath); var stopWatch = new Stopwatch(); CompilerParameters compilerParams; List<string> sourceArray; if ((context.LibraryType & LibraryTypes.System) == LibraryTypes.System) { stopWatch.Start(); GenerateLanguageResources(outputPath, context.SourceCode, true); AddLibraryInfo(context.SourceCode, true); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Client sys lib resource generation time: {0}", stopWatch.ElapsedMilliseconds)); stopWatch.Reset(); stopWatch.Start(); compilerParams = GetCompilerParamters(context, sysLibFileName); compilerParams.EmbeddedResources.Add(Path.Combine(outputPath, Constants.SystemProcessesLibName + ".LocalizedStrings.resources")); sourceArray = context.SourceCode.Where(c => c.Value.IsSystem).Select(c => c.Value.ClientCode).ToList(); CompileProcessLibrary(context, sourceArray, new string[0], compilerParams); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Client sys lib compile time: {0} ms", stopWatch.ElapsedMilliseconds)); stopWatch.Reset(); CompileLocalizedProcessLibraries(context, sourceArray, outputPath, stopWatch, compilerParams, Constants.SystemProcessesLibName, sysLibFileName); } if ((context.LibraryType & LibraryTypes.Custom) != LibraryTypes.Custom) { return; } stopWatch.Start(); GenerateLanguageResources(outputPath, context.SourceCode, false); AddLibraryInfo(context.SourceCode, false); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Client cust lib resource generation time: {0}", stopWatch.ElapsedMilliseconds)); stopWatch.Reset(); stopWatch.Start(); compilerParams = GetCompilerParamters(context, custLibFileName); compilerParams.EmbeddedResources.Add(Path.Combine(outputPath, Constants.ProcessesLibName + ".LocalizedStrings.resources")); sourceArray = context.SourceCode.Where(c => !c.Value.IsSystem).Select(c => c.Value.ClientCode).ToList(); CompileProcessLibrary(context, sourceArray, new[] { sysLibFileName }, compilerParams); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Client cust lib compile time: {0} ms", stopWatch.ElapsedMilliseconds)); CompileLocalizedProcessLibraries(context, sourceArray, outputPath, stopWatch, compilerParams, Constants.ProcessesLibName, custLibFileName); } catch (Exception ex) { Log4NetLogger.Instance.Log(LogSeverity.Error, "Client cust lib error", ex); throw; } }
/// <summary> /// The compile process library. /// </summary> /// <param name="context">Library builder context.</param> /// <param name="code">Code for compilation.</param> /// <param name="extraReferences">The extra references.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="ProcessCompilationException">Raised then assembly compilation ended with errors.</exception> protected override void CompileProcessLibrary(ILibraryBuilderContext context, IList<string> code, string[] extraReferences, CompilerParameters parameters) { try { base.CompileProcessLibrary(context, code, extraReferences, parameters); } catch (ProcessCompilationException pex) { pex.IsServerSide = false; throw; } }
/// <summary> /// Build mock projects for development and testing purpose. /// </summary> /// <param name="context">The context with parameters.</param> public override void BuildProjects(ILibraryBuilderContext context) { var path = TestFolder; if (!string.IsNullOrWhiteSpace(path)) { if ((context.LibraryType & LibraryTypes.System) != 0) { BuildClientProject( context.SourceCode.Where(c => c.Value.IsSystem).ToDictionary(c => c.Key, c => c.Value), path, Constants.SystemProcessesLibName); } if ((context.LibraryType & LibraryTypes.Custom) != 0) { BuildClientProject( context.SourceCode.Where(c => !c.Value.IsSystem).ToDictionary(c => c.Key, c => c.Value), path, Constants.ProcessesLibName, Constants.SystemProcessesLibName + ".Client"); } } }
/// <summary> /// Compiles the localized process libraries. /// </summary> /// <param name="context">The context.</param> /// <param name="sourceArray">The source array.</param> /// <param name="outputPath">The output path.</param> /// <param name="stopWatch">The stop watch.</param> /// <param name="compilerParams">The compiler parameters.</param> /// <param name="libFileName">Name of the library file.</param> /// <param name="libFilePath">The library file path.</param> private void CompileLocalizedProcessLibraries(ILibraryBuilderContext context, IList<string> sourceArray, string outputPath, Stopwatch stopWatch, CompilerParameters compilerParams, string libFileName, string libFilePath) { foreach (var dto in LocalizationList) { var cultureFolder = Path.Combine(Path.GetDirectoryName(libFilePath) ?? string.Empty, dto.CultureName); if (!Directory.Exists(cultureFolder)) { if (!TryCreateDirectory(cultureFolder)) continue; } var cultureResource = Path.Combine(outputPath, dto.CultureName, libFileName + ".LocalizedStrings.resources"); if (File.Exists(cultureResource)) { stopWatch.Reset(); stopWatch.Start(); compilerParams.EmbeddedResources.Clear(); compilerParams.EmbeddedResources.Add(cultureResource); compilerParams.OutputAssembly = Path.Combine(cultureFolder, libFileName + ".dll"); compilerParams.TempFiles = new TempFileCollection(outputPath, false); CompileProcessLibrary(context, sourceArray, new string[0], compilerParams); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("{0} for {1} locale compile time: {2} ms", libFileName, dto.EnglishName, stopWatch.ElapsedMilliseconds)); stopWatch.Reset(); } else { var localizedXapFile = Path.Combine(SilverlightOutputFolder, Constants.DynamicSubfolder, string.Format("{0}.{1}.xap", libFileName, dto.CultureName)); if (File.Exists(localizedXapFile)) File.Delete(localizedXapFile); } } }
/// <summary> /// The get compiler parameters. /// </summary> /// <param name="context">Library builder context.</param> /// <param name="libName">The lib name.</param> /// <returns>The <see cref="CompilerParameters" />.</returns> protected override CompilerParameters GetCompilerParamters(ILibraryBuilderContext context, string libName) { var outputFolder = Path.Combine(SilverlightOutputFolder, "Source"); var cParams = new CompilerParameters { GenerateInMemory = context.GenerateInMemory, OutputAssembly = libName, TreatWarningsAsErrors = false, WarningLevel = 4, TempFiles = new TempFileCollection(outputFolder, true), CompilerOptions = string.Format(" /optimize /nostdlib /define:{0}", string.Join(";", GetConditionalCompilationSymbols())), IncludeDebugInformation = false, }; return cParams; }
/// <summary> /// The get compiler parameters. /// </summary> /// <param name="context">Library builder context.</param> /// <param name="libName">The lib name.</param> /// <returns>The <see cref="CompilerParameters" />.</returns> protected abstract CompilerParameters GetCompilerParamters(ILibraryBuilderContext context, string libName);
/// <summary> /// Build assembly library. /// </summary> /// <param name="context">The build context with parameters required to build library.</param> public abstract void BuildLibraries(ILibraryBuilderContext context);
/// <summary> /// The compile process library. /// </summary> /// <param name="context">Library builder context.</param> /// <param name="code">Code for compilation.</param> /// <param name="extraReferences">The extra references.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="Cebos.Veyron.Library.Common.ProcessCompilationException"></exception> /// <exception cref="ProcessCompilationException">Raised then assembly compilation ended with errors.</exception> protected virtual void CompileProcessLibrary(ILibraryBuilderContext context, IList<string> code, string[] extraReferences, CompilerParameters parameters) { if (!string.IsNullOrWhiteSpace(parameters.OutputAssembly)) { var outputPath = Path.GetDirectoryName(parameters.OutputAssembly); if (!string.IsNullOrEmpty(outputPath) && !Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } } using (var provider = new Microsoft.CSharp.CSharpCodeProvider()) { if (parameters.ReferencedAssemblies.Count == 0) { parameters.ReferencedAssemblies.AddRange(GetReferencedAssemblies(true).ToArray()); parameters.ReferencedAssemblies.AddRange(extraReferences); code.Add(GetAssemblyInfoText()); } var results = provider.CompileAssemblyFromSource(parameters, code.ToArray()); context.BuildErrors = (from CompilerError error in results.Errors where !error.IsWarning select new ErrorInfo { Column = error.Column, ErrorNumber = error.ErrorNumber, ErrorText = error.ErrorText, FileName = error.FileName, IsWarning = error.IsWarning, Line = error.Line }).ToArray(); if (context.BuildErrors == null || context.BuildErrors.Count == 0) { // Don't load the CompiledAssembly when running outside of unit tests because it locks the dll file. if (context.GenerateInMemory) context.GeneratedAssembly = results.CompiledAssembly; } CompilerError firstError = null; var compilerErrorsList = results.Errors.Cast<CompilerError>().Where(err => err.IsWarning == false); foreach (var err in compilerErrorsList) { Log4NetLogger.Instance.Log(LogSeverity.Error, GetType().ToString(), FormatErrorText(err)); Console.WriteLine(err.ErrorText); if (firstError == null && File.Exists(err.FileName)) { firstError = err; } } if (firstError != null) { string processName = string.Empty; if (!string.IsNullOrWhiteSpace(firstError.FileName)) { var str = File.ReadAllText(firstError.FileName); try { processName = Regex.Match(str, "public class (?<className>[a-zA-Z0-9_]+)Info").Groups["className"].Value; } catch (ArgumentException) { // Syntax error in the regular expression } } throw new ProcessCompilationException(firstError, processName); } } }
/// <summary> /// Build mock projects for development and testing purpose. /// </summary> /// <param name="context">The context with parameters.</param> public abstract void BuildProjects(ILibraryBuilderContext context);
/// <summary> /// Build assembly library. /// </summary> /// <param name="context">The build context with parameters required to build library.</param> /// <exception cref="System.ArgumentNullException">context</exception> public override void BuildLibraries(ILibraryBuilderContext context) { if (context == null) throw new ArgumentNullException("context"); try { var outputFolder = ServerOutputFolder; if (string.IsNullOrEmpty(outputFolder)) throw new ApplicationException("Invalid ServerOutputFolder!"); if (!Directory.Exists(outputFolder)) Directory.CreateDirectory(outputFolder); var outputPath = Path.GetDirectoryName(Path.Combine(ServerOutputFolder, "Source\\")); if (string.IsNullOrEmpty(outputPath)) return; if (!Directory.Exists(outputPath)) Directory.CreateDirectory(outputPath); foreach (var file in new DirectoryInfo(outputPath).GetFiles()) file.Delete(); var sysLibFileName = Path.Combine(outputFolder, Constants.SystemProcessesLibName + ".dll"); var custLibFileName = Path.Combine(outputFolder, Constants.ProcessesLibName + ".dll"); var stopWatch = new Stopwatch(); CompilerParameters compilerParams; if ((context.LibraryType & LibraryTypes.System) == LibraryTypes.System) { GenerateLanguageResources(outputPath, context.SourceCode, true); AddLibraryInfo(context.SourceCode, true); stopWatch.Start(); compilerParams = GetCompilerParamters(context, sysLibFileName); EmbedLocalizedResources(compilerParams, outputPath, true); CompileProcessLibrary(context, context.SourceCode.Where(c => c.Value.IsSystem).Select(c => c.Value.ServerCode).ToList(), new string[0], compilerParams); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Server sys lib compile time: {0}", stopWatch.ElapsedMilliseconds)); stopWatch.Reset(); } if ((context.LibraryType & LibraryTypes.Custom) != LibraryTypes.Custom) { return; } GenerateLanguageResources(outputPath, context.SourceCode, false); AddLibraryInfo(context.SourceCode, false); stopWatch.Start(); compilerParams = GetCompilerParamters(context, custLibFileName); EmbedLocalizedResources(compilerParams, outputPath, false); string[] extraReferences = File.Exists(sysLibFileName) ? new[] {sysLibFileName} : new string[0]; CompileProcessLibrary(context, context.SourceCode.Where(c => !c.Value.IsSystem).Select(c => c.Value.ServerCode).ToList(), extraReferences, compilerParams); Log4NetLogger.Instance.Log(LogSeverity.Information, string.Empty, string.Format("Server cust lib compile time: {0}", stopWatch.ElapsedMilliseconds)); } catch (Exception ex) { Log4NetLogger.Instance.Log(LogSeverity.Error, "Server cust lib error", ex); throw; } }
/// <summary> /// Build mock projects for development and testing purpose. /// </summary> /// <param name="context">The context with parameters.</param> public override void BuildProjects(ILibraryBuilderContext context) { var v = new SynchronizationContext(); v.AddWarning("adf"); var path = TestFolder; if (string.IsNullOrWhiteSpace(path)) return; if (!Directory.Exists(path)) Directory.CreateDirectory(path); if ((context.LibraryType & LibraryTypes.System) != 0) { BuildServerProject( context.SourceCode.Where(c => c.Value.IsSystem).ToDictionary(c => c.Key, c => c.Value), path, Constants.SystemProcessesLibName); } if ((context.LibraryType & LibraryTypes.Custom) != 0) { BuildServerProject( context.SourceCode.Where(c => !c.Value.IsSystem).ToDictionary(c => c.Key, c => c.Value), path, Constants.ProcessesLibName, Constants.SystemProcessesLibName + ".Server"); } }
/// <summary> /// Creates compiler parameters for building assembly. /// </summary> /// <param name="context">Library Builder context.</param> /// <param name="libName">The library name.</param> /// <returns>The compiler parameters for building assembly <see cref="CompilerParameters" />.</returns> protected override CompilerParameters GetCompilerParamters(ILibraryBuilderContext context, string libName) { return new CompilerParameters { GenerateInMemory = context.GenerateInMemory, OutputAssembly = libName, TreatWarningsAsErrors = false, WarningLevel = 4, TempFiles = !string.IsNullOrWhiteSpace(ServerOutputFolder) ? new TempFileCollection(Path.Combine(ServerOutputFolder, "Source"), true) : new TempFileCollection { KeepFiles = false }, CompilerOptions = string.Format("/optimize /define:{0}", string.Join(";", GetConditionalCompilationSymbols())), IncludeDebugInformation = false, }; }