public void RefreshPatches(string modelVersion) { // Import the initial patch file in a temporary directory var sourceMongoSource = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); InitSourceDir(sourceMongoSource); var destMongoSource = GetMongoSourceDir(modelVersion); InitSourceDir(destMongoSource); var mongoPatchDir = GetMongoPatchDir(modelVersion); var patchFiles = Directory.GetFiles(mongoPatchDir, "*.patch"); Array.Sort(patchFiles); foreach (var file in patchFiles) { Console.WriteLine($"\tProcessing {Path.GetFileName(file)}"); var patchNoStr = Path.GetFileName(file).Substring(0, 4); var patchNo = int.Parse(patchNoStr); TestHelper.Run(Git, $"am --ignore-whitespace {file}", sourceMongoSource); TestHelper.Run(Git, $"tag r{patchNo}", sourceMongoSource); CopyFiles(sourceMongoSource, destMongoSource, true); RestoreDatabaseInternal(modelVersion); SaveDatabaseInternal(mongoPatchDir, modelVersion, ExtractSubject(file), patchNo, destMongoSource); } RobustIO.DeleteDirectoryAndContents(sourceMongoSource); }
private static void Merge(Options.MergeOptions options) { RobustIO.DeleteDirectoryAndContents(options.WorkDir); Settings.TempDir = options.WorkDir; // restore previous data var restoreOptions = new Options.RestoreOptions(options); RestoreLanguageDepot(restoreOptions); RestoreMongoDb(restoreOptions); // run merge LfMergeHelper.Run($"--project {options.Project} --clone --action=Synchronize"); Console.WriteLine("Successfully merged test data"); // save merged data var saveOptions = new Options.SaveOptions(options) { WorkDir = Path.Combine(options.WorkDir, "LanguageDepot"), CommitMsg = options.CommitMsg ?? "Merged test data" }; SaveLanguageDepot(saveOptions); SaveLanguageDepotNoOpPatchIfNecessary(options.ModelVersion, options.LanguageDepotVersion + 1); SaveMongoDb(saveOptions); }
public void MigrateOldConfigurationsIfNeeded_MatchesLabelsWhenUIIsLocalized() { // Localize a Part's label to German (sufficient to cause a mismatched nodes crash if one config's labels are localized) var localizedPartLabels = new Dictionary <string, string>(); localizedPartLabels["Main Entry"] = "Haupteintrag"; var pathsToL10NStrings = (Dictionary <string, Dictionary <string, string> >)ReflectionHelper.GetField(StringTable.Table, "m_pathsToStrings"); pathsToL10NStrings["group[@id = 'LocalizedAttributes']/"] = localizedPartLabels; var configSettingsDir = LcmFileHelper.GetConfigSettingsDir(Path.GetDirectoryName(Cache.ProjectId.Path)); var newConfigFilePath = Path.Combine(configSettingsDir, DictionaryConfigurationListener.DictionaryConfigurationDirectoryName, "Lexeme" + DictionaryConfigurationModel.FileExtension); Assert.False(File.Exists(newConfigFilePath), "should not yet be migrated"); Directory.CreateDirectory(configSettingsDir); File.WriteAllLines(Path.Combine(configSettingsDir, "Test.fwlayout"), new[] { @"<layoutType label='Lexeme-based (complex forms as main entries)' layout='publishStem'><configure class='LexEntry' label='Main Entry' layout='publishStemEntry' />", @"<configure class='LexEntry' label='Minor Entry' layout='publishStemMinorEntry' hideConfig='true' /></layoutType>'" }); var migrator = new DictionaryConfigurationMigrator(m_propertyTable, m_mediator); Assert.DoesNotThrow(() => migrator.MigrateOldConfigurationsIfNeeded(), "ArgumentException indicates localized labels."); // SUT var updatedConfigModel = new DictionaryConfigurationModel(newConfigFilePath, Cache); Assert.AreEqual(2, updatedConfigModel.Parts.Count, "Should have 2 top-level nodes"); Assert.AreEqual("Main Entry", updatedConfigModel.Parts[0].Label); RobustIO.DeleteDirectoryAndContents(configSettingsDir); }
public override void FixtureTeardown() { RobustIO.DeleteDirectoryAndContents(Cache.ProjectId.Path); base.FixtureTeardown(); m_application.Dispose(); m_window.Dispose(); m_mediator.Dispose(); FwRegistrySettings.Release(); }
public void AsyncLocalCheckIn_NoPreviousRepoCreation_Throws() { Assert.Throws <InvalidOperationException>(() => { //simulate not having previously created a repository RobustIO.DeleteDirectoryAndContents(_pathToTestRoot.CombineForPath(".hg")); _model.AsyncLocalCheckIn("testing", null); }); }
public void DeleteDirectoryAndContents_ContainsReadOnlyFile_StillRemoves() { using (var tempDir = new TemporaryFolder("DeleteDirectoryAndContents_ContainsReadOnlyFile_StillRemoves")) { var fileName = tempDir.Combine("tempFile.txt"); File.WriteAllText(fileName, @"Some test text"); new FileInfo(fileName).IsReadOnly = true; RobustIO.DeleteDirectoryAndContents(tempDir.Path); Assert.IsFalse(Directory.Exists(tempDir.Path), "Did not delete directory"); } }
public void DeleteDirectoryAndContents_NoOverrideContainsReadOnlyFile_ReturnsFalse() { using (var tempDir = new TemporaryFolder("DeleteDirectoryAndContents_NoOverrideContainsReadOnlyFile_ReturnsFalse")) { var fileName = tempDir.Combine("tempFile.txt"); File.WriteAllText(fileName, @"Some test text"); new FileInfo(fileName).IsReadOnly = true; Assert.IsFalse(RobustIO.DeleteDirectoryAndContents(tempDir.Path, overrideReadOnly: false)); Assert.IsTrue(Directory.Exists(tempDir.Path), "Did not expect it to delete directory because of the readonly file"); } }
public static void InitializeNewProject(string modelVersion, string initialPatchFile) { // Import the initial patch file in a temporary directory var tempMongoSource = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); InitSourceDir(tempMongoSource); TestHelper.Run(Git, $"am --ignore-whitespace {initialPatchFile}", tempMongoSource); // Then copy the files to the real mongoSourceDir var mongoSourceDir = GetMongoSourceDir(modelVersion); InitSourceDir(mongoSourceDir); CopyFiles(tempMongoSource, mongoSourceDir); RobustIO.DeleteDirectoryAndContents(tempMongoSource); }
private static void RestoreLanguageDepot(Options.RestoreOptions options) { var dir = Path.Combine(Settings.TempDir, "LanguageDepot"); RobustIO.DeleteDirectoryAndContents(dir); using (var ld = new LanguageDepotHelper(true)) { for (var i = 0; i <= options.LanguageDepotVersion.Value; i++) { ld.ApplySinglePatch(options.ModelVersion, i); } } // Now in FLEx get project from colleague from USB Console.WriteLine("Successfully restored languagedepot test data for model " + $"{options.ModelVersion}"); }
protected override void Dispose(bool disposing) { base.Dispose(disposing); if (!disposing) { return; } if (string.IsNullOrEmpty(RepoDir)) { return; } RobustIO.DeleteDirectoryAndContents(RepoDir); RepoDir = null; }
public void MigrateOldConfigurationsIfNeeded_BringsPreHistoricFileToCurrentVersion() { var configSettingsDir = LcmFileHelper.GetConfigSettingsDir(Path.GetDirectoryName(Cache.ProjectId.Path)); var newConfigFilePath = Path.Combine(configSettingsDir, DictionaryConfigurationListener.DictionaryConfigurationDirectoryName, "Lexeme" + DictionaryConfigurationModel.FileExtension); Assert.False(File.Exists(newConfigFilePath), "should not yet be migrated"); Directory.CreateDirectory(configSettingsDir); File.WriteAllLines(Path.Combine(configSettingsDir, "Test.fwlayout"), new[] { @"<layoutType label='Lexeme-based (complex forms as main entries)' layout='publishStem'><configure class='LexEntry' label='Main Entry' layout='publishStemEntry' />", @"<configure class='LexEntry' label='Minor Entry' layout='publishStemMinorEntry' hideConfig='true' /></layoutType>'" }); var migrator = new DictionaryConfigurationMigrator(m_propertyTable, m_mediator); migrator.MigrateOldConfigurationsIfNeeded(); // SUT var updatedConfigModel = new DictionaryConfigurationModel(newConfigFilePath, Cache); Assert.AreEqual(DictionaryConfigurationMigrator.VersionCurrent, updatedConfigModel.Version); RobustIO.DeleteDirectoryAndContents(configSettingsDir); }
public void EnchantDictionaryMigrated() { // make temp folder with enchant dictionary in it var outputFolder = Path.Combine(Path.GetTempPath(), "EnchantDictionaryMigrationTestOut"); var inputFolder = Path.Combine(Path.GetTempPath(), "EnchantDictionaryMigrationTestIn"); RobustIO.DeleteDirectoryAndContents(outputFolder); RobustIO.DeleteDirectoryAndContents(inputFolder); Directory.CreateDirectory(outputFolder); Directory.CreateDirectory(inputFolder); // ReSharper disable LocalizableElement File.AppendAllText(Path.Combine(inputFolder, "test.dic"), "nil"); File.AppendAllText(Path.Combine(inputFolder, "test.exc"), "nil"); // ReSharper restore LocalizableElement // SUT Assert.DoesNotThrow(() => SpellingHelper.AddAnySpellingExceptionsFromBackup(inputFolder, outputFolder)); // Verify that the correct files were copied Assert.True(File.Exists(Path.Combine(outputFolder, "test.exc")), "The exception file should have been copied."); Assert.False(File.Exists(Path.Combine(outputFolder, "test.dic")), "The .dic file should not have been copied."); }
private void HandleOkButtonClick(object sender, EventArgs e) { if (m_model.RecordingProjectName != RecordingProjectName && File.Exists(Project.GetProjectFilePath(IsoCode, PublicationId, RecordingProjectName))) { var msg = string.Format(LocalizationManager.GetString("DialogBoxes.ProjectSettingsDlg.OverwriteProjectPrompt", "A {0} project with an ID of {1} and a Recording Project Name of {2} already exists for this language. Do you want to overwrite it?"), ProductName, m_txtPublicationId.Text, RecordingProjectName); if (MessageBox.Show(this, msg, ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) != DialogResult.Yes) { DialogResult = DialogResult.None; return; } var existingProjectPath = Project.GetProjectFolderPath(IsoCode, PublicationId, RecordingProjectName); if (!RobustIO.DeleteDirectoryAndContents(existingProjectPath)) { var failedMsg = string.Format(LocalizationManager.GetString("DialogBoxes.ProjectSettingsDlg.OverwriteProjectFailed", "{0} was unable to delete all of the files in {1}. You can try to clean this up manually and then re-attempt saving these changes."), ProductName, existingProjectPath); MessageBox.Show(this, failedMsg, ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } } m_model.RecordingProjectName = RecordingProjectName; m_model.AudioStockNumber = AudioStockNumber; m_model.ChapterAnnouncementStyle = ChapterAnnouncementStyle; m_model.SkipChapterAnnouncementForFirstChapter = !m_chkChapterOneAnnouncements.Checked; m_model.Project.ReferenceTextProxy = ((KeyValuePair <string, ReferenceTextProxy>)m_ReferenceText.SelectedItem).Value; m_model.Project.DramatizationPreferences.BookIntroductionsDramatization = (ExtraBiblicalMaterialSpeakerOption)m_bookIntro.SelectedValue; m_model.Project.DramatizationPreferences.SectionHeadDramatization = (ExtraBiblicalMaterialSpeakerOption)m_sectionHeadings.SelectedValue; m_model.Project.DramatizationPreferences.BookTitleAndChapterDramatization = (ExtraBiblicalMaterialSpeakerOption)m_titleChapters.SelectedValue; m_model.Project.ProjectSettingsStatus = ProjectSettingsStatus.Reviewed; DialogResult = DialogResult.OK; Close(); }
public void MigrateOldConfigurationsIfNeeded_PreservesOrderOfBibliographies() { var configSettingsDir = LcmFileHelper.GetConfigSettingsDir(Path.GetDirectoryName(Cache.ProjectId.Path)); var newConfigFilePath = Path.Combine(configSettingsDir, DictionaryConfigurationListener.ReversalIndexConfigurationDirectoryName, "AllReversalIndexes" + DictionaryConfigurationModel.FileExtension); Assert.False(File.Exists(newConfigFilePath), "should not yet be migrated"); Directory.CreateDirectory(configSettingsDir); File.WriteAllLines(Path.Combine(configSettingsDir, "Test.fwlayout"), new[] { @"<layoutType label='All Reversal Indexes' layout='publishReversal'>", @"<configure class='ReversalIndexEntry' label='Reversal Entry' layout='publishReversalEntry' /></layoutType>'" }); var migrator = new DictionaryConfigurationMigrator(m_propertyTable, m_mediator); migrator.MigrateOldConfigurationsIfNeeded(); // SUT var updatedConfigModel = new DictionaryConfigurationModel(newConfigFilePath, Cache); var refdSenseChildren = updatedConfigModel.Parts[0].Children.Find(n => n.Label == "Referenced Senses").Children; var bibCount = 0; for (var i = 0; i < refdSenseChildren.Count; i++) { var bibNode = refdSenseChildren[i]; if (!bibNode.Label.StartsWith("Bibliography")) { continue; } StringAssert.StartsWith("Bibliography (", bibNode.Label, "Should specify (entry|sense), lest we never know"); Assert.False(bibNode.IsCustomField, bibNode.Label + " should not be custom."); // Rough test to ensure Bibliography nodes aren't bumped to the end of the list. In the defaults, the later Bibliography // node is a little more than five nodes from the end Assert.LessOrEqual(i, refdSenseChildren.Count - 5, "Bibliography nodes should not have been bumped to the end of the list"); ++bibCount; } Assert.AreEqual(2, bibCount, "Should be exactly two Bibliography nodes (sense and entry)"); RobustIO.DeleteDirectoryAndContents(configSettingsDir); }
public void CompressExportedFiles_IncludesAcceptableMediaTypes() { var view = new MockWebonaryDlg(); var tempDirectoryToCompress = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Directory.CreateDirectory(tempDirectoryToCompress); try { var zipFileToUpload = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); // TIFF var tiffFilename = Path.GetFileName(Path.GetTempFileName() + ".tif"); var tiffPath = Path.Combine(tempDirectoryToCompress, tiffFilename); var tiffMagicNumber = new byte[] { 0x49, 0x49, 0x2A }; File.WriteAllBytes(tiffPath, tiffMagicNumber); // JPEG var jpegFilename = Path.GetFileName(Path.GetTempFileName() + ".jpg"); var jpegPath = Path.Combine(tempDirectoryToCompress, jpegFilename); var jpegMagicNumber = new byte[] { 0xff, 0xd8 }; File.WriteAllBytes(jpegPath, jpegMagicNumber); // MP4 var mp4Filename = Path.GetFileName(Path.GetTempFileName() + ".mp4"); var mp4Path = Path.Combine(tempDirectoryToCompress, mp4Filename); var mp4MagicNumber = new byte[] { 0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32 }; File.WriteAllBytes(mp4Path, mp4MagicNumber); var xhtmlFilename = Path.GetFileName(Path.GetTempFileName() + ".xhtml"); var xhtmlPath = Path.Combine(tempDirectoryToCompress, xhtmlFilename); var xhtmlContent = "<xhtml/>"; File.WriteAllText(xhtmlPath, xhtmlContent); // SUT UploadToWebonaryController.CompressExportedFiles(tempDirectoryToCompress, zipFileToUpload, view); // Verification const string unsupported = ".*nsupported.*"; const string unsupportedRegex = ".*{0}" + unsupported; using (var uploadZip = new ZipFile(zipFileToUpload)) { Assert.False(uploadZip.EntryFileNames.Contains(tiffFilename), "Should not have included unsupported TIFF file in file to upload."); Assert.True(uploadZip.EntryFileNames.Contains(jpegFilename), "Should have included supported JPEG file in file to upload."); Assert.True(uploadZip.EntryFileNames.Contains(mp4Filename), "Should have included supported MP4 file in file to upload."); } var query = string.Format(unsupportedRegex, tiffFilename); Assert.True(view.StatusStrings.Exists(statusString => Regex.Matches(statusString, query).Count == 1), "Lack of support for the tiff file should have been reported to the user."); query = string.Format(unsupportedRegex, jpegFilename); Assert.False(view.StatusStrings.Exists(statusString => Regex.Matches(statusString, query).Count == 1), "Should not have reported lack of support for the jpeg file."); query = string.Format(unsupportedRegex, mp4Filename); Assert.False(view.StatusStrings.Exists(statusString => Regex.Matches(statusString, query).Count == 1), "Should not have reported lack of support for the mp4 file."); Assert.That(view.StatusStrings.Count(statusString => Regex.Matches(statusString, unsupported).Count > 0), Is.EqualTo(1), "Too many unsupported files reported."); } finally { RobustIO.DeleteDirectoryAndContents(tempDirectoryToCompress); } }
public static void Cleanup() { RobustIO.DeleteDirectoryAndContents(Path.Combine(Settings.TempDir, "patches")); }
[RequestSizeLimit(250_000_000)] // 250MB. public async Task <IActionResult> UploadLiftFile(string projectId, [FromForm] FileUpload fileUpload) { if (!await _permissionService.HasProjectPermission(HttpContext, Permission.ImportExport)) { return(Forbid()); } // Sanitize projectId if (!Sanitization.SanitizeId(projectId)) { return(new UnsupportedMediaTypeResult()); } // Ensure Lift file has not already been imported. if (!await _projRepo.CanImportLift(projectId)) { return(BadRequest("A Lift file has already been uploaded.")); } var liftStoragePath = FileStorage.GenerateLiftImportDirPath(projectId); // Clear out any files left by a failed import RobustIO.DeleteDirectoryAndContents(liftStoragePath); var file = fileUpload.File; if (file is null) { return(BadRequest("Null File")); } // Ensure file is not empty if (file.Length == 0) { return(BadRequest("Empty File")); } // Copy zip file data to a new temporary file fileUpload.FilePath = Path.GetTempFileName(); await using (var fs = new FileStream(fileUpload.FilePath, FileMode.OpenOrCreate)) { await file.CopyToAsync(fs); } // Make temporary destination for extracted files var extractDir = FileOperations.GetRandomTempDir(); // Extract the zip to new created directory. FileOperations.ExtractZipFile(fileUpload.FilePath, extractDir, true); // Check number of directories extracted var directoriesExtracted = Directory.GetDirectories(extractDir); var extractedDirPath = ""; switch (directoriesExtracted.Length) { // If there was one directory, we're good case 1: { extractedDirPath = directoriesExtracted.First(); break; } // If there were two, and there was a __MACOSX directory, ignore it case 2: { var numDirs = 0; foreach (var dir in directoriesExtracted) { if (dir.EndsWith("__MACOSX")) { Directory.Delete(dir, true); } else // This directory probably matters { extractedDirPath = dir; numDirs++; } } // Both directories seemed important if (numDirs == 2) { return(BadRequest("Your zip file should have one directory.")); } break; } // There were 0 or more than 2 directories default: { return(BadRequest( "Your zip file structure has the wrong number of directories.")); } } // Copy the extracted contents into the persistent storage location for the project. FileOperations.CopyDirectory(extractedDirPath, liftStoragePath); Directory.Delete(extractDir, true); // Search for the lift file within the extracted files var extractedLiftNameArr = Directory.GetFiles(liftStoragePath); var extractedLiftPath = Array.FindAll(extractedLiftNameArr, x => x.EndsWith(".lift")); if (extractedLiftPath.Length > 1) { return(BadRequest("More than one .lift file detected.")); } if (extractedLiftPath.Length == 0) { return(BadRequest("No lift files detected.")); } int liftParseResult; // Sets the projectId of our parser to add words to that project var liftMerger = _liftService.GetLiftImporterExporter(projectId, _wordRepo); try { // Add character set to project from ldml file var proj = await _projRepo.GetProject(projectId); if (proj is null) { return(NotFound(projectId)); } _liftService.LdmlImport( Path.Combine(liftStoragePath, "WritingSystems"), proj.VernacularWritingSystem.Bcp47, _projRepo, proj); var parser = new LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample>(liftMerger); // Import words from lift file liftParseResult = parser.ReadLiftFile(extractedLiftPath.FirstOrDefault()); await liftMerger.SaveImportEntries(); } catch (Exception e) { _logger.LogError(e, $"Error importing lift file {fileUpload.Name} into project {projectId}."); return(BadRequest("Error processing the lift data. Contact support for help.")); } // Store that we have imported Lift data already for this project to signal the frontend // not to attempt to import again. var project = await _projRepo.GetProject(projectId); if (project is null) { return(NotFound(projectId)); } project.LiftImported = true; await _projRepo.Update(projectId, project); return(Ok(liftParseResult)); }
private static void Wizard(Options.WizardOptions wizardOptions) { var workdir = wizardOptions.WorkDir; MongoHelper.AddTestUser(Settings.DataDir); for (var modelVersion = wizardOptions.MinModel; modelVersion <= wizardOptions.MaxModel; modelVersion++) { if (modelVersion == 7000071) { continue; } Console.WriteLine( "--------------------------------------------------------------"); Console.WriteLine($"Processing model version {modelVersion}"); if (string.IsNullOrEmpty(workdir)) { wizardOptions.WorkDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); Settings.TempDir = wizardOptions.WorkDir; Directory.CreateDirectory(wizardOptions.WorkDir); } wizardOptions.ModelVersion = modelVersion; var patchDir = Path.Combine(Settings.DataDir, modelVersion.ToString()); if (!Directory.Exists(patchDir)) { Directory.CreateDirectory(patchDir); } var mongoDir = Path.Combine(patchDir, "mongo"); if (!Directory.Exists(mongoDir)) { Directory.CreateDirectory(mongoDir); } var outputDir = Path.Combine(wizardOptions.FwRoot, $"Output{modelVersion}"); if (!Directory.Exists(outputDir)) { Console.WriteLine($"Can't find FW output directory {outputDir}"); continue; } var sourceEnviron = modelVersion < 7000072 ? "fwenviron" : "fwenviron9"; var targetEnviron = Path.Combine(outputDir, "fwenviron"); if (!File.Exists(targetEnviron)) { File.Copy( Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), sourceEnviron), targetEnviron); } // LanguageDepot if (wizardOptions.NewProject) { wizardOptions.LanguageDepotVersion = 0; wizardOptions.MongoVersion = 0; // Delete FW project RobustIO.DeleteDirectoryAndContents( Path.Combine(wizardOptions.GetFwProjectDirectory(modelVersion), $"test-{modelVersion}")); RobustIO.DeleteDirectoryAndContents( Path.Combine(wizardOptions.UsbDirectory, $"test-{modelVersion}")); Console.WriteLine( $"Create a new project 'test-{modelVersion}' and send this project for the first time to the USB stick"); Run("/bin/bash", $"-i -c \"cd {outputDir} && . fwenviron && cd Output_$(uname -m)/Debug && mono --debug FieldWorks.exe > /dev/null\"", outputDir, true); } else { Console.WriteLine($"Restoring version {wizardOptions.LanguageDepotVersion} " + $"of chorus repo for model {modelVersion}"); RestoreLanguageDepot(new Options.RestoreOptions(wizardOptions)); DirectoryHelper.Copy(Path.Combine(wizardOptions.WorkDir, "LanguageDepot", ".hg"), Path.Combine(wizardOptions.UsbDirectory, $"test-{modelVersion}", ".hg"), true); // Delete FW project RobustIO.DeleteDirectoryAndContents( Path.Combine(wizardOptions.GetFwProjectDirectory(modelVersion), $"test-{modelVersion}")); Console.WriteLine( $"Now get project 'test-{modelVersion}' from USB stick and make changes and afterwards do a s/r with the USB stick."); // for whatever reason we have to pass -i, otherwise 7000069 won't be able to bring // up the s/r dialog! Run("/bin/bash", $"-i -c \"cd {outputDir} && . fwenviron && cd Output_$(uname -m)/Debug && mono --debug FieldWorks.exe > /dev/null\"", outputDir, true); } Console.WriteLine($"Saving chorus repo test data for {modelVersion}"); DirectoryHelper.Copy( Path.Combine(wizardOptions.UsbDirectory, $"test-{modelVersion}", ".hg"), Path.Combine(wizardOptions.WorkDir, "LanguageDepot", ".hg"), true); SaveLanguageDepot(new Options.SaveOptions(wizardOptions) { WorkDir = null, CommitMsg = wizardOptions.CommitMsg ?? "New test data" }); SaveLanguageDepotNoOpPatchIfNecessary(modelVersion, wizardOptions.LanguageDepotVersion + 1); // Mongo if (wizardOptions.NewProject) { // Since this is a new project, we copy the first patch for mongo from an // older model version, if we can find it int tmpModelVersion; var mongoPatchFile = string.Empty; for (tmpModelVersion = Settings.MinModelVersion; tmpModelVersion <= Settings.MaxModelVersion; tmpModelVersion++) { var dirInfo = new DirectoryInfo(Path.Combine(Settings.DataDir, tmpModelVersion.ToString(), "mongo")); var patchFiles = dirInfo.GetFiles("0001-*.patch"); if (patchFiles.Length != 1) { continue; } mongoPatchFile = patchFiles[0].FullName; break; } if (tmpModelVersion > Settings.MaxModelVersion) { Console.WriteLine( "ERROR: Can't find first mongo patch. Please create mongo project some other way."); return; } MongoHelper.InitializeNewProject(modelVersion.ToString(), mongoPatchFile); Console.WriteLine($"Saving LanguageForge test data for {modelVersion}"); SaveMongoDb(new Options.SaveOptions(wizardOptions) { CommitMsg = "Add empty project" }); } else { Console.WriteLine( $"Restoring LanguageForge mongo data version {wizardOptions.MongoVersion} for model {modelVersion}"); RestoreMongoDb(new Options.RestoreOptions(wizardOptions)); Console.WriteLine( $"Now make the changes to '{wizardOptions.Project}' in your local" + " LanguageForge, then press return. Don't do a send/receive!"); Console.WriteLine( "(You might have to empty the cached IndexedDB data in your " + "browser's developer tools)"); Console.WriteLine("Login as user 'test' with password 'passwordfortest'"); Run("/bin/bash", "-c \"xdg-open http://languageforge.local/app/projects\"", outputDir); Console.ReadLine(); Console.WriteLine($"Saving LanguageForge test data for {modelVersion}"); SaveMongoDb(new Options.SaveOptions(wizardOptions) { CommitMsg = wizardOptions.CommitMsg ?? "New test data" }); // Merge the data we just created Console.WriteLine($"Merge test data for {modelVersion}"); Merge(new Options.MergeOptions(wizardOptions) { LanguageDepotVersion = wizardOptions.LanguageDepotVersion + 1, MongoVersion = wizardOptions.MongoVersion + 1 }); } if (string.IsNullOrEmpty(workdir)) { // we created a temporary workdir, so delete it again RobustIO.DeleteDirectoryAndContents(wizardOptions.WorkDir); } } }
public static void Cleanup() { RobustIO.DeleteDirectoryAndContents(BaseDir); }
///<summary> /// Perform the migration ///</summary> ///<exception cref="InvalidOperationException"></exception> public virtual void Migrate() { var problems = new List <FolderMigratorProblem>(); // Clean up root backup path if (Directory.Exists(MigrationPath)) { if (!RobustIO.DeleteDirectoryAndContents(MigrationPath)) { //for user, ah well //for programmer, tell us Debug.Fail("(Debug mode only) Couldn't delete the migration folder"); } } //check if there is anything to migrate. If not, don't do anything if (!Directory.Exists(SourcePath)) { return; } if (GetLowestVersionInFolder(SourcePath) == ToVersion) { return; } // Backup current folder to backup path under backup root CopyDirectory(SourcePath, BackupPath, MigrationPath); string currentPath = BackupPath; int lowestVersionInFolder; int lowestVersoinInFolder1 = -1; while ((lowestVersionInFolder = GetLowestVersionInFolder(currentPath)) != ToVersion) { //This guards against an empty Folder if (lowestVersionInFolder == int.MaxValue) { break; } if (lowestVersionInFolder == lowestVersoinInFolder1) { var fileNotMigrated = _versionCache.First(info => info.Version == lowestVersionInFolder).FileName; throw new ApplicationException( String.Format("The migration strategy for {0} failed to migrate the file '{1} from version {2}", SearchPattern, fileNotMigrated, lowestVersoinInFolder1) ); } int currentVersion = lowestVersionInFolder; // Get all files in folder with this version var fileNamesToMigrate = GetFilesOfVersion(currentVersion, currentPath); // Find a strategy to migrate this version IMigrationStrategy strategy = MigrationStrategies.Find(s => s.FromVersion == currentVersion); if (strategy == null) { throw new InvalidOperationException( String.Format("No migration strategy could be found for version {0}", currentVersion) ); } string destinationPath = Path.Combine( MigrationPath, String.Format("{0}_{1}", strategy.FromVersion, strategy.ToVersion) ); Directory.CreateDirectory(destinationPath); // Migrate all the files of the current version foreach (var filePath in Directory.GetFiles(currentPath, SearchPattern)) { string fileName = Path.GetFileName(filePath); if (fileName == null) { continue; } string sourceFilePath = Path.Combine(currentPath, fileName); string targetFilePath = Path.Combine(destinationPath, fileName); if (fileNamesToMigrate.Contains(sourceFilePath)) { strategy.Migrate(sourceFilePath, targetFilePath); } else { File.Copy(sourceFilePath, targetFilePath); } } try { strategy.PostMigrate(currentPath, destinationPath); } catch (Exception e) { problems.Add(new FolderMigratorProblem { Exception = e, FilePath = currentPath }); } // Setup for the next iteration currentPath = destinationPath; lowestVersoinInFolder1 = lowestVersionInFolder; } // Delete all the files in SourcePath matching SearchPattern foreach (var filePath in Directory.GetFiles(SourcePath, SearchPattern)) { File.Delete(filePath); } // Copy the migration results into SourcePath CopyDirectory(currentPath, SourcePath, ""); if (!RobustIO.DeleteDirectoryAndContents(MigrationPath)) { //for user, ah well //for programmer, tell us Debug.Fail("(Debug mode only) Couldn't delete the migration folder"); } // Call the problem handler if we encountered problems during migration. if (problems.Count > 0) { _problemHandler(problems); } }