class VersionResolver {

  MigrationVersion resolve(SqlScript script, FlywayConfiguration configuration) {
    String normalizedDir = normalizedDirs.get(script.dir);
    if (normalizedDir == null) {
      // 5.0.0-M1 -> 5.0.0.M1 -> 5.0.0.1
      normalizedDir =
          NOT_VERSION_CHARS_PATTERN.matcher(script.dir.replace("-", ".")).replaceAll("");
      normalizedDirs.put(script.dir, normalizedDir);
    }

    // separate version from the other part of the name
    final int sepIdx = script.name.indexOf(configuration.getSqlMigrationSeparator());
    if (sepIdx == -1) {
      throw new FlywayException(
          format(
              "sql script name '%s' is not valid, name must contain '%s'",
              script.name, configuration.getSqlMigrationSeparator()));
    }

    // check whether part before separator is not empty
    String version = script.name.substring(0, sepIdx);
    if (version.isEmpty()) {
      throw new FlywayException(
          format(
              "sql script name '%s' is not valid, name must provide version like "
                  + "'%s4%smigration_description.sql",
              configuration.getSqlMigrationPrefix(),
              script.name,
              configuration.getSqlMigrationSeparator()));
    }

    // extract sql script version without prefix
    final String prefix = configuration.getSqlMigrationPrefix();
    if (!isNullOrEmpty(prefix) && script.name.startsWith(prefix)) {
      version = version.substring(prefix.length());
    }
    return MigrationVersion.fromVersion(normalizedDir + '.' + version);
  }

}