Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        public static async Task <int> GenerateSources(
            string folder,
            string[] templates,
            string[] templatePackageIds,
            string[] larnacaFiles,
            string[] larnacaFilesPackageIdentities,
            string projFile,
            string outCsSourcesToCompile,
            string outAnalysisProject
            )
        {
            string collectorTypesAsm = typeof(DatabaseMeta).Assembly.Location;
            string genUtilsAsm       = typeof(gen.utils.DalUtils).Assembly.Location;
            string newtonsoftAsm     = typeof(Newtonsoft.Json.JsonConvert).Assembly.Location;

            List <Task <TemplateGenerationResult> >      allTasks = new List <Task <TemplateGenerationResult> >();
            Dictionary <string, DeserializedLarancaFile> larnacaFilesByPackageid = new Dictionary <string, DeserializedLarancaFile>();

            for (int i = 0; i < larnacaFiles.Length; i++)
            {
                var tryLoad = DeserializedLarancaFile.Load(larnacaFiles[i]);
                if (tryLoad.Fail())
                {
                    Console.Error.WriteLine($"Failed to deserialize laranca file {larnacaFiles[i]}: {tryLoad.StatusMessage}");
                }
                else
                {
                    larnacaFilesByPackageid[larnacaFilesPackageIdentities[i]] = tryLoad.Data;
                }
            }

            for (int i = 0; i < templates.Length; i++)
            {
                var currentTemplate          = templates[i];
                var currentTemplatePackageId = templatePackageIds[i];

                DeserializedLarancaFile larnacaFile = null;
                if (currentTemplatePackageId.Equals("none", StringComparison.OrdinalIgnoreCase) || larnacaFilesByPackageid.TryGetValue(currentTemplatePackageId, out larnacaFile))
                {
                    allTasks.Add(Task.Run(() => GenerateTemplate(currentTemplate, larnacaFile, folder, collectorTypesAsm, genUtilsAsm, newtonsoftAsm, projFile)));
                }
                else
                {
                    Console.Error.WriteLine($"Failed to find larnaca file {currentTemplatePackageId} for template {currentTemplate}");
                }
            }

            var allResults = await Task.WhenAll(allTasks).ConfigureAwait(false);

            if (!string.IsNullOrWhiteSpace(outCsSourcesToCompile))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outCsSourcesToCompile));
                File.WriteAllText(outCsSourcesToCompile, string.Join(Environment.NewLine, allResults.Where(r => !string.IsNullOrWhiteSpace(r.CSFileToCompile)).Select(r => r.CSFileToCompile)));
            }

            if (!string.IsNullOrWhiteSpace(outAnalysisProject))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outAnalysisProject));
                var analysisProject = allResults.FirstOrDefault(r => !string.IsNullOrWhiteSpace(r.AnalysisProjectFileToBuild))?.AnalysisProjectFileToBuild ?? "";
                File.WriteAllText(outAnalysisProject, analysisProject);
            }

            return(allResults.Max(r => r.StatusCode));
        }