detect that autogenpb has already been run and exit
This commit is contained in:
parent
d9e5edb3a8
commit
e07c6a35fd
3
Makefile
3
Makefile
|
@ -1,7 +1,7 @@
|
||||||
VERSION = $(shell git describe --tags)
|
VERSION = $(shell git describe --tags)
|
||||||
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
|
||||||
|
|
||||||
full: clean auto goimports vet build test
|
full: install clean auto goimports vet build test
|
||||||
@echo everything worked and the example ran
|
@echo everything worked and the example ran
|
||||||
|
|
||||||
test: goimports build test
|
test: goimports build test
|
||||||
|
@ -20,6 +20,7 @@ recover:
|
||||||
build:
|
build:
|
||||||
GO111MODULE=off go build \
|
GO111MODULE=off go build \
|
||||||
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
|
||||||
|
# autogen uses autogen to build. keep a working copy somewhere
|
||||||
cp -f autogenpb autogenpb.${BUILDTIME}
|
cp -f autogenpb autogenpb.${BUILDTIME}
|
||||||
|
|
||||||
bak:
|
bak:
|
||||||
|
|
56
addMutex.go
56
addMutex.go
|
@ -21,6 +21,14 @@ func (pb *Files) addMutex(f *File) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if autogenpb has already looked at this file
|
||||||
|
for _, line := range strings.Split(string(data), "\n") {
|
||||||
|
if strings.Contains(line, "autogenpb DO NOT EDIT") {
|
||||||
|
log.Info("autogenpb has already been run")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
w, _ := os.OpenFile(f.Pbfilename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
|
|
||||||
pbHeaderComment(w)
|
pbHeaderComment(w)
|
||||||
|
@ -34,30 +42,20 @@ func (pb *Files) addMutex(f *File) error {
|
||||||
// fmt.Fprintln(w, "package "+"main")
|
// fmt.Fprintln(w, "package "+"main")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var found bool
|
|
||||||
for _, msg := range f.MsgNames {
|
if f.structMatch(line) {
|
||||||
start := "type " + msg.Name + " struct {"
|
if argv.Mutex {
|
||||||
// marshalThing(w, msg.Name)
|
log.Info("Adding Mutex to:", line)
|
||||||
// log.Info("line:", line)
|
fmt.Fprintln(w, line)
|
||||||
if strings.HasPrefix(line, start) {
|
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
|
||||||
msg.MutexFound = true
|
fmt.Fprintln(w, "")
|
||||||
found = true
|
} else {
|
||||||
if argv.Mutex {
|
log.Info("Skipping. Mutex = false for:", line)
|
||||||
log.Info("Adding Mutex to line:", line)
|
fmt.Fprintln(w, line)
|
||||||
fmt.Fprintln(w, line)
|
fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
|
||||||
fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
|
fmt.Fprintln(w, "")
|
||||||
fmt.Fprintln(w, "")
|
|
||||||
} else {
|
|
||||||
log.Info("Skipping. Mutex = false for", msg.Name)
|
|
||||||
fmt.Fprintln(w, line)
|
|
||||||
fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg")
|
|
||||||
fmt.Fprintln(w, "")
|
|
||||||
}
|
|
||||||
// important to exit here. somehow this matched twice at one point. notsure how
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
if !found {
|
|
||||||
fmt.Fprintln(w, line)
|
fmt.Fprintln(w, line)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,3 +66,15 @@ func (pb *Files) addMutex(f *File) error {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// is this struct supposed to have a Mutex added?
|
||||||
|
func (pf *File) structMatch(line string) bool {
|
||||||
|
for _, msg := range pf.MsgNames {
|
||||||
|
start := "type " + msg.Name + " struct {"
|
||||||
|
if strings.HasPrefix(line, start) {
|
||||||
|
msg.MutexFound = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
|
@ -17,10 +17,12 @@ build:
|
||||||
GO111MODULE=off go build -v
|
GO111MODULE=off go build -v
|
||||||
|
|
||||||
withMutex:
|
withMutex:
|
||||||
../autogenpb --proto fruit.proto --package main --mutex
|
../autogenpb --proto fruit.proto --package main
|
||||||
|
../autogenpb --proto file.proto --package main
|
||||||
|
|
||||||
withoutMutex:
|
withoutMutex:
|
||||||
../autogenpb --proto fruit.proto --package main
|
../autogenpb --proto fruit.proto --package main --mutex=false
|
||||||
|
../autogenpb --proto file.proto --package main --mutex=false
|
||||||
|
|
||||||
goimports:
|
goimports:
|
||||||
goimports -w *.go
|
goimports -w *.go
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
syntax = "proto3";
|
syntax = "proto3";
|
||||||
|
|
||||||
|
// Look at "example/fruit.proto" not this file
|
||||||
|
|
||||||
|
// this file is actually used by autogenpb
|
||||||
|
|
||||||
// here are some docs, but probably it's just easier to run
|
// here are some docs, but probably it's just easier to run
|
||||||
// autogenpb on this file and see what gets autogenerated
|
// autogenpb on this file and see what gets autogenerated
|
||||||
// in this directory. All autogenerated files are named *.pb.go
|
// in this directory. All autogenerated files are named *.pb.go
|
||||||
|
@ -15,76 +19,43 @@ syntax = "proto3";
|
||||||
|
|
||||||
package main;
|
package main;
|
||||||
|
|
||||||
message Apple { // `autogenpb:marshal`
|
|
||||||
string name = 1; // `autogenpb:unique` // generates SortByxxx() and AppendUnique() functions
|
|
||||||
string genus = 2; // `autogenpb:unique` // generates same thing here but SortByGenus()
|
|
||||||
}
|
|
||||||
|
|
||||||
message Apples { // `autogenpb:marshal` `autogenpb:mutex`
|
|
||||||
string uuid = 1; // `autogenpb:default:b2a2de35-07b6-443b-8188-709e27bee8a7`
|
|
||||||
string version = 2; // `autogenpb:default:2`
|
|
||||||
repeated Apple apples = 3; // `autogenpb:sort` `autogenpb:unique`
|
|
||||||
repeated Pear pears = 4; // `autogenpb:sort` `autogenpb:unique`
|
|
||||||
repeated Pear more = 5; // `autogenpb:sort` `autogenpb:unique` // not supported. 'More' can only be the string 'Pears'
|
|
||||||
repeated string color = 6; // `autogenpb:sort` `autogenpb:unique`
|
|
||||||
}
|
|
||||||
|
|
||||||
message Pear {
|
|
||||||
string name = 1; //
|
|
||||||
string favorite = 2; // `autogenpb:sort`
|
|
||||||
}
|
|
||||||
|
|
||||||
// above is an example
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// below are the actual structs autogen uses
|
// below are the actual structs autogen uses
|
||||||
// autogen parses the .proto file and then store the information
|
// autogen parses the .proto file and then store the information
|
||||||
// it needs in these protobuf files, then it processes the
|
// it needs in these protobuf files, then it processes the
|
||||||
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
|
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
|
||||||
//
|
//
|
||||||
|
message MsgVar {
|
||||||
|
string varName = 1; // the variable name
|
||||||
|
string varType = 2; // the variable type
|
||||||
|
bool isRepeated = 3; // does the variable repeate
|
||||||
|
}
|
||||||
|
|
||||||
message MsgName {
|
message MsgName {
|
||||||
// If you have:
|
|
||||||
//
|
|
||||||
// "Shelf" for msgname
|
|
||||||
// "Books" for name
|
|
||||||
//
|
|
||||||
// Then in the proto file, that would mean it would look like:
|
|
||||||
//
|
|
||||||
// message Shelf {
|
|
||||||
// and then
|
|
||||||
// repeated string Books = 42;
|
|
||||||
//
|
|
||||||
// autogenpb will then generate sort functions for each 'name'
|
|
||||||
// things like:
|
|
||||||
//
|
|
||||||
// for _, b := range all.Book {
|
|
||||||
//
|
|
||||||
// and sort functions like:
|
|
||||||
//
|
|
||||||
// func (a ShelfBook) Less(i, j int) bool { return a[i].Book < a[j].Book }
|
|
||||||
//
|
|
||||||
|
|
||||||
string name = 1; // the name of the message aka struct. for this example: "Shelf"
|
string name = 1; // the name of the message aka struct. for this example: "Shelf"
|
||||||
bool marshal = 2; // if "Shelf" should have Marshal & Unmarshal functions
|
string lockname = 2; // name of the lockfile. ends in Mu
|
||||||
bool mutex = 3; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
|
bool doMarshal = 3; // if msg struct should have Marshal & Unmarshal functions
|
||||||
repeated string sort = 4; // "Book", "Picture", etc
|
bool doMutex = 4; // true if a mutex is needed for the message struct
|
||||||
repeated string aq = 5; // if the fields should have AppendUnique() functions
|
bool doProtocMutex = 5; // an experiment to insert a mutex into the protoc generated msg struct (bad idea?)
|
||||||
repeated string uniq = 6; // the non-repeating fields that should be unique
|
bool mutexFound = 6; // true if the mutex was added to the protoc pb.go file
|
||||||
|
repeated string sort = 7; // keys to sort on
|
||||||
|
repeated string unique = 8; // if the fields should have AppendUnique() functions
|
||||||
|
repeated MsgVar vars = 9; // store all the vars in the message
|
||||||
}
|
}
|
||||||
|
|
||||||
message Unique {
|
message File {
|
||||||
string name = 1; // the variable name of the repeatable struct that must be unique
|
string Package = 1; // whatever the package name is at the top of the .go file
|
||||||
string msgName = 2; // the struct that is repeated
|
string filename = 2; // yellow.proto
|
||||||
repeated string keys = 3; // the variables in that structure to check are unique
|
string pbfilename = 3; // yellow.pb.go
|
||||||
}
|
string filebase = 4; // yellow
|
||||||
|
string uuid = 5; // the uuid to use in a func NewMsgName()
|
||||||
|
string version = 6; // the version to use in a func NewMsgName()
|
||||||
|
MsgName bases = 7; // the message in "plural" form
|
||||||
|
MsgName base = 8; // the primary repeated message for the master struct
|
||||||
|
|
||||||
message File { // `autogenpb:nomarshal`
|
// every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
|
||||||
string name = 1; // for this one: autogen.proto
|
repeated MsgName msgNames = 9;
|
||||||
string uuid = 2; // the uuid to use in a func NewMsgName()
|
repeated MsgName sortNames = 10; // variables that are repeated can have the standard functions generated (Sort(), etc)
|
||||||
int64 version = 3; // the version to use in a func NewMsgName()
|
|
||||||
|
|
||||||
// in this proto file, this would have "Apple", "Apples", ... "File", etc...
|
|
||||||
repeated MsgName msgNames = 4; // `autogenpb:unique` // in this file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// I know, I know, the whole point of using protobuf
|
// I know, I know, the whole point of using protobuf
|
||||||
|
@ -94,8 +65,8 @@ message File { // `autogenpb:nomarshal`
|
||||||
// also, this could be used to modify /usr/bin/file /usr/share/magic to identify the files
|
// also, this could be used to modify /usr/bin/file /usr/share/magic to identify the files
|
||||||
// maybe this is already been done and is pointless, but it seems like a good idea
|
// maybe this is already been done and is pointless, but it seems like a good idea
|
||||||
message Files { // `autogenpb:marshal`
|
message Files { // `autogenpb:marshal`
|
||||||
string uuid = 1; // `autogenpb:uuid:fakeuuid`
|
string uuid = 1; // `autogenpb:uuid:6c9ae4dd-648d-4b51-9738-bd59fb8fafd5`
|
||||||
string version = 2; // `autogenpb:id:42`
|
string version = 2; // `autogenpb:version:v0.0.38`
|
||||||
repeated File Files = 3; // an array of each .proto file in the working directory
|
repeated File Files = 3; // an array of each .proto file in the working directory
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,12 @@ package main;
|
||||||
// it needs in these protobuf files, then it processes the
|
// it needs in these protobuf files, then it processes the
|
||||||
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
|
// protobuf files to write out *.sort.pb.go and *.marshal.pb.go files
|
||||||
//
|
//
|
||||||
|
message MsgVar {
|
||||||
|
string varName = 1; // the variable name
|
||||||
|
string varType = 2; // the variable type
|
||||||
|
bool isRepeated = 3; // does the variable repeate
|
||||||
|
}
|
||||||
|
|
||||||
message MsgName {
|
message MsgName {
|
||||||
string name = 1; // the name of the message aka struct. for this example: "Shelf"
|
string name = 1; // the name of the message aka struct. for this example: "Shelf"
|
||||||
string lockname = 2; // name of the lockfile. ends in Mu
|
string lockname = 2; // name of the lockfile. ends in Mu
|
||||||
|
@ -34,6 +40,7 @@ message MsgName {
|
||||||
bool mutexFound = 6; // true if the mutex was added to the protoc pb.go file
|
bool mutexFound = 6; // true if the mutex was added to the protoc pb.go file
|
||||||
repeated string sort = 7; // keys to sort on
|
repeated string sort = 7; // keys to sort on
|
||||||
repeated string unique = 8; // if the fields should have AppendUnique() functions
|
repeated string unique = 8; // if the fields should have AppendUnique() functions
|
||||||
|
repeated MsgVar vars = 9; // store all the vars in the message
|
||||||
}
|
}
|
||||||
|
|
||||||
message File {
|
message File {
|
||||||
|
@ -48,6 +55,7 @@ message File {
|
||||||
|
|
||||||
// every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
|
// every struct in this proto file, this file has: "Apple", "Apples", ... "File", etc...
|
||||||
repeated MsgName msgNames = 9;
|
repeated MsgName msgNames = 9;
|
||||||
|
repeated MsgName sortNames = 10; // variables that are repeated can have the standard functions generated (Sort(), etc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// I know, I know, the whole point of using protobuf
|
// I know, I know, the whole point of using protobuf
|
||||||
|
|
Loading…
Reference in New Issue