What is the best way to package Elixir CLI application? - erlang

Suppose I have a CLI application with subcommands and arguments (like application foo --bar baz). How can I package it for distribution without requiring user to install Erlang?
I know there's mix escript Mix task, but it builds a binary that requires Erlang to be installed, also Mix reference states that escripts should be used only for development purposes.
mix release, however, produces redundant shell scripts that I don't want to see in dist.
So, is there a way to make a standalone distributable of an Elixir CLI application?
P. S. This is actually my first experience with Elixir (and the whole Erlang ecosystem)

Elixir applications can be packaged as Erlang releases, see also here. These can include Erlang VM. Elixir since 1.9 supports building releases without extra tooling, using included mix task mix release - please check the documentation for greasy details: https://hexdocs.pm/mix/Mix.Tasks.Release.html
You might benefit from a quick look at this blog post for inspiration and conceptual overview, noticing that for simple CLI app it is much simpler: https://www.cogini.com/blog/best-practices-for-deploying-elixir-apps/

Bakeware generates a single executable file.
They have added a CLI app example here.

Related

What is the difference between rebar and rebar3? or erlang.mk

I am very new to Erlang programming language. Is there a standard build tool in Erlang?
I have googled out these, not sure which one I should use. I don't know that what kind of occasion is it used for?
Rebar2: It is first usable and de facto build tool that most of the Erlang projects are using it. It uses Erlang script for getting dependencies, compiling, testing and making release of your project. However it is not a modern build tool and suffers from slowness of compiling in developing phase, difficulty for using in larger projects and a bit hard to understand for newcomers.
Rebar3: It is a successor to rebar2 with an attempt to improving its mechanism and providing new features which is compatible with modern build tools. Also it is easier to use for newcomers
Erlang.mk: It is a big Makefile. As Makefile is fast and is available by default in every unix system, so you can benefit from these features for your Erlang application build tool. It has a package index of most well-known Erlang projects and other standard features like Rebar. Also it is faster that rebar2 in developing phase (preliminary results show that rebar3 is notably faster than Erlang.mk)
I myself use Rebar and this possible duplicate of your question has two answers that recommend Rebar as well. But it is a matter of taste and I recommend to consider two different approaches and choose what is closer to your purposes.

Source Code vs Packaged App in Ruby

I'm learning to code Ruby, starting to get into learning open source code. How do I turn the open source code I see on github to a runnable application? For example how do I turn source code from Pomodori's Github to the downloadable app from this page? I'm interested in being able to manipulate the source code, and then convert it to a runnable app to see the changes. Any tips are appreciated!
You can see Pomodori's only dependency (listed in it's Gemfile), is the hotcococa gem:
# A sample Gemfile
source "http://rubygems.org"
gem "hotcocoa"
Here is a tutorial for building a simple GUI application with hotcocoa. Hotcocoa, as my fellow posters have pointed out is built on Macruby.
I think going directly with Macruby itself, is a better bet. There are lots of tutorials, and it is relatively up to date. You can build a simple stopwatch app with macruby (full installation instructions are here).
Personally if I was learning ruby again, I'd start with David Black's The Well Grounded Rubyist, or if you are looking for free resources, by working through the bastards book or code academy's ruby course.
update: it occurred to me overnight that you may not be familiar with Ruby's REPL (read evaluate print loop) cli tool IRB, open terminal an enter:
$ irb
This loads an interactive ruby session, you can read more about irb on Ruby's official site.
Generally speaking, Ruby isn't compiled, like C, C++, etc. -- it is executed directly from its source code.
Compilation in other languages is basically good for 1) converting as much of the app to machine code as possible, so it's faster at runtime and 2) removing the source code from the end result. There are ways to do both of these things to some extent in Ruby, but they are highly dependent on the specific Ruby implementation (e.g. MRI, JRuby, MacRuby) as well as the operating system the app will run on.
As Wukerplank notes, Pomodori is written in MacRuby, which is a Mac-specific implementation that has a tool called macruby-deploy that can make actual OS X apps. Unfortunately, the last time I checked (in early 2014), MacRuby didn't run correctly on OS X 10.9; its development mostly stalled out in the 10.7/10.8 era when its principal author (Laurent Sansonetti) created the commercial product RubyMotion. RubyMotion may be something you'd like to investigate if you don't mind yearly subscription fees; it additionally targets iOS and its apps can go in the App Store; it does sacrifice some of Ruby's dynamism, however.
But again, generally speaking people run Ruby programs by calling the Ruby interpreter on them (e.g. ruby program.rb). Ruby interpreters these days do some just-in-time compilation to speed up execution, but the source code is still visible to the user of the code.

