237 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			237 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Adaptation.FileHandlers.json.WorkItems;
 | |
| using Adaptation.Shared;
 | |
| using Adaptation.Shared.Duplicator;
 | |
| using Adaptation.Shared.Methods;
 | |
| using log4net;
 | |
| using System;
 | |
| using System.Collections.Generic;
 | |
| using System.Collections.ObjectModel;
 | |
| using System.Globalization;
 | |
| using System.IO;
 | |
| using System.Text.Json;
 | |
| using System.Text.Json.Serialization;
 | |
| 
 | |
| namespace Adaptation.FileHandlers.Kanban;
 | |
| 
 | |
| #nullable enable
 | |
| 
 | |
| public class ProcessData : IProcessData
 | |
| {
 | |
| 
 | |
|     private readonly List<object> _Details;
 | |
| 
 | |
|     List<object> Shared.Properties.IProcessData.Details => _Details;
 | |
| 
 | |
|     private readonly ILog _Log;
 | |
| 
 | |
|     string IProcessData.GetCurrentReactor(IFileRead fileRead, Logistics logistics, Dictionary<string, string> reactors) =>
 | |
|         throw new Exception(string.Concat("See ", nameof(WriteFiles)));
 | |
| 
 | |
|     Tuple<string, Test[], JsonElement[], List<FileInfo>> IProcessData.GetResults(IFileRead fileRead, Logistics logistics, List<FileInfo> fileInfoCollection) =>
 | |
|         new(logistics.Logistics1[0], Array.Empty<Test>(), Array.Empty<JsonElement>(), fileInfoCollection);
 | |
| 
 | |
|     public ProcessData(IFileRead fileRead, Logistics logistics, Calendar calendar, string targetFileLocation, string url, List<FileInfo> fileInfoCollection)
 | |
|     {
 | |
|         if (fileRead.IsEAFHosted)
 | |
|         {
 | |
| 
 | |
|         }
 | |
|         if (url is null)
 | |
|             throw new ArgumentNullException(nameof(url));
 | |
|         _Details = new List<object>();
 | |
|         _Log = LogManager.GetLogger(typeof(ProcessData));
 | |
|         WriteFiles(fileRead, logistics, calendar, targetFileLocation, fileInfoCollection);
 | |
|     }
 | |
| 
 | |
|     private static void WriteFiles(IFileRead fileRead, Calendar calendar, string destinationDirectory, bool keepRelations, WorkItem[] workItems)
 | |
|     {
 | |
|         string json;
 | |
|         string text;
 | |
|         string jsonOld;
 | |
|         string jsonFile;
 | |
|         string textFile;
 | |
|         string weekOfYear;
 | |
|         WorkItem workItem;
 | |
|         DirectoryInfo directory;
 | |
|         DirectoryInfo kanbnDirectory;
 | |
|         DirectoryInfo tasksDirectory;
 | |
|         DirectoryInfo visualStudioCodeDirectory;
 | |
|         ReadOnlyDictionary<int, Record> keyValuePairs = GetWorkItems(workItems, keepRelations);
 | |
|         foreach (KeyValuePair<int, Record> keyValuePair in keyValuePairs)
 | |
|         {
 | |
|             workItem = keyValuePair.Value.WorkItem;
 | |
|             json = JsonSerializer.Serialize(workItem, WorkItemSourceGenerationContext.Default.WorkItem);
 | |
|             weekOfYear = calendar.GetWeekOfYear(workItem.CreatedDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
 | |
|             directory = new(Path.Combine(destinationDirectory, "{}", $"{workItem.CreatedDate:yyyy}", $"{workItem.CreatedDate:yyyy}_Week_{weekOfYear}", $"{workItem.Id}"));
 | |
|             text = GetTaskText(directory.FullName);
 | |
|             visualStudioCodeDirectory = new(Path.Combine(directory.FullName, ".vscode"));
 | |
|             if (!visualStudioCodeDirectory.Exists)
 | |
|                 _ = Directory.CreateDirectory(visualStudioCodeDirectory.FullName);
 | |
|             textFile = Path.Combine(visualStudioCodeDirectory.FullName, "tasks.json");
 | |
|             if (fileRead.IsEAFHosted && !File.Exists(textFile))
 | |
|                 File.WriteAllText(textFile, text);
 | |
|             kanbnDirectory = new(Path.Combine(directory.FullName, ".kanbn"));
 | |
|             tasksDirectory = new(Path.Combine(kanbnDirectory.FullName, "tasks"));
 | |
|             if (!tasksDirectory.Exists)
 | |
|                 _ = Directory.CreateDirectory(tasksDirectory.FullName);
 | |
|             jsonFile = Path.Combine(kanbnDirectory.FullName, $"{workItem.Id}.json");
 | |
|             jsonOld = File.Exists(jsonFile) ? File.ReadAllText(jsonFile) : string.Empty;
 | |
|             if (fileRead.IsEAFHosted && jsonOld != json)
 | |
|                 File.WriteAllText(jsonFile, json);
 | |
|             if (keyValuePair.Value.Children is not null && keyValuePair.Value.Children.Length > 0)
 | |
|                 WriteFiles(fileRead, tasksDirectory, keyValuePair.Value.Children);
 | |
|             if (visualStudioCodeDirectory.LastWriteTime != workItem.CreatedDate)
 | |
|                 Directory.SetLastWriteTime(visualStudioCodeDirectory.FullName, workItem.CreatedDate);
 | |
|             if (kanbnDirectory.LastWriteTime != workItem.CreatedDate)
 | |
|                 Directory.SetLastWriteTime(kanbnDirectory.FullName, workItem.CreatedDate);
 | |
|             if (directory.LastWriteTime != workItem.CreatedDate)
 | |
|                 Directory.SetLastWriteTime(directory.FullName, workItem.CreatedDate);
 | |
|             if (visualStudioCodeDirectory.CreationTime != workItem.CreatedDate)
 | |
|                 Directory.SetCreationTime(visualStudioCodeDirectory.FullName, workItem.CreatedDate);
 | |
|             if (kanbnDirectory.CreationTime != workItem.CreatedDate)
 | |
|                 Directory.SetCreationTime(kanbnDirectory.FullName, workItem.CreatedDate);
 | |
|             if (directory.CreationTime != workItem.CreatedDate)
 | |
|                 Directory.SetCreationTime(directory.FullName, workItem.CreatedDate);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private void WriteFiles(IFileRead fileRead, Logistics logistics, Calendar calendar, string destinationDirectory, List<FileInfo> fileInfoCollection)
 | |
|     {
 | |
|         if (fileInfoCollection is null)
 | |
|             throw new ArgumentNullException(nameof(fileInfoCollection));
 | |
|         bool keepRelations = true;
 | |
|         string json = File.ReadAllText(logistics.ReportFullPath);
 | |
|         WorkItem[]? workItems = JsonSerializer.Deserialize<WorkItem[]>(json);
 | |
|         if (workItems is null)
 | |
|             throw new Exception(nameof(workItems));
 | |
|         _Details.Add(workItems);
 | |
|         if (!Directory.Exists(destinationDirectory))
 | |
|             _ = Directory.CreateDirectory(destinationDirectory);
 | |
|         WriteFiles(fileRead, calendar, destinationDirectory, workItems);
 | |
|         WriteFiles(fileRead, calendar, destinationDirectory, keepRelations, workItems);
 | |
|     }
 | |
| 
 | |
|     private static void WriteFiles(IFileRead fileRead, Calendar calendar, string destinationDirectory, WorkItem[] workItems)
 | |
|     {
 | |
|         string old;
 | |
|         string json;
 | |
|         string directory;
 | |
|         string checkFile;
 | |
|         string weekOfYear;
 | |
|         foreach (WorkItem workItem in workItems)
 | |
|         {
 | |
|             weekOfYear = calendar.GetWeekOfYear(workItem.CreatedDate, CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString("00");
 | |
|             directory = Path.Combine(destinationDirectory, "[]", $"{workItem.CreatedDate:yyyy}", $"{workItem.CreatedDate:yyyy}_Week_{weekOfYear}", $"{workItem.Id}");
 | |
|             if (!Directory.Exists(directory))
 | |
|                 _ = Directory.CreateDirectory(directory);
 | |
|             json = JsonSerializer.Serialize(workItem, WorkItemSourceGenerationContext.Default.WorkItem);
 | |
|             checkFile = Path.Combine(directory, $"{workItem.Id}.json");
 | |
|             old = File.Exists(checkFile) ? File.ReadAllText(checkFile) : string.Empty;
 | |
|             if (!fileRead.IsEAFHosted || old == json)
 | |
|                 continue;
 | |
|             File.WriteAllText(checkFile, json);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     private static ReadOnlyDictionary<int, Record> GetWorkItems(WorkItem[] workItems, bool keepRelations)
 | |
|     {
 | |
|         ReadOnlyDictionary<int, Record> results;
 | |
|         Dictionary<int, WorkItem> keyValuePairs = new();
 | |
|         foreach (WorkItem workItem in workItems)
 | |
|             keyValuePairs.Add(workItem.Id, workItem);
 | |
|         results = GetKeyValuePairs(new(keyValuePairs), keepRelations);
 | |
|         return results;
 | |
|     }
 | |
| 
 | |
|     private static ReadOnlyDictionary<int, Record> GetKeyValuePairs(ReadOnlyDictionary<int, WorkItem> keyValuePairs, bool keepRelations)
 | |
|     {
 | |
|         Dictionary<int, Record> results = new();
 | |
|         Record record;
 | |
|         List<bool> nests = new();
 | |
|         WorkItem? parentWorkItem;
 | |
|         ReadOnlyCollection<Record> childRecords;
 | |
|         ReadOnlyCollection<Record> relatedRecords;
 | |
|         ReadOnlyCollection<Record> successorRecords;
 | |
|         foreach (KeyValuePair<int, WorkItem> keyValuePair in keyValuePairs)
 | |
|         {
 | |
|             nests.Clear();
 | |
|             if (keyValuePair.Value.Parent is null)
 | |
|                 parentWorkItem = null;
 | |
|             else
 | |
|                 _ = keyValuePairs.TryGetValue(keyValuePair.Value.Parent.Value, out parentWorkItem);
 | |
|             try
 | |
|             {
 | |
|                 childRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Child", nests, keepRelations); // Forward
 | |
|                 relatedRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Related", nests, keepRelations); // Related
 | |
|                 successorRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Successor", nests, keepRelations); // Forward
 | |
|                 // predecessorRecords = Record.GetKeyValuePairs(keyValuePairs, keyValuePair.Value, "Predecessor", nests, keepRelations); // Reverse
 | |
|                 record = Record.Get(keyValuePair.Value, parentWorkItem, childRecords, relatedRecords, successorRecords, keepRelations);
 | |
|             }
 | |
|             catch (Exception)
 | |
|             {
 | |
|                 Dictionary<string, string>? tag = null;
 | |
|                 record = new(keyValuePair.Value, parentWorkItem, Array.Empty<Record>(), Array.Empty<Record>(), Array.Empty<Record>(), tag);
 | |
|             }
 | |
|             results.Add(keyValuePair.Key, record);
 | |
|         }
 | |
|         return new(results);
 | |
|     }
 | |
| 
 | |
|     private static string GetTaskText(string directory) =>
 | |
|         string.Join(Environment.NewLine, new string[]
 | |
|         {
 | |
|             "{",
 | |
|             "\"version\": \"2.0.0\",",
 | |
|             "\"tasks\": [",
 | |
|             "{",
 | |
|             "\"label\": \"File-Folder-Helper AOT s X Day-Helper-2025-02-04\",",
 | |
|             "\"type\": \"shell\",",
 | |
|             "\"command\": \"L:/DevOps/Mesa_FI/File-Folder-Helper/bin/Release/net8.0/win-x64/publish/File-Folder-Helper.exe\",",
 | |
|             "\"args\": [",
 | |
|             "\"s\",",
 | |
|             "\"X\",",
 | |
|             $"\"{directory.Replace('\\', '/')}\",",
 | |
|             "\"Day-Helper-2025-02-04\",",
 | |
|             "],",
 | |
|             "\"problemMatcher\": []",
 | |
|             "}",
 | |
|             "]",
 | |
|             "}",
 | |
|         });
 | |
| 
 | |
|     private static void WriteFiles(IFileRead fileRead, DirectoryInfo tasksDirectory, Record[] records)
 | |
|     {
 | |
|         string old;
 | |
|         string json;
 | |
|         string checkFile;
 | |
|         WorkItem workItem;
 | |
|         foreach (Record record in records)
 | |
|         {
 | |
|             workItem = record.WorkItem;
 | |
|             json = JsonSerializer.Serialize(workItem, WorkItemSourceGenerationContext.Default.WorkItem);
 | |
|             checkFile = Path.Combine(tasksDirectory.FullName, $"{workItem.Id}.json");
 | |
|             old = File.Exists(checkFile) ? File.ReadAllText(checkFile) : string.Empty;
 | |
|             if (!fileRead.IsEAFHosted || old == json)
 | |
|                 continue;
 | |
|             File.WriteAllText(checkFile, json);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     internal static List<Description> GetDescriptions(JsonElement[] jsonElements)
 | |
|     {
 | |
|         List<Description> results = new();
 | |
|         Description? description;
 | |
|         JsonSerializerOptions jsonSerializerOptions = new() { NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.WriteAsString };
 | |
|         foreach (JsonElement jsonElement in jsonElements)
 | |
|         {
 | |
|             if (jsonElement.ValueKind != JsonValueKind.Object)
 | |
|                 throw new Exception();
 | |
|             description = JsonSerializer.Deserialize<Description>(jsonElement.ToString(), jsonSerializerOptions);
 | |
|             if (description is null)
 | |
|                 continue;
 | |
|             results.Add(description);
 | |
|         }
 | |
|         return results;
 | |
|     }
 | |
| 
 | |
| } |