How to get groovy compiler time (phase) source code - groovy

Groovy console allows me to get the Groovy AST browser. In this, I can select the compiler "end of phase" option. Then, I can see the source and bytecode of the source code. My goal is to automate this process based on compiler phases and to get the "source" section as illustrated in the console output figure below. More particularly, I don't want to use the Groovy console every time. Is there a way to do this?
For example, the code below is shown in source section of the AST browser, starting with public class Dog extends...
class Dog {
static void main(String[] args) {
println "hello"
}
}
class Cat {

"My goal is to automate this process..." What process? Unless you rewrite the "Groovy AST Browser" yourself, I think you have to use the Groovy Console to get to it. However, if what you want to do is automate access to the AST Tree so you can, for example, intercept method calls or inject methods, then there is a way to do that. Check out http://groovy-lang.org/metaprogramming.html section 2 - compile time meta-programming

Related

Resharper run configurations

I'm trying to make a reshaprer plugin to add one (or more) configurations, besides executable, static method, project, at resharper's build/run window.
Any guidelines where to start? Or how to access build's context and configure?
Currently examining the JetBrains.IDE.RunConfig, SolutionBuilders etc but one help would be appreciated.
Should this plugin be a SolutionComponent or a SolutionInstanceComponent?
Resharper's sdk help lucks documentation on build/run component.
Thank in advance!
You can extend the available run configuration types by implementing IRunConfig and IRunConfigProvider.
The IRunConfigProvider class needs to be marked as [ShellComponent], and can derive from the RunConfigProviderBase abstract base class. You get to specify a name, e.g. "Executable", a type identifier, e.g. "exe" and an icon ID. There's also the CreateNew method, which will create a new instance of your IRunConfig class, which will be mostly unconfigured, at this point.
The IRunConfig interface doesn't need to marked as a component, and should also derive from RunConfigBase - take a look at RunConfigExe in dotPeek to see an example of how to implement. You should override Execute in order to actually run whatever it is you need to run. You can use the RunConfigContext class passed in to actually execute a process from a ProcessStartInfo, or an IProject - this will execute it either by running the process, debugging it, or something else, such as code coverage or profiling.
For an .exe, this is as simple as:
public override void Execute(RunConfigContext context)
{
context.ExecutionProvider.Execute(GetStartInfo(context), context, this);
}
But for a more complicated example, look at RunConfigMethod.Execute, which uses its own standalone launcher executable, and passes in command line parameters to load the correct assembly and execute the given static method.
Settings are implemented with ReadSpecific/SaveSpecific, and you can provide an editor view model with CreateEditor. You'll need a settings class, something like:
[SettingsKey(typeof (ConfigSettings), ".exe config")]
public class ExeSettings
{
[SettingsEntry(null, "Path to .exe")] public string Executable;
[SettingsEntry(null, "Working directory")] public string WorkingDirectory;
[SettingsEntry(null, "Command line arguments")] public string Arguments;
}
The view for the editor is provided by a WPF control that is displayed in a dialog that ReSharper controls. The view needs to be decorated with the [View] attribute and must implement IView<T> where T is the concrete class returned from CreateEditor. This is how ReSharper will locate the view for the view model returned by CreateEditor. Again, take a look at RunConfigMethodView in dotPeek for some more idea of what's going on (and if you look in the resources, you'll be able to see the XAML itself).

How to commit text, annotations etc. to/from groovy-script from/to other GATE-plugin?

I want to create GATE-pipeline like this:
... -> Plugin no.1 -> Groovy-Script -> Plugin no.2 -> ...
As a GATE beginner, I don't know how I can receive the document-text and its annotations from plugin no.1 to read it into my groovy-script. Then I want to edit the given document-text and/or set some more annotations with my groovy script - how can I commit this to the next plugin in the pipeline?
Edit: OK, now I see the question above isn't my problem.
My script starts like this:
public class MainApp {
public static void main(String[] args) throws IOException {
Gate.init();
System.out.println(doc.getContent());
}
}
But when I try to load the script into GATE, I get the "Script compilation failed"-error. I don't get it, because this script
public class MainApp {
public static void main(String[] args) throws IOException {
System.out.println("hello");
}
}
and this script
Gate.init();
System.out.println(doc.getContent());
both work.
(I didn't tested the last one until now, that's why I thought I do a wrong call)
As explained in the Groovy Script PR documentation, there are a number of pre-defined variables available within a script that is run by the script PR:
doc is the Document currently being processed
inputAS is the AnnotationSet from that document corresponding to the inputASName runtime parameter
outputAS is the AnnotationSet from that document corresponding to the outputASName runtime parameter
You can read the document content via doc.getContent() and modify it using doc.edit, read annotations from previous PRs from the inputAS and create annotations for subsequent PRs in the outputAS.
Edit: I think you're mis-understanding what the Script PR expects - you should not add a class body, just a script, i.e. the script file should contain just the code that would be inside a method body without the surrounding class and method declarations. And you should definitely not call Gate.init() in the script - your script will be called by GATE, once per document. The single line:
println doc.getContent()
on its own would be a valid script for the PR, and would display the text content of each document in the Messages pane.

