+ All Categories
Home > Software > Rock-solid shell scripting with Ammonite

Rock-solid shell scripting with Ammonite

Date post: 23-Jan-2018
Category:
Upload: maxim-novak
View: 6,304 times
Download: 0 times
Share this document with a friend
34
http://www.lihaoyi.com/Ammonite/ Rock-solid shell scripting with Ammonite Maxim Novak @maximnovak [email protected] https://github.com/maximn
Transcript
Page 1: Rock-solid shell scripting with Ammonite

http://www.lihaoyi.com/Ammonite/

Rock-solid shell scripting with Ammonite

Maxim Novak

@[email protected] https://github.com/maximn

Page 2: Rock-solid shell scripting with Ammonite

01

Page 3: Rock-solid shell scripting with Ammonite

List files

Page 4: Rock-solid shell scripting with Ammonite

List files (recursively)

Page 5: Rock-solid shell scripting with Ammonite

List all *.scala files

Page 6: Rock-solid shell scripting with Ammonite

List the 3 largest *.scala files

Challenge… let’s ask Google

Page 7: Rock-solid shell scripting with Ammonite

List the 3 largest *.scala files

Can’t really understand it

Seems to work…

http://toonclips.com/design/5803

Page 8: Rock-solid shell scripting with Ammonite

http://www.clipartpanda.com/categories/man-thinking-drawing

(Lack of) Error handling

Page 9: Rock-solid shell scripting with Ammonite

http://www.clipartpanda.com/categories/man-thinking-drawing

(Lack of) Error handling

Success

Page 10: Rock-solid shell scripting with Ammonite

http://www.clipartpanda.com/categories/man-thinking-drawing

(Lack of) Error handling

Failed

Page 11: Rock-solid shell scripting with Ammonite

Bash scripts are hard to understand, hard to maintain, and very fragile

Page 12: Rock-solid shell scripting with Ammonite

Bash scripts are hard to understand, hard to maintain, and very fragile

We need something better

Page 13: Rock-solid shell scripting with Ammonite

Why not ?

02

Page 14: Rock-solid shell scripting with Ammonite

List files

import java.io.File

val file = new File(".")if (file.exists && file.isDirectory) {file.listFiles.toSeq

} else {Nil

}

Page 15: Rock-solid shell scripting with Ammonite

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/Uh-Oh-542255224

Verbose…

Page 16: Rock-solid shell scripting with Ammonite

What you really want is

Page 17: Rock-solid shell scripting with Ammonite

What you really want isthe power of Scala

with the semantics of a shell

Page 18: Rock-solid shell scripting with Ammonite

Welcome Ammonite!

03

Page 19: Rock-solid shell scripting with Ammonite

List files

http://toonclips.com/design/9164

Structured data, yes!

ls! wd is an alias for ls(wd)

Page 20: Rock-solid shell scripting with Ammonite

List files (recursively)

Page 21: Rock-solid shell scripting with Ammonite

List all *.scala files

Page 22: Rock-solid shell scripting with Ammonite

List 3 largest *.scala files

Page 23: Rock-solid shell scripting with Ammonite

Change directory

Page 24: Rock-solid shell scripting with Ammonite

Write text to file

Page 25: Rock-solid shell scripting with Ammonite

Running an executable

Page 26: Rock-solid shell scripting with Ammonite

It’s easy breezy.

Page 27: Rock-solid shell scripting with Ammonite

It’s easy breezy. And it’s safe.

Page 28: Rock-solid shell scripting with Ammonite

Type safety

Page 29: Rock-solid shell scripting with Ammonite

Main.scala:296: type mismatch;

found : ammonite.ops.Path

required: ammonite.ops.RelPath

relative/absolute

^

Compilation Failed

Type safety

Page 30: Rock-solid shell scripting with Ammonite

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

Page 31: Rock-solid shell scripting with Ammonite

It’s easy breezy. And it’s safe. Also, you can load any JVM library to the shell.

Page 32: Rock-solid shell scripting with Ammonite

Loading an external library

Page 33: Rock-solid shell scripting with Ammonite

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/

Page 34: Rock-solid shell scripting with Ammonite

Thank YouMaxim Novak

@[email protected] https://github.com/maximn


Recommended