From f40cfc78bb897f97131fbca98476cecdb1adbcd3 Mon Sep 17 00:00:00 2001 From: mpl Date: Fri, 15 Jul 2016 02:10:07 +0200 Subject: [PATCH] make.go: generate search types for app/publisher Using go:generate to call a shell script with some go doc + sed hackery. we could probably do it better with go/types later if needed. Change-Id: Ie1cf04d418b8b498f83f7029eb736dbc779feeb5 --- app/publisher/js/gensearchtypes.sh | 49 ++++++++++++++++++++++ app/publisher/js/main.go | 2 + app/publisher/js/{search.go => zsearch.go} | 48 +++++++++------------ make.go | 38 ++++++++++++++++- 4 files changed, 106 insertions(+), 31 deletions(-) create mode 100755 app/publisher/js/gensearchtypes.sh rename app/publisher/js/{search.go => zsearch.go} (84%) diff --git a/app/publisher/js/gensearchtypes.sh b/app/publisher/js/gensearchtypes.sh new file mode 100755 index 000000000..a5df48ca8 --- /dev/null +++ b/app/publisher/js/gensearchtypes.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +outfile=zsearch.go +cat >$outfile <> $outfile +go doc camlistore.org/pkg/search.SearchResultBlob | sed '/^func.*/d' | sed '/^ .*/d' >> $outfile +go doc camlistore.org/pkg/search.DescribeResponse | sed '/^func.*/d' | sed '/^ .*/d' >> $outfile +go doc camlistore.org/pkg/search.MetaMap | sed '/^func.*/d' | sed '/^ .*/d' >> $outfile +# stripping DescribeRequest from DescribeBlob because it would pull a lot more of search pkg in +go doc camlistore.org/pkg/search.DescribedBlob | sed '/^ Request\ \*DescribeRequest.*/d' | sed '/^func.*/d' | sed '/^ .*/d' >> $outfile +go doc camlistore.org/pkg/search.DescribedPermanode | sed '/^func.*/d' | sed '/^ .*/d' >> $outfile +gofmt -w $outfile diff --git a/app/publisher/js/main.go b/app/publisher/js/main.go index 98b1c3949..65519978d 100644 --- a/app/publisher/js/main.go +++ b/app/publisher/js/main.go @@ -1,3 +1,5 @@ +//go:generate ./gensearchtypes.sh + /* Copyright 2016 The Camlistore Authors. diff --git a/app/publisher/js/search.go b/app/publisher/js/zsearch.go similarity index 84% rename from app/publisher/js/search.go rename to app/publisher/js/zsearch.go index 174e08927..795575eb7 100644 --- a/app/publisher/js/search.go +++ b/app/publisher/js/zsearch.go @@ -1,3 +1,5 @@ +// generated by gensearchtypes.sh; DO NOT EDIT + /* Copyright 2016 The Camlistore Authors. @@ -17,7 +19,6 @@ limitations under the License. package main import ( - "fmt" "net/url" "time" @@ -32,10 +33,24 @@ import ( // camlistore.org/pkg/search to use its types instead of the ones below, we grow to // ~5.7MB. -// TODO(mpl): keep these types in sync with camlistore.org/pkg/search. -// Brad suggested using go:generate +type SearchResult struct { + Blobs []*SearchResultBlob `json:"blobs"` + Describe *DescribeResponse `json:"description"` + + // Continue optionally specifies the continuation token to to + // continue fetching results in this result set, if interrupted + // by a Limit. + Continue string `json:"continue,omitempty"` +} + +type SearchResultBlob struct { + Blob blob.Ref `json:"blob"` +} + +type DescribeResponse struct { + Meta MetaMap `json:"meta"` +} -// A MetaMap is a map from blobref to a DescribedBlob. type MetaMap map[string]*DescribedBlob type DescribedBlob struct { @@ -66,28 +81,3 @@ type DescribedPermanode struct { Attr url.Values `json:"attr"` // a map[string][]string ModTime time.Time `json:"modtime,omitempty"` } - -// SearchResult is the result of the Search method for a given SearchQuery. -type SearchResult struct { - Blobs []*SearchResultBlob `json:"blobs"` - Describe *DescribeResponse `json:"description"` - - // Continue optionally specifies the continuation token to to - // continue fetching results in this result set, if interrupted - // by a Limit. - Continue string `json:"continue,omitempty"` -} - -type SearchResultBlob struct { - Blob blob.Ref `json:"blob"` - // ... file info, permanode info, blob info ... ? -} - -func (r *SearchResultBlob) String() string { - return fmt.Sprintf("[blob: %s]", r.Blob) -} - -// DescribeResponse is the JSON response from $searchRoot/camli/search/describe. -type DescribeResponse struct { - Meta MetaMap `json:"meta"` -} diff --git a/make.go b/make.go index 4dc5ecf23..ea801f882 100644 --- a/make.go +++ b/make.go @@ -79,10 +79,10 @@ var ( // Our temporary source tree root and build dir, i.e: buildGoPath + "src/camlistore.org" buildSrcDir string // files mirrored from camRoot to buildSrcDir - rxMirrored = regexp.MustCompile(`^([a-zA-Z0-9\-\_]+\.(?:blobs|camli|css|eot|err|gif|go|s|pb\.go|gpg|html|ico|jpg|js|json|xml|min\.css|min\.js|mp3|otf|png|svg|pdf|psd|tiff|ttf|woff|xcf|tar\.gz|gz|tar\.xz|tbz2|zip))$`) + rxMirrored = regexp.MustCompile(`^([a-zA-Z0-9\-\_]+\.(?:blobs|camli|css|eot|err|gif|go|s|pb\.go|gpg|html|ico|jpg|js|json|xml|min\.css|min\.js|mp3|otf|png|svg|pdf|psd|tiff|ttf|woff|xcf|tar\.gz|gz|tar\.xz|tbz2|zip|sh))$`) // base file exceptions for the above matching, so as not to complicate the regexp any further mirrorIgnored = map[string]bool{ - "gopherjs.js": true, // because this file is (re)generated after the mirroring + "publisher.js": true, // because this file is (re)generated after the mirroring } ) @@ -374,10 +374,44 @@ func moveGopherjs() error { return os.RemoveAll(src) } +// genSearchTypes duplicates some of the camlistore.org/pkg/search types into +// camlistore.org/app/publisher/js/zsearch.go , because it's too costly (in output +// file size) for now to import the search pkg into gopherjs. +func genSearchTypes() error { + sourceFile := filepath.Join(buildSrcDir, filepath.FromSlash("pkg/search/describe.go")) + outputFile := filepath.Join(buildSrcDir, filepath.FromSlash("app/publisher/js/zsearch.go")) + fi1, err := os.Stat(sourceFile) + if err != nil { + return err + } + fi2, err := os.Stat(outputFile) + if err != nil && !os.IsNotExist(err) { + return err + } + if err == nil && fi2.ModTime().After(fi1.ModTime()) { + wantDestFile[outputFile] = true + return nil + } + args := []string{"generate", "camlistore.org/app/publisher/js"} + cmd := exec.Command("go", args...) + cmd.Env = append(cleanGoEnv(), + "GOPATH="+buildGoPath, + ) + if out, err := cmd.CombinedOutput(); err != nil { + return fmt.Errorf("go generate for publisher js error: %v, %v", err, string(out)) + } + wantDestFile[outputFile] = true + log.Printf("generated %v", outputFile) + return nil +} + // genPublisherJS runs the gopherjs command, using the gopherjsBin binary, on // camlistore.org/app/publisher/js, to generate the javascript code at // app/publisher/publisher.js func genPublisherJS(gopherjsBin string) error { + if err := genSearchTypes(); err != nil { + return err + } // Run gopherjs on a temporary output file, so we don't change the // modtime of the existing gopherjs.js if there was no reason to. output := filepath.Join(buildSrcDir, filepath.FromSlash(publisherJS))