Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 1a8b9b727 -> 0558d537a


newt - Allow syscfg.vals to be set; "target set"


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/0558d537
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/0558d537
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/0558d537

Branch: refs/heads/develop
Commit: 0558d537ac0b2089169d8fb7710f8a3fab8136c3
Parents: 823c05c
Author: Christopher Collins <ccoll...@apache.org>
Authored: Tue Oct 18 15:08:09 2016 -0700
Committer: Christopher Collins <ccoll...@apache.org>
Committed: Tue Oct 18 15:08:35 2016 -0700

----------------------------------------------------------------------
 newt/cli/target_cmds.go  | 57 ++++++++++++++++++++++++++++++++++++++++---
 newt/pkg/localpackage.go | 37 ++++++++++++++++++++++++++++
 newt/target/target.go    |  4 +++
 3 files changed, 94 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0558d537/newt/cli/target_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go
index e325713..4050639 100644
--- a/newt/cli/target_cmds.go
+++ b/newt/cli/target_cmds.go
@@ -36,6 +36,7 @@ import (
        "mynewt.apache.org/newt/newt/syscfg"
        "mynewt.apache.org/newt/newt/target"
        "mynewt.apache.org/newt/util"
+       "mynewt.apache.org/newt/viper"
 )
 
 var targetForce bool = false
@@ -122,8 +123,8 @@ func targetShowCmd(cmd *cobra.Command, args []string) {
                }
 
                // A few variables come from the base package rather than the 
target.
-               kvPairs["features"] = pkgVarSliceString(target.Package(),
-                       "pkg.features")
+               kvPairs["syscfg"] = targetSyscfgKVToStr(
+                       
target.Package().SyscfgV.GetStringMapString("syscfg.vals"))
                kvPairs["cflags"] = pkgVarSliceString(target.Package(), 
"pkg.cflags")
                kvPairs["lflags"] = pkgVarSliceString(target.Package(), 
"pkg.lflags")
                kvPairs["aflags"] = pkgVarSliceString(target.Package(), 
"pkg.aflags")
@@ -143,6 +144,51 @@ func targetShowCmd(cmd *cobra.Command, args []string) {
        }
 }
 
+func targetSyscfgKVFromStr(str string) map[string]string {
+       vals := map[string]string{}
+
+       if strings.TrimSpace(str) == "" {
+               return vals
+       }
+
+       // Separate syscfg vals are delimited by ':'.
+       fields := strings.Split(str, ":")
+
+       // Key-value pairs are delimited by '='.  If no '=' is present, assume 
the
+       // string is the key name and the value is 1.
+       for _, f := range fields {
+               kv := strings.SplitN(f, "=", 2)
+               switch len(kv) {
+               case 1:
+                       vals[f] = "1"
+               case 2:
+                       vals[kv[0]] = kv[1]
+               }
+       }
+
+       return vals
+}
+
+func targetSyscfgKVToStr(syscfgKv map[string]string) string {
+       str := ""
+
+       names := make([]string, 0, len(syscfgKv))
+       for k, _ := range syscfgKv {
+               names = append(names, k)
+       }
+       sort.Strings(names)
+
+       for i, name := range names {
+               if i != 0 {
+                       str += ":"
+               }
+
+               str += fmt.Sprintf("%s=%s", name, syscfgKv[name])
+       }
+
+       return str
+}
+
 func targetSetCmd(cmd *cobra.Command, args []string) {
        if len(args) < 2 {
                NewtUsage(cmd,
@@ -185,8 +231,11 @@ func targetSetCmd(cmd *cobra.Command, args []string) {
        for _, kv := range vars {
                // A few variables are special cases; they get set in the base 
package
                // instead of the target.
-               if kv[0] == "target.features" ||
-                       kv[0] == "target.cflags" ||
+               if kv[0] == "target.syscfg" {
+                       t.Package().SyscfgV = viper.New()
+                       kv := targetSyscfgKVFromStr(kv[1])
+                       t.Package().SyscfgV.Set("syscfg.vals", kv)
+               } else if kv[0] == "target.cflags" ||
                        kv[0] == "target.lflags" ||
                        kv[0] == "target.aflags" {
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0558d537/newt/pkg/localpackage.go
----------------------------------------------------------------------
diff --git a/newt/pkg/localpackage.go b/newt/pkg/localpackage.go
index 755a477..3e8e325 100644
--- a/newt/pkg/localpackage.go
+++ b/newt/pkg/localpackage.go
@@ -26,6 +26,7 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
+       "sort"
        "strings"
 
        log "github.com/Sirupsen/logrus"
@@ -231,6 +232,42 @@ func (pkg *LocalPackage) sequenceString(key string) string 
{
        }
 }
 
+func (lpkg *LocalPackage) SaveSyscfgVals() error {
+       dirpath := lpkg.BasePath()
+       if err := os.MkdirAll(dirpath, 0755); err != nil {
+               return util.NewNewtError(err.Error())
+       }
+
+       filepath := dirpath + "/" + SYSCFG_YAML_FILENAME
+
+       syscfgVals := lpkg.SyscfgV.GetStringMapString("syscfg.vals")
+       if syscfgVals == nil || len(syscfgVals) == 0 {
+               os.Remove(filepath)
+               return nil
+       }
+
+       file, err := os.Create(filepath)
+       if err != nil {
+               return util.NewNewtError(err.Error())
+       }
+       defer file.Close()
+
+       names := make([]string, 0, len(syscfgVals))
+       for k, _ := range syscfgVals {
+               names = append(names, k)
+       }
+       sort.Strings(names)
+
+       fmt.Fprintf(file, "### Package: %s\n", lpkg.Name())
+       fmt.Fprintf(file, "\n")
+       fmt.Fprintf(file, "syscfg.vals:\n")
+       for _, name := range names {
+               fmt.Fprintf(file, "    %s: %s\n", name, syscfgVals[name])
+       }
+
+       return nil
+}
+
 // Saves the package's pkg.yml file.
 // NOTE: This does not save every field in the package.  Only the fields
 // necessary for creating a new target get saved.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/0558d537/newt/target/target.go
----------------------------------------------------------------------
diff --git a/newt/target/target.go b/newt/target/target.go
index 00d3e72..26ca4cf 100644
--- a/newt/target/target.go
+++ b/newt/target/target.go
@@ -250,6 +250,10 @@ func (t *Target) Save() error {
                file.WriteString(k + ": " + yaml.EscapeString(t.Vars[k]) + "\n")
        }
 
+       if err := t.basePkg.SaveSyscfgVals(); err != nil {
+               return err
+       }
+
        return nil
 }
 

Reply via email to