migrate.interfaces.Migrate Maven / Gradle / Ivy
The newest version!
package migrate.interfaces;
import coursierapi.Dependency;
import coursierapi.Fetch;
import coursierapi.ResolutionParams;
import coursierapi.ScalaVersion;
import java.io.File;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.List;
public interface Migrate {
void migrate(List unmanagedSources,
List managedSources,
Path targetRoot,
List scala2Classpath,
List scala2CompilerOptions,
List scala3Classpath,
List scala3CompilerOptions,
Path scala3ClassDirectory,
Path baseDirectory);
MigratedScalacOptions migrateScalacOption(List scala3CompilerOptions);
void migrateSyntax(List unmanagedSources,
Path targetRoot,
List scala2Classpath,
List scala2CompilerOptions,
Path baseDirectory);
static ClassLoader getClassLoader(String migrateVersion, String scalaVersion) throws Exception {
List jars = getJars(migrateVersion, scalaVersion);
ClassLoader parent = new MigrateClassloader(Migrate.class.getClassLoader());
URLClassLoader classLoader = new URLClassLoader(jars.stream().toArray(URL[]::new), parent);
return classLoader;
}
// Todo: Maybe using ServiceLoader could simplify this code a bit:
// https://www.baeldung.com/java-spi
static Migrate getInstance(ClassLoader classLoader, Logger logger) throws Exception {
Class> cls = classLoader.loadClass("migrate.interfaces.MigrateImpl");
Constructor> ctor = cls.getDeclaredConstructor(Logger.class);
ctor.setAccessible(true);
return (Migrate) ctor.newInstance(logger);
}
// put all needed dependecies here.
static List getJars(String migrateVersion, String scalaVersion) throws Exception {
ScalaVersion scalaV = ScalaVersion.of(scalaVersion);
Dependency migrate = Dependency.parse("ch.epfl.scala::scala3-migrate-core:" + migrateVersion, scalaV);
List jars = fetch(Collections.singletonList(migrate), ResolutionParams.create());
return jars;
}
static List fetch(List dependencies, ResolutionParams resolutionParams) throws Exception {
List jars = new ArrayList<>();
List files = Fetch.create().withDependencies(dependencies.stream().toArray(Dependency[]::new))
.withResolutionParams(resolutionParams).fetch();
for (File file : files) {
URL url = file.toURI().toURL();
jars.add(url);
}
return jars;
}
}