buildSrc/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java [126:337]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  public void apply(Project rootProject) {

    // Create extensions
    OkBuckExtension okbuckExt =
        rootProject.getExtensions().create(OKBUCK, OkBuckExtension.class, rootProject);

    // Create configurations
    rootProject.getConfigurations().maybeCreate(TransformManager.CONFIGURATION_TRANSFORM);
    rootProject.getConfigurations().maybeCreate(FORCED_OKBUCK);

    rootProject.afterEvaluate(
        rootBuckProject -> {
          // Create autovalue extension configurations
          Set<String> configs =
              okbuckExt.getExternalDependenciesExtension().getAutoValueConfigurations();
          for (String config : configs) {
            rootBuckProject.getConfigurations().maybeCreate(config);
          }

          // Create tasks
          Task setupOkbuck = rootBuckProject.getTasks().create("setupOkbuck");
          setupOkbuck.setGroup(GROUP);
          setupOkbuck.setDescription("Setup okbuck cache and dependencies");

          // Create buck file manager.
          BuckFileManager buckFileManager =
              new BuckFileManager(okbuckExt.getRuleOverridesExtension());

          dependencyFactory = new DependencyFactory();

          // Create Annotation Processor cache
          String processorBuildFile = PROCESSOR_BUILD_FOLDER + "/" + okbuckExt.buildFileName;
          annotationProcessorCache =
              new AnnotationProcessorCache(rootBuckProject, buckFileManager, processorBuildFile);

          // Create Dependency manager
          dependencyManager =
              new DependencyManager(
                  rootBuckProject, okbuckExt, buckFileManager, createDependencyExporter(okbuckExt));

          // Create Lint Manager
          String lintBuildFile = LINT_BUILD_FOLDER + "/" + okbuckExt.buildFileName;
          lintManager = new LintManager(rootBuckProject, lintBuildFile, buckFileManager);

          // Create Kotlin Manager
          kotlinManager = new KotlinManager(rootBuckProject, buckFileManager);

          // Create Scala Manager
          scalaManager = new ScalaManager(rootBuckProject, buckFileManager);

          // Create Scala Manager
          groovyManager = new GroovyManager(rootBuckProject, buckFileManager);

          // Create Jetifier Manager
          jetifierManager = new JetifierManager(rootBuckProject, buckFileManager);

          // Create Robolectric Manager
          robolectricManager = new RobolectricManager(rootBuckProject, buckFileManager);

          // Create Transform Manager
          transformManager = new TransformManager(rootBuckProject, buckFileManager);

          // Create D8 Manager
          d8Manager = new D8Manager(rootBuckProject);

          // Create Buck Manager
          buckManager = new BuckManager(rootBuckProject);

          // Create Manifest Merger Manager
          manifestMergerManager = new ManifestMergerManager(rootBuckProject, buckFileManager);

          KotlinExtension kotlin = okbuckExt.getKotlinExtension();
          ScalaExtension scala = okbuckExt.getScalaExtension();

          Task rootOkBuckTask =
              rootBuckProject
                  .getTasks()
                  .create(OKBUCK, OkBuckTask.class, okbuckExt, kotlin, scala, buckFileManager);
          rootOkBuckTask.dependsOn(setupOkbuck);
          rootOkBuckTask.doLast(
              task -> {
                annotationProcessorCache.finalizeProcessors();
                dependencyManager.resolveCurrentRawDeps();
                dependencyManager.finalizeDependencies(okbuckExt);
                jetifierManager.finalizeDependencies(okbuckExt);
                lintManager.finalizeDependencies();
                kotlinManager.finalizeDependencies(okbuckExt);
                scalaManager.finalizeDependencies(okbuckExt);
                groovyManager.finalizeDependencies(okbuckExt);
                robolectricManager.finalizeDependencies(okbuckExt);
                transformManager.finalizeDependencies(okbuckExt);
                buckManager.finalizeDependencies();
                manifestMergerManager.finalizeDependencies(okbuckExt);
                dependencyFactory.finalizeDependencies();

                writeExportedFileRules(rootBuckProject, okbuckExt);

                // Reset root project's scope cache at the very end
                ProjectCache.resetScopeCache(rootProject);

                // Reset all project's target cache at the very end.
                // This cannot be done for a project just after its okbuck task since,
                // the target cache is accessed by other projects and have to
                // be available until okbuck tasks of all the projects finishes.
                ProjectCache.resetTargetCacheForAll(rootProject);
              });

          WrapperExtension wrapper = okbuckExt.getWrapperExtension();
          // Create wrapper task
          rootBuckProject
              .getTasks()
              .create(
                  BUCK_WRAPPER,
                  BuckWrapperTask.class,
                  wrapper.repo,
                  wrapper.watch,
                  wrapper.sourceRoots,
                  wrapper.ignoredDirs);

          Map<String, Configuration> extraConfigurations =
              okbuckExt.extraDepCachesMap.keySet().stream()
                  .collect(
                      Collectors.toMap(
                          Function.identity(),
                          cacheName ->
                              rootBuckProject
                                  .getConfigurations()
                                  .maybeCreate(cacheName + "ExtraDepCache")));

          setupOkbuck.doFirst(
              task -> {
                if (System.getProperty("okbuck.wrapper", "false").equals("false")) {
                  throw new IllegalArgumentException(
                      "Okbuck cannot be invoked without 'okbuck.wrapper' set to true. Use buckw instead");
                }
              });

          // Configure setup task
          setupOkbuck.doLast(
              task -> {
                // Init all project's target cache at the very start since a project
                // can access other project's target cache. Hence, all target cache
                // needs to be initialized before any okbuck task starts.
                ProjectCache.initTargetCacheForAll(rootProject);

                // Init root project's scope cache.
                ProjectCache.initScopeCache(rootProject);

                depCache = new DependencyCache(rootBuckProject, dependencyManager, FORCED_OKBUCK);

                // Fetch Lint deps if needed
                if (!okbuckExt.getLintExtension().disabled
                    && okbuckExt.getLintExtension().version != null) {
                  lintManager.fetchLintDeps(okbuckExt.getLintExtension().version);
                }

                // Fetch transform deps if needed
                if (!okbuckExt.getTransformExtension().transforms.isEmpty()) {
                  transformManager.fetchTransformDeps();
                }

                // Setup d8 deps
                d8Manager.copyDeps(buckFileManager, okbuckExt);

                // Fetch robolectric deps if needed
                if (okbuckExt.getTestExtension().robolectric) {
                  robolectricManager.download();
                }

                if (JetifierManager.isJetifierEnabled(rootProject)) {
                  jetifierManager.setupJetifier(okbuckExt.getJetifierExtension().version);
                }

                extraConfigurations.forEach(
                    (cacheName, extraConfiguration) ->
                        new DependencyCache(
                                rootBuckProject,
                                dependencyManager,
                                okbuckExt.extraDepCachesMap.getOrDefault(cacheName, false))
                            .build(extraConfiguration));

                buckManager.setupBuckBinary();

                manifestMergerManager.fetchManifestMergerDeps();
              });

          // Create clean task
          Task okBuckClean =
              rootBuckProject
                  .getTasks()
                  .create(OKBUCK_CLEAN, OkBuckCleanTask.class, okbuckExt.buckProjects);
          rootOkBuckTask.dependsOn(okBuckClean);

          // Create okbuck task on each project to generate their buck file
          okbuckExt.buckProjects.stream()
              .filter(p -> p.getBuildFile().exists())
              .forEach(
                  bp -> {
                    bp.getConfigurations().maybeCreate(BUCK_LINT);

                    Task okbuckProjectTask = bp.getTasks().maybeCreate(OKBUCK);
                    okbuckProjectTask.doLast(
                        task -> {
                          ProjectCache.initScopeCache(bp);
                          BuckFileGenerator.generate(bp, buckFileManager, okbuckExt);
                          ProjectCache.resetScopeCache(bp);
                        });
                    okbuckProjectTask.dependsOn(setupOkbuck);
                    okBuckClean.dependsOn(okbuckProjectTask);
                  });
        });
  }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



