Пример #1
0
 private void createProjects(
     ProjectDescriptor rootProjectDescriptor,
     GradleInternal gradle,
     ClassLoaderScope buildRootClassLoaderScope) {
   ProjectInternal rootProject =
       projectFactory.createProject(
           rootProjectDescriptor,
           null,
           gradle,
           buildRootClassLoaderScope.createChild("root-project"),
           buildRootClassLoaderScope);
   gradle.setRootProject(rootProject);
   addProjects(rootProject, rootProjectDescriptor, gradle, buildRootClassLoaderScope);
 }
 private void applyScript(Object script) {
   URI scriptUri = resolver.resolveUri(script);
   UriScriptSource scriptSource = new UriScriptSource("script", scriptUri);
   ClassLoaderScope classLoaderScopeChild = classLoaderScope.createChild();
   ScriptHandler scriptHandler = scriptHandlerFactory.create(scriptSource, classLoaderScopeChild);
   ScriptPlugin configurer =
       configurerFactory.create(
           scriptSource,
           scriptHandler,
           classLoaderScopeChild,
           classLoaderScope,
           "buildscript",
           DefaultScript.class,
           false);
   for (Object target : targets) {
     configurer.apply(target);
   }
 }
Пример #3
0
 public Class<? extends Plugin> resolve() {
   ClassPath classPath = classPathFactory.create();
   Factory<? extends ClassLoader> loader = parent.loader(classPath);
   PluginRegistry pluginRegistry = new DefaultPluginRegistry(loader, instantiator);
   return pluginRegistry.getTypeForId(pluginId.toString());
 }
    public void apply(final Object target) {
      DefaultServiceRegistry services = new DefaultServiceRegistry();
      services.add(ScriptPluginFactory.class, DefaultScriptPluginFactory.this);
      services.add(ScriptHandlerFactory.class, scriptHandlerFactory);
      services.add(ClassLoaderScope.class, targetScope);
      services.add(LoggingManagerInternal.class, loggingManagerFactory.create());
      services.add(Instantiator.class, instantiator);
      services.add(ScriptHandler.class, scriptHandler);
      services.add(FileLookup.class, fileLookup);
      services.add(ModelRuleSourceDetector.class, modelRuleSourceDetector);

      ScriptSource withImports = importsReader.withImports(scriptSource);

      PluginDependenciesService pluginDependenciesService =
          new PluginDependenciesService(getSource());
      services.add(PluginDependenciesService.class, pluginDependenciesService);

      ScriptCompiler compiler = scriptCompilerFactory.createCompiler(withImports);
      compiler.setClassloader(baseScope.getExportClassLoader());

      boolean supportsPluginsBlock = ProjectScript.class.isAssignableFrom(scriptType);
      String onPluginBlockError =
          supportsPluginsBlock ? null : "Only Project build scripts can contain plugins {} blocks";

      PluginsAndBuildscriptTransformer scriptBlockTransformer =
          new PluginsAndBuildscriptTransformer(
              classpathClosureName, onPluginBlockError, documentationRegistry);

      StatementExtractingScriptTransformer classpathScriptTransformer =
          new StatementExtractingScriptTransformer(classpathClosureName, scriptBlockTransformer);

      compiler.setTransformer(classpathScriptTransformer);

      ScriptRunner<? extends BasicScript> classPathScriptRunner = compiler.compile(scriptType);
      classPathScriptRunner.getScript().init(target, services);
      classPathScriptRunner.run();

      List<PluginRequest> pluginRequests = pluginDependenciesService.getRequests();
      PluginAwareInternal pluginAware =
          target instanceof PluginAwareInternal ? (PluginAwareInternal) target : null;
      pluginRequestApplicator.applyPlugins(pluginRequests, scriptHandler, pluginAware, targetScope);

      compiler.setClassloader(targetScope.getLocalClassLoader());

      BuildScriptTransformer transformer =
          new BuildScriptTransformer(
              "no_" + classpathScriptTransformer.getId(),
              classpathScriptTransformer.invert(),
              scriptSource);
      compiler.setTransformer(transformer);

      // TODO - find a less tangled way of getting this in here, see the verifier impl for why it's
      // needed
      compiler.setVerifier(new ClosureCreationInterceptingVerifier());

      ScriptRunner<? extends BasicScript> runner = compiler.compile(scriptType);

      BasicScript script = runner.getScript();
      script.init(target, services);
      if (ownerScript && target instanceof ScriptAware) {
        ((ScriptAware) target).setScript(script);
      }
      runner.run();
    }
  public void applyPlugins(
      Iterable<? extends PluginRequest> requests,
      final ScriptHandler scriptHandler,
      final PluginAware target,
      ClassLoaderScope classLoaderScope) {
    final PluginResolutionApplicator resolutionApplicator = new PluginResolutionApplicator(target);

    List<Result> results =
        collect(
            requests,
            new Transformer<Result, PluginRequest>() {
              public Result transform(PluginRequest request) {
                return resolveToFoundResult(request);
              }
            });

    ImmutableListMultimap<Boolean, Result> categorizedResults =
        groupBy(
            results,
            new Transformer<Boolean, Result>() {
              public Boolean transform(Result original) {
                return original.legacyFound != null;
              }
            });

    final List<Result> legacy = categorizedResults.get(true);
    List<Result> nonLegacy = categorizedResults.get(false);

    // Could be different to ids in the requests as they may be unqualified
    final Map<Result, String> legacyActualPluginIds = Maps.newLinkedHashMap();
    if (!legacy.isEmpty()) {
      final RepositoryHandler repositories = scriptHandler.getRepositories();
      final List<MavenArtifactRepository> mavenRepos =
          repositories.withType(MavenArtifactRepository.class);

      for (final Result result : legacy) {
        result.legacyFound.action.execute(
            new LegacyPluginResolveContext() {
              public Dependency add(
                  String pluginId, final String m2RepoUrl, Object dependencyNotation) {
                legacyActualPluginIds.put(result, pluginId);

                boolean repoExists =
                    any(
                        mavenRepos,
                        new Spec<MavenArtifactRepository>() {
                          public boolean isSatisfiedBy(MavenArtifactRepository element) {
                            return element.getUrl().toString().equals(m2RepoUrl);
                          }
                        });
                if (!repoExists) {
                  repositories.maven(
                      new Action<MavenArtifactRepository>() {
                        public void execute(MavenArtifactRepository mavenArtifactRepository) {
                          mavenArtifactRepository.setUrl(m2RepoUrl);
                        }
                      });
                }

                return scriptHandler
                    .getDependencies()
                    .add(ScriptHandler.CLASSPATH_CONFIGURATION, dependencyNotation);
              }
            });
      }
    }

    Configuration classpathConfiguration =
        scriptHandler.getConfigurations().getByName(ScriptHandler.CLASSPATH_CONFIGURATION);
    Set<File> files = classpathConfiguration.getFiles();
    if (!files.isEmpty()) {
      ClassPath classPath = new DefaultClassPath(files);
      Factory<? extends ClassLoader> loader = classLoaderScope.getParent().loader(classPath);
      classLoaderScope.export(loader);
    }

    classLoaderScope.lock();

    // We're making an assumption here that the target's plugin registry is backed classLoaderScope.
    // Because we are only build.gradle files right now, this holds.
    // It won't for arbitrary scripts though.
    for (final Map.Entry<Result, String> entry : legacyActualPluginIds.entrySet()) {
      final PluginRequest request = entry.getKey().request;
      final String id = entry.getValue();
      applyPlugin(
          request,
          id,
          new Runnable() {
            public void run() {
              target.getPlugins().apply(id);
            }
          });
    }

    for (final Result result : nonLegacy) {
      applyPlugin(
          result.request,
          result.found.resolution.getPluginId().toString(),
          new Runnable() {
            public void run() {
              resolutionApplicator.execute(result.found.resolution);
            }
          });
    }
  }