internal static OperationResult InstallUpdateTemplates(string csproj, string[] larancaFiles, string[] larnacaFilesPackageIdentities, string targetDir, string outAddedTemplates) { var templatesRootPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(typeof(TemplateFileManager).Assembly.Location), "templates")); var csprojFolder = Path.GetDirectoryName(Path.GetFullPath(csproj)); var targetFolderFullPath = Path.GetFullPath(Path.Combine(csprojFolder, targetDir)); var rellativeTargetPath = Path.GetRelativePath(csprojFolder, targetFolderFullPath); List <TemplateFile> templates = new List <TemplateFile>(); foreach (var currentTemplate in Directory.EnumerateFiles(templatesRootPath, "*.lca.tt", SearchOption.AllDirectories)) { ETemplateType templateType = ETemplateType.undefined; ETemplateReplication templateReplication = ETemplateReplication.undefined; bool templateError; var fullTemplatePath = Path.GetFullPath(currentTemplate); try { var templateContent = File.ReadAllText(fullTemplatePath); var generator = new ToolTemplateGenerator(); var pt = ParsedTemplate.FromText(templateContent, generator); var extractResult = TemplateLarnacaProperties.Extract(pt); if (extractResult.Fail()) { templateError = true; Console.Error.WriteLine($"Failed to extract larnaca properties from template {fullTemplatePath}. {extractResult.StatusMessage}"); } else { templateError = false; templateType = extractResult.Data.Type; templateReplication = extractResult.Data.Replication; } } catch (Exception ex) { templateError = true; Console.Error.WriteLine($"Failed to load template {currentTemplate}: {ex}"); } if (templateError) { continue; } string subPath; if (fullTemplatePath.StartsWith(templatesRootPath, StringComparison.OrdinalIgnoreCase)) { subPath = fullTemplatePath.Substring(templatesRootPath.Length + 1); } else { subPath = Path.GetFileName(currentTemplate); } string targetRelativePath; if (templateReplication == ETemplateReplication.Single) { // no need to larnaca package subdir string targetFullPath = Path.Combine(targetFolderFullPath, subPath); targetRelativePath = Path.GetRelativePath(csprojFolder, targetFullPath); } else { targetRelativePath = null; } bool singleTemplateWritten = false; if (templateType == ETemplateType.Analysis) { if (templateReplication != ETemplateReplication.Single) { Console.Error.WriteLine($"Invalid template {currentTemplate}, cannot have templateType={templateType} and templateReplication={templateReplication}"); } else { templates.Add(new TemplateFile(fullTemplatePath, subPath, targetRelativePath, "none")); } } else { for (int i = 0; i < larancaFiles.Length; i++) { string currentLarnacaFile = larancaFiles[i]; string currentLarnacaPackageId = larnacaFilesPackageIdentities[i]; var loadedFile = DeserializedLarancaFile.Load(currentLarnacaFile); if (loadedFile.Fail()) { Console.Error.WriteLine($"Failed to load larnaca file ({currentLarnacaFile}): {loadedFile.StatusMessage}"); } else { if (loadedFile.Data.DatabaseMeta != null && templateType == ETemplateType.DB) { if (templateReplication == ETemplateReplication.Project) { var targetLarnacaPackageSubdir = Path.Combine(targetFolderFullPath, currentLarnacaPackageId); string targetFullPath = Path.Combine(targetLarnacaPackageSubdir, subPath); targetRelativePath = Path.GetRelativePath(csprojFolder, targetFullPath); templates.Add(new TemplateFile(fullTemplatePath, subPath, targetRelativePath, currentLarnacaPackageId)); } else { if (!singleTemplateWritten) { singleTemplateWritten = true; templates.Add(new TemplateFile(fullTemplatePath, subPath, targetRelativePath, "none")); } } } } } } } var updateCsprojOp = WriteTemplateFilesToCsproj(csproj, templates); if (updateCsprojOp.Fail()) { return(updateCsprojOp); } foreach (var currentTemplate in templates) { // todo: check if newer if (currentTemplate.TemplateUpdateTemplateMode != ETemplateUpdateTemplateMode.None) { var targetPath = Path.GetFullPath(Path.Combine(csprojFolder, currentTemplate.TargetRelativePath)); Directory.CreateDirectory(Path.GetDirectoryName(targetPath)); File.Copy(currentTemplate.SourceFullPath, targetPath, true); } } if (!string.IsNullOrWhiteSpace(outAddedTemplates)) { Directory.CreateDirectory(Path.GetDirectoryName(outAddedTemplates)); File.WriteAllText(outAddedTemplates, string.Join(Environment.NewLine, updateCsprojOp.Data.Values.Select(t => t.TargetRelativePath))); } return(new OperationResult()); }
private static TemplateGenerationResult GenerateTemplate( string templateFile, DeserializedLarancaFile larancaFile, string folder, string collectorTypesAsm, string genUtilsAsm, string newtonsoftAsm, string projFile) { var generator = new ToolTemplateGenerator(); var inputFile = templateFile; string inputContent; try { inputContent = File.ReadAllText(inputFile); } catch (IOException ex) { Console.Error.WriteLine("Could not read input file '" + inputFile + "':\n" + ex); return(new TemplateGenerationResult() { StatusCode = 1 }); } if (inputContent.Length == 0) { Console.Error.WriteLine("Input is empty"); return(new TemplateGenerationResult() { StatusCode = 1 }); } var pt = ParsedTemplate.FromText(inputContent, generator); var larnacaPropertiesExtractResult = TemplateLarnacaProperties.Extract(pt); if (larnacaPropertiesExtractResult.Fail()) { Console.Error.WriteLine($"Failed to parse larnaca propertsions of template: {templateFile}. {larnacaPropertiesExtractResult.StatusMessage}"); } var settings = TemplatingEngine.GetSettings(generator, pt); settings.Log = Console.Out; if (pt.Errors.Count > 0) { foreach (var currentError in pt.Errors) { var currentCompilerError = (CompilerError)currentError; if (currentCompilerError.IsWarning) { Console.WriteLine(currentCompilerError.ToString()); } else { Console.Error.WriteLine(currentCompilerError.ToString()); generator.Errors.Add(currentCompilerError); } } } var outputFile = inputFile; if (Path.HasExtension(outputFile)) { var dir = Path.GetDirectoryName(outputFile); var fn = Path.GetFileNameWithoutExtension(outputFile); outputFile = Path.Combine(dir, fn + (settings.Extension ?? ".txt")); } else { outputFile = outputFile + (settings.Extension ?? ".txt"); } HashSet <string> assemblyNamesToRemove = new HashSet <string>(new[] { Path.GetFileName(collectorTypesAsm), Path.GetFileName(genUtilsAsm), Path.GetFileName(newtonsoftAsm), }, StringComparer.OrdinalIgnoreCase); //fix template assemblies path foreach (var x in settings.Assemblies.ToArray()) { if (assemblyNamesToRemove.Contains(Path.GetFileName(x))) { settings.Assemblies.Remove(x); } else { settings.Assemblies.Add(FixPath(x, folder)); } } settings.Assemblies.Add(collectorTypesAsm); settings.Assemblies.Add(genUtilsAsm); settings.Assemblies.Add(newtonsoftAsm); string outputContent = null; if (!generator.Errors.HasErrors) { var larnacaDirective = pt.Directives.FirstOrDefault(d => d.Name.Equals("larnaca", StringComparison.OrdinalIgnoreCase)); if (larancaFile?.DatabaseMeta != null) { generator.AddParameter(null, null, "dbMeta", larancaFile.DatabaseMeta); } generator.AddParameter(null, null, "projFile", projFile); outputContent = generator.ProcessTemplate(pt, inputFile, inputContent, ref outputFile, settings); } if (!generator.Errors.HasErrors) { try { if (outputFile.EndsWith(".g.g.cs", StringComparison.OrdinalIgnoreCase)) { outputFile = outputFile.Substring(0, outputFile.Length - ".g.g.cs".Length) + ".g.cs"; } File.WriteAllText(outputFile, outputContent, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false)); if (outputFile.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)) { if (larnacaPropertiesExtractResult.Data?.DoNotCompile ?? false) { return(new TemplateGenerationResult()); } else { return(new TemplateGenerationResult() { CSFileToCompile = outputFile }); } } else if (outputFile.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)) { if ((larnacaPropertiesExtractResult.Data?.Type ?? ETemplateType.undefined) == ETemplateType.Analysis) { return(new TemplateGenerationResult() { AnalysisProjectFileToBuild = outputFile }); } else { return(new TemplateGenerationResult()); } } else { return(new TemplateGenerationResult()); } } catch (IOException ex) { Console.Error.WriteLine("Could not write output file '" + outputFile + "':\n" + ex); return(new TemplateGenerationResult() { StatusCode = 1 }); } } else { Console.Error.WriteLine(inputFile == null ? "Processing failed." : $"Processing '{inputFile}' failed."); return(new TemplateGenerationResult() { StatusCode = 1 }); } }