main.go raw

   1  package main
   2  
   3  import (
   4  	"go/format"
   5  	"io/ioutil"
   6  	"net/http"
   7  	"strings"
   8  
   9  	"github.com/p9c/gel/apputil"
  10  )
  11  
  12  func main() {
  13  	src := getSourceCode("icons", getIcons())
  14  	filename := "../icons/icons.go"
  15  	apputil.EnsureDir(filename)
  16  	var e error
  17  	if e = ioutil.WriteFile(filename, src, 0600); E.Chk(e) {
  18  		panic(e)
  19  	}
  20  }
  21  
  22  func getIcons() (iconNames []string) {
  23  	url := "https://raw.githubusercontent.com/golang/exp/54ebac48fca0f39f9b63e0112b50a168ee5b5c00/shiny/materialdesign/icons/data.go"
  24  	var e error
  25  	var r *http.Response
  26  	if r, e = http.Get(url); E.Chk(e) {
  27  		panic(e)
  28  	}
  29  	var b []byte
  30  	if b, e = ioutil.ReadAll(r.Body); E.Chk(e) {
  31  		panic(e)
  32  	}
  33  	if e = r.Body.Close(); E.Chk(e) {
  34  		panic(e)
  35  	}
  36  	s := string(b)
  37  	split := strings.Split(s, "var ")
  38  	iconNames = make([]string, len(split))
  39  	for i, x := range split[1:] {
  40  		split2 := strings.Split(x, " ")
  41  		iconNames[i] = split2[0]
  42  	}
  43  	return iconNames
  44  }
  45  
  46  func getSourceCode(packagename string, iconNames []string) []byte {
  47  	o := `// Package icons bundles the entire set of several icon sets into one package as maps to allow iteration
  48  
  49  `+`//go:generate go run ./icongen/.
  50  
  51  package ` + packagename + `
  52  
  53  
  54  import (
  55  	"golang.org/x/exp/shiny/materialdesign/icons"
  56  )
  57  
  58  var Material = map[string]*[]byte {
  59  `
  60  	for i := range iconNames {
  61  		if iconNames[i] == "" {
  62  			continue
  63  		}
  64  		o += "\t" + `"` + iconNames[i] + `": &icons.` + iconNames[i] + ",\n"
  65  	}
  66  	o += "}\n"
  67  	// I.Ln(o)
  68  	var e error
  69  	var out []byte
  70  	if out, e = format.Source([]byte(o)); e != nil {
  71  		panic(e)
  72  	}
  73  	return out
  74  }
  75