Skip to content

Commit

Permalink
Merge pull request #34 from linquize/writego
Browse files Browse the repository at this point in the history
Add optional 'gofile' and 'gofilepackage' parameters to generate a go file that can be used into your code to access the version information.
  • Loading branch information
josephspurrier committed Jul 3, 2020
2 parents e59661e + 4e854d1 commit bc05c04
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 72 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
@@ -1,2 +1,8 @@
*.sh text eol=lf
*.hex binary
*.rc text eol=crlf
*.manifest text eol=crlf
*.ico binary
testdata/go/*.go text eol=lf
go.mod text eol=lf
go.sum text eol=lf
7 changes: 5 additions & 2 deletions README.md
@@ -1,6 +1,7 @@
GoVersionInfo
==========
[![Build Status](https://travis-ci.org/josephspurrier/goversioninfo.svg)](https://travis-ci.org/josephspurrier/goversioninfo) [![Coverage Status](https://coveralls.io/repos/josephspurrier/goversioninfo/badge.svg)](https://coveralls.io/r/josephspurrier/goversioninfo) [![GoDoc](https://godoc.org/github.com/josephspurrier/goversioninfo?status.svg)](https://godoc.org/github.com/josephspurrier/goversioninfo)

[![Go Report Card](https://goreportcard.com/badge/github.com/josephspurrier/goversioninfo)](https://goreportcard.com/report/github.com/josephspurrier/goversioninfo) [![Build Status](https://travis-ci.org/josephspurrier/goversioninfo.svg)](https://travis-ci.org/josephspurrier/goversioninfo) [![Coverage Status](https://coveralls.io/repos/josephspurrier/goversioninfo/badge.svg)](https://coveralls.io/r/josephspurrier/goversioninfo) [![GoDoc](https://godoc.org/github.com/josephspurrier/goversioninfo?status.svg)](https://godoc.org/github.com/josephspurrier/goversioninfo)

Microsoft Windows File Properties/Version Info and Icon Resource Generator for the Go Language

Expand Down Expand Up @@ -40,12 +41,14 @@ Complete list of the flags for goversioninfo:
-company="": StringFileInfo.CompanyName
-copyright="": StringFileInfo.LegalCopyright
-description="": StringFileInfo.FileDescription
-example=false: just dump out an example versioninfo.json to stdout
-example=false: dump out an example versioninfo.json to stdout
-file-version="": StringFileInfo.FileVersion
-icon="": icon file name
-internal-name="": StringFileInfo.InternalName
-manifest="": manifest file name
-o="resource.syso": output file name
-gofile="": Go output file name (optional) - generates a Go file to access version information internally
-gofilepackage="main": Go output package name (optional, requires parameter: 'gofile')
-platform-specific=false: output i386 and amd64 named resource.syso, ignores -o
-original-name="": StringFileInfo.OriginalFilename
-private-build="": StringFileInfo.PrivateBuild
Expand Down
9 changes: 8 additions & 1 deletion cmd/goversioninfo/main.go
Expand Up @@ -14,8 +14,10 @@ import (
)

func main() {
flagExample := flag.Bool("example", false, "just dump out an example versioninfo.json to stdout")
flagExample := flag.Bool("example", false, "dump out an example versioninfo.json to stdout")
flagOut := flag.String("o", "resource.syso", "output file name")
flagGo := flag.String("gofile", "", "Go output file name (optional)")
flagPackage := flag.String("gofilepackage", "main", "Go output package name (optional, requires parameter: 'gofile')")
flagPlatformSpecific := flag.Bool("platform-specific", false, "output i386 and amd64 named resource.syso, ignores -o")
flagIcon := flag.String("icon", "", "icon file name")
flagManifest := flag.String("manifest", "", "manifest file name")
Expand Down Expand Up @@ -173,6 +175,11 @@ func main() {
// Write the data to a buffer.
vi.Walk()

// If the flag is set, then generate the optional Go file.
if *flagGo != "" {
vi.WriteGo(*flagGo, *flagPackage)
}

// List of the architectures to output.
var archs []string

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Expand Up @@ -2,4 +2,7 @@ module github.com/josephspurrier/goversioninfo

go 1.12

require github.com/akavel/rsrc v0.8.0
require (
github.com/akavel/rsrc v0.8.0
github.com/stretchr/testify v1.6.1
)
10 changes: 10 additions & 0 deletions go.sum
@@ -1,2 +1,12 @@
github.com/akavel/rsrc v0.8.0 h1:zjWn7ukO9Kc5Q62DOJCcxGpXC18RawVtYAGdz2aLlfw=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
65 changes: 63 additions & 2 deletions goversioninfo.go
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"reflect"
"strconv"
"strings"

"github.com/akavel/rsrc/binutil"
"github.com/akavel/rsrc/coff"
Expand Down Expand Up @@ -160,7 +161,7 @@ func (t Translation) getTranslation() string {
// IO Methods
// *****************************************************************************

// Walk writes the data buffer with hexidecimal data from the structs
// Walk writes the data buffer with hexadecimal data from the structs
func (vi *VersionInfo) Walk() {
// Create a buffer
var b bytes.Buffer
Expand Down Expand Up @@ -192,7 +193,7 @@ func (vi *VersionInfo) WriteSyso(filename string, arch string) error {
// Create a new RSRC section
rsrc := coff.NewRSRC()

// Set the architechture
// Set the architecture
err := rsrc.Arch(arch)
if err != nil {
return err
Expand Down Expand Up @@ -232,6 +233,66 @@ func (vi *VersionInfo) WriteHex(filename string) error {
return ioutil.WriteFile(filename, vi.Buffer.Bytes(), 0655)
}

// WriteGo creates a Go file that contains the version info so you can access
// it in the application
func (vi *VersionInfo) WriteGo(filename, packageName string) error {
if len(packageName) == 0 {
packageName = "main"
}

out, err := os.Create(filename)
if err != nil {
return err
}

ffib, err := json.MarshalIndent(vi.FixedFileInfo, "\t", "\t")
if err != nil {
return err
}

sfib, err := json.MarshalIndent(vi.StringFileInfo, "\t", "\t")
if err != nil {
return err
}

vfib, err := json.MarshalIndent(vi.VarFileInfo, "\t", "\t")
if err != nil {
return err
}

replace := "`\" + \"`\" + \"`"
str := "`{\n\t"
str += `"FixedFileInfo":`
str += strings.Replace(string(ffib), "`", replace, -1)
str += ",\n\t"
str += `"StringFileInfo":`
str += strings.Replace(string(sfib), "`", replace, -1)
str += ",\n\t"
str += `"VarFileInfo":`
str += strings.Replace(string(vfib), "`", replace, -1)
str += "\n"
str += "}`"
fmt.Fprintf(out, `// Auto-generated file by goversioninfo. Do not edit.
package %v
import (
"encoding/json"
"github.com/josephspurrier/goversioninfo"
)
func unmarshalGoVersionInfo(b []byte) goversioninfo.VersionInfo {
vi := goversioninfo.VersionInfo{}
json.Unmarshal(b, &vi)
return vi
}
var versionInfo = unmarshalGoVersionInfo([]byte(%v))
`, packageName, string(str))

return nil
}

func writeCoff(coff *coff.Coff, fnameout string) error {
out, err := os.Create(fnameout)
if err != nil {
Expand Down

0 comments on commit bc05c04

Please sign in to comment.