plugin/src/main/java/com/uber/okbuck/OkBuckGradlePlugin.java [126:337]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  public void apply(Project rootProject) {

    // Create extensions
    OkBuckExtension okbuckExt =
        rootProject.getExtensions().create(OKBUCK, OkBuckExtension.class, rootProject);

    // Create configurations
    rootProject.getConfigurations().maybeCreate(TransformManager.CONFIGURATION_TRANSFORM);
    rootProject.getConfigurations().maybeCreate(FORCED_OKBUCK);

    rootProject.afterEvaluate(
        rootBuckProject -> {
          // Create autovalue extension configurations
          Set<String> configs =
              okbuckExt.getExternalDependenciesExtension().getAutoValueConfigurations();
          for (String config : configs) {
            rootBuckProject.getConfigurations().maybeCreate(config);
          }

          // Create tasks
          Task setupOkbuck = rootBuckProject.getTasks().create("setupOkbuck");
          setupOkbuck.setGroup(GROUP);
          setupOkbuck.setDescription("Setup okbuck cache and dependencies");

          // Create buck file manager.
          BuckFileManager buckFileManager =
              new BuckFileManager(okbuckExt.getRuleOverridesExtension());

          dependencyFactory = new DependencyFactory();

          // Create Annotation Processor cache
          String processorBuildFile = PROCESSOR_BUILD_FOLDER + "/" + okbuckExt.buildFileName;
          annotationProcessorCache =
              new AnnotationProcessorCache(rootBuckProject, buckFileManager, processorBuildFile);

          // Create Dependency manager
          dependencyManager =
              new DependencyManager(
                  rootBuckProject, okbuckExt, buckFileManager, createDependencyExporter(okbuckExt));

          // Create Lint Manager
          String lintBuildFile = LINT_BUILD_FOLDER + "/" + okbuckExt.buildFileName;
          lintManager = new LintManager(rootBuckProject, lintBuildFile, buckFileManager);

          // Create Kotlin Manager
          kotlinManager = new KotlinManager(rootBuckProject, buckFileManager);

          // Create Scala Manager
          scalaManager = new ScalaManager(rootBuckProject, buckFileManager);

          // Create Scala Manager
          groovyManager = new GroovyManager(rootBuckProject, buckFileManager);

          // Create Jetifier Manager
          jetifierManager = new JetifierManager(rootBuckProject, buckFileManager);

          // Create Robolectric Manager
          robolectricManager = new RobolectricManager(rootBuckProject, buckFileManager);

          // Create Transform Manager
          transformManager = new TransformManager(rootBuckProject, buckFileManager);

          // Create D8 Manager
          d8Manager = new D8Manager(rootBuckProject);

          // Create Buck Manager
          buckManager = new BuckManager(rootBuckProject);

          // Create Manifest Merger Manager
          manifestMergerManager = new ManifestMergerManager(rootBuckProject, buckFileManager);

          KotlinExtension kotlin = okbuckExt.getKotlinExtension();
          ScalaExtension scala = okbuckExt.getScalaExtension();

          Task rootOkBuckTask =
              rootBuckProject
                  .getTasks()
                  .create(OKBUCK, OkBuckTask.class, okbuckExt, kotlin, scala, buckFileManager);
          rootOkBuckTask.dependsOn(setupOkbuck);
          rootOkBuckTask.doLast(
              task -> {
                annotationProcessorCache.finalizeProcessors();
                dependencyManager.resolveCurrentRawDeps();
                dependencyManager.finalizeDependencies(okbuckExt);
                jetifierManager.finalizeDependencies(okbuckExt);
                lintManager.finalizeDependencies();
                kotlinManager.finalizeDependencies(okbuckExt);
                scalaManager.finalizeDependencies(okbuckExt);
                groovyManager.finalizeDependencies(okbuckExt);
                robolectricManager.finalizeDependencies(okbuckExt);
                transformManager.finalizeDependencies(okbuckExt);
                buckManager.finalizeDependencies();
                manifestMergerManager.finalizeDependencies(okbuckExt);
                dependencyFactory.finalizeDependencies();

                writeExportedFileRules(rootBuckProject, okbuckExt);

                // Reset root project's scope cache at the very end
                ProjectCache.resetScopeCache(rootProject);

                // Reset all project's target cache at the very end.
                // This cannot be done for a project just after its okbuck task since,
                // the target cache is accessed by other projects and have to
                // be available until okbuck tasks of all the projects finishes.
                ProjectCache.resetTargetCacheForAll(rootProject);
              });

          WrapperExtension wrapper = okbuckExt.getWrapperExtension();
          // Create wrapper task
          rootBuckProject
              .getTasks()
              .create(
                  BUCK_WRAPPER,
                  BuckWrapperTask.class,
                  wrapper.repo,
                  wrapper.watch,
                  wrapper.sourceRoots,
                  wrapper.ignoredDirs);

          Map<String, Configuration> extraConfigurations =
              okbuckExt.extraDepCachesMap.keySet().stream()
                  .collect(
                      Collectors.toMap(
                          Function.identity(),
                          cacheName ->
                              rootBuckProject
                                  .getConfigurations()
                                  .maybeCreate(cacheName + "ExtraDepCache")));

          setupOkbuck.doFirst(
              task -> {
                if (System.getProperty("okbuck.wrapper", "false").equals("false")) {
                  throw new IllegalArgumentException(
                      "Okbuck cannot be invoked without 'okbuck.wrapper' set to true. Use buckw instead");
                }
              });

          // Configure setup task
          setupOkbuck.doLast(
              task -> {
                // Init all project's target cache at the very start since a project
                // can access other project's target cache. Hence, all target cache
                // needs to be initialized before any okbuck task starts.
                ProjectCache.initTargetCacheForAll(rootProject);

                // Init root project's scope cache.
                ProjectCache.initScopeCache(rootProject);

                depCache = new DependencyCache(rootBuckProject, dependencyManager, FORCED_OKBUCK);

                // Fetch Lint deps if needed
                if (!okbuckExt.getLintExtension().disabled
                    && okbuckExt.getLintExtension().version != null) {
                  lintManager.fetchLintDeps(okbuckExt.getLintExtension().version);
                }

                // Fetch transform deps if needed
                if (!okbuckExt.getTransformExtension().transforms.isEmpty()) {
                  transformManager.fetchTransformDeps();
                }

                // Setup d8 deps
                d8Manager.copyDeps(buckFileManager, okbuckExt);

                // Fetch robolectric deps if needed
                if (okbuckExt.getTestExtension().robolectric) {
                  robolectricManager.download();
                }

                if (JetifierManager.isJetifierEnabled(rootProject)) {
                  jetifierManager.setupJetifier(okbuckExt.getJetifierExtension().version);
                }

                extraConfigurations.forEach(
                    (cacheName, extraConfiguration) ->
                        new DependencyCache(
                                rootBuckProject,
                                dependencyManager,
                                okbuckExt.extraDepCachesMap.getOrDefault(cacheName, false))
                            .build(extraConfiguration));

                buckManager.setupBuckBinary();

                manifestMergerManager.fetchManifestMergerDeps();
              });

          // Create clean task
          Task okBuckClean =
              rootBuckProject
                  .getTasks()
                  .create(OKBUCK_CLEAN, OkBuckCleanTask.class, okbuckExt.buckProjects);
          rootOkBuckTask.dependsOn(okBuckClean);

          // Create okbuck task on each project to generate their buck file
          okbuckExt.buckProjects.stream()
              .filter(p -> p.getBuildFile().exists())
              .forEach(
                  bp -> {
                    bp.getConfigurations().maybeCreate(BUCK_LINT);

                    Task okbuckProjectTask = bp.getTasks().maybeCreate(OKBUCK);
                    okbuckProjectTask.doLast(
                        task -> {
                          ProjectCache.initScopeCache(bp);
                          BuckFileGenerator.generate(bp, buckFileManager, okbuckExt);
                          ProjectCache.resetScopeCache(bp);
                        });
                    okbuckProjectTask.dependsOn(setupOkbuck);
                    okBuckClean.dependsOn(okbuckProjectTask);
                  });
        });
  }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



