Advertisement

Rock-solid shell scripting with Ammonite

Team Lead @ Wix.com at Wix.com
May. 4, 2016
Advertisement

More Related Content

Advertisement

Rock-solid shell scripting with Ammonite

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

Editor's Notes

  1. 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.
  2. 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.
  3. List files is easy
  4. Now let’s do it recursively
  5. We still can read and understand it …
  6. 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
  7. 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 !??!
  8. 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 :(
  9. 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 :(
  10. 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 :(
  11. We’re all Scala developers! Why don’t we use Scala? Let’s try
  12. 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
  13. 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)
  14. 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.
  15. 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.
  16. Ammonite is doing pretty print and fold long results.
  17. 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
  18. 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
  19. Let’s say we want to work with git.
  20. 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.
  21. But what will happen when we will try to add an absolute path to a relative path? Boom! Compilation error!
  22. 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.
  23. 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.
  24. 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
Advertisement