Successfully reported this slideshow.

Rock-solid shell scripting with Ammonite

6

Share

Upcoming SlideShare
METABUSCADORES Y BUSCADORES
METABUSCADORES Y BUSCADORES
Loading in …3
×
1 of 34
1 of 34

Rock-solid shell scripting with Ammonite

6

Share

Download to read offline

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?

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?

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Rock-solid shell scripting with Ammonite

  1. 1. http://www.lihaoyi.com/Ammonite/ Rock-solid shell scripting with Ammonite Maxim Novak @maximnovakmaximn@wix.com https://github.com/maximn
  2. 2. 01
  3. 3. List files
  4. 4. List files (recursively)
  5. 5. List all *.scala files
  6. 6. List the 3 largest *.scala files Challenge… let’s ask Google
  7. 7. List the 3 largest *.scala files Can’t really understand it Seems to work… http://toonclips.com/design/5803
  8. 8. http://www.clipartpanda.com/categories/man-thinking-drawing (Lack of) Error handling
  9. 9. http://www.clipartpanda.com/categories/man-thinking-drawing (Lack of) Error handling Success
  10. 10. http://www.clipartpanda.com/categories/man-thinking-drawing (Lack of) Error handling Failed
  11. 11. Bash scripts are hard to understand, hard to maintain, and very fragile
  12. 12. Bash scripts are hard to understand, hard to maintain, and very fragile We need something better
  13. 13. Why not ? 02
  14. 14. List files import java.io.File val file = new File(".") if (file.exists && file.isDirectory) { file.listFiles.toSeq } else { Nil }
  15. 15. Write text to file import java.nio.file.{Paths, Files} import java.nio.charset.StandardCharsets val file = Paths.get("hi.txt") val content = "hello".getBytes(StandardCharsets.UTF_8) Files.write(file, content) http://humandefect.deviantart.com/art/U h-Oh-542255224 Verbose…
  16. 16. What you really want is
  17. 17. What you really want is the power of Scala with the semantics of a shell
  18. 18. Welcome Ammonite! 03
  19. 19. List files http://toonclips.com/design/9164 Structured data, yes! ls! wd is an alias for ls(wd)
  20. 20. List files (recursively)
  21. 21. List all *.scala files
  22. 22. List 3 largest *.scala files
  23. 23. Change directory
  24. 24. Write text to file
  25. 25. Running an executable
  26. 26. It’s easy breezy.
  27. 27. It’s easy breezy. And it’s safe.
  28. 28. Type safety
  29. 29. Main.scala:296: type mismatch; found : ammonite.ops.Path required: ammonite.ops.RelPath relative/absolute ^ Compilation Failed Type safety
  30. 30. Error handling java.nio.file.NoSuchFileException: /Users/max/noSuchPath sun.nio.fs.UnixException.translateToIOException(UnitExce sun.nio.fs.UnixException.rethrowAsIOException(UnitExcept sun.nio.fs.UnixException.rethrowAsIOException(UnitExcept sun.nio.fs.UnixFileAttributeViews$Basic.readAttributes(U
  31. 31. It’s easy breezy. And it’s safe. Also, you can load any JVM library to the shell.
  32. 32. Loading an external library
  33. 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/
  34. 34. Thank You Maxim Novak @maximnovakmaximn@wix.com https://github.com/maximn

Editor's Notes

  • 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
  • ×