Skip to content

Commit

Permalink
Fix case where category is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
randronache committed Jul 29, 2024
1 parent 4d849bd commit bdecde4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,39 +65,52 @@ func (i *IconifyGenerator) generateFromCategory(category, prefix string, icons [
}
defer file.Close()

hasGeneratedIcon := false
for _, icon := range icons {
i.generateIcon(file, prefix, icon)
res := i.generateIcon(file, prefix, icon)
if res {
hasGeneratedIcon = true
}
}

if !hasGeneratedIcon {
err = os.Remove(filePath)
if err != nil {
log.Fatal("Error removing empty file: ", err)
}
}
}

var iconFunctionTemplate = ""

// add an icon function to the file
func (i *IconifyGenerator) generateIcon(file *os.File, prefix, icon string) {
// add an icon function to the file and return true if the icon was added
func (i *IconifyGenerator) generateIcon(file *os.File, prefix, icon string) bool {
body, err := iconify.GetIcon(i.API, prefix, icon)
if err != nil {
return
return false
}

functionName := getIconFunctionName(icon)
if iconFunctionTemplate == "" {
data, err := os.ReadFile(i.CWD + "/pkg/generator/icon_function.tpl.txt")
if err != nil {
log.Fatal(err)
return
return false
}
iconFunctionTemplate = string(data)
}

if slices.Contains(i.GeneratedIcons[prefix], functionName) {
return
return false
}

iconFunction := strings.ReplaceAll(iconFunctionTemplate, "$FUNCION-NAME$", functionName)
iconFunction = strings.ReplaceAll(iconFunction, "$FUNCTION-PARAMS$", "")
iconFunction = strings.ReplaceAll(iconFunction, "$FUNCTION-BODY$", body)
file.WriteString(iconFunction)
i.GeneratedIcons[prefix] = append(i.GeneratedIcons[prefix], functionName)

return true
}

// create a file to store the icon functions
Expand Down

0 comments on commit bdecde4

Please sign in to comment.