Re: Running groovy programs

Andriy Rysin <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAOZT1o3=KTErWtx1_MZRKCOtLWSYHobc1qQbnMeOadjf27jNZA@mail.gmail.com>
Hi Jochen and all,

so a while ago my groovy script grew (big enough) and I had to split it
into multiple classes. But I wanted my users to still be able to run it
with a simple command (e.g. ./TagText.groovy on Linux and groovy
TagText.groovy on Windows).

So now all my scripts have a wrapper, example of one of them is below - the
main script was called TagText.groovy and now the main functionality moved
to TagTextCore.groovy.
This works, but I had to add such a wrapper to all scripts like that. And I
feel that this type of code does not belong in the apps - this is
system-level functionality.

Regards,
Andriy

@CompileStatic

class TagText {


@groovy.transform.SourceURI

static URI SOURCE_URI

static String SCRIPT_DIR=new File(SOURCE_URI).parent


static void main(String[] args) {

long tm1 = System.currentTimeMillis()

def cl = new GroovyClassLoader()

cl.addClasspath(SCRIPT_DIR + "/../../../../")


def resourceDir = SCRIPT_DIR + "/../../../../../resources"

if( ! new File(resourceDir).isDirectory() ) {
new File(resourceDir).mkdirs()

}

cl.addClasspath(resourceDir)

def basePkg = TagText.class.getPackageName()

def tagTextClass = cl.loadClass("${basePkg}.tag.TagTextCore")

def m = tagTextClass.getMethod("main", String[].class)

def mArgs = [args].toArray() // new Object[]{args} - Eclipse chokes on this


m.invoke(null, mArgs)

}

}

On Thu, Mar 24, 2022 at 3:25 AM Jochen Theodorou <[email protected]> wrote:

> Hi Andriy,
>
> We can add an option, that is then changing the compiler configuration
> and a transform could react to that to add a path to the classpath
> (package corrected of course) This is certainly possible.
>
> bye Jochen
>
>
> On 23.03.22 16:37, Andriy Rysin wrote:
> > I wonder if the 'groovy' executable could be smart enough to detect: if
> > I am running src/pkg1/main.groovy and there's "package pkg1" in that
> > script that we need to add src/ to classpath.
> > But I guess for that groovy would have to compile it first so it's a
> > chicken and egg problem
> >
> > Andriy
> >
> > On Mon, Mar 21, 2022 at 3:41 PM Jochen Theodorou <[email protected]
> > <mailto:[email protected]>> wrote:
> >
> >     On 21.03.22 15:26, Andriy Rysin wrote:
> >      > Ah, sorry I didn't get it at first. Technically if we need extra
> >     wrapper
> >      > to start a program I could probably do a shell/bat scripts. I was
> >     hoping
> >      > we could use the same command (I have other tools that are
> invoked by
> >      > their groovy file, so I was trying to keep the more complex one
> >     the same).
> >
> >     It surely would not be impossible to make a global transform, which
> >     helps the compiler with the setup. But this global transform would
> have
> >     to be on the classpath. And that could be done by placing it in
> >     ~/.groovy/lib/
> >
> >     The transform would then add the source path to the classpath for the
> >     compiler used for the compilation during the conversion phase. Still
> >     requires a one-time setup step of course.
> >
> >     Out of the box I think we have no solution for this really.
> >
> >     bye Jochen
> >
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.