(Ruby) Compiling dynamic applications for Windows? - windows

Does anyone have any insights regarding compiling Ruby code for Windows? I've tried both "Ruby2Exe" and "OCRA", but both present their own issues. Ruby2Exe keeps presenting vague or confusing warnings such as "can't modify frozen string". OCRA on the other hand seems to want to run your script and assumes that there are no dynamic items.
For the record, my script accepts command line arguments as well as reading in and parsing a text file. OCRA doesn't like this aspect at all, and actually throws the warnings in my code as if I tried to run the script.
Anyway, if anyone has any quality means by which to compile ruby code for Windows, I'm all ears.
As a bit of an FYI, my goal with this particular script is to send email over SMTP. It is part of a larger non-ruby application, but the framework is incapable of sending email. I find Ruby enjoyable and rather easy to work with but don't wish to have every end user install Ruby -- hence, the need/desire to "compile" it.
I'm on a short time table and can't really afford to expend resources on writing this in C++, etc. However, if anyone has any insights on any existing Windows-compatible libaries/applications, do tell.
Much appreciated.

"OCRA on the other hand seems to want to run your script..."
The constant Ocra is defined at compile-time but not at run-time. So you can include logic based on whether or not the Ocra constant is defined. For example:
app = MyApp.new
if not defined?(Ocra)
app.main_loop
end

Related

AppleScript: "use framework" errors out in "Expected end of line, etc. but found identifier."

