mirror of
https://github.com/pikami/tiktok-dl.git
synced 2024-11-25 17:25:42 +00:00
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
checkErr "github.com/pikami/tiktok-dl/utils/checkErr"
|
||
|
fileio "github.com/pikami/tiktok-dl/utils/fileio"
|
||
|
)
|
||
|
|
||
|
type resource struct {
|
||
|
Package string
|
||
|
FileName string
|
||
|
Values map[string]string
|
||
|
}
|
||
|
|
||
|
func (r resource) generate() {
|
||
|
filename := fmt.Sprintf("%s/%s", outputDir, r.FileName)
|
||
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||
|
checkErr.CheckErr(err)
|
||
|
defer f.Close()
|
||
|
|
||
|
// Header
|
||
|
header := fmt.Sprintf("// Package %s - This file is automatically generated.\n"+
|
||
|
"// Do not edit this file manually.\n"+
|
||
|
"// Check `/generator/resources.go` to change generated content\n"+
|
||
|
"package %s\n", r.Package, r.Package)
|
||
|
|
||
|
if _, err := f.WriteString(header); err != nil {
|
||
|
checkErr.CheckErr(err)
|
||
|
}
|
||
|
|
||
|
// Values
|
||
|
for key, value := range r.Values {
|
||
|
value = strings.ReplaceAll(value, "\n", "\\n")
|
||
|
value = strings.ReplaceAll(value, "\r", "\\r")
|
||
|
valueLine := fmt.Sprintf("\n//%s -\nvar %s = \"%s\"\n", key, key, value)
|
||
|
if _, err := f.WriteString(valueLine); err != nil {
|
||
|
checkErr.CheckErr(err)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func fileContentsOrDefault(file string) string {
|
||
|
defer func() {
|
||
|
if r := recover(); r != nil {
|
||
|
fmt.Printf("Failed to load file: %s\n", file)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
return safeString(fileio.ReadFileToString(file))
|
||
|
}
|
||
|
|
||
|
func safeString(str string) string {
|
||
|
escaped := strings.ReplaceAll(str, "\"", "\\\"")
|
||
|
return strings.ReplaceAll(escaped, "\n", "")
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
for _, i := range res {
|
||
|
i.generate()
|
||
|
}
|
||
|
}
|