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

Post life cycle #244

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 13 additions & 0 deletions docs/modifiers.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,19 @@ val message = "hello world!"
```
````

In addition to the `process` method, the `PostModifier` trait also has several
life-cycle methods that signal when a `PostModifier` instance:
* Has started for the first time (`onStart`) when MDoc is launched;
* Just before compilation and processing occurs (`preProcess`) on each source
document file;
* Just after compilation and processing has finished (`postProcess`) on each
source document file;
* Has finished after processing the last source document file (`onExit`)
before MDoc terminates.
These methods can be used to initialize and deactivate resources required by
the `PostModifier` instances.


## StringModifier

A `StringModifier` is a custom modifier that processes the plain text contents
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mdoc.docs.EvilplotModifier
mdoc.docs.LifeCycleModifier
11 changes: 11 additions & 0 deletions mdoc-docs/src/main/scala/mdoc/docs/EvilplotModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package mdoc.docs
import com.cibo.evilplot.geometry.Drawable
import java.nio.file.Files
import java.nio.file.Paths

import mdoc._
import mdoc.internal.cli.{Exit, Settings}
Copy link
Member

Choose a reason for hiding this comment

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

Settings can’t be exposed in the public api since it’s internal. We could add a public interface on the class if that helps

Copy link
Author

Choose a reason for hiding this comment

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

@olafurpg Apologies for closing this but the CI/CD failed. In particular one tests assumes that the files are processed in a given order. Need to correct this.

Settings are not required so I will remove it.

Copy link
Member

Choose a reason for hiding this comment

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

You can add MainSettings as an argument

https://github.com/scalameta/mdoc/blob/cf4aa2872e44a1abef1badbfe0544bf39d1a554c/mdoc/src/main/scala/mdoc/MainSettings.scala

We can add public methods down the road to access fields on the private Settings

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively, we can add a abstract class Settings in the public mdoc API and pass that in even if it has no public members for now. It's desirable to pass in one parameter just so we can pass in more information in the future without breaking the public API

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand the use of MainSettings. Settings does not extend this class. Note that I call onStart method in MainOps. Can you explain?

Copy link
Member

Choose a reason for hiding this comment

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

Settings is a case class which gets auto generated methods like “copy” that break binary compatibility on removal/addition of fields. This makes case classes essentially unsuitable for public APIs unless you’re certain you will never need to add/remove a field.

MainSettings is just a thin wrapper around the case class that has a stable api that we can evolve in a binary compatible way.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the explanation. MainSettingsis now being used. I have tried to make as few changes as possible. They are basically limited to MainOps. Not too happy with it because I would have liked to make the calls within the MainOps.runmethod. However this would entail changing the parameters of the constructor or run (Context is used in many places and my not be the best change).

Please see #245


import scala.meta.inputs.Position

class EvilplotModifier extends PostModifier {
Expand Down Expand Up @@ -36,4 +39,12 @@ class EvilplotModifier extends PostModifier {
""
}
}

override def onStart(settings: Settings): Unit = ()

override def preProcess(ctx: PostModifierContext): Unit = ()

override def postProcess(ctx: PostModifierContext): Unit = ()

override def onExit(exit: Exit): Unit = ()
}
50 changes: 50 additions & 0 deletions mdoc-docs/src/main/scala/mdoc/docs/LifeCycleModifier.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package mdoc.docs

import mdoc._
import mdoc.internal.cli.{Exit, Settings}

/**
* Global counter used to test the [[mdoc.Main]] process counting.
*/
object LifeCycleCounter {
val numberOfStarts: ThreadLocal[Integer] = ThreadLocal.withInitial( () => 0 )
val numberOfExists: ThreadLocal[Integer] = ThreadLocal.withInitial( () => 0 )
}

class LifeCycleModifier extends PostModifier {
val name = "lifecycle"

// Starts and stops per instance
var numberOfStarts = 0
var numberOfExists = 0
// Pre and post processing per instance
var numberOfPreProcess = 0
var numberOfPostProcess = 0

def process(ctx: PostModifierContext): String = {
// Used for checking the counting
s"numberOfStarts = $numberOfStarts ; numberOfExists = $numberOfExists ; numberOfPreProcess = $numberOfPreProcess ; numberOfPostProcess = $numberOfPostProcess"
}

/**
* This is called once when the [[mdoc.Main]] process starts
* @param settings CLI or API settings used by mdoc
*/
override def onStart(settings: Settings): Unit = {
numberOfStarts += 1
LifeCycleCounter.numberOfStarts.set(LifeCycleCounter.numberOfStarts.get() + 1)
}

override def preProcess(ctx: PostModifierContext): Unit = { numberOfPreProcess += 1}

override def postProcess(ctx: PostModifierContext): Unit = { numberOfPostProcess += 1 }

/**
* This is called once when the [[mdoc.Main]] process finsihes
* @param exit is the exit code returned by mdoc's processing
*/
override def onExit(exit: Exit): Unit = {
numberOfExists += 1
LifeCycleCounter.numberOfExists.set(LifeCycleCounter.numberOfExists.get() + 1)
}
}
8 changes: 7 additions & 1 deletion mdoc/src/main/scala/mdoc/PostModifier.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package mdoc

import java.util.ServiceLoader
import mdoc.internal.cli.Settings

import mdoc.internal.cli.{Exit, Settings}
import metaconfig.ConfDecoder
import metaconfig.ConfEncoder
import metaconfig.ConfError
import metaconfig.generic.Surface

import scala.meta.inputs.Input
import scala.meta.io.AbsolutePath
import scala.collection.JavaConverters._
import scala.meta.io.RelativePath

trait PostModifier {
val name: String
def onStart(settings: Settings): Unit
Copy link
Member

Choose a reason for hiding this comment

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

These need to have default implementations for backwards compatibility

Copy link
Author

Choose a reason for hiding this comment

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

Ok. Will do. Will a simple return of () do?

Apologies for the newbie questions.

I as planning to make another pull request. Should I or do I keep working here? If I do open a new one, their seem to be a "review pull " option. Should I use that instead?

Copy link
Member

Choose a reason for hiding this comment

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

: Unit = () is a fine default implementation

Copy link
Author

Choose a reason for hiding this comment

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

Done

def preProcess(ctx: PostModifierContext): Unit
def process(ctx: PostModifierContext): String
def postProcess(ctx: PostModifierContext): Unit
def onExit(exit: Exit): Unit
}

object PostModifier {
Expand Down
2 changes: 2 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/cli/MainOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,15 @@ final class MainOps(
if (settings.watch) {
startLivereload()
}
context.settings.postModifiers.foreach(_.onStart(settings))
val isOk = generateCompleteSite()
if (settings.isFileWatching) {
waitingForFileChanges()
runFileWatcher()
// exit code doesn't matter when file watching
Exit.success
} else {
context.settings.postModifiers.foreach(_.onExit(isOk))
isOk
}
}
Expand Down
2 changes: 2 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/markdown/Processor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ class Processor(implicit ctx: Context) {
inputFile,
ctx.settings
)
modifier.preProcess(postCtx)
val postRender = modifier.process(postCtx)
modifier.postProcess(postCtx)
replaceNodeWithText(doc, block, postRender)
case m: Modifier.Builtin =>
if (m.isPassthrough) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
tests.markdown.EvilplotPostModifier
tests.markdown.BulletPostModifier
tests.markdown.LifeCycleModifier

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ import mdoc.PostModifierContext
class EvilplotPostModifier extends PostModifier {
val name = "evilplot"
def process(ctx: PostModifierContext): String = ""

override def onStart(ctx: PostModifierContext): Unit = ()

override def preProcess(ctx: PostModifierContext): Unit = ()

override def postProcess(ctx: PostModifierContext): Unit = ()

override def onExit(ctx: PostModifierContext): Unit = ()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package tests.markdown

import com.cibo.evilplot.geometry.Drawable
import java.nio.file.Files

import mdoc.PostModifier
import mdoc.PostModifierContext
import mdoc.internal.cli.{Exit, Settings}

class EvilplotPostModifier extends PostModifier {
val name = "evilplot"
Expand All @@ -28,4 +30,13 @@ class EvilplotPostModifier extends PostModifier {
""
}
}

override def onStart(settings: Settings): Unit = ()

override def preProcess(ctx: PostModifierContext): Unit = ()

override def postProcess(ctx: PostModifierContext): Unit = ()

override def onExit(exit: Exit): Unit = ()

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,26 @@ class PostModifierSuite extends BaseMarkdownSuite {
"error: expected int runtime value. Obtained message"
)

check(
"lifecycle-1",
"""
|```scala mdoc:lifecycle
|val x = "message"
|```
""".stripMargin,
"numberOfStarts = 0 ; numberOfExists = 0 ; numberOfPreProcess = 1 ; numberOfPostProcess = 0"
)

// Process counts are per PostModifier instance, starts and exists per mdoc.Main process
// Because each test runs its own mdoc.Main process, the process counts are the same
check(
"lifecycle-2",
"""
|```scala mdoc:lifecycle
|val x = "message"
|```
""".stripMargin,
"numberOfStarts = 0 ; numberOfExists = 0 ; numberOfPreProcess = 1 ; numberOfPostProcess = 0"
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ package tests.markdown

import mdoc.PostModifier
import mdoc.PostModifierContext
import mdoc.internal.cli.{Exit, Settings}

class EvilplotPostModifier extends PostModifier {
val name = "evilplot"
def process(ctx: PostModifierContext): String = ""

override def onStart(settings: Settings): Unit = ()

override def preProcess(ctx: PostModifierContext): Unit = ()

override def postProcess(ctx: PostModifierContext): Unit = ()

override def onExit(exit: Exit): Unit = ()
}
46 changes: 46 additions & 0 deletions tests/unit/src/test/scala/tests/cli/CliSuite.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tests.cli

import java.nio.file.Files

import mdoc.internal.BuildInfo
import tests.markdown.{LifeCycleCounter, LifeCycleModifier}

class CliSuite extends BaseCliSuite {

Expand Down Expand Up @@ -191,4 +193,48 @@ class CliSuite extends BaseCliSuite {
|""".stripMargin
)

checkCli(
"lifeCycle",
"""
|/file1.md
|# file 1
|One
|```scala mdoc:lifecycle
|val x1 = 1
|```
|/file2.md
|# file 2
|Two
|```scala mdoc:lifecycle
|val x2 = 2
|```
| """.stripMargin,
"""
|/file1.md
|# file 1
|One
|numberOfStarts = 1 ; numberOfExists = 0 ; numberOfPreProcess = 1 ; numberOfPostProcess = 0
|/file2.md
|# file 2
|Two
|numberOfStarts = 1 ; numberOfExists = 0 ; numberOfPreProcess = 2 ; numberOfPostProcess = 1
""".stripMargin, // process counts per PostModifier instance, starts and exists per mdoc.Main process
setup = { fixture =>
// Global thread local counter updated by all mdoc.Main process
// All tests in this test suite run sequentially but change the counter
// So make sure we start anew for this test
LifeCycleCounter.numberOfStarts.set(0)
LifeCycleCounter.numberOfExists.set(0)
},
onStdout = { out =>
assert(out.contains("Compiling 2 files to"))
assert(out.contains("Compiled in"))
assert(out.contains("(0 errors)"))
// Should start and stop one only once in this test (several times for test-suite)
assert( LifeCycleCounter.numberOfExists.get() == 1)
assert( LifeCycleCounter.numberOfStarts.get() == 1)
}
)


}
10 changes: 10 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/BulletPostModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tests.markdown

import mdoc.PostModifier
import mdoc.PostModifierContext
import mdoc.internal.cli.{Exit, Settings}

class BulletPostModifier extends PostModifier {
val name = "bullet"
Expand All @@ -14,4 +15,13 @@ class BulletPostModifier extends PostModifier {
""
}
}

override def onStart(settings: Settings): Unit = ()

override def preProcess(ctx: PostModifierContext): Unit = ()

override def postProcess(ctx: PostModifierContext): Unit = ()

override def onExit(exit: Exit): Unit = ()

}
50 changes: 50 additions & 0 deletions tests/unit/src/test/scala/tests/markdown/LifeCycleModifier.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tests.markdown

import mdoc._
import mdoc.internal.cli.{Exit, Settings}

/**
* Global counter used to test the [[mdoc.Main]] process counting.
*/
object LifeCycleCounter {
val numberOfStarts: ThreadLocal[Integer] = ThreadLocal.withInitial( () => 0 )
val numberOfExists: ThreadLocal[Integer] = ThreadLocal.withInitial( () => 0 )
}

class LifeCycleModifier extends PostModifier {
val name = "lifecycle"

// Starts and stops per instance
var numberOfStarts = 0
var numberOfExists = 0
// Pre and post processing per instance
var numberOfPreProcess = 0
var numberOfPostProcess = 0

def process(ctx: PostModifierContext): String = {
// Used for checking the counting
s"numberOfStarts = $numberOfStarts ; numberOfExists = $numberOfExists ; numberOfPreProcess = $numberOfPreProcess ; numberOfPostProcess = $numberOfPostProcess"
}

/**
* This is called once when the [[mdoc.Main]] process starts
* @param settings CLI or API settings used by mdoc
*/
override def onStart(settings: Settings): Unit = {
numberOfStarts += 1
LifeCycleCounter.numberOfStarts.set(LifeCycleCounter.numberOfStarts.get() + 1)
}

override def preProcess(ctx: PostModifierContext): Unit = { numberOfPreProcess += 1}

override def postProcess(ctx: PostModifierContext): Unit = { numberOfPostProcess += 1 }

/**
* This is called once when the [[mdoc.Main]] process finsihes
* @param exit is the exit code returned by mdoc's processing
*/
override def onExit(exit: Exit): Unit = {
numberOfExists += 1
LifeCycleCounter.numberOfExists.set(LifeCycleCounter.numberOfExists.get() + 1)
}
}