setting working directory in REPL

Hi, I’ve just started getting familiar with CommandBox. I opened the REPL and did this:

`
CFSCRIPT-REPL: expandpath( “./”);
=> “/Users/rmunn/.CommandBox/cfml/system/”

`

Which makes sense. I am wondering if I can set the path in the REPL. I would like to instantiate some objects from within the REPL, but the paths are invalid because of the current working directory in the REPL. I’ve already found that I can create objects, CFML or Java:

`
uri = createObject( “java”, “java.net.URI”);
writeDump( uri );

`

Now I need to be able to map components from my projects so I can instantiate them. Is there a way to create mappings? Sorry if this is an obvious answer to someone already familiar with CommandBox. I’ve been looking at the docs but I haven’t found a clear answer. I would like to debug some objects from within the REPL, so I need a way to call them.

That is super interesting. Create an enhancement ticket for this, so the command can take in a “directory” argument that can be the home directory.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

Glad to hear. New issue in JIRA:

https://ortussolutions.atlassian.net/browse/COMMANDBOX-204

I’ve thought about doing this as well. The biggest issue is that the current path is determined by Railo internally based on the location of the CFC that actually runs your REPL code. The only way I can think of to override that would be to create dynamic mappings on the fly so “/foo” for instance pointed to a directory of your choice. We could also try temporarily overriding the default “/” mapping, but i’m not sure if that would break anything inside of CommandBox.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

How about using a base mapping for REPL or CommandBox like /repl/ or /cb/. That might be too big of a change internally in CommandBox, I realize, but once it is done you can set a base directory wherever you want it without breaking anything.

Robert. We also welcome pull requests :slight_smile:

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

CommandBox already has a base mapping called “/commandbox” which points to the “cfml” directory. If you look in
~.CommandBox\cfml\Application.cfc you can see it. I don’t see how that helps you through since your projects are outside of the CommandBox home. Also, for what it’s worth, CommandBox (or, Railo really) maps “/” to your drive root. At least on Windows, it does. So expandPath("/") shows me C:\

If you want to be able to do

new com.foo.bar()

and have it actually create an instance of

C:\mydocs\random\sites\mySite\www\com\foo\bar.cfc

then the only way is to override the “/” mapping to point to your site root. I’d have to test and see if that will break CommandBox itself though.

Another option is if we created a mapping called something like “/repl” that pointed to the CWD (or a directory of your choice). Then it would be

new repl.com.foo.bar()

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

I see. A convenient way to make it work would be for “/“ to map to your current working directory, so if you run box from:

/projects/myproject/www

that would be your root.

A /repl mapping might break dependencies inside objects being instantiated.

Another way might be to define mappings inside the repl, so if you want to:

new com.foo.bar()

all you need to do is define mapping /com.

If there was a way to set per application mappings like in Application.cfc, that would work. so

this.mappings = {“com”: “/project/www/com”};

I would always assume that running the REPL would run from the current directory box is started from, much like IRB for Ruby. Never took notice to try to init cfc’s and see it not work. Good catch though :wink:

Adding a mapping on the start up to root like you mentioned would probably do it.

I think that might override some CommandBox internals. I think maybe creating a virtual mapping to the current working directory as /repl would be good. Right.

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com
P/F: 1-888-557-8057
Direct: (909) 248-3408

ColdBox Platform: http://www.coldbox.org

ContentBox Platform: http://www.gocontentbox.org
Linked In: http://www.linkedin.com/pub/3/731/483

Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano | twitter.com/gocontentbox

On Linux you can write scripts that the OS will execute with CommandBox,
and thus everything is relative to the filesystem.

In other words, the reason "/" maps to the drive root is so that scripts
located outside of the project root can be run.

If you work on more than one project at a time, it can be handy. :slight_smile:

-Den

Even if you’re on Windows you can get the same thing with a .cfm file like so:

C:> box foo.cfm

expandPath("") will be where the file is.

(Note, if you run “execute foo.cfm” from inside the interactive shell, you will get a different behavior)

But none of that affects the REPL. THe crux of the REPL behavior is that CommandBox is a CFML application of its own. It has an Application.cfc, mappings, and needs to be able to create its internals. The REPL is sort of just a command that is along for the ride inside the CommandBox ‘framework’ so it’s sort of bound to it.

A more drastic measure would be if the REPL spawned in its own process separate from CommandBox. Of course, that would entail the overhead of spinning up another JVM and loading Railo again, etc. To be honest, I’ve never done anything thus far with CFCs or file manipulation yet in the REPL. I’ve just used it for simple stuff like testing functions or playing with different syntaxes, etc. If you want to get real funky, you can always just spin up a full server in any directory with the ‘start’ command and now you can not only stick test code in an index.cfm, but run entire apps, create CFCs, the whole shebang. Since I use Coldbox/WireBox for everything, manually creating CFCs is a thing of the past so the REPL wouldn’t even work there. Well, I suppose it would if I used the REPL to create WireBox and asked it for my CFCs, but to me, that’s way more than the REPL is designed for. That’s what I use the embedded server for.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com

The use case occurred to me based on a comment Sean Corfield made in response to Adam Cameron’s weekend code challenge puzzle from a little while ago. I think Sean mentioned that he built the solution in the Clojure REPL, and I remembered that when I started playing around with CommandBox. In this specific instance, I was trying to implement various Java libraries for a webSocket client appender in LogBox (which works and is super-cool). I was having trouble getting the implementation correct and looking for ways to debug the solution. I ended up writing a test case in Java and debugging it inside Eclipse, but I was thinking that being able to go line by line in the REPL would have made it easier for me.

Anyway, CFML has the concept of components and OO programming, and you can instantiate any Java classes that are in the REPL instance classpath, so I found it a bit incongruent that you could not instantiate CFCs the same way.

> you can instantiate any Java classes that are in the REPL instance classpath, so I found it a bit incongruent that you could not instantiate CFCs the same way.

That’s not really a fair statement since Java and CFML have a completely different method of loading classes in the first place, in or out of a REPL.

In Java, you have a class path that you can just dump .class or .jar files into that define any number of classes in packages of their choice. If a jar wants to have a .com.foo.bar, it just declares that package and then builds out the class hierarchy relative to the jar’s root. The JVM pays no mind to where the actual .jar file is relative to the Java application and resolves them all the same.

In CFML, we have a different paradigm where classes (CFCs) are not allowed to simply declare a package-- their physical location relative to a number of “lookup” folders starting with the web root (another concept Java doesn’t have) controls what the full path of the CFC is. With this important dependance on the actual structure of the folders inside your application, there are limitations in CFML that don’t exist in Java.

I’m not entirely sure how Clojure resolves classes (and about 10 minutes of Googling were more confusing than not), so I’m not sure how it factors into their REPL implementation.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com