Is it possible to run erlang without compilation?

Is there any VM for Erlang that allows you to do compilation on the fly instead of compiling before?
There is a possibility to compile from the shell, thanks Martin.
Now, from the Erlang shell (or some other module!):
1> compile:file("mymod.erl").
{ok,mymod}
2> mymod:myfun().
Hello Joe
Is there any pros or cons with doing this?
Will you still be able to hot swap code?
Is it the regular use-case to handle code?
What benefits does the compiler give you in the end then?
From the Erlang shell, you can compile a module on the fly using c("path/to/module.erl"). You can also access this functionality through the compile module, specifically the compile:file/{1,2} functions.
For example, suppose we have a file mymod.erl:
-module(mymod).
-export([myfun/0]).
myfun() -> io:format("Hello Joe~n").
Now, from the Erlang shell (or some other module!):
1> compile:file("mymod.erl").
{ok,mymod}
2> mymod:myfun().
Hello Joe
See Erldocs on the compile module for more information.
You can do a great deal with the Erlang compiler in runtime. For example, you can dynamically generate code for a module (use erl_syntax!) and then compile it without even writing it to a file using compile:forms/{1,2}.
(Insert standard speech on great power and great responsibility.)
Will you still be able to hot swap code?
Yes.
Is it the regular use-case to handle code?
No. Normally Erlang code is compiled ahead of time into BEAM bytecode. Depending on whether Erlang was started in embedded or interactive mode, the modules are either loaded on startup, or dynamically as they are referenced. If you are building a release, you basically have to compile ahead of time.
What benefits does the compiler give you in the end then?
Well, for one thing, we can build compact releases without unnecessary components like the compiler. Of course, we also get all the traditional benefits of ahead-of-time compilation, particularly that of not having to waste time compiling all the time.
To sum it up, unless you fully understand the implications and have a very good reason not to compile your code ahead of time, please follow the standard practices.
The Erlang VM can only run compiled code! If you want to interpret Erlang code then you need an interpreter. The module erl_eval implements an Erlang interpreter and is part of the standard Erlang/OTP distribution. It is used by the Erlang shell to interpret the expressions entered.
All code handling in the Erlang VM, whether compiling, loading or updating, is done at the module level so it is impossible to compile or load a just one function. The Erlang compiler is written in Erlang and always available and can compile to either a file or a binary which can be immediately loaded into the system. As #MartinTörnwall has pointed out compiling a module from the shell using c(module) is in essence compiling on the fly.
So there would be no problems in automatically compiling code on the fly when it is used, at the module level. It is just that the current system is not designed to work that way and by default when it tries to load a module it only looks for the pre-compiled object file, the .beam file.
Erlang has an interpreter escript. Entire Erlang archive can be written in script. Almost all features are available.
By default, the script will be interpreted. You can force it to be compiled by including the -mode(compile). in the script.
Though it depends on the way you design your application, regular practice is to have .erl files which are compiled and run than having escript files.
So now you have many options.
Compile .erl file to .beam using c(my_module) this auto loads the .beam file. So the existing VM can run it on the fly. On in code you can use compile module functions like file, purge and load to load and run it on the fly.
Compile and keep the .erl files using erlc, erl -make, rebar, etc (Erlang has rich support) and then run it. You can build archives, boot scripts, rel etc to manage running and release of the Erlang software. This usually is the practice for production.
Use escript and run everything in interpreted mode.
Use escript and give -mode(compile) option to tell Erlang VM that at runtime (when starting to run escript) compile the code and run the compiled code (in memory)
Is there any pros or cons with doing this?
Compiled code is faster than interpreted code. I dont see any other right now in Erlang as pretty much everything is supported in both. Erlang even supports combination (Calling compiled code from interpreted code)
Will you still be able to hot swap code?
Yes in all cases. Your code also should be able to handle this.
Is it the regular use-case to handle code?
Option 2 for production. Option for 1 for learning / simple development. Option 3 and 4 in need basis for specific requirements (May be one time running).
What benefits does the compiler give you in the end then?
To make it clear, erlc program provides a common way to run all compilers in the Erlang system and compile module gives an interface to Erlang compilers. Compiler gives intermediate binary .beam file which helps in running Erlang code faster than interpreted counterpart. They also catch syntax errors (compilation errors).

