public void AddCar(Car car) { var stringBuilder = new StringBuilder(); var serializeObject = _jsonSerialize.SerializeObject(car); DbC.Require(() => serializeObject != null, "No se ha serializado"); stringBuilder.Append(serializeObject); FileUtilities.CreateFolderIfNotExists(_path); File.AppendAllText(Path.Combine(_path, "data.txt"), stringBuilder.ToString()); stringBuilder.Clear(); }
/// <summary> /// This is the main processor for this class /// </summary> public void Process() { #region Step 1 - Create ExternalContent folder under Areas\{Module} and add to web project var webFolder = _settings.DestinationWebFolder; var solutionFolder = _settings.DestinationSolutionFolder; // Get the ModuleID specifier from the webFolder path var moduleId = ExtractModuleIdFromPath(webFolder); var areasFolder = Path.Combine(webFolder, Constants.AreasFolderName); var areasModuleFolder = Path.Combine(areasFolder, moduleId); var externalContentFolder = Path.Combine(areasModuleFolder, Constants.ExternalContentFolderName); var folderAlreadyExisted = FileUtilities.CreateFolderIfNotExists(externalContentFolder); #endregion #region Step 2 - Get file names for menuIcon and menuBackGroundImage from {module}MenuDetails.xml (We will search for these later) var menuManager = new MenuManager(_settings.DestinationSolutionFolder); // Get the two filenames and their paths from the {module}MenuDetails.xml file var backgroundImagePathFromMenu = menuManager.GetMenuBackgroundImagePath(); var iconImagePathFromMenu = menuManager.GetMenuIconImagePath(); var backgroundImageFilenameFromMenu = new FileInfo(backgroundImagePathFromMenu).Name; var iconImageFilenameFromMenu = new FileInfo(iconImagePathFromMenu).Name; #endregion #region Step 3 - Setup the new names for the menu background and icon images string newBackgroundImageFilename = string.Format(Constants.Post2019Dot0BackgroundImageNameTemplate, moduleId.ToLower()); string newIconImageFilename = string.Format(Constants.Post2019Dot0IconImageNameTemplate, moduleId.ToLower()); // Determine if the filenames contained in the Menu XML file are the old format // or the new 2019.0 introduced format var compareType = StringComparison.InvariantCultureIgnoreCase; var sameBackgroundImageFilename = backgroundImageFilenameFromMenu.Equals(newBackgroundImageFilename, compareType); var sameIconImageFilename = iconImageFilenameFromMenu.Equals(newIconImageFilename, compareType); #endregion #region Step 4 - Update the XXMenuDetails.xml file with the new values, if necessary var moduleIdUpper = moduleId.ToUpper(); var folder = Constants.ExternalContentFolderName; var areas = Constants.AreasFolderName; var relPath = Constants.RelativePathDesignator; if (sameBackgroundImageFilename == false) { // The background image file referenced in the menu is the pre 2019.0 image filename string newBackgroundImageFilePathForMenu = $"{relPath}/{areas}/{moduleIdUpper}/{folder}/{newBackgroundImageFilename}"; menuManager.SetMenuBackgroundImage(newBackgroundImageFilePathForMenu); string backgroundImageLocation = FileUtilities.EnumerateFiles(solutionFolder, backgroundImageFilenameFromMenu).ToArray()[0]; var targetPath = Path.Combine(externalContentFolder, newBackgroundImageFilename); File.Move(backgroundImageLocation, targetPath); } else { // Menu background image referenced in the menu is the new one // Get the full path to the old background image (name and location) var oldImagePath = Path.Combine(webFolder, Constants.Pre2019Dot0ImageLocation, Constants.Pre2019Dot0BackgroundImageName); if (File.Exists(oldImagePath)) { // The old file exists in the original location. // Let's copy it over to the new location with the new name. var targetPath = Path.Combine(externalContentFolder, newBackgroundImageFilename); File.Move(oldImagePath, targetPath); } } if (sameIconImageFilename == false) { // The icon image file referenced in the menu is the pre 2019.0 icon image filename string newMenuIconImageFilePathForMenu = $"{relPath}/{areas}/{moduleIdUpper}/{folder}/{newIconImageFilename}"; menuManager.SetMenuIconImage(newMenuIconImageFilePathForMenu); string iconImageLocation = FileUtilities.EnumerateFiles(solutionFolder, iconImageFilenameFromMenu).ToArray()[0]; var targetPath = Path.Combine(externalContentFolder, newIconImageFilename); File.Move(iconImageLocation, targetPath); } else { // Menu background image referenced in the menu is the new one // Get the full path to the old background image (name and location) var oldImagePath = Path.Combine(webFolder, Constants.Pre2019Dot0ImageLocation, Constants.Pre2019Doc0IconImageName); if (File.Exists(oldImagePath)) { // The old file exists in the original location. // Let's copy it over to the new location with the new name. var targetPath = Path.Combine(externalContentFolder, newIconImageFilename); File.Move(oldImagePath, targetPath); } } #endregion #region Step 5 - Update the Web project with the new folder and files // Now that the new 'ExternalContent' folder has been created and the // two images have been renamed and moved there, let's add all of // these to the Web project. // Open the Web.csproj file and insert the following: // // <ItemGroup> // <Content Include="Areas\VM\ExternalContent\**" /> // </ItemGroup> // // Get the company name from the Web folder var webFolderParts = webFolder.Split(new char[] { Path.DirectorySeparatorChar }); var webFolderNameOnly = webFolderParts[webFolderParts.Length - 1]; var webProjectName = $"{webFolderNameOnly}.{Constants.CSharpProjectExtensionName}"; var webProjectFilePath = Path.Combine(webFolder, webProjectName); var allLines = File.ReadAllLines(webProjectFilePath); var txtLines = allLines.ToList(); var trimLines = allLines.Select(l => l.Trim()).ToList(); // Let's see if this line already exists. If it does, then we can skip the rest. var mainContentLine = $"<Content Include=\"Areas\\{moduleId.ToUpper()}\\{Constants.ExternalContentFolderName}\\**\" />"; if (trimLines.Contains(mainContentLine) == false) { // Build the content to insert into the project file var sb = new StringBuilder(); sb.AppendLine($" <ItemGroup>"); sb.AppendLine($" {mainContentLine}"); sb.Append($" </ItemGroup>"); // Look for the last </ItemGroup>. // We will insert the block just after this. var lastIndex = trimLines.LastIndexOf(@"</ItemGroup>"); if (lastIndex > -1) { var insertionIndex = lastIndex + 1; txtLines.Insert(insertionIndex, sb.ToString()); File.WriteAllLines(webProjectFilePath, txtLines); } } #endregion }