override fun executeTask()

in plugin-bazel/src/main/kotlin/org/jetbrains/bazel/intellij/CopyPluginToSandboxBeforeRunTaskProvider.kt [41:97]


  override fun executeTask(
    context: DataContext,
    configuration: RunConfiguration,
    environment: ExecutionEnvironment,
    task: Task,
  ): Boolean {
    val runConfiguration = environment.runProfile as? BazelRunConfiguration ?: return false
    if (runConfiguration.handler !is IntellijPluginRunHandler) return false
    val pluginSandbox =
      checkNotNull(environment.getUserData(INTELLIJ_PLUGIN_SANDBOX_KEY)) {
        "INTELLIJ_PLUGIN_SANDBOX_KEY must be passed"
      }

    val pluginJars = mutableListOf<Path>()

    val targetUtils = configuration.project.targetUtils
    for (target in runConfiguration.targets) {
      val targetInfo = targetUtils.getBuildTargetForLabel(target)
      val module = targetInfo?.getModule(environment.project) ?: continue
      OrderEnumerator.orderEntries(module).librariesOnly().recursively().withoutSdk().forEachLibrary { library ->
        // Use URLs directly because getFiles will be empty until VFS refresh.
        library
          .getUrls(OrderRootType.CLASSES)
          .mapNotNull { "file://" + it.removePrefix("jar://").removeSuffix("!/") }
          .map { URI.create(it).toPath() }
          .forEach { pluginJars.add(it) }
        true
      }
    }

    if (pluginJars.isEmpty()) {
      showError(BazelPluginBundle.message("console.task.exception.no.plugin.jars"))
      return false
    }
    for (pluginJar in pluginJars) {
      if (!pluginJar.exists()) {
        showError(BazelPluginBundle.message("console.task.exception.plugin.jar.not.found", pluginJar))
        return false
      }
      try {
        pluginSandbox.createDirectories()
        pluginJar.copyTo(pluginSandbox.resolve(pluginJar.name), overwrite = true)
      } catch (e: IOException) {
        val errorMessage =
          BazelPluginBundle.message(
            "console.task.exception.plugin.jar.could.not.copy",
            pluginJar,
            pluginSandbox,
            e.message.orEmpty(),
          )
        showError(errorMessage)
        return false
      }
    }

    return true
  }