I have the following piece of AppleScript.
use framework "Foundation"
display dialog "foo"
Note: the Foundation framework is not actually used in this example, because that part of the code is irrelevant for this example. This code alone already produces the error. In real life, we obviously only import a framework to use it, duh :-)
When I run this I get the unhelpful error:
Expected end of line, etc. but found identifier.
The identifier on which the macOS Script Editor stops is "dialog".
When I change the code to:
display dialog "foo"
The script runs as I expect it.
I have two questions:
Why does the top example produce an error?
Why does it produce this exact error? Or in other words: why is this error so unhelpful? Is this the case for AppleScript in general?
The predication in your answer is outdated. You can import frameworks in standard scripts nowadays (AFAIR since Yosemite).
If you apply an use framework statement you have to add
use scripting additions
to be able to access display dialog
! The information I based my answer on was out of date, see under my answer for the update.
The reason why the script errors is because:
"The importation of frameworks via the use statement is only supported in script libraries, not in other scripts or applets."
So basically: you're not allowed to use the use statement in a regular AppleScript script. You can only use it in script libraries.
To fix this we create a "script library", which is just another AppleScript file. Let's say we call this script library chewbacca.scpt. You need to place the script in a specific location on your Mac. (You have a few options for this location). AppleScript only looks in those locations when trying to import script libraries.
Then, to use the script library do something like:
tell script "chewbacca"
display dialog "foo"
end tell
That should give the desired result.
Update:
After reading some answers and reading some more documentation:
The way AppleScript is extended is by importing a library, these libraries are called osax (or "Scripting Additions") because their file names end in .osax. OSAX stands for "Open Scripting Architecture eXtension". To import an osax we write use library in our script (I'm not 100% sure about this). By importing an osax we can use the commands in that osax.
AppleScript (the language) does not have commands for things like: user interaction dialogs (display dialog), reading and writing files, file system commands, date functions, and text and mathematical operations.
But: Apple does provide an osax that offers these commands: StandardAdditions.osax, it's not hard to see why this is one of the most commonly used osax.
To import this osax:
use scripting additions
Now back to my question:
I see AppleScript behaving differently under certain conditions:
a script does not import an osax
a script imports an osax (but not StandardAdditions.osax)
In situation 1 it seems like AppleScript (the runtime?) silently auto-imports StandardAdditions.osax. I think this is the case because I can use display dialog.
In situation 2 AppleScript (the runtime) does not auto-import StandardAdditions.osax.
I can theorize about this different behavior:
I suspect for situation 1 they want to make it easier for people to get started with AppleScript so they auto-import the basic commands most people/beginners probably want to use.
The thinking behind situation 2 might have been something like:
"the developer is explicitly importing an osax so they may not have a
need for StandardAdditions.osax so let's not auto-import it".
I've found somewhere that it's a good idea to always explicitly import StandardAdditions.osax by adding use scripting additions to your script.
I'll follow this advice in the future.

Ruby 'compile' while coding

Using Java big IDEs compile my code while it is written so that errors are detected before runtime.
Is that possible with Ruby too? Actually I code in a Text editor. Errors are detetected at runtime only.
Is that possible with Ruby too?
If by that you mean "compiling", then no. If you mean "edit-time error detection", then also no.
Smart IDEs, like RubyMine, can guess/detect some errors, but only simple cases. And they are often confused by ruby's dynamic nature. (can't find location for a method, even though it's defined within the project. Or the opposite, find too many false positives).
In ruby, you simply can't know what does a piece of code do without running it.

Fetching a URL without C Extensions/Libraries

I'm working within the context of RPG Maker VX Ace. It has some Ruby extensions, and it has a subset of the Ruby 1.8 library built in. It also allows me to add my own .rb files -- such as the Ruby 1.9.3 source files -- albeit that I cannot use any code that has C extensions. If it requires a .so file, it won't work -- I'll get an error that "this application cannot load extensions/plugins."
Having understood this, I need some way to make an HTTP request. What I mean is:
Call a URL (eg. http://myserver.com/blah?data=abcd
Return the response (which will be a string -- not full HTML, just an identifier of some sort)
I have tried using open-uri and net/http. Both of them have different issues that prevent me from using. One (I think it's the latter) requires TcpSocket, which is implemented in C and is not "pure" Ruby. The other one gives me an error about an undefined constant related to sockets.
I've also tried using the Socket class, and it gives me a similar error.
TLDR: Is there some way I can make a Ruby HTTP call with pure Ruby, no C extensions? That is what I need.
I solved this by writing some code and calling it from Ruby. Easy breezy.
Using .NET, I created a non-command-line, non-service application; something which I can invoke like a command-line app, but without the command-line window. To do this:
Create a new command-line project
Go to project properties
Change the type to "Windows Forms Application"
When you run your project, it'll run like any command-line project (args, etc.) but no windows will appear.
From there, it was easy to make an asynchronous HTTP request and store the response in a text file. This is sufficient for now.
You could call out to a shell and execute curl (depending on your environment and the speed you want)
result = `curl "http://myserver.com/blah?data=abcd" -s`
The -s makes curl silent. See if that stops the dos box coming up.

In Ruby, what's the equivalent of Java's technique of limiting access to source in a cowork situation?

In Java when you compile a .java file which defines a class, it creates a .class file. If you provide these class files to your coworkers then they cannot modify your source. You can also bundle all of these class files into a jar file to package it up more neatly and distribute it as a single library.
Does Ruby have any features like these when you want to share your functionality with your coworkers but you don't want them to be able to modify the source (unless they ask you for the actual .rb source file and tell you that they want to change it)?
I believe the feature you are looking for is called "trust" (and a source code control repository). Ruby isn't compiled in the same way that Java is, so no you can't do this.
I have to say your are in a rough position, not wanting to share code with a coworker. However, given that this is an unassailable constraint perhaps you could change the nature of the problem.
If you have a coworker that needs access to some service provided by a library of yours, perhaps you could expose it by providing a web/rest service instead of as a .rb file.
This way you can hide your code behind a web server, and if there is a network architecture that allows for low latency making these service calls, you can effectively achive the same goal.
Trust is a lot easier though.
edit:
Just saw this on HN: http://blog.astrails.com/2009/5/12/ruby-http-require, allows a ruby file to include another file through http instead of the filesystem.
Ruby is
A dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
So like all interpreted languages, you need to give the source code to anyone who want's to execute your program/script.
By the way searching "compiled ruby" on google returned quiet a few results.
I don't think there is one. Ruby is purely an interpreted language, which means ruby interprets your source code directly in order to run it. Java is compiled, so there's an intermediate bytecode (the .class). You can obfuscate your ruby if you really wish, but it's probably more trouble than it's worth.
Just to make sure you realize, however, upwards of 95% of Java can be decompiled back into source using various free utilities, so in reality, Java's compilation isn't much better than distributing Ruby source.
This is not a language specific problem and one that can be managed more effectively through source control software.
There is a library called ruby2c that compiles a subset of Ruby into C code (which you can then compile into native code, if you want).
It was actually originally written as a Ruby code obfuscator (but has since been used for lots of other stuff, including Ruby Arduino development).

Tool purely for Syntax Checking?

We have a proprietry system that we develop scripting code in.
We currently do not have a developer environment (apart from Notepad++) and cannot debug or compile this code. We have to submit it to the vendor to insert the code into the test or live system.
The language is essentially C like and has the same syntax.
Basically we want a tool to be able to simply check the syntax of chunks of code we send to the vendor.
Does a tool exist that will do this for me?
You write code in a proprietary scripting language, so you require syntax checking because you cannot compile or debug the code onsite? I'd suggest getting a copy of the language reference (including the BNF if possible) from your vendor, get a compiler-compiler like Coco/R (http://www.ssw.uni-linz.ac.at/coco/), and build yourself a quick and dirty compiler that just validates the abstract syntax tree.
That is to say, yes, there are tools you can use, though perhaps they involve more work than what you may have hoped.
If it's really the same syntax as C you can use a C compiler. Usually there's a syntax check only option (/Zs for MSVC).
I'm not sure how many problems you'll run into since C compilers are pretty picky, and being "like C" is not the same as being C.
It does seem odd that you're being asked to develop code without having any capability to run or even compile it. Kind of like writing a book without being able to proof read it before publishing. I have a hard time getting even "Hello World" programs to compile & run without some sort of goof-up on the very first go.

Resources