Skip to content

Commit

Permalink
cli-branch-for-new-studio-frontend (#91)
Browse files Browse the repository at this point in the history
* check for duplicate names inside system, modules, etc...

* experimental ordered collection

* fix broken tests

* branch for new frontend
  • Loading branch information
jryannel authored Aug 8, 2023
1 parent 349544a commit c6939b9
Show file tree
Hide file tree
Showing 28 changed files with 373 additions and 90 deletions.
4 changes: 2 additions & 2 deletions data/spec/bad.module.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
schema: apigear.module/1.0
name: Hello
version: "1.0"
name: demo
version: 1.0.0
interfaces:
- name: Hello
properties:
Expand Down
79 changes: 62 additions & 17 deletions pkg/cfg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

func ConfigDir() string {
rw.RLock()
file := v.ConfigFileUsed()
rw.RUnlock()
return filepath.Dir(file)
}

Expand All @@ -27,8 +29,15 @@ func AppendRecentEntry(value string) error {
}
// prepend the new value
recent = append([]string{value}, recent...)

rw.Lock()
v.Set(KeyRecent, recent)
return v.WriteConfig()
err := v.WriteConfig()
rw.Unlock()
if err != nil {
return err
}
return nil
}

// RemoveRecentEntry removes a recent entry from the list
Expand All @@ -40,93 +49,129 @@ func RemoveRecentEntry(value string) error {
break
}
}
rw.Lock()
v.Set(KeyRecent, recent)
return v.WriteConfig()
err := v.WriteConfig()
rw.Unlock()
if err != nil {
return err
}
return nil
}

// RecentEntries returns the list of recent entries
func RecentEntries() []string {
rw.RLock()
items := v.GetStringSlice(KeyRecent)
rw.RUnlock()
if len(items) == 0 {
return []string{}
}
if len(items) > 5 {
return items[len(items)-5:]
}
return items
}

func SetBuildInfo(version, commit, date string) {
rw.Lock()
v.Set(KeyVersion, version)
v.Set(KeyCommit, commit)
v.Set(KeyDate, date)
err := v.WriteConfig()
rw.Unlock()
if err != nil {
log.Printf("error writing config: %v", err)
}
}

func IsSet(key string) bool {
return v.IsSet(key)
rw.RLock()
result := v.IsSet(key)
rw.RUnlock()
return result
}

func Set(key string, value any) {
rw.Lock()
v.Set(key, value)
rw.Unlock()
}

func Get(key string) any {
return v.Get(key)
rw.RLock()
result := v.Get(key)
rw.RUnlock()
return result
}

func GetString(key string) string {
return v.GetString(key)
rw.RLock()
result := v.GetString(key)
rw.RUnlock()
return result
}

func WriteConfig() error {
return v.WriteConfig()
rw.Lock()
err := v.WriteConfig()
rw.Unlock()
if err != nil {
return err
}
return nil
}

func EditorCommand() string {
return v.GetString(KeyEditorCommand)
return GetString(KeyEditorCommand)
}

func ServerPort() string {
return v.GetString(KeyServerPort)
return GetString(KeyServerPort)
}

func UpdateChannel() string {
return v.GetString(KeyUpdateChannel)
return GetString(KeyUpdateChannel)
}

func RegistryDir() string {
return v.GetString(KeyRegistryDir)
return GetString(KeyRegistryDir)
}

func RegistryCachePath() string {
return filepath.Join(RegistryDir(), "registry.json")
}

func AllSettings() map[string]interface{} {
return v.AllSettings()
rw.RLock()
result := v.AllSettings()
rw.RUnlock()
return result
}

func ConfigFileUsed() string {
return v.ConfigFileUsed()
rw.RLock()
result := v.ConfigFileUsed()
rw.RUnlock()
return result
}

func CacheDir() string {
return v.GetString(KeyCacheDir)
return GetString(KeyCacheDir)
}

func RegistryUrl() string {
return v.GetString(KeyRegistryUrl)
return GetString(KeyRegistryUrl)
}

func BuildVersion() string {
return v.GetString(KeyVersion)
return GetString(KeyVersion)
}

func BuildDate() string {
return v.GetString(KeyDate)
return GetString(KeyDate)
}

