Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize package name #17

Merged
merged 11 commits into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: scala
scala:
- 2.11.11
- 2.12.2
- 2.12.6

# These directories are cached to S3 at the end of the build
cache:
Expand Down
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Prerequisites:

Open a console and run the following command to apply this template:
```
sbt -Dsbt.version=0.13.15 new akka/akka-quickstart-scala.g8
sbt -Dsbt.version=0.13.17 new akka/akka-quickstart-scala.g8
```

This template will prompt for the name of the project. Press `Enter` if the default values suit you.
Expand All @@ -26,16 +26,6 @@ Once inside the project folder, run the application with:
sbt run
```

This template also provides build descriptors for maven and gradle. You can use any of the following commands to run
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm good catch

the application:
```
mvn exec:exec
```
or
```
gradle run
```

## Template license

Written in 2017 by Lightbend, Inc.
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ lazy val root = (project in file("."))
test in Test := {
val _ = (g8Test in Test).toTask("").value
},
scriptedLaunchOpts ++= List("-Xms1024m", "-Xmx1024m", "-XX:ReservedCodeCacheSize=128m", "-XX:MaxPermSize=256m", "-Xss2m", "-Dfile.encoding=UTF-8"),
scriptedLaunchOpts ++= List("-Xms1024m", "-Xmx1024m", "-XX:ReservedCodeCacheSize=128m", "-XX:MaxMetaspaceSize=256m", "-Xss2m", "-Dfile.encoding=UTF-8"),
resolvers += Resolver.url("typesafe", url("http://repo.typesafe.com/typesafe/ivy-releases/"))(Resolver.ivyStylePatterns)
)

Expand Down
2 changes: 1 addition & 1 deletion docs/build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Uses the out of the box generic theme.
paradoxTheme := Some(builtinParadoxTheme("generic"))

scalaVersion := "2.12.2"
scalaVersion := "2.12.6"

paradoxProperties in Compile ++= Map(
"snip.g8root.base_dir" -> "../../../../src/main/g8",
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/paradox/communicate-with-actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ You might be wondering what the Actor is doing when it is not processing message

To put a message into an Actor's mailbox, use the `!` (bang) method on the `ActorRef`. For example, the main class of Hello World sends messages to the `Greeter` Actor like this:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #main-send-messages }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #main-send-messages }

The `Greeter` Actor also sends a message to the `Printer` Actor:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #greeter-send-message }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #greeter-send-message }

We've looked at how to create actors and send messages. Now, let's review by looking at the `Main` class in its entirety.
2 changes: 1 addition & 1 deletion docs/src/main/paradox/create-actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Actor and `ActorSystem` names are important in Akka. For example, you use them f

The previous topic reviewed the definition of Hello World Actors. Let's look at the code in the `AkkaQuickstart.scala` file that creates `Greeter` and `Printer` Actor instances:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #create-actors }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #create-actors }

Notice the following:

Expand Down
12 changes: 6 additions & 6 deletions docs/src/main/paradox/define-actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ Lets see how the companion objects and Actor implementations for `Greeter` and `

The following code snippet in the `AkkaQuickstart.scala` file for the companion object defines the messages handled by the `Greeter` Actor:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #greeter-messages }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #greeter-messages }

The following snippet defines the `props` method.

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #greeter-companion }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #greeter-companion }

The `props` method creates and returns a `Props` instance. `Props` is a configuration class to specify options for the creation of actors, think of it as an immutable and thus freely shareable recipe for creating an actor that can include associated deployment information. This example simply passes the parameters that the Actor requires when being constructed. We will see the `props` method in action later in this tutorial.

#### The Greeter Actor implementation

The following snippet from the `AkkaQuickstart.scala` implements the `Greeter` Actor:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #greeter-actor }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #greeter-actor }

Let's break down the functionality:

Expand All @@ -49,11 +49,11 @@ Let's break down the functionality:

Similar to 'Greeter', the companion object defines the messages handled by the `Printer` Actor:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #printer-messages }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #printer-messages }

And, the companion object of `Printer` defines a `props` method and the message that the Actor expects:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #printer-companion }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #printer-companion }

### Printer Actor implementation

Expand All @@ -62,4 +62,4 @@ The `Printer` implementation is very simple:
* It extends `akka.actor.ActorLogging` to automatically get a reference to a logger. By doing this we can write `log.info()` in the Actor without any additional importing or wiring.
* It just handles one type of message, `Greeting`, and logs the content of that message.

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #printer-actor }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #printer-actor }
2 changes: 1 addition & 1 deletion docs/src/main/paradox/full-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

