diff --git a/migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model/Module.scala b/migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model/Module.scala index 77783719..e1a56cc7 100644 --- a/migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model/Module.scala +++ b/migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model/Module.scala @@ -2,6 +2,11 @@ package com.wix.bazel.migrator.model import com.wixpress.build.maven.{Coordinates, Dependency} +case class SourceModuleWithoutDeps(relativePathFromMonoRepoRoot: String, + coordinates: Coordinates, + resourcesPaths: Set[String] = Set.empty, + ) + case class SourceModule(relativePathFromMonoRepoRoot: String, coordinates: Coordinates, resourcesPaths: Set[String] = Set.empty, @@ -9,7 +14,8 @@ case class SourceModule(relativePathFromMonoRepoRoot: String, ) case class ModuleDependencies(directDependencies: Set[Dependency] = Set.empty, - allDependencies: Set[Dependency] = Set.empty) + allDependencies: Set[Dependency] = Set.empty, + managedDependencies: Set[Dependency] = Set.empty) //Omits version since source dependency //Omits packaging and classifier since they are very hard to generalize diff --git a/migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/transform/BUILD.bazel b/migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/transform/BUILD.bazel index f517e57f..df7874c7 100644 --- a/migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/transform/BUILD.bazel +++ b/migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/transform/BUILD.bazel @@ -11,9 +11,9 @@ scala_library( "//dependency-resolver/maven-dependency-resolver-api/src/main/scala/com/wixpress/build/maven", "//migrator/bazel-migrator:main_dependencies", "//migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model", + "//migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/analyze", "//models/maven-model/src/main/scala/com/wixpress/build/maven", "@org_jgrapht_jgrapht_core", - "//migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/analyze:analyze", ], ) diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BUILD.bazel b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BUILD.bazel new file mode 100644 index 00000000..52b926d3 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BUILD.bazel @@ -0,0 +1,13 @@ +package(default_visibility = ["//visibility:public"]) + +sources() + +scala_library( + name = "v2", + srcs = [":sources"], + deps = [ + "//migrator/bazel-migrator-model/src/main/java/com/wix/bazel/migrator/model", + "//migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/analyze", + "//migrator/bazel-migrator/src/main/java/com/wix/bazel/migrator/transform", + ], +) diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BuildFilesBuilder.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BuildFilesBuilder.scala new file mode 100644 index 00000000..3f18c117 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/BuildFilesBuilder.scala @@ -0,0 +1,10 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.model + +/** + * Takes bazel packages and transforms it into a build file content + */ +trait BuildFilesBuilder { + def extractBuildFile(bazelPackage: model.Package): FileToWrite +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsExtractor.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsExtractor.scala new file mode 100644 index 00000000..24ce1c83 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsExtractor.scala @@ -0,0 +1,12 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.model.{ModuleDependencies, SourceModule} + +/** + * Extracts external dependencies from repo modules, transforms them into a single object of module dependencies + * That includes: direct deps, all deps and managed deps + * Consider side effect / additional return of report of collisions durin the reduction operation + */ +trait ExternalDepsExtractor { + def collectAndConsolidateExternalDeps(modules: Set[SourceModule]): ModuleDependencies +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsLoader.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsLoader.scala new file mode 100644 index 00000000..431c07c1 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ExternalDepsLoader.scala @@ -0,0 +1,13 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.model.ModuleDependencies + +/** + * Translate the external dependencies that were found into loader functions (bzl files) + * Dictates what needs to be written to WORKSPACE file in order to load those rules + */ +trait ExternalDepsLoader { + def workspacePart: String + + def externalDepsLoadersOf(repoExternalDependencies: ModuleDependencies): Set[FileToWrite] +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/FilesWriter.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/FilesWriter.scala new file mode 100644 index 00000000..cb1e9c3f --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/FilesWriter.scala @@ -0,0 +1,10 @@ +package com.wix.bazel.migrator.app.v2 + +/** + * Actually writes stuff to disk + */ +trait FilesWriter { + def appendToWorkspaceFile(content:String): Unit + def appendToFile(fileToWrite: FileToWrite): Unit +} +case class FileToWrite(relativePathToFile: String, content: String) diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MavenModuleDiscovery.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MavenModuleDiscovery.scala new file mode 100644 index 00000000..29b61822 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MavenModuleDiscovery.scala @@ -0,0 +1,10 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.model.SourceModuleWithoutDeps + +/** + * Discovers repo modules without resolving the deps of them + */ +trait MavenModuleDiscovery { + def findModules(): Set[SourceModuleWithoutDeps] +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MigratorV2.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MigratorV2.scala new file mode 100644 index 00000000..4e915cec --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/MigratorV2.scala @@ -0,0 +1,79 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.analyze.{Code, DependencyAnalyzer} +import com.wix.bazel.migrator.app.v2.MigratorV2.CodeModules +import com.wix.bazel.migrator.model +import com.wix.bazel.migrator.model.{SourceModule, SourceModuleWithoutDeps} +import com.wix.bazel.migrator.transform.CodeAnalysisTransformer + +class MigratorV2(modulesDiscovery: MavenModuleDiscovery, + modulesDepsDiscovery: ModulesDepsDiscovery, + dependencyAnalyzer: DependencyAnalyzer, + externalDepsExtractor: ExternalDepsExtractor, + externalDepsLoader: ExternalDepsLoader, + buildFilesBuilder: BuildFilesBuilder, + staticFiles: StaticFiles, + filesWriter: FilesWriter) { + + def migrate(): Unit = { + // Step 0: find modules with deps + val modulesWithDeps: Set[SourceModule] = findModulesWithDeps + + // Step 1 : write bazel packages (depends on thread0) + val codeModules: CodeModules = modulesWithDeps.map(m => m -> dependencyAnalyzer.allCodeForModule(m)).toMap + val bazelPackages: Set[model.Package] = transformer(codeModules).transform(modulesWithDeps) + bazelPackages.foreach(writeBuildFile) + + // Step 2 : write bazel deps (depends thread 0) + val consolidatedDeps = externalDepsExtractor.collectAndConsolidateExternalDeps(modulesWithDeps) + externalDepsLoader.externalDepsLoadersOf(consolidatedDeps).foreach(filesWriter.appendToFile) + + // Step 3 : Static files (don't depend on anything) + filesWriter.appendToWorkspaceFile(staticFiles.workspaceFileHeader) + filesWriter.appendToWorkspaceFile(externalDepsLoader.workspacePart) + staticFiles.otherFiles.foreach(filesWriter.appendToFile) + filesWriter.appendToWorkspaceFile(staticFiles.worksapceFileFooter) + + } + + private def writeBuildFile(babelPackage: model.Package): Unit = { + filesWriter.appendToFile(buildFilesBuilder.extractBuildFile(babelPackage)) + } + + private def findModulesWithDeps = { + val modules: Set[SourceModuleWithoutDeps] = modulesDiscovery.findModules() + val modulesWithDeps: Set[SourceModule] = modules.map(modulesDepsDiscovery.findModuleDeps) + modulesWithDeps + } + + // workaround since CodeAnalysisTransformer requires DependencyAnalyzer instance + private def transformer(codeModules: CodeModules) = new CodeAnalysisTransformer((module: SourceModule) => codeModules(module)) + +} + +object MigratorV2 { + type CodeModules = Map[SourceModule, List[Code]] + + // all of those members should be instantiated using the run config at the beginning of the run + val modulesDiscovery: MavenModuleDiscovery = ??? + val modulesDepsDiscovery: ModulesDepsDiscovery = ??? + val dependencyAnalyzer: DependencyAnalyzer = ??? + val externalDepsExtractor: ExternalDepsExtractor = ??? + val externalDepsLoader: ExternalDepsLoader = ??? + val filesWriter: FilesWriter = ??? + val buildFilesBuilder: BuildFilesBuilder = ??? + val staticFiles: StaticFiles = ??? + + def main(args: Array[String]): Unit = { + val migrator = new MigratorV2(modulesDiscovery, + modulesDepsDiscovery, + dependencyAnalyzer, + externalDepsExtractor, + externalDepsLoader, + buildFilesBuilder, + staticFiles, + filesWriter) + + migrator.migrate() + } +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ModulesDepsDiscovery.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ModulesDepsDiscovery.scala new file mode 100644 index 00000000..415eadc4 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/ModulesDepsDiscovery.scala @@ -0,0 +1,10 @@ +package com.wix.bazel.migrator.app.v2 + +import com.wix.bazel.migrator.model.{SourceModule, SourceModuleWithoutDeps} + +/** + * Finds deps of given module: direct deps, all deps and managed deps. + */ +trait ModulesDepsDiscovery { + def findModuleDeps(sourceModule: SourceModuleWithoutDeps): SourceModule +} diff --git a/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/StaticFiles.scala b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/StaticFiles.scala new file mode 100644 index 00000000..197eca56 --- /dev/null +++ b/migrator/wix-bazel-migrator/src/main/java/com/wix/bazel/migrator/app/v2/StaticFiles.scala @@ -0,0 +1,13 @@ +package com.wix.bazel.migrator.app.v2 + +/** + * Provides the static content of files that should be written in any case. + * One way to implement it is to give a path to static "template" folder + list of key-value to replace in files. + */ +trait StaticFiles { + def workspaceFileHeader: String + + def worksapceFileFooter: String + + def otherFiles: Set[FileToWrite] +}