Access tied files through groovy in jenkins

I'm building a Jenkins plugin, and am handling the UI components using Groovy. In jelly, you can use "${it.something}" to access information in the java file tied to the jelly file, as shown here:
class:
public String getMyString() {
return "Hello Jenkins!";
}
jelly:
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout"
xmlns:t="/lib/hudson" xmlns:f="/lib/form">
${it.myString}
</j:jelly>
from https://wiki.jenkins-ci.org/display/JENKINS/Basic+guide+to+Jelly+usage+in+Jenkins.
I'd like to do the same thing in groovy, but can't seem to find an example of how it's done. Any examples?
After even more searching and some luck, I found the correct way to do this. If I was to use the class in my question but wanted to use groovy instead of jelly, the groovy code would look like this (this puts the string in the textbox):
package something.something;
import lib.JenkinsTagLib
import lib.FormTagLib
def f = namespace(lib.FormTagLib)
t=namespace(JenkinsTagLib.class)
f.entry(title:"text", field:"text") {
f.textbox(value:instance?.text)
}

Populating variables in a groovy script from a java program

I know how to call Groovy code from Java code, but can you, while running Groovy script script.groovy, ask a Java program to populate its varabies?
// script.groovy
import static org.mydomain.WiseJavaProgram.*;
pleasePopulateMeWithVariables();
println numberOfLegs // 2
// We didn't declare numberOfLegs in our sript, the Java program set it for us.
I guess maybe for that script.groovy should pass its environment to the pleasePopulateMeWithVariables method, but I haven't found how to do that. Is this even possible?
The case where I want to use such behavior: I have script.groovy, and also an undefined number of groovy scripts of which Java program knows, but of which script.groovy doesn't know, and I want to add what is declared in them to script.groovy. Basically those additional scripts are data, for example descriptions of various products and their features. This is easy if you run a Java program that creates GroovyShell and evaluates in it data-scripts and then script.groovy, but for me it is necessary that we initially call script.groovy, not the Java program.
Just as usual, while asking the question I was one step behind the answer -__-
The "environment" I was searching for is the Script object itself, available from script.groovy simply as this. So:
// script.groovy
import static org.mydomain.WiseJavaProgram.*;
populateWithVariables(this);
println numberOfLegs // 2
// WiseJavaProgram.java
public class WiseJavaProgram {
public static void populateWithVariables(Script script) {
script.evaluate('numberOfLegs = 2');
script.evaluate(new File("another.groovy"));
script.evaluate(new File("yetAnother.groovy"));
}
}

Calling groovy script from other groovy script

I want to call a groovy script from the other groovy script... anybody can help me in this ASAP..
example :
Class A having some code and it should call from B
class A{
static main(args){
println "Hello.. calling A Class"
}
}
I want to create a new class like B.groovy
class B{
static main(args){
// I need code for this to call A.groovy
}
}
Putting the following at the top of your script will load the contents of a groovy file.
evaluate(new File("/path/to/script/MyScript.groovy"))
You could also add this to the groovy classpath if you need to do something like this often. Hope this helps.
additionally if you need to run other scripts from your script you could do the following...
def script = new GroovyShell();
def args = ['Hello World'];
script.run(new File("/path/to/script/MyScript.groovy"), args as String[]);
Too late for the party (any beer for me?) but here I´ll show you 2 more flavors:
1) Remember the concept of Java´s CLASSPATH? That aplies to Groovy (because Groovy is Java!):
"The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.”
In order to run the script B.groovy you have to inform the location of the A.groovy (A class):
groovy –cp c:\groovy\yourscripts c:\groovy\scripts\B.groovy
The command above is telling the runtime to look in the c:\groovy\yourscripts folder because there is where we have our classes and we need them to run the B.groovy script successfully.
2) Use the GroovyClassLoader to load your scripts at runtime and using the code!
Both ways solve your needs. Now the next question is when to use each?

Resources