Skip to content
Merged
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
28 changes: 23 additions & 5 deletions sbt-play-ebean/src/main/scala/play/ebean/sbt/PlayEbean.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import xsbti.compile.CompileResult
import xsbti.compile.analysis.Stamp
import sbt._
import xsbti.VirtualFileRef
import scala.util.control.NonFatal

object PlayEbean extends AutoPlugin {

Expand Down Expand Up @@ -64,6 +63,25 @@ object PlayEbean extends AutoPlugin {
// This is replacement of old Stamp `Exists` representation
private final val notPresent = "absent"

/**
* Turns the configured models into the packages the enhancer processes: packages (`models.*` or `models`) as they
* are, and single classes (`models.Task`) via their package. Entries without compiled classes (e.g. packages not
* existing yet, or provided by a dependency) are skipped, because the enhancer fails for them.
*/
private def packagesToEnhance(models: Seq[String], classes: File): Seq[String] = {
def isPackage(name: String) = new File(classes, name.replace('.', '/')).isDirectory
models
.map(_.trim)
.flatMap { model =>
// Like the enhancer, ignore a trailing ".*" or ".**"
val pkg = model.stripSuffix("*").stripSuffix("*").stripSuffix(".")
if (isPackage(pkg)) Some(pkg)
else if (pkg == model && model.contains('.')) Some(model.substring(0, model.lastIndexOf('.'))).filter(isPackage)
else None
}
.distinct
}

def ebeanEnhance: Def.Initialize[Task[CompileResult]] =
Def.task {

Expand All @@ -85,10 +103,10 @@ object PlayEbean extends AutoPlugin {
val transformer = new Transformer(classLoader, agentArgsString)
val fileTransform = new OfflineFileTransform(transformer, classLoader, classes.getAbsolutePath)

try {
fileTransform.process(playEbeanModels.value.mkString(","))
} catch {
case NonFatal(_) =>
val packages = packagesToEnhance(playEbeanModels.value, classes)
// An empty list would make the enhancer process all classes
if (packages.nonEmpty) {
fileTransform.process(packages.mkString(","))
}

} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package models;

import java.util.*;
import jakarta.persistence.*;

import io.ebean.*;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class Task extends Model {

public static final Finder<Long, Task> find = new Finder<>(Task.class);

@Id
public Long id;

@Constraints.Required
public String name;

public boolean done;

@Formats.DateTime(pattern = "dd/MM/yyyy")
public Date dueDate = new Date();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

lazy val root = project
.in(file("."))
.enablePlugins(PlayJava, PlayEbean)

scalaVersion := sys.props("scala.version")

Test / sourceDirectory := baseDirectory.value / "tests"

Test / scalaSource := baseDirectory.value / "tests"

Test / javaSource := baseDirectory.value / "tests"

resolvers ++= DefaultOptions.resolvers(snapshot = true)

libraryDependencies += "com.h2database" % "h2" % "2.5.252"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# A package without any compiled classes (listed first, so it must not stop the enhancement of the
# following entries) and a single model class instead of a package
ebean.default = [ "missing.*", "models.Task" ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

resolvers ++= DefaultOptions.resolvers(snapshot = true)
addSbtPlugin("com.typesafe.play" % "sbt-play-ebean" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> test
$ exists conf/evolutions/default/1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package models;

import org.junit.*;

import static org.junit.Assert.*;

import play.test.*;
import play.Application;

import static play.test.Helpers.*;

public class TestTask extends WithApplication {

protected Application provideApplication() {
return fakeApplication(inMemoryDatabase());
}

@Test
public void saveAndFind() {
Task task = new Task();
task.id = 10L;
task.name = "Hello";
task.done = false;
task.save();

Task saved = Task.find.byId(10L);
assertEquals("Hello", saved.name);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package models;

import java.util.*;
import jakarta.persistence.*;

import io.ebean.*;
import play.data.format.*;
import play.data.validation.*;

@Entity
public class Task extends Model {

public static final Finder<Long, Task> find = new Finder<>(Task.class);

@Id
public Long id;

@Constraints.Required
public String name;

public boolean done;

@Formats.DateTime(pattern = "dd/MM/yyyy")
public Date dueDate = new Date();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package other;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

/** An entity outside of the configured package, so it must not get enhanced. */
@Entity
public class Other {
@Id public Long id;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

lazy val root = project
.in(file("."))
.enablePlugins(PlayJava, PlayEbean)

scalaVersion := sys.props("scala.version")

Test / sourceDirectory := baseDirectory.value / "tests"

Test / scalaSource := baseDirectory.value / "tests"

Test / javaSource := baseDirectory.value / "tests"

resolvers ++= DefaultOptions.resolvers(snapshot = true)

libraryDependencies += "com.h2database" % "h2" % "2.5.252"

// A plain package name (without ".*"), which must not be taken for a class
Compile / playEbeanModels := Seq("models")
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>

resolvers ++= DefaultOptions.resolvers(snapshot = true)
addSbtPlugin("com.typesafe.play" % "sbt-play-ebean" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
*/

package models;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import io.ebean.bean.EntityBean;
import org.junit.Test;

public class EnhancementTest {

@Test
public void enhancesOnlyTheConfiguredPackage() {
assertTrue(EntityBean.class.isAssignableFrom(models.Task.class));
assertFalse(EntityBean.class.isAssignableFrom(other.Other.class));
}
}