Skip to content

Commit

Permalink
bali supprot toml format project metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
fcharlie committed Aug 4, 2020
1 parent fcedf8b commit fe5fc1a
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 82 deletions.
56 changes: 35 additions & 21 deletions cmd/bali/bali.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ import (
"path/filepath"

"github.com/balibuild/bali/base"
"github.com/pelletier/go-toml"
)

// Executable todo
type Executable struct {
Name string `json:"name"`
Destination string `json:"destination,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Links []string `json:"links,omitempty"` // create symlink
GoFlags []string `json:"goflags,omitempty"`
VersionInfo string `json:"versioninfo,omitempty"`
IconPath string `json:"icon,omitempty"`
Manifest string `json:"manifest,omitempty"`
Name string `json:"name" toml:"name"`
Destination string `json:"destination,omitempty" toml:"destination,omitempty"`
Description string `json:"description,omitempty" toml:"description,omitempty"`
Version string `json:"version,omitempty" toml:"version,omitempty"`
Links []string `json:"links,omitempty" toml:"links,omitempty"` // create symlink
GoFlags []string `json:"goflags,omitempty" toml:"goflags,omitempty"`
VersionInfo string `json:"versioninfo,omitempty" toml:"versioninfo,omitempty"`
IconPath string `json:"icon,omitempty" toml:"icon,omitempty"`
Manifest string `json:"manifest,omitempty" toml:"manifest,omitempty"`
}

// File todo
type File struct {
Path string `json:"path"`
Destination string `json:"destination"`
NewName string `json:"newname,omitempty"`
NoRename bool `json:"norename,omitempty"`
Executable bool `json:"executable,omitempty"`
Path string `json:"path" toml:"path"`
Destination string `json:"destination" toml:"destination"`
NewName string `json:"newname,omitempty" toml:"newname,omitempty"`
NoRename bool `json:"norename,omitempty" toml:"norename,omitempty"`
Executable bool `json:"executable,omitempty" toml:"executable,omitempty"`
}

// Base get BaliFile base
Expand Down Expand Up @@ -64,11 +65,11 @@ func (file *File) Configure(workdir, outdir string) error {

// Project todo
type Project struct {
Name string `json:"name"`
Version string `json:"version,omitempty"`
Files []File `json:"files,omitempty"`
Dirs []string `json:"dirs,omitempty"`
Respond string `json:"respond,omitempty"`
Name string `json:"name" toml:"name"`
Version string `json:"version,omitempty" toml:"version,omitempty"`
Files []File `json:"files,omitempty" toml:"files,omitempty"`
Dirs []string `json:"dirs,omitempty" toml:"dirs,omitempty"`
Respond string `json:"respond,omitempty" toml:"respond,omitempty"`
}

// FileConfigure todo
Expand All @@ -81,8 +82,8 @@ func (bm *Project) FileConfigure(workdir, outdir string) error {
return nil
}

// LoadMetadata todo
func LoadMetadata(file string, v interface{}) error {
// LoadJSONMetadata todo
func LoadJSONMetadata(file string, v interface{}) error {
fd, err := os.Open(file)
if err != nil {
return err
Expand All @@ -93,3 +94,16 @@ func LoadMetadata(file string, v interface{}) error {
}
return nil
}

// LoadTomlMetadata todo
func LoadTomlMetadata(file string, v interface{}) error {
fd, err := os.Open(file)
if err != nil {
return err
}
defer fd.Close()
if err := toml.NewDecoder(fd).Decode(v); err != nil {
return err
}
return nil
}
34 changes: 26 additions & 8 deletions cmd/bali/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,34 @@ func (exe *Executable) MakeLinks(destfile string, be *Executor) error {
return nil
}

func (be *Executor) loadExecutable(wd string) (*Executable, error) {
balisrc := filepath.Join(wd, "balisrc.toml")
if base.PathExists(balisrc) {
DbgPrint("%s support toml metadata", wd)
var exe Executable
if err := LoadTomlMetadata(balisrc, &exe); err != nil {
return nil, err
}
return &exe, nil
}
balisrc = filepath.Join(wd, "balisrc.json")
if base.PathExists(balisrc) {
DbgPrint("%s support json metadata", wd)
var exe Executable
if err := LoadJSONMetadata(balisrc, &exe); err != nil {
return nil, err
}
return &exe, nil
}
fmt.Fprintf(os.Stderr, "%s not found any balisrc.toml/balisrc.json\n", wd)
return nil, os.ErrNotExist
}

// Compile todo
func (be *Executor) Compile(wd string) error {
balisrc := filepath.Join(wd, "balisrc.json")
if !base.PathExists(balisrc) {
fmt.Fprintf(os.Stderr, "%s not exists\n", balisrc)
return os.ErrNotExist
}
var exe Executable
if err := LoadMetadata(balisrc, &exe); err != nil {
fmt.Fprintf(os.Stderr, "load %s error %s\n", balisrc, err)
exe, err := be.loadExecutable(wd)
if err != nil {
fmt.Fprintf(os.Stderr, "load balisrc from %s error %s\n", wd, err)
return err
}
if len(exe.Name) == 0 {
Expand Down
18 changes: 16 additions & 2 deletions cmd/bali/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,24 @@ func resolveDistSupport(target, arch string) bool {
return false
}

func (be *Executor) initializeProject() error {
balitoml := filepath.Join(be.workdir, "bali.toml")
if base.PathExists(balitoml) {
DbgPrint("%s support toml metadata", be.workdir)
return LoadTomlMetadata(balitoml, &be.bm)
}
balijson := filepath.Join(be.workdir, "bali.json")
if base.PathExists(balijson) {
DbgPrint("%s support json metadata", be.workdir)
return LoadJSONMetadata(balijson, &be.bm)
}
fmt.Fprintf(os.Stderr, "%s not found 'bali.toml(or bali.json)'\n", be.workdir)
return os.ErrNotExist
}

// Initialize todo
func (be *Executor) Initialize() error {
bali := filepath.Join(be.workdir, "bali.json")
if err := LoadMetadata(bali, &be.bm); err != nil {
if err := be.initializeProject(); err != nil {
return err
}
if len(be.bm.Version) == 0 {
Expand Down
51 changes: 0 additions & 51 deletions cmd/bali/toml.go

This file was deleted.

0 comments on commit fe5fc1a

Please sign in to comment.