Merge "make.go: generate search types for app/publisher"

This commit is contained in:
Mathieu Lonjaret 2016-07-18 23:01:41 +00:00 committed by Gerrit Code Review
commit ebc1fa12d7
4 changed files with 106 additions and 31 deletions

View File

@ -0,0 +1,49 @@
#!/bin/sh
outfile=zsearch.go
cat >$outfile <<EOF
// generated by gensearchtypes.sh; DO NOT EDIT
/*
Copyright 2016 The Camlistore Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"net/url"
"time"
"camlistore.org/pkg/blob"
"camlistore.org/pkg/types/camtypes"
)
// Duplicating the search pkg types in here - since we only use them for json
// decoding - , instead of importing them through the search package, which would
// bring in more dependencies, and hence a larger js file.
// To give an idea, the generated publisher.js is ~3.5MB, whereas if we instead import
// camlistore.org/pkg/search to use its types instead of the ones below, we grow to
// ~5.7MB.
EOF
go doc camlistore.org/pkg/search.SearchResult | sed '/^func.*/d' | sed '/^ .*/d' >> $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

View File

@ -1,3 +1,5 @@
//go:generate ./gensearchtypes.sh
/* /*
Copyright 2016 The Camlistore Authors. Copyright 2016 The Camlistore Authors.

View File

@ -1,3 +1,5 @@
// generated by gensearchtypes.sh; DO NOT EDIT
/* /*
Copyright 2016 The Camlistore Authors. Copyright 2016 The Camlistore Authors.
@ -17,7 +19,6 @@ limitations under the License.
package main package main
import ( import (
"fmt"
"net/url" "net/url"
"time" "time"
@ -32,10 +33,24 @@ import (
// camlistore.org/pkg/search to use its types instead of the ones below, we grow to // camlistore.org/pkg/search to use its types instead of the ones below, we grow to
// ~5.7MB. // ~5.7MB.
// TODO(mpl): keep these types in sync with camlistore.org/pkg/search. type SearchResult struct {
// Brad suggested using go:generate 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 MetaMap map[string]*DescribedBlob
type DescribedBlob struct { type DescribedBlob struct {
@ -66,28 +81,3 @@ type DescribedPermanode struct {
Attr url.Values `json:"attr"` // a map[string][]string Attr url.Values `json:"attr"` // a map[string][]string
ModTime time.Time `json:"modtime,omitempty"` 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"`
}

38
make.go
View File

@ -79,10 +79,10 @@ var (
// Our temporary source tree root and build dir, i.e: buildGoPath + "src/camlistore.org" // Our temporary source tree root and build dir, i.e: buildGoPath + "src/camlistore.org"
buildSrcDir string buildSrcDir string
// files mirrored from camRoot to buildSrcDir // 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 // base file exceptions for the above matching, so as not to complicate the regexp any further
mirrorIgnored = map[string]bool{ 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) 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 // genPublisherJS runs the gopherjs command, using the gopherjsBin binary, on
// camlistore.org/app/publisher/js, to generate the javascript code at // camlistore.org/app/publisher/js, to generate the javascript code at
// app/publisher/publisher.js // app/publisher/publisher.js
func genPublisherJS(gopherjsBin string) error { 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 // 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. // modtime of the existing gopherjs.js if there was no reason to.
output := filepath.Join(buildSrcDir, filepath.FromSlash(publisherJS)) output := filepath.Join(buildSrcDir, filepath.FromSlash(publisherJS))