Take a look at the definition of Actors, their companion objects, and Messages:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala)
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala)

As another best practice we should provide some test coverage.
6 changes: 3 additions & 3 deletions docs/src/main/paradox/intellij-idea.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Fill out the settings according to the above and press `OK` to import the projec

### Inspecting project code

If we open up the file `src/main/scala/com/lightbend/akka/sample/AkkaQuickstart.scala` we will see a lot of lines beginning with `//# ...`. These lines are used as directives for this documentation. To get rid of these lines from the source code we can utilize the awesome Find/Replace functionality in IntelliJ. Select `Edit -> Find -> Replace in Path...`. Check the `Regex` box and add the following regex `[//#].*` and click on `Replace in Find Window...`. Select to replace all occurrences and voila the lines are gone!
If we open up the file `src/main/scala/$package$/AkkaQuickstart.scala` we will see a lot of lines beginning with `//# ...`. These lines are used as directives for this documentation. To get rid of these lines from the source code we can utilize the awesome Find/Replace functionality in IntelliJ. Select `Edit -> Find -> Replace in Path...`. Check the `Regex` box and add the following regex `[//#].*` and click on `Replace in Find Window...`. Select to replace all occurrences and voila the lines are gone!

### Testing and running

For testing we simply right click on the file `src/test/scala/com/lightbend/akka/sample/AkkaQuickstartSpec.scala` and select `Run 'AkkaQuickstartSpec'`:
For testing we simply right click on the file `src/test/scala/$package$/AkkaQuickstartSpec.scala` and select `Run 'AkkaQuickstartSpec'`:

![IntelliJ](images/intellij-test-output.png)

Similarly to run the application we right click on the file `src/main/scala/com/lightbend/akka/sample/AkkaQuickstart.scala` and select `Run 'AkkaQuickstart'`:
Similarly to run the application we right click on the file `src/main/scala/$package$/AkkaQuickstart.scala` and select `Run 'AkkaQuickstart'`:

![IntelliJ](images/intellij-run-output.png)

Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/paradox/main-class.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The `Main` class in Hello World creates and controls the actors. Notice the use of an `ActorSystem` as a container and the `actorOf` method to create the Actors. Finally, the class creates the messages to send to the Actors.

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #main-class }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #main-class }

Similarly, let's look again at the full source code that defines the Actors and the Messages they accept.

2 changes: 1 addition & 1 deletion docs/src/main/paradox/running-the-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Remember that the test implementation set the `Printer` Actor to use Akka logger

This is the result of our code that sends messages to the `Greeter` Actor:

