Are you tired of Bash scripts? Do you always forget the syntax, Googling the same commands over and over again? Don’t like Bash's dynamic nature?
We deserve better than that! Ammonite enables shell-like scripting in the Scala programming language. With all of Scala's power at your fingertips, why compromise on Bash's quirks?
33. JVM ecosystem
at your fingertips
Power and safety
of Scala
Shell semantics
https://pixabay.com/en/shells-massage-therapy-sand-beach-792912/
http://youandworld.am/news/5790.html
Ammonite blurs the line between
“system shell” and a “programming language”
http://www.lihaoyi.com/Ammonite/
Hello everybody, my name is Max. In the next 10 minutes I will introduce you with a new powerful tool for writing rock-solid shell scripts.
To understand why we need it, let’s start with the traditional way - bash.
I assume that most of you are familiar with bash.
Let’s see what's wrong with it.
List files is easy
Now let’s do it recursively
We still can read and understand it …
A bit more complicated ...Who can do it without Google?
You might be familiar of this process … let’s ask google.
And you need to google for everything, even if you had to do the same thing few weeks ago.
Who even remember how to do simple stuff like for loop or an if statement?
So usually you pick the one with most votes on StackOverflow
You google for it, and you find many results.
Then you look at it and try to understand it, look at it again …. and still don’t understand it. Probably very few people in this room can understand it.
So you run it! Seems to work! -> You happy!
That’s the amount of confidence we usually have in many our bash scripts
In addition to that amount of confidence, the result is a string, so to use it we need to parse it and extract what we need.
-- maybe replace ‘can’t understand’ with - WAT !??!
Now let’s have a look at another example.
What will happen?
It depends! If the function returned the path to delete, it will delete everything under this path.
If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
Now let’s have a look at another example.
What will happen?
It depends! If the function returned the path to delete, it will delete everything under this path.
If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
Now let’s have a look at another example.
What will happen?
It depends! If the function returned the path to delete, it will delete everything under this path.
If the function failed, it will fail silently and the execution of the script will continue with an empty string as the result of the function - Oh! Then it will delete the root folder :(
We’re all Scala developers! Why don’t we use Scala?
Let’s try
So a simple bash `ls` command unfolds to more than 5 lines of Scala code.
We get where this goes, I’m not going to go into the recursive examples here.
Let’s try something else
Write text to a file. What do you think?
Again, Scala is much more verbose.
So probably Scala isn’t better.
Don’t we deserve something better?
//It’s very verbose, and I don’t even want to go into all problems scala REPL have (like CTRL+C kicks us out of REPL)
Welcome Ammonite! It’s a combination of a better Scala REPL and a shell like operations library.
Let’s try to do the same things with Ammonite.
Very easy, almost like bash.
To list files we use `ls!`, the bang means executing a shell command.
The cool part here is that the result is a Scala class!
//This is another syntax-saving extension, that makes it easy to call functions without having to constantly be opening and closing brackets. It does nothing else.
Ammonite is doing pretty print and fold long results.
To list all Scala files we list the working directory and then just run `filter` on the extension.
Because it’s statically typed we even can use code completion to get the .ext
BTW, you’re getting some kind of code completion and highlighting
Then to get the to 3 largest files is very simple.
I believe that everyone in this room can understand this command.
Let’s have a look at something else
Let’s say we want to work with git.
Another big advantage of Scala is type safety.
We define these values that represents relative and absolute paths.
Now let’s try to combine them, it’s working as expected.
But what will happen when we will try to add an absolute path to a relative path?
Boom! Compilation error!
In case of a runtime error - an exception is thrown - I oppose to bash no silent failure so the execution of the script will stop.
Cross platform!
we simply load jsoup, then load wix homepage
and using selector to get the header of the page - “It all starts with a stunning website. Create yours. It’s easy and free.
Ammonite blurs the line between “system shell” to a “programming language”
It gives you the familiarity and ease of use of a bash like shell
combines it with the power and safety of Sacla
And on top of it you have all the JVM ecosystem at your fingertips
This talk gave you a little of taste of what Ammonite is. I invite you all to go to the website for more details and encourage you to try it