protected override bool TryExecute(IFileGenerator generator, GeneratorExecutionHost executionHost) { ISolution solution = generator.DataProvider.Solution; IProjectFile sourceFile = generator.DataProvider.SourceFile.ToProjectFile(); if (!ExecutionHelpers.TryExecuteGenerator(generator, out string generatedText, out string filePath)) { return(false); } bool fileCreated = TryGenerateFile( solution, sourceFile, filePath, generatedText, out IProjectFile generatedFile ); if (!fileCreated) { return(false); } NavigateToFile(solution, generatedFile); return(true); }
private static bool TryExecuteGenerators( IGenerator[] generators, out List <FileGeneratorOutput> fileGeneratorOutputs, out List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs) { fileGeneratorOutputs = new List <FileGeneratorOutput>(); inPlaceGeneratorOutputs = new List <InPlaceGeneratorOutput>(); List <FileGeneratorOutput> localFileGeneratorOutputs = fileGeneratorOutputs; List <InPlaceGeneratorOutput> localInPlaceGeneratorOutputs = inPlaceGeneratorOutputs; foreach (IGenerator generator in generators) { bool executed = ExecutionHelpers.TryExecuteGenerator( generator, () => { switch (generator) { case IFileGenerator fileGenerator: var fileGeneratorOutput = new FileGeneratorOutput( fileGenerator.GetFileName(), fileGenerator.TransformText(), fileGenerator.DataProvider.SourceFile.ToProjectFile() ); localFileGeneratorOutputs.Add(fileGeneratorOutput); break; case IInPlaceGenerator inPlaceGenerator: var inPlaceGeneratorOutput = new InPlaceGeneratorOutput( inPlaceGenerator.DataProvider.SourceFile.ToProjectFile(), inPlaceGenerator.GetTextRangeToDelete(), inPlaceGenerator.GetPositionToInsert(), inPlaceGenerator.TransformText(), inPlaceGenerator.MissingUsingDirectives ); localInPlaceGeneratorOutputs.Add(inPlaceGeneratorOutput); break; default: throw new NotSupportedException( $"The generator of type '{generator.GetType()}' is not supported as a part of MultipleOutputGenerator." + "Only IFileGenerator and IInPlaceGenerator are suppored." ); } } ); if (!executed) { return(false); } } return(true); }
protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost) { IDocument document = generator.DataProvider.Document; bool executed = ExecutionHelpers.TryExecuteGenerator( generator, out string generatedText, out TextRange rangeToDelete, out int positionToInsert, out IReadOnlyCollection <IUsingDirective> missingUsingDirectives ); if (!executed) { return(false); } ISolution solution = generator.DataProvider.Solution; using (var progressIndicator = NullProgressIndicator.Create()) { IProjectModelTransactionCookie transaction = solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator); using (transaction) { if (!rangeToDelete.IsEmpty) { document.DeleteText(rangeToDelete); } document.InsertText(positionToInsert, generatedText); var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length); ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives); transaction.Commit(progressIndicator); } } return(true); }
protected override bool TryExecute( IMultipleOutputGenerator generator, GeneratorExecutionHost executionHost) { ISolution solution = generator.DataProvider.Solution; IGenerator[] generators = null; bool gotGenerators = ExecutionHelpers.TryExecuteGenerator( generator, () => generators = generator.GetGenerators().ToArray() ); if (!gotGenerators) { return(false); } bool generatorsExecuted = TryExecuteGenerators( generators, out List <FileGeneratorOutput> fileGeneratorOutputs, out List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs ); if (!generatorsExecuted) { return(false); } if (!TryGenerateFiles(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs)) { return(false); } NavigateToFiles(solution, fileGeneratorOutputs, inPlaceGeneratorOutputs); return(true); }