@@snip [AkkaQuickstart.scala]($g8src$/scala/com/lightbend/akka/sample/AkkaQuickstart.scala) { #main-send-messages }
@@snip [AkkaQuickstart.scala]($g8src$/scala/$package$/AkkaQuickstart.scala) { #main-send-messages }

To run the tests, enter `test` at the sbt prompt.

Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/paradox/testing-actors.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ The tests in the Hello World example illustrates use of the [ScalaTest](http://w

Let's start by looking at the test class definition in the `AkkaQuickstartSpec.scala` source file:

@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/com/lightbend/akka/sample/AkkaQuickstartSpec.scala) { #test-classes }
@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/$package$/AkkaQuickstartSpec.scala) { #test-classes }

The test class extends `akka.test.TestKit`, which is a module for integration testing of actors and actor systems. This class only uses a fraction of the functionality provided by [TestKit](http://doc.akka.io/docs/akka/current/scala/testing.html). The other extended classes are from `ScalaTest`. They provide syntax and functionality for the Hello World test cases. For example, by extending `FlatSpecLike`, we can write test specifications in the format of "X should Y", like this for example:

@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/com/lightbend/akka/sample/AkkaQuickstartSpec.scala) { #specification-example }
@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/$package$/AkkaQuickstartSpec.scala) { #specification-example }

### Test methods

Now that we know some of the basic concepts, let's look at the implemented test methods.

Integration testing can help us ensure that Actors are behaving asynchronously. This first test uses `TestProbe` to interrogate and verify the expected behavior. Let's look at a source code snippet:

@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/com/lightbend/akka/sample/AkkaQuickstartSpec.scala) { #first-test }
@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/$package$/AkkaQuickstartSpec.scala) { #first-test }

Once we have a reference to `TestProbe` we pass it to `Greeter` as part of the constructor arguments. We thereafter send two messages to `Greeter`; one to set the greeting person to greet and another to trigger the sending of a `Greeting`. The `expectMsg` method on the `TestProbe` verifies whether the message got sent.

### Full test code

And, here is the complete code:

@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/com/lightbend/akka/sample/AkkaQuickstartSpec.scala) { #full-example }
@@snip [AkkaQuickstartSpec.scala]($g8srctest$/scala/$package$/AkkaQuickstartSpec.scala) { #full-example }

The example code just scratches the surface of the functionality available in `TestKit`. A complete overview can be found [here](http://doc.akka.io/docs/akka/current/scala/testing.html).

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.15
sbt.version=0.13.17
2 changes: 0 additions & 2 deletions project/default.properties

This file was deleted.

2 changes: 1 addition & 1 deletion project/giter8.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("org.foundweekends.giter8" %% "sbt-giter8" % "0.7.2")
addSbtPlugin("org.foundweekends.giter8" %% "sbt-giter8" % "0.10.0")
2 changes: 1 addition & 1 deletion project/paradox.sbt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// sbt-paradox, used for documentation
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.2.11")
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.3.2")
6 changes: 3 additions & 3 deletions src/main/g8/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name := "akka-quickstart-scala"

version := "1.0"

scalaVersion := "2.12.2"
scalaVersion := "2.12.6"

lazy val akkaVersion = "2.5.12"
lazy val akkaVersion = "$akka_version$"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-testkit" % akkaVersion,
"org.scalatest" %% "scalatest" % "3.0.1" % "test"
"org.scalatest" %% "scalatest" % "3.0.5" % "test"
)
3 changes: 2 additions & 1 deletion src/main/g8/default.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
name = akka-quickstart-scala
description = Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven apps. This simple application will get you started building Actor based systems with Scala. This app uses Akka, Scala, and ScalaTest.
verbatim = *.scala
akka_version=2.5.12
package=com.example
2 changes: 1 addition & 1 deletion src/main/g8/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.15
sbt.version=0.13.17
2 changes: 1 addition & 1 deletion src/main/g8/project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("io.spray" % "sbt-revolver" % "0.8.0")
addSbtPlugin("io.spray" % "sbt-revolver" % "0.9.1")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//#full-example
package com.lightbend.akka.sample
package $package$

import akka.actor.{ Actor, ActorLogging, ActorRef, ActorSystem, Props }

Expand All @@ -24,7 +24,7 @@ class Greeter(message: String, printerActor: ActorRef) extends Actor {

def receive = {
case WhoToGreet(who) =>
greeting = s"$message, $who"
greeting = message + ", " + who
case Greet =>
//#greeter-send-message
printerActor ! Greeting(greeting)
Expand All @@ -50,7 +50,7 @@ class Printer extends Actor with ActorLogging {

def receive = {
case Greeting(greeting) =>
log.info(s"Greeting received (from ${sender()}): $greeting")
log.info("Greeting received (from " + sender() + "): " + greeting)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a shame, wouldn't there be some other way to get rid of the Fix mismatched input '$' expecting LPAREN` warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know we can use backslash \ to handle this,

log.info(s"Greeting received (from \${sender()}): \$greeting")

but paradox didn't handle this. It will appear those escape characters at Lightbend Tech Hub. That's why I chose this way. If there is another way to handle this, please let me know. Thanks! :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so it's an interaction between paradox and g8: since we removed verbatim = *.scala (to make the variable substitution for the package name work) it now also tries to interpret the $ signs in the rest of the code, but paradox isn't prepared for that. This is fine for now then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(added #20 so we can revisit this later)

}
}
//#printer-actor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//#full-example
package com.lightbend.akka.sample
package $package$

import org.scalatest.{ BeforeAndAfterAll, FlatSpecLike, Matchers }
import akka.actor.{ Actor, Props, ActorSystem }
import akka.testkit.{ ImplicitSender, TestKit, TestActorRef, TestProbe }
import akka.actor.ActorSystem
import akka.testkit.{ TestKit, TestProbe }
import scala.concurrent.duration._
import scala.language.postfixOps
import Greeter._
import Printer._

Expand Down Expand Up @@ -32,7 +33,7 @@ class AkkaQuickstartSpec(_system: ActorSystem)
val greetPerson = "Akka"
helloGreeter ! WhoToGreet(greetPerson)
helloGreeter ! Greet
testProbe.expectMsg(500 millis, Greeting(s"$helloGreetingMessage, $greetPerson"))
testProbe.expectMsg(500 millis, Greeting(helloGreetingMessage + ", " + greetPerson))
}
//#first-test
}
Expand Down