func BuildCommit() string {
return v.GetString(KeyCommit)
return GetString(KeyCommit)
}
8 changes: 7 additions & 1 deletion pkg/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cfg
import (
"fmt"
"os"
"sync"

"github.com/apigear-io/cli/pkg/helper"
"github.com/spf13/viper"
Expand All @@ -28,7 +29,8 @@ const (
)

var (
v *viper.Viper
v *viper.Viper
rw = sync.RWMutex{}
)

func init() {
Expand All @@ -43,7 +45,9 @@ func init() {
fmt.Println(err)
os.Exit(1)
}
rw.Lock()
v = vip
rw.Unlock()
}

func NewConfig(cfgDir string) (*viper.Viper, error) {
Expand Down Expand Up @@ -105,5 +109,7 @@ func NewConfig(cfgDir string) (*viper.Viper, error) {
}

func SetConfig(c *viper.Viper) {
rw.Lock()
v = c
rw.Unlock()
}
4 changes: 4 additions & 0 deletions pkg/helper/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func ReadYamlFromData(in []byte, out any) error {
return yaml.Unmarshal(in, out)
}

func ReadYamlFromString(in string, out any) error {
return yaml.Unmarshal([]byte(in), out)
}

func YamlToJson(in []byte) ([]byte, error) {
out := make(map[string]any)
err := yaml.Unmarshal(in, &out)
Expand Down
25 changes: 25 additions & 0 deletions pkg/idl/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package idl

import "github.com/apigear-io/cli/pkg/model"

func LoadIdlFromString(name string, content string) (*model.System, error) {
system := model.NewSystem(name)
parser := NewParser(system)
err := parser.ParseString(content)
if err != nil {
return nil, err
}
return system, nil
}

func LoadIdlFromFiles(name string, files []string) (*model.System, error) {
system := model.NewSystem(name)
for _, file := range files {
parser := NewParser(system)
err := parser.ParseFile(file)
if err != nil {
return nil, err
}
}
return system, nil
}
4 changes: 2 additions & 2 deletions pkg/idl/idl_advanced_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestManyParamsFuncs(t *testing.T) {
s, err := loadIdl("advanced", []string{"./testdata/advanced.idl"})
s, err := LoadIdlFromFiles("advanced", []string{"./testdata/advanced.idl"})
assert.NoError(t, err)
assert.NotNil(t, s)
table := []struct {
Expand Down Expand Up @@ -39,7 +39,7 @@ func TestManyParamsFuncs(t *testing.T) {
}

func TestManyParamsSigs(t *testing.T) {
s, err := loadIdl("advanced", []string{"./testdata/advanced.idl"})
s, err := LoadIdlFromFiles("advanced", []string{"./testdata/advanced.idl"})
assert.NoError(t, err)
assert.NotNil(t, s)
table := []struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/idl/idl_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestDataProps(t *testing.T) {
s, err := loadIdl("data", []string{"./testdata/data.idl"})
s, err := LoadIdlFromFiles("data", []string{"./testdata/data.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestDataProps(t *testing.T) {
}

func TestDataFuncs(t *testing.T) {
s, err := loadIdl("data", []string{"./testdata/data.idl"})
s, err := LoadIdlFromFiles("data", []string{"./testdata/data.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestDataFuncs(t *testing.T) {
}

func TestDataSignals(t *testing.T) {
s, err := loadIdl("data", []string{"./testdata/data.idl"})
s, err := LoadIdlFromFiles("data", []string{"./testdata/data.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestDataSignals(t *testing.T) {
}

func TestStructs(t *testing.T) {
s, err := loadIdl("structs", []string{"./testdata/data.idl"})
s, err := LoadIdlFromFiles("structs", []string{"./testdata/data.idl"})
assert.NoError(t, err)
table := []struct {
sName string
Expand Down
8 changes: 4 additions & 4 deletions pkg/idl/idl_enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func TestEnumIdl(t *testing.T) {
s, err := loadIdl("enum", []string{"./testdata/enum.idl"})
s, err := LoadIdlFromFiles("enum", []string{"./testdata/enum.idl"})
assert.NoError(t, err)
table := []struct {
eName string
Expand Down Expand Up @@ -40,7 +40,7 @@ func TestEnumIdl(t *testing.T) {
}

func TestEnumProps(t *testing.T) {
s, err := loadIdl("enum", []string{"./testdata/enum.idl"})
s, err := LoadIdlFromFiles("enum", []string{"./testdata/enum.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestEnumProps(t *testing.T) {
}

func TestEnumFuncs(t *testing.T) {
s, err := loadIdl("enum", []string{"./testdata/enum.idl"})
s, err := LoadIdlFromFiles("enum", []string{"./testdata/enum.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestEnumFuncs(t *testing.T) {
}

func TestEnumSignals(t *testing.T) {
s, err := loadIdl("enum", []string{"./testdata/enum.idl"})
s, err := LoadIdlFromFiles("enum", []string{"./testdata/enum.idl"})
assert.NoError(t, err)
table := []struct {
iName string
Expand Down
2 changes: 1 addition & 1 deletion pkg/idl/idl_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestManyModules(t *testing.T) {
s, err := loadIdl("many", []string{"./testdata/simple.idl", "./testdata/data.idl", "./testdata/enum.idl"})
s, err := LoadIdlFromFiles("many", []string{"./testdata/simple.idl", "./testdata/data.idl", "./testdata/enum.idl"})
assert.NoError(t, err)
assert.NotNil(t, s)
assert.Equal(t, "many", s.Name)
Expand Down
Loading

0 comments on commit c6939b9

Please sign in to comment.