-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Add a TwirlModule to compile Twirl templates #271
Conversation
@@ -17,7 +17,7 @@ object CacherTests extends TestSuite{ | |||
} | |||
object Middle extends Middle | |||
trait Middle extends Base{ | |||
def value = T{ super.value() + 2} | |||
override def value = T{ super.value() + 2} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intellij IDEA is complaining about this line (preventing me from running the tests) but Mill can compile the code just fine 😐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's fine in mill, because there is a compiler plugin that makes override
word optional, but only in build files
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation.
Do you know if there is a way to configure intellij IDEA in order to use this compiler plugin for this file ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically it should be possible, If I understand correctly, you need to include http://search.maven.org/#artifactdetails%7Ccom.lihaoyi%7Cmill-moduledefs_2.12%7C0.1.7%7Cjar in classpath and add "-P:auto-override-plugin". to scalac options.
@Mogztter I'll take a look at those |
@rockjam Ok. but I'm no longer sure that this issue can be reproduced with EDIT: I can't reproduce this issue when running the tests with @rockjam is it possible to do remote debugging ? I've tried |
I think the reflection is struggling with the parameter type Though, I'm unable to call the compile method with a primitive boolean using reflection, so |
Honestly, I didn't try. You can try |
@rockjam I've created a simple project where I'm using Java reflection to call the Twirl compiler and it's working fine: https://github.com/Mogztter/mill-twirl-compiler/blob/master/app/src/Main.scala If I'm using the same code inside the I've checked both classpath and I didn't see any difference. Do you reproduce this issue ? I must be doing something wrong but I can't see what... thanks again for your help 👍 |
@Mogztter I got one question, doesn't twirl comes with some kind of CLI, or main class? In this case, we could just run it as a subprocess |
Indeed it will make the integration easier but I don't think there is a CLI. I'm really clueless about this exception since I can't reproduce it outside Mill. |
I'll try code from your PR to figure out what's going on with it in next couple days |
9ebd19b
to
779e453
Compare
@rockjam Did you have a chance to reproduce the issue ? |
f277471
to
896ed8a
Compare
Rebased on master |
@Mogztter sorry, didn't have a chance to look at it yet. I'll try to take a look this weekend. Is it ok for you? |
Sure thanks for taking the time. |
} | ||
} | ||
|
||
trait TestTwirlModule extends TwirlModule with TestModule { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure that this trait would be useful
} | ||
} | ||
'compileTwirl - { | ||
'fromScratch - workspaceTest(HelloWorld) { eval => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fromScratch
adds one more level of nesting for single test. How about unwrapping it, and leaving 'compileTwirl -workspaceTest(HelloWorld) {...}
?
assert( | ||
result.classes.path == eval.outPath / 'core / 'compileTwirl / 'dest / 'html, | ||
outputFiles.nonEmpty, | ||
outputFiles.forall(expectedClassfiles.contains), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compileClassfiles
has only a single element. I guess we could check for a single file in tests too.
@Mogztter I couldn't reproduce the |
@rockjam Thanks for your review!
You were able to call the Scala API https://github.com/playframework/twirl/blob/881b41e58350320570e6d3786cca87db5d04ecb4/compiler/src/main/scala/play/twirl/compiler/TwirlCompiler.scala#L167 ?
Ok I will fix them 👍 |
@Mogztter I actually did succeed with scala API of twirl. Here is a commit that uses scala API, but passes default values(from reflected I also included scala parser-combinator library in classpath, since otherwise it failed. with |
@Mogztter and here is the second take that removes reflective calls to default methods. But I'm not sure that @lihaoyi will aprove such tricks with classloading, since we mix mill's classloader with classloader of twirl, and we may go into trouble when |
Oh I see... 🤔
So it's not just me 😌
Indeed, we should not mess with the classloader 😉
|
@Mogztter Well, to pass |
Sorry I've been busy lately, I will try to go back to this pull request next week. |
Sounds great. We could add a sample play app in our integration test suite after this PR gets merged |
That would be awesome 👍 We can use this template https://github.com/playframework/play-scala-seed.g8 ? |
I guess we could. Or we could port some real play application, built with mill. But let's do it in separate PR |
Ok 👌 I've integrated your commit and rebased on master so I think we are ready. One last question about the API, do you think we should keep the
Yes I guess we could. A popular Play application on GitHub is Lichess: https://github.com/ornicar/lila But it will be quite challenging to build it with Mill 😋 |
No need to squash, Github can squash during the merge through the UI whenever @rockjam is ready. Lichess seems like a bit ambitious, probably should start with something smaller like Hello World =P |
Do you mean twirl prefix in package names? If yes, lets keep it. I will take a look at this PR this weekend |
@rockjam No I meant the prefix in function names:
Also I noticed that
Thanks 👍 |
Keeping the `twirl` prefix seems like a reasonable thing to do; that will
avoids ambiguity when you have N different versions that a user may care
about (scalaversion, scalafmt version, scalajs version, ...)
…On Sat, May 12, 2018 at 2:54 PM, Guillaume Grossetie < ***@***.***> wrote:
@rockjam <https://github.com/rockjam> No I meant the prefix in function
names:
- twirlVersion -> version
- twirlSources -> sources
- twirlClasspath -> classpath
- compileTwirl -> compile
Also I noticed that mill dev.assembly does not contain TwirlModule, I
guess we need to add twirllib here https://github.com/lihaoyi/mill/blob/
032ae8b/build.sc#L294
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#271 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ATpwjL339odqusT_DKxKwrB87TMgoV_2ks5tx1okgaJpZM4TC4_z>
.
|
Ok should we rename
I agree. |
@Mogztter created separate issue for play routes compiler: #324
I don't have strong preference. |
sourceDirectory, | ||
generatedDirectory, | ||
formatterType, | ||
defaultAdditionalImportsMethod.invoke(additionalImports), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does it work, can you explain a little bit? Why do we invoke method with additionalImports
parameter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a test case.
The basic idea is to be able to override/define the default argument by invoking the method.
So instead of using Nil
the Twirl compiler can be configured with additional imports. But maybe that's not how to do it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rockjam Good catch! It's not working 😞
defaultAdditionalImportsMethod
is a method that does not take any argument and the call is wrong (ie. the first argument must be the object):
defaultAdditionalImportsMethod.invoke(null, additionalImports)
If I replace defaultAdditionalImportsMethod.invoke(null)
with additionalImports
then the reflection does not work anymore and the following exception is thrown:
java.lang.IllegalArgumentException: argument type mismatch
Back to square one I guess 😢
So it's working but we cannot define/override additionalImports
, constructorAnnotations
, codec
, or inclusiveDot
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, additionalImports
is an instance of scala.collection.immutable.$colon$colon
(maybe that's why the reflection is confused ?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep the defaults for these arguments for now, and merge this PR like that. And we can address modifying of these arguments in subsequent PR. How about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will revert my changes to invoke the method without argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rockjam Done! I've created (private) functions in TwirlModule
and add a comment so when we know how to override these arguments, everything will be ready.
@rockjam Thanks for creating the issue. |
@Mogztter travis fails for some reason. Can you rebase it on master to trigger CI? |
* Add tests * Update the code to comply with the new API * Use reflection to call TwirlCompiler.compile function
@rockjam Done! |
@Mogztter thank you |
Based on @pleira implementation
This is a work in progress:
java.lang.ClassNotFoundException: scala.util.parsing.input.Positional
TwirlCompiler.compile
function is invoked using Reflectioncompile
method with a primitive boolean using reflection: https://github.com/playframework/twirl/blob/f4ed321dee30c977b7f303ef1229169180beab45/compiler/src/main/java/play/japi/twirl/compiler/TwirlCompiler.java#L38For now the main issue is that an exception is thrown when
TwirlCompiler.compile
is invoked.Oddly tests are running fine in Intellij IDEA. In this case the reflection is calling
JavaMirrors.JavaVanillaMethodMirror.apply(args: Any*)
. But when I run the tests usingmill twirllib.test
, the reflection is callingJavaMirrors.JavaTransformingMethodMirror.apply(args: Any*)
.I've also tried to use Java reflection but without success:
Help, feedback or review will be greatly appreciated 😄