Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
emulate: further changes
Further changes to the emulate actions in chromedp.
  • Loading branch information
Kenneth Shaw authored and mvdan committed Jun 17, 2019
1 parent 275a364 commit d55cf90
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 446 deletions.
370 changes: 267 additions & 103 deletions device/device.go

Large diffs are not rendered by default.

48 changes: 29 additions & 19 deletions device/gen.go
Expand Up @@ -18,7 +18,7 @@ import (

const deviceDescriptorsURL = "https://raw.githubusercontent.com/GoogleChrome/puppeteer/master/lib/DeviceDescriptors.js"

var flagOut = flag.String("out", "params.go", "out")
var flagOut = flag.String("out", "device.go", "out")

func main() {
flag.Parse()
Expand Down Expand Up @@ -49,37 +49,41 @@ func run() error {
return err
}

buf := new(bytes.Buffer)
fmt.Fprintf(buf, "package device\n\n")
fmt.Fprintf(buf, "// Generated by gen.go. DO NOT EDIT.\n\n")
fmt.Fprintf(buf, "// See: %s\n\n", deviceDescriptorsURL)
// add reset device
descriptors = append([]deviceDescriptor{deviceDescriptor{}}, descriptors...)

fmt.Fprintf(buf, "// Devices.\n")
fmt.Fprintf(buf, "const (\n")
fmt.Fprintf(buf, "_ Device = iota // make the zero value invalid\n\n")
for _, d := range descriptors {
name := cleanRE.ReplaceAllString(d.Name, "")
name = strings.ToUpper(name[0:1]) + name[1:]
fmt.Fprintf(buf, "// %s is the %q device.\n", name, d.Name)
fmt.Fprintf(buf, "%s\n\n", name)
buf := new(bytes.Buffer)
fmt.Fprintf(buf, hdr, deviceDescriptorsURL)
fmt.Fprintln(buf, "\n// Devices.")
fmt.Fprintln(buf, "const (")
for i, d := range descriptors {
if i == 0 {
fmt.Fprintln(buf, "// Reset is the reset device.")
fmt.Fprintln(buf, "Reset deviceType = iota\n")
} else {
name := cleanRE.ReplaceAllString(d.Name, "")
name = strings.ToUpper(name[0:1]) + name[1:]
fmt.Fprintf(buf, "// %s is the %q device.\n", name, d.Name)
fmt.Fprintf(buf, "%s\n\n", name)
}
}
fmt.Fprintf(buf, ")\n\n")
fmt.Fprintln(buf, ")\n")

fmt.Fprintf(buf, "// devices is the list of devices.\n")
fmt.Fprintf(buf, "var devices = [...]device{\n")
fmt.Fprintln(buf, "// devices is the list of devices.")
fmt.Fprintln(buf, "var devices = [...]Device{")
for _, d := range descriptors {
fmt.Fprintf(buf, "{%q, %q, %d, %d, %f, %t, %t, %t},\n",
d.Name, d.UserAgent,
d.Viewport.Width, d.Viewport.Height, d.Viewport.DeviceScaleFactor,
d.Viewport.IsLandscape, d.Viewport.IsMobile, d.Viewport.HasTouch,
)
}
fmt.Fprintf(buf, "}\n")
fmt.Fprintln(buf, "}")

// write and format
if err := ioutil.WriteFile(*flagOut, buf.Bytes(), 0644); err != nil {
return err
}

return exec.Command("gofmt", "-w", "-s", *flagOut).Run()
}

Expand Down Expand Up @@ -126,6 +130,12 @@ func get(v interface{}) error {
return json.Unmarshal(buf, v)
}

const tpl = `package device
const (
hdr = `package device
// See: %s
// Generated by gen.go. DO NOT EDIT.
`
)

0 comments on commit d55cf90

Please sign in to comment.