Good practices when developing an application in Erlang (and Riak)?

We are getting familiar with Erlang/OTP and Riak (Core, KV, Pipe, and so on) trying to build an open-source distributed application in turn. Our project is going to have a bunch of dependencies : tools like erlang_js, protobuffs, etc., but also services that we need to have running, such as Riak KV.
In Python/Ruby/Node.js, if modules are placed in a standard subdirectory relative to your project's, you can reference them, and later package them in releases. You can fire up a shell in the project's directory, play with your modules, do tests and so on, all just easily if good practices are followed.
What are the best practices for organizing a development environment in Erlang/OTP, with all dependencies reachable (and easily updatable to newest version), shell access to running nodes, testing, making releases, and so forth?
Check out rebar3: used for packaging, templating and managing releases of Erlang/OTP applications. You will find there an entire tutorial on how to use it.
Check out this tutorial on OTP first, before you can switch to this one which shows you releases and how they are handled in Erlang. Use this entire book as reference as you develop your project and because the author is still progressively adding more advanced stuff that you may need.
You may also want to keep reading about Erlang Applications and then do check out these quick links below:
Erlang Packaging, Process OneOTP Packaging Video by Chicago Boss GuysErlang Dependency ManagementRichard Jones Advice and examples on Packaging Erlang Projects with DependenciesMaven Tool and how it is used in packaging Erlang Projects
Most importantly, take rebar very seriously. It is very important in managing Erlang applications which have dependencies. You can keep posting your questions here on Stack Overflow for any assistance as you progress.
Before I forget, do check out a lot of stuff from the Riak Community.
Also, its important to check out the system documentation on creating target systems and also how to use Reltool to handle releases. The good thing with erlang is that it has several options of how to do something, as long as its easy to maintain your application in that way. With target systems, you will learn how to embed the Erlang VM, how to run Erlang applications on Solaris, VxWorks, and creating Erlang applications as Services on Windows NT using erlsrv.
Normally, we make sure that as an operating system is booting a Server, our application starts with it. Solaris so far has more customizations than any other OS as regards embedding Erlang/OTP VM. You can always communicate with (an) embedded Erlang VM(s) using Escript where by the escript creates a an erlang node which is allowed to connect to the embedded VM (so they have to share same cookie) and the embedded VM must have allowed the temporarily created Node to connect by executing net_kernel:allow(List_of_nodes)
Make sure to call this method to make your embedded VM allow connections only from an known strictly specified number of Nodes.

Starting Lua, what to use?

I'm trying to learn Lua, but I don't really know which binary to download. There's 2 choices:
Lua Binaries
Lua for Windows
The second option Lua for Windows seems to be the recommended option, but the installer weighs in at 26.6Mb, which is pretty hefty for what is supposed to be a v.lightweight language.
I'm thinking of using Lua as a scripting language for games, and perhaps as a fast development language for file processing like how Python or Ruby does it. So it must be something lightweight, not a 26.6Mb file.
Which is the appropriate one to download and start?
Luaforwindows, no doubt. It's simpler, easier and faster.
The installer comes with lots of stuff (Scite editor & several extra libs if I remember well). But the installer asks you before installing all those extra stuff. Just install the minimum and you will be fine.
Lua for Windows includes a handful of other, useful libraries and tools. The actual Lua executable included is still tiny, in the 1-2MB range as expected.
Having the extras there already will only make things easier, and disk space is cheap: go with Lua for Windows.
You may also want to check ZeroBrane Studio, which is only 4M download on Windows and is based on the same editor as SciTE that comes with Lua for Windows. ZBS also comes with 50+ Lua examples and few simple lessons to get started quickly with Lua programming.
Quoting from here.
Installation
The LuaBinaries files are intended for advanced users and programmers who want to incorporate Lua in their applications or distributions and would like to keep compatibility with LuaBinaries, so they also will be compatible with many other modules available on the Internet.
If what you want is a full Lua installation, please check other projects such as the Lua for Windows and LuaRocks.
Seems quite clear to me that you should download Lua for Windows.

Resources