Esempio n. 1
0
  private void addNode(ModelNodeInternal child, ModelRegistration registration) {
    ModelPath childPath = child.getPath();
    if (!getPath().isDirectChild(childPath)) {
      throw new IllegalArgumentException(
          String.format(
              "Element registration has a path (%s) which is not a child of this node (%s).",
              childPath, getPath()));
    }

    ModelNodeInternal currentChild = links.get(childPath.getName());
    if (currentChild != null) {
      if (!currentChild.isAtLeast(Created)) {
        throw new DuplicateModelException(
            String.format(
                "Cannot create '%s' using creation rule '%s' as the rule '%s' is already registered to create this model element.",
                childPath,
                describe(registration.getDescriptor()),
                describe(currentChild.getDescriptor())));
      }
      throw new DuplicateModelException(
          String.format(
              "Cannot create '%s' using creation rule '%s' as the rule '%s' has already been used to create this model element.",
              childPath,
              describe(registration.getDescriptor()),
              describe(currentChild.getDescriptor())));
    }
    if (!isMutable()) {
      throw new IllegalStateException(
          String.format(
              "Cannot create '%s' using creation rule '%s' as model element '%s' is no longer mutable.",
              childPath, describe(registration.getDescriptor()), getPath()));
    }
    links.put(child.getPath().getName(), child);
    modelRegistry.registerNode(child, registration.getActions());
  }
 private ModelReference<?> mapSubject(ModelReference<?> subject, ModelPath targetPath) {
   if (subject.getPath() == null) {
     return subject.inScope(targetPath);
   } else {
     return subject.withPath(targetPath.descendant(subject.getPath()));
   }
 }
 private void mapInputs(List<ModelReference<?>> inputs, ModelPath targetPath) {
   for (int i = 0; i < inputs.size(); i++) {
     ModelReference<?> input = inputs.get(i);
     if (input.getPath() != null) {
       inputs.set(i, input.withPath(targetPath.descendant(input.getPath())));
     } else {
       inputs.set(i, input.inScope(ModelPath.ROOT));
     }
   }
 }
Esempio n. 4
0
  public void apply(final Project project) {
    RepositoryHandler repositories = publicationServices.createRepositoryHandler();
    PublicationContainer publications =
        instantiator.newInstance(DefaultPublicationContainer.class, instantiator);

    // TODO Registering an extension should register it with the model registry as well
    final PublishingExtension extension =
        project
            .getExtensions()
            .create(
                PublishingExtension.NAME,
                DefaultPublishingExtension.class,
                repositories,
                publications);

    project.afterEvaluate(
        new Action<Project>() {
          public void execute(Project project) {
            for (Publication publication : extension.getPublications()) {
              PublicationInternal internalPublication = (PublicationInternal) publication;
              publicationRegistry.registerPublication(
                  project.getPath(),
                  new DefaultProjectPublication(internalPublication.getCoordinates()));
            }
          }
        });

    ModelPath extensionModelPath = ModelPath.path(PublishingExtension.NAME);

    modelRules.register(extensionModelPath.toString(), extension);

    modelRules.rule(
        new ModelRule() {
          public void triggerDeferredConfigurables(PublishingExtension publishingExtension) {
            project.getExtensions().getByType(DefaultPublishingExtension.class);
          }
        });

    Task publishLifecycleTask = project.getTasks().create(PUBLISH_LIFECYCLE_TASK_NAME);
    publishLifecycleTask.setDescription("Publishes all publications produced by this project.");
    publishLifecycleTask.setGroup(PUBLISH_TASK_GROUP);
  }
 public VisualStudioProjectConfiguration lookupProjectConfiguration(
     NativeBinarySpec nativeBinary) {
   // Looks in the correct project registry for this binary
   ProjectInternal componentProject = getComponentProject(nativeBinary);
   VisualStudioExtension visualStudioExtension =
       componentProject
           .getModelRegistry()
           .get(ModelPath.path("visualStudio"), ModelType.of(VisualStudioExtension.class));
   VisualStudioProjectRegistry projectRegistry =
       ((VisualStudioExtensionInternal) visualStudioExtension).getProjectRegistry();
   return projectRegistry.getProjectConfiguration(nativeBinary);
 }
  private void validateRuleMethod(
      MethodRuleDefinition<?, ?> ruleDefinition,
      Method ruleMethod,
      ValidationProblemCollector problems) {
    if (Modifier.isPrivate(ruleMethod.getModifiers())) {
      problems.add(ruleMethod, "A rule method cannot be private");
    }
    if (Modifier.isAbstract(ruleMethod.getModifiers())) {
      problems.add(ruleMethod, "A rule method cannot be abstract");
    }

    if (ruleMethod.getTypeParameters().length > 0) {
      problems.add(ruleMethod, "Cannot have type variables (i.e. cannot be a generic method)");
    }

    // TODO validations on method: synthetic, bridge methods, varargs, abstract, native
    ModelType<?> returnType = ModelType.returnType(ruleMethod);
    if (returnType.isRawClassOfParameterizedType()) {
      problems.add(
          ruleMethod,
          "Raw type "
              + returnType
              + " used for return type (all type parameters must be specified of parameterized type)");
    }

    for (int i = 0; i < ruleDefinition.getReferences().size(); i++) {
      ModelReference<?> reference = ruleDefinition.getReferences().get(i);
      if (reference.getType().isRawClassOfParameterizedType()) {
        problems.add(
            ruleMethod,
            "Raw type "
                + reference.getType()
                + " used for parameter "
                + (i + 1)
                + " (all type parameters must be specified of parameterized type)");
      }
      if (reference.getPath() != null) {
        try {
          ModelPath.validatePath(reference.getPath().toString());
        } catch (Exception e) {
          problems.add(
              ruleDefinition,
              "The declared model element path '"
                  + reference.getPath()
                  + "' used for parameter "
                  + (i + 1)
                  + " is not a valid path",
              e);
        }
      }
    }
  }
 /** Invoked by transformed DSL creation rules */
 public <T> void create(String modelPathString, Class<T> type, Closure<?> closure) {
   SourceLocation sourceLocation = ruleLocationExtractor.transform(closure);
   ModelPath modelPath = ModelPath.path(modelPathString);
   ModelRuleDescriptor descriptor = toDescriptor(sourceLocation, modelPath);
   try {
     NodeInitializerRegistry nodeInitializerRegistry =
         modelRegistry.realize(
             DefaultNodeInitializerRegistry.DEFAULT_REFERENCE.getPath(),
             DefaultNodeInitializerRegistry.DEFAULT_REFERENCE.getType());
     NodeInitializer nodeInitializer =
         nodeInitializerRegistry.getNodeInitializer(ModelType.of(type));
     modelRegistry.create(
         ModelCreators.of(modelPath, nodeInitializer).descriptor(descriptor).build());
   } catch (ModelTypeInitializationException e) {
     throw new InvalidModelRuleDeclarationException(descriptor, e);
   }
   registerAction(modelPath, type, descriptor, ModelActionRole.Initialize, closure);
 }
 /** Invoked by transformed DSL configuration rules */
 public void configure(String modelPathString, Closure<?> closure) {
   SourceLocation sourceLocation = ruleLocationExtractor.transform(closure);
   ModelPath modelPath = ModelPath.path(modelPathString);
   ModelRuleDescriptor descriptor = toDescriptor(sourceLocation, modelPath);
   registerAction(modelPath, Object.class, descriptor, ModelActionRole.Mutate, closure);
 }