Ejemplo n.º 1
0
 public MultiPopulation(int p1, int p2, MigrationStrategies.Random migrationStrategy)
 {
     // TODO: Complete member initialization
     this.p1 = p1;
     this.p2 = p2;
     this.migrationStrategy = migrationStrategy;
 }
Ejemplo n.º 2
0
        public void Migrate()
        {
            var filesToDelete = new List <string>();

            if (File.Exists(BackupFilePath))
            {
                File.Delete(BackupFilePath);
            }
            File.Copy(SourceFilePath, BackupFilePath);
            filesToDelete.Add(BackupFilePath);
            int    currentVersion      = GetFileVersion();
            string sourceFilePath      = SourceFilePath;
            string destinationFilePath = "";

            while (currentVersion != ToVersion)
            {
                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)
                              );
                }
                destinationFilePath = String.Format("{0}.Migrate_{1}_{2}", SourceFilePath, strategy.FromVersion, strategy.ToVersion);
                strategy.Migrate(sourceFilePath, destinationFilePath);
                filesToDelete.Add(destinationFilePath);
                currentVersion = strategy.ToVersion;
                sourceFilePath = destinationFilePath;
            }
            File.Delete(SourceFilePath);
            File.Move(destinationFilePath, SourceFilePath);
            foreach (var filePath in filesToDelete)
            {
                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
        }
Ejemplo n.º 3
0
        ///<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);
            }
        }