File: genassets.go

package info (click to toggle)
syncthing 1.19.2~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 21,484 kB
  • sloc: javascript: 36,375; sh: 1,804; xml: 1,049; makefile: 67
file content (147 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download | duplicates (3)
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
147
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

//go:build ignore
// +build ignore

package main

import (
	"bytes"
	"compress/gzip"
	"flag"
	"fmt"
	"go/format"
	"io"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"text/template"
	"time"
)

var tpl = template.Must(template.New("assets").Parse(`// Code generated by genassets.go - DO NOT EDIT.

package auto

import (
	"time"

	"github.com/syncthing/syncthing/lib/assets"
)

func Assets() map[string]assets.Asset {
	var ret = make(map[string]assets.Asset, {{.Assets | len}})
	t := time.Unix({{.Generated}}, 0)

{{range $asset := .Assets}}
	ret["{{$asset.Name}}"] = assets.Asset{
		Content:  {{$asset.Data}},
		Gzipped:  {{$asset.Gzipped}},
		Length:   {{$asset.Length}},
		Filename: {{$asset.Name | printf "%q"}},
		Modified: t,
	}
{{end}}
	return ret
}

`))

type asset struct {
	Name    string
	Data    string
	Length  int
	Gzipped bool
}

var assets []asset

func walkerFor(basePath string) filepath.WalkFunc {
	return func(name string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}

		if strings.HasPrefix(filepath.Base(name), ".") {
			// Skip dotfiles
			return nil
		}

		if info.Mode().IsRegular() {
			data, err := os.ReadFile(name)
			if err != nil {
				return err
			}
			length := len(data)

			var buf bytes.Buffer
			gw, _ := gzip.NewWriterLevel(&buf, gzip.BestCompression)
			gw.Write(data)
			gw.Close()

			// Only replace asset by gzipped version if it is smaller.
			// In practice, this means HTML, CSS, SVG etc. get compressed,
			// while PNG and WOFF files are left uncompressed.
			// lib/assets detects gzip and sets headers/decompresses.
			gzipped := buf.Len() < len(data)
			if gzipped {
				data = buf.Bytes()
			}

			name, _ = filepath.Rel(basePath, name)
			assets = append(assets, asset{
				Name:    filepath.ToSlash(name),
				Data:    fmt.Sprintf("%q", string(data)),
				Length:  length,
				Gzipped: gzipped,
			})
		}

		return nil
	}
}

type templateVars struct {
	Assets    []asset
	Generated int64
}

func main() {
	outfile := flag.String("o", "", "Name of output file (default stdout)")
	flag.Parse()

	filepath.Walk(flag.Arg(0), walkerFor(flag.Arg(0)))
	var buf bytes.Buffer

	// Generated time is now, except if the SOURCE_DATE_EPOCH environment
	// variable is set (for reproducible builds).
	generated := time.Now().Unix()
	if s, _ := strconv.ParseInt(os.Getenv("SOURCE_DATE_EPOCH"), 10, 64); s > 0 {
		generated = s
	}

	tpl.Execute(&buf, templateVars{
		Assets:    assets,
		Generated: generated,
	})
	bs, err := format.Source(buf.Bytes())
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	out := io.Writer(os.Stdout)
	if *outfile != "" {
		out, err = os.Create(*outfile)
		if err != nil {
			fmt.Fprintln(os.Stderr, err)
			os.Exit(1)
		}
	}
	out.Write(bs)
}