1 | package root |
---|
2 | |
---|
3 | import sbt._, Keys._ |
---|
4 | import java.io.File |
---|
5 | |
---|
6 | /** |
---|
7 | * |
---|
8 | * This is an autoplugin for sbt which makes it possible to |
---|
9 | * create an correctly made icns file for usage as app icon. |
---|
10 | * |
---|
11 | * The task "convertToICNSTask" should become available for us to define |
---|
12 | * because of this class. |
---|
13 | * |
---|
14 | * sips is only a command on Mac OS X afaik so this would probably need to be |
---|
15 | * built on a mac. |
---|
16 | * |
---|
17 | * @since 0.9.35 |
---|
18 | */ |
---|
19 | object IconHelper extends AutoPlugin { |
---|
20 | |
---|
21 | def convertToICNS(inF: File, outF: File) { |
---|
22 | if (inF.ext == "icns") { |
---|
23 | IO.copyFile(inF, outF, preserveLastModified = true) |
---|
24 | } else { |
---|
25 | import sys.process._ |
---|
26 | val lines = Seq("sips", "-g", "pixelHeight", "-g", "pixelWidth", inF.getPath).lines |
---|
27 | val PixelWidth = "\\s+pixelWidth: (\\d+)".r |
---|
28 | val PixelHeight = "\\s+pixelHeight: (\\d+)".r |
---|
29 | val srcWidth = lines.collect { case PixelWidth (s) => s.toInt } .head |
---|
30 | val srcHeight = lines.collect { case PixelHeight(s) => s.toInt } .head |
---|
31 | val supported = IndexedSeq(16, 32, 48, 128, 256, 512) |
---|
32 | val srcSize = math.min(512, math.max(srcWidth, srcHeight)) |
---|
33 | val tgtSize = supported(supported.indexWhere(_ >= srcSize)) |
---|
34 | val args0 = Seq(inF.getPath, "--out", outF.getPath) |
---|
35 | val args1 = if (tgtSize != srcWidth || tgtSize != srcHeight) { |
---|
36 | Seq("-z", tgtSize.toString, tgtSize.toString) |
---|
37 | } else { |
---|
38 | Seq.empty |
---|
39 | } |
---|
40 | val args = Seq("sips", "-s", "format", "icns") ++ args1 ++ args0 |
---|
41 | args.!! |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | object autoImport { |
---|
46 | val convertToICNSTask = taskKey[Unit]("Converts an image to Mac OS X's ICNS icon format") |
---|
47 | } |
---|
48 | |
---|
49 | import autoImport._ |
---|
50 | |
---|
51 | } |
---|
52 | |
---|