Skip to content

Commit

Permalink
Add pkg/analysis.
Browse files Browse the repository at this point in the history
It's a deprecated package that will be removed from upstream soon.
  • Loading branch information
dmitshur committed Nov 8, 2017
1 parent 2ea4ff8 commit 62348e5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 1 deletion.
2 changes: 1 addition & 1 deletion main.go
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/go-gl/mathgl/mgl64" "github.com/go-gl/mathgl/mgl64"
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
intmath "github.com/pkg/math" intmath "github.com/pkg/math"
"github.com/shurcooL-legacy/Conception-go/pkg/analysis"
"github.com/shurcooL-legacy/Conception-go/pkg/exp11" "github.com/shurcooL-legacy/Conception-go/pkg/exp11"
"github.com/shurcooL-legacy/Conception-go/pkg/exp12" "github.com/shurcooL-legacy/Conception-go/pkg/exp12"
"github.com/shurcooL-legacy/Conception-go/pkg/exp13" "github.com/shurcooL-legacy/Conception-go/pkg/exp13"
Expand All @@ -60,7 +61,6 @@ import (
"github.com/shurcooL/github_flavored_markdown/gfmstyle" "github.com/shurcooL/github_flavored_markdown/gfmstyle"
"github.com/shurcooL/go-goon" "github.com/shurcooL/go-goon"
"github.com/shurcooL/go-goon/bypass" "github.com/shurcooL/go-goon/bypass"
"github.com/shurcooL/go/analysis"
"github.com/shurcooL/go/gddo" "github.com/shurcooL/go/gddo"
"github.com/shurcooL/go/open" "github.com/shurcooL/go/open"
"github.com/shurcooL/go/pipeutil" "github.com/shurcooL/go/pipeutil"
Expand Down
51 changes: 51 additions & 0 deletions pkg/analysis/generated_detection.go
@@ -0,0 +1,51 @@
// Package analysis provides a routine that determines if a file is generated or handcrafted.
//
// Deprecated: Use github.com/shurcooL/go/generated package instead. This implementation
// was done ad-hoc before a standard was proposed.
package analysis

import (
"bufio"
"io"
"os"
"path/filepath"
"strings"
)

// IsFileGenerated returns true if the specified file is generated, or false if it's handcrafted.
// rootDir is the filepath of root directory, but name is a '/'-separated path to file.
//
// It considers vendored files as "generated", in the sense that they are not the canonical
// version of a file. This behavior would ideally be factored out into a higher level utility,
// since it has nothing to do with generated comments.
//
// Deprecated: Use generated.ParseFile instead, which is more well defined because it
// implements a specification.
func IsFileGenerated(rootDir, name string) (bool, error) {
// Detect from name.
switch {
case strings.HasPrefix(name, "vendor/") || strings.Contains(name, "/vendor/"):
return true, nil
case strings.HasPrefix(name, "Godeps/"):
return true, nil
}

// Detect from file contents.
f, err := os.Open(filepath.Join(rootDir, filepath.FromSlash(name)))
if err != nil {
return false, err
}
defer f.Close()
r := bufio.NewReader(f)
s, err := r.ReadString('\n')
if err == io.EOF {
// Empty file or exactly 1 line is not considered to be generated.
return false, nil
} else if err != nil {
return false, err
}
if strings.Contains(s, "Code generated by") { // Consistent with https://golang.org/cl/15073.
return true, nil
}
return (strings.Contains(s, "GENERATED") || strings.Contains(s, "generated")) && strings.Contains(s, "DO NOT EDIT"), nil
}
28 changes: 28 additions & 0 deletions pkg/analysis/generated_detection_test.go
@@ -0,0 +1,28 @@
package analysis_test

import (
"fmt"
"os"

"github.com/shurcooL-legacy/Conception-go/pkg/analysis"
)

func ExampleIsFileGenerated() {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}

fmt.Println(analysis.IsFileGenerated(cwd, "testdata/generated_0.go.txt"))
fmt.Println(analysis.IsFileGenerated(cwd, "testdata/handcrafted_0.go.txt"))
fmt.Println(analysis.IsFileGenerated(cwd, "testdata/handcrafted_1.go.txt"))
fmt.Println(analysis.IsFileGenerated(cwd, "vendor/github.com/shurcooL/go/analysis/file.go"))
fmt.Println(analysis.IsFileGenerated(cwd, "subpkg/vendor/math/math.go"))

// Output:
// true <nil>
// false <nil>
// false <nil>
// true <nil>
// true <nil>
}
32 changes: 32 additions & 0 deletions pkg/analysis/testdata/generated_0.go.txt
@@ -0,0 +1,32 @@
// generated by vfsgen; DO NOT EDIT

// +build !dev

package issues

import (
"bytes"
"compress/gzip"
"fmt"
"io"
"net/http"
"os"
pathpkg "path"
"time"
)

// Assets statically implements the virtual filesystem given to vfsgen as input.
var Assets = func() http.FileSystem {
mustUnmarshalTextTime := func(text string) time.Time {
var t time.Time
err := t.UnmarshalText([]byte(text))
if err != nil {
panic(err)
}
return t
}

fs := _vfsgen_fs{
...
}
}
9 changes: 9 additions & 0 deletions pkg/analysis/testdata/handcrafted_0.go.txt
@@ -0,0 +1,9 @@
// Package foo offers bar.
package foo

import "strings"

// Bar is bar.
func Bar() string {
return strings.Title("bar")
}
1 change: 1 addition & 0 deletions pkg/analysis/testdata/handcrafted_1.go.txt
@@ -0,0 +1 @@
// Code generated by protoc-gen-gogo. Actually it isn't, because it's only 1 line.

0 comments on commit 62348e5

Please sign in to comment.