public TransformationRunFactoryService(Uri serviceUri, int timeout) : base(serviceUri) { this.timeout = timeout; lastUsed = DateTime.Now; runFactory = new RemoteTransformationRunFactory(Guid.NewGuid()); }
public async Task RemoteProcedureTestWithDummyHost() { IProcessTransformationRunFactory factory = await RemotingServices.ConnectAsync <IProcessTransformationRunFactory>(new Uri($"ipc://{TransformationRunFactory.TransformationRunFactoryService}/{TransformationRunFactory.TransformationRunFactoryMethod}")); IProcessTextTemplatingEngine engine = new TemplatingEngine(); if (factory?.IsRunFactoryAlive() ?? default) { IProcessTransformationRunner runner = engine.PrepareTransformationRunner(Samples.template, new DummyHost(), factory); runner.Should().NotBeNull(); ITextTemplatingCallback result = factory.StartTransformation(runner.RunnerId); if (result.Errors.HasErrors) { foreach (TemplateError error in result.Errors) { Console.Out.WriteLine(error.Message); } } result.TemplateOutput.Should().Be(Samples.outcome); } }
public async Task RemotingShouldReturnRemoteProcedureObjectProxy() { IProcessTransformationRunFactory factory = await RemotingServices.ConnectAsync <IProcessTransformationRunFactory>(new Uri($"ipc://{TransformationRunFactory.TransformationRunFactoryService}/{TransformationRunFactory.TransformationRunFactoryMethod}")); factory.Should().NotBeNull(); if (factory is IProcessTransformationRunFactory runFactory) { runFactory.IsRunFactoryAlive().Should().BeTrue(); if (runFactory.CreateTransformationRunner() is IProcessTransformationRunner runner) { runner.Should().NotBeNull(); runner.RunnerId.Should().NotBeEmpty(); } } }
public IProcessTransformationRunner PrepareTransformationRunner(string content, ITextTemplatingEngineHost host, IProcessTransformationRunFactory runFactory, bool debugging = false) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (host == null) { throw new ArgumentNullException(nameof(host)); } if (runFactory == null) { throw new ArgumentNullException(nameof(runFactory)); } if (host is ITextTemplatingSessionHost sessionHost) { if (sessionHost.Session == null) { sessionHost.Session = sessionHost.CreateSession(); } } IProcessTransformationRunner runner = null; ParsedTemplate pt = ParsedTemplate.FromText(content, host); TemplateSettings settings = GetSettings(host, pt); settings.Debug = debugging || settings.CachedTemplates; EnsureParameterValuesExist(settings, host, pt); try { if (pt.Errors.HasErrors) { return(null); } runner = CompileAndPrepareRunner(pt, content, host, runFactory, settings); } catch (Exception ex) { if (IsCriticalException(ex)) { throw; } pt.LogError(string.Format(CultureInfo.CurrentCulture, VsTemplatingErrorResources.ExceptionProcessingTemplate, ex), new Location(host.TemplateFile, -1, -1)); } finally { host.LogErrors(pt.Errors); } return(runner); }
protected virtual IProcessTransformationRunner CompileAndPrepareRunner(ParsedTemplate pt, string content, ITextTemplatingEngineHost host, IProcessTransformationRunFactory runFactory, TemplateSettings settings) { TransformationRunner runner = null; bool success = false; if (pt == null) { throw new ArgumentNullException(nameof(pt)); } if (host == null) { throw new ArgumentNullException(nameof(host)); } if (runFactory == null) { throw new ArgumentNullException(nameof(runFactory)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } try { try { if (runFactory.CreateTransformationRunner() is TransformationRunner theRunner) { runner = theRunner; } } catch (Exception ex) { if (IsCriticalException(ex)) { throw; } pt.LogError(ex.ToString(), new Location(host.TemplateFile)); } if (runner != null && !runner.Errors.HasErrors) { // reference the assemblies and ensure they are path rooted settings.SetAssemblies(ProcessReferences(host, pt, settings)); if (!pt.Errors.HasErrors) { try { success = runFactory.PrepareTransformation(runner.RunnerId, pt, content, host, settings); } catch (SerializationException) { pt.LogError(VsTemplatingErrorResources.SessionHostMarshalError, new Location(host.TemplateFile)); throw; } } } } catch (Exception ex) { if (IsCriticalException(ex)) { throw; } pt.LogError(ex.ToString(), new Location(host.TemplateFile, -1, -1)); } finally { if (runner != null) { pt.Errors.AddRange(runFactory.GetErrors(runner.RunnerId)); } } return(success ? runner : null); }
public async ThreadingTasks.Task <string> ProcessTemplateAsync(string inputFilename, string content, ITextTemplatingCallback callback, object hierarchy, bool debugging = false) { if (this is ITextTemplatingComponents Component) { Component.Hierarchy = hierarchy; Component.Callback = callback; Component.TemplateFile = inputFilename; LastInvocationRaisedErrors = false; Component.Host.SetFileExtension(SearchForLanguage(content, "C#") ? ".cs" : ".vb"); await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); try { try { runFactory = await GetTransformationRunFactoryAsync(subSonicOutput.GetOutputTextWriter(), SubSonicCoreVisualStudioAsyncPackage.Singleton.CancellationTokenSource.Token); } catch (Exception ex) { await LogErrorAsync(string.Format(CultureInfo.CurrentCulture, SubSonicCoreErrors.ExceptionStartingRunFactoryProcess, ex), new Location(inputFilename)); runFactory = null; } if (runFactory == null) { await LogErrorAsync(SubSonicCoreErrors.ErrorStartingRunFactoryProcess, new Location(TemplateFile)); if (debugging) { ProcessTemplateEventArgs args = new ProcessTemplateEventArgs { TemplateOutput = SubSonicCoreErrors.DebugErrorOutput, Succeeded = false }; this.OnTransformProcessCompleted(args); } return(SubSonicCoreErrors.DebugErrorOutput); } if (!SubSonicCoreVisualStudioAsyncPackage.Singleton.CancellationTokenSource.IsCancellationRequested) { // we have not been cancelled so let's continue // we need to prepare the transformation and send the important parts over to the T4 Host Process if (Engine.PrepareTransformationRunner(content, Component.Host, runFactory, debugging) is IProcessTransformationRunner runner) { if (!debugging) { // if we are not debugging we can wait for the process to finish // and we do not need to attach to the host process. try { if (runFactory.StartTransformation(runner.RunnerId) is TextTemplatingCallback result) { if (result.Errors.HasErrors) { Callback.Errors.AddRange(result.Errors); foreach (var templateError in result.Errors) { await LogErrorAsync(templateError.Message, templateError.Location, templateError.IsWarning); } } Component.Callback.SetFileExtension(result.Extension); return(result.TemplateOutput); } else if (runFactory.GetErrors(runner.RunnerId) is TemplateErrorCollection errors) { Callback.Errors.AddRange(errors); foreach (var templateError in errors) { await LogErrorAsync(templateError.Message, templateError.Location, templateError.IsWarning); } } } catch (Exception ex) { if (TemplatingEngine.IsCriticalException(ex)) { throw; } await LogErrorAsync(ex.ToString(), new Location(Host.TemplateFile)); } finally { runFactory.DisposeOfRunner(runner.RunnerId); // clean up the runner } } else { // when debugging this we will attach to the process // the ui thread will be waiting for a callback from process to complete generation of file. bool success = false; try { foreach (EnvDTE.Process process in package.DTE.Debugger.LocalProcesses) { if (process.ProcessID == (this.transformProcess?.Id ?? default)) { process.Attach(); success = true; break; } } } catch (Exception attachException) { await LogErrorAsync(string.Format(CultureInfo.CurrentCulture, SubSonicCoreErrors.ExceptionAttachingToRunFactoryProcess, attachException), new Location(inputFilename)); } if (success) { Dispatcher uiDispatcher = Dispatcher.CurrentDispatcher; this.debugThread = new Thread(() => StartTransformation(inputFilename, runner, uiDispatcher)); this.debugThread.Start(); } else { await LogErrorAsync(SubSonicCoreErrors.ErrorAttachingToRunFactoryProcess, new Location(inputFilename)); ProcessTemplateEventArgs args = new ProcessTemplateEventArgs { TemplateOutput = SubSonicCoreErrors.DebugErrorOutput, Succeeded = false }; this.OnTransformProcessCompleted(args); } } } else { ProcessTemplateEventArgs args = new ProcessTemplateEventArgs(); if (debugging) { args.TemplateOutput = SubSonicCoreErrors.DebugErrorOutput; args.Succeeded = false; this.OnTransformProcessCompleted(args); } await LogErrorsAsync(transformationHost.Errors.ToCompilerErrorCollection()); } } } catch (IOException) { if (transformProcess != null) { try { transformProcess.Kill(); } catch (Exception) { } finally { transformProcess = null; } } RemotingServices.Disconnect(new Uri($"ipc://{TransformationRunFactory.TransformationRunFactoryService}")); } } return(SubSonicCoreErrors.DebugErrorOutput); }