-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
146 lines (128 loc) · 2.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"image"
"image/png"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/disintegration/gift"
"github.com/urfave/cli"
)
// Declarations
const imgIcon = "icon.png"
const imgSplash = "splash.png"
const splashPath = string(os.PathSeparator) + "splash" + string(os.PathSeparator)
func main() {
//Defining Version
cli.VersionFlag = cli.BoolFlag{
Name: "version, v",
Usage: "prints utility version",
}
// Magic happens here
app := cli.NewApp()
app.Name = "generate, g"
app.Version = "1.0.0"
app.Usage = "generate ionic icons and splash screens!"
app.Action = func(c *cli.Context) error {
for _, platform := range Dirs(".") {
GenerateResources(GetImages(platform))
}
return nil
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func Exists(name string) bool {
if _, err := os.Stat(name); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func Dirs(path string) []string {
var platforms []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if IsDir(file.Name()) {
platforms = append(platforms, file.Name())
}
}
return platforms
}
func IsDir(path string) bool {
fileInfo, _ := os.Stat(path)
return fileInfo.IsDir()
}
func GenerateResources(pngs []string) {
for _, p := range pngs {
width, height := GetImageDimension(p)
src := LoadImage(imgIcon)
if strings.Contains(p, splashPath) {
src = LoadImage(imgSplash)
}
filters := map[string]gift.Filter{
"crop_to_size": gift.ResizeToFill(width, height, gift.LanczosResampling, gift.CenterAnchor),
}
for _, filter := range filters {
g := gift.New(filter)
dst := image.NewNRGBA(g.Bounds(src.Bounds()))
g.Draw(dst, src)
SaveImage(p, dst)
}
fmt.Printf("✓ Updated — %s (%dx%d)\n", p, width, height)
}
}
func GetImages(root string) []string {
var pngs []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".png" {
pngs = append(pngs, path)
}
return nil
})
if err != nil {
panic(err)
}
return pngs
}
func GetImageDimension(path string) (int, int) {
file, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", path, err)
}
return image.Width, image.Height
}
func LoadImage(filename string) image.Image {
f, err := os.Open(filename)
if err != nil {
log.Fatalf("os.Open failed: %v", err)
}
img, _, err := image.Decode(f)
if err != nil {
log.Fatalf("image.Decode failed: %v", err)
}
return img
}
func SaveImage(filename string, img image.Image) {
f, err := os.Create(filename)
if err != nil {
log.Fatalf("os.Create failed: %v", err)
}
err = png.Encode(f, img)
if err != nil {
log.Fatalf("png.Encode failed: %v", err)
}
}