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