Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ 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,
dependencies: ModuleDependencies = ModuleDependencies(),
)

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -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",
],
)
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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]
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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]
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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]
}