From 0829ebc9962cae1aabcdf234de34938678d6d97a Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Mon, 1 Oct 2012 17:26:19 -0700 Subject: [PATCH] add tool/measure --- .gitignore | 3 +- src/043-state-goroutine/state-goroutine.go | 2 +- tool/build-html | 2 +- tool/compile | 3 +- tool/measure | 6 +++ tool/src/{generate.go => build-html-inner.go} | 2 - tool/src/measure-inner.go | 42 +++++++++++++++++++ 7 files changed, 54 insertions(+), 6 deletions(-) create mode 100755 tool/measure rename tool/src/{generate.go => build-html-inner.go} (98%) create mode 100644 tool/src/measure-inner.go diff --git a/.gitignore b/.gitignore index e58e6db..8991f3b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build -tool/generate +tool/build-html-inner tool/number +tool/measure-inner diff --git a/src/043-state-goroutine/state-goroutine.go b/src/043-state-goroutine/state-goroutine.go index c90c313..6eff8b4 100644 --- a/src/043-state-goroutine/state-goroutine.go +++ b/src/043-state-goroutine/state-goroutine.go @@ -64,7 +64,7 @@ func generateWrites(writes chan *writeOp) { key := randKey() val := randVal() write := &writeOp{ - key: key, + key: key, val: val, resp: make(chan bool)} writes <- write diff --git a/tool/build-html b/tool/build-html index 9748440..75b38a3 100755 --- a/tool/build-html +++ b/tool/build-html @@ -1,3 +1,3 @@ #!/bin/bash -ls src/0*/*.{go,sh} | xargs tool/generate > build/go-by-example.html +ls src/0*/*.{go,sh} | xargs tool/build-html-inner > build/go-by-example.html diff --git a/tool/compile b/tool/compile index ce6f04b..55753f2 100755 --- a/tool/compile +++ b/tool/compile @@ -2,5 +2,6 @@ set -e -go build -o tool/generate tool/src/generate.go +go build -o tool/build-html-inner tool/src/build-html-inner.go go build -o tool/number tool/src/number.go +go build -o tool/measure-inner tool/src/measure-inner.go diff --git a/tool/measure b/tool/measure new file mode 100755 index 0000000..44fcafa --- /dev/null +++ b/tool/measure @@ -0,0 +1,6 @@ +#!/bin/bash + +set -e +set -o pipefail + +ls src/0*/*.{go,sh} | xargs tool/measure-inner diff --git a/tool/src/generate.go b/tool/src/build-html-inner.go similarity index 98% rename from tool/src/generate.go rename to tool/src/build-html-inner.go index fd8e75b..8d79fbf 100644 --- a/tool/src/generate.go +++ b/tool/src/build-html-inner.go @@ -1,5 +1,3 @@ -// Generate an HTML file from the book source. Derived from golit. - package main import ( diff --git a/tool/src/measure-inner.go b/tool/src/measure-inner.go new file mode 100644 index 0000000..9edbf22 --- /dev/null +++ b/tool/src/measure-inner.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + "strings" +) + +func check(err error) { + if err != nil { + panic(err) + } +} + +func readLines(path string) []string { + srcBytes, err := ioutil.ReadFile(path) + check(err) + return strings.Split(string(srcBytes), "\n") +} + +func main() { + if len(os.Args) <= 1 { + fmt.Fprintln(os.Stderr, "usage: tool/measure *.{go,sh}") + os.Exit(1) + } + foundLongFile := false + for _, sourcePath := range os.Args[1:] { + foundLongLine := false + lines := readLines(sourcePath) + for _, line := range lines { + if len(line) > 60 && !foundLongLine { + fmt.Println(sourcePath) + foundLongLine = true + foundLongFile = true + } + } + } + if foundLongFile { + os.Exit(1) + } +}