You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2017/03/28 22:41:46 UTC
[49/67] [abbrv] [partial] incubator-mynewt-newtmgr git commit:
newtmgr - Remove newt code
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/pkg_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/pkg_cmds.go b/newt/cli/pkg_cmds.go
deleted file mode 100644
index 472369b..0000000
--- a/newt/cli/pkg_cmds.go
+++ /dev/null
@@ -1,288 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "io/ioutil"
- "os"
- "path"
- "regexp"
- "strings"
-
- "github.com/spf13/cobra"
- "mynewt.apache.org/newt/newt/interfaces"
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/newt/pkg"
- "mynewt.apache.org/newt/newt/project"
- "mynewt.apache.org/newt/util"
-)
-
-var NewTypeStr = "pkg"
-
-func pkgNewCmd(cmd *cobra.Command, args []string) {
-
- if len(args) == 0 {
- NewtUsage(cmd, util.NewNewtError("Must specify a package name"))
- }
-
- if len(args) != 1 {
- NewtUsage(cmd, util.NewNewtError("Exactly one argument required"))
- }
-
- NewTypeStr = strings.ToUpper(NewTypeStr)
-
- pw := project.NewPackageWriter()
- if err := pw.ConfigurePackage(NewTypeStr, args[0]); err != nil {
- NewtUsage(cmd, err)
- }
- if err := pw.WritePackage(); err != nil {
- NewtUsage(cmd, err)
- }
-}
-
-type dirOperation func(string, string) error
-
-func pkgCopyCmd(cmd *cobra.Command, args []string) {
- pkgCloneOrMoveCmd(cmd, args, util.CopyDir, "Copying")
-}
-
-func pkgMoveCmd(cmd *cobra.Command, args []string) {
- pkgCloneOrMoveCmd(cmd, args, util.MoveDir, "Moving")
-}
-
-func pkgCloneOrMoveCmd(cmd *cobra.Command, args []string, dirOpFn dirOperation, opStr string) {
- if len(args) != 2 {
- NewtUsage(cmd, util.NewNewtError("Exactly two arguments required to pkg move"))
- }
-
- srcLoc := args[0]
- dstLoc := args[1]
-
- proj := TryGetProject()
- interfaces.SetProject(proj)
-
- wd, err := os.Getwd()
- if err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-
- if err := os.Chdir(proj.Path() + "/"); err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-
- defer os.Chdir(wd)
-
- /* Find source package, defaulting search to the local project if no
- * repository descriptor is found.
- */
- srcRepoName, srcName, err := newtutil.ParsePackageString(srcLoc)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- srcRepo := proj.LocalRepo()
- if srcRepoName != "" {
- srcRepo = proj.FindRepo(srcRepoName)
- }
-
- srcPkg, err := proj.ResolvePackage(srcRepo, srcName)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- /* Resolve the destination package to a physical location, and then
- * move the source package to that location.
- * dstLoc is assumed to be in the format "@repo/pkg/loc"
- */
- repoName, pkgName, err := newtutil.ParsePackageString(dstLoc)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- dstPath := proj.Path() + "/"
- repo := proj.LocalRepo()
- if repoName != "" {
- dstPath += "repos/" + repoName + "/"
- repo = proj.FindRepo(repoName)
- if repo == nil {
- NewtUsage(cmd, util.NewNewtError("Destination repo "+
- repoName+" does not exist"))
- }
- }
- dstPath += pkgName + "/"
-
- if util.NodeExist(dstPath) {
- NewtUsage(cmd, util.NewNewtError("Cannot overwrite existing package, "+
- "use pkg delete first"))
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "%s package %s to %s\n",
- opStr, srcLoc, dstLoc)
-
- if err := dirOpFn(srcPkg.BasePath(), dstPath); err != nil {
- NewtUsage(cmd, err)
- }
-
- /* Replace the package name in the pkg.yml file */
- pkgData, err := ioutil.ReadFile(dstPath + "/pkg.yml")
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- re := regexp.MustCompile(regexp.QuoteMeta(srcName))
- res := re.ReplaceAllString(string(pkgData), pkgName)
-
- if err := ioutil.WriteFile(dstPath+"/pkg.yml", []byte(res), 0666); err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-
- /* If the last element of the package path changes, rename the include
- * directory.
- */
- if path.Base(pkgName) != path.Base(srcPkg.Name()) {
- dirOpFn(dstPath+"/include/"+path.Base(srcPkg.Name()),
- dstPath+"/include/"+path.Base(pkgName))
- }
-}
-
-func pkgRemoveCmd(cmd *cobra.Command, args []string) {
- if len(args) != 1 {
- NewtUsage(cmd, util.NewNewtError("Must specify a package name to delete"))
- }
-
- proj := TryGetProject()
- interfaces.SetProject(proj)
-
- wd, err := os.Getwd()
- if err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-
- if err := os.Chdir(proj.Path() + "/"); err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-
- defer os.Chdir(wd)
-
- /* Resolve package, and get path from package to ensure we're being asked
- * to remove a valid path.
- */
- repoName, pkgName, err := newtutil.ParsePackageString(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- repo := proj.LocalRepo()
- if repoName != "" {
- repo = proj.FindRepo(repoName)
- if repo == nil {
- NewtUsage(cmd, util.NewNewtError("Destination repo "+
- repoName+" does not exist"))
- }
- }
-
- pkg, err := pkg.LoadLocalPackage(repo, pkgName)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Removing package %s\n",
- args[0])
-
- if err := os.RemoveAll(pkg.BasePath()); err != nil {
- NewtUsage(cmd, util.ChildNewtError(err))
- }
-}
-
-func AddPackageCommands(cmd *cobra.Command) {
- /* Add the base package command, on top of which other commands are
- * keyed
- */
- pkgHelpText := "Commands for creating and manipulating packages"
- pkgHelpEx := " newt pkg new --type=pkg sys/mylib"
-
- pkgCmd := &cobra.Command{
- Use: "pkg",
- Short: "Create and manage packages in the current workspace",
- Long: pkgHelpText,
- Example: pkgHelpEx,
- Run: func(cmd *cobra.Command, args []string) {
- cmd.Help()
- },
- }
-
- cmd.AddCommand(pkgCmd)
-
- /* Package new command, create a new package */
- newCmdHelpText := ""
- newCmdHelpEx := ""
-
- newCmd := &cobra.Command{
- Use: "new <package-name>",
- Short: "Create a new package in the current directory, from a template",
- Long: newCmdHelpText,
- Example: newCmdHelpEx,
- Run: pkgNewCmd,
- }
-
- newCmd.PersistentFlags().StringVarP(&NewTypeStr, "type", "t",
- "pkg", "Type of package to create: pkg, bsp, sdk.")
-
- pkgCmd.AddCommand(newCmd)
-
- copyCmdHelpText := "Create a new package <dst-pkg> by cloning <src-pkg>"
- copyCmdHelpEx := " newt pkg copy apps/blinky apps/myapp"
-
- copyCmd := &cobra.Command{
- Use: "copy <src-pkg> <dst-pkg>",
- Short: "Copy an existing package into another",
- Long: copyCmdHelpText,
- Example: copyCmdHelpEx,
- Run: pkgCopyCmd,
- }
-
- pkgCmd.AddCommand(copyCmd)
-
- moveCmdHelpText := ""
- moveCmdHelpEx := " newt pkg move apps/blinky apps/myapp"
-
- moveCmd := &cobra.Command{
- Use: "move <oldpkg> <newpkg>",
- Short: "Move a package from one location to another",
- Long: moveCmdHelpText,
- Example: moveCmdHelpEx,
- Run: pkgMoveCmd,
- }
-
- pkgCmd.AddCommand(moveCmd)
-
- removeCmdHelpText := ""
- removeCmdHelpEx := ""
-
- removeCmd := &cobra.Command{
- Use: "remove <package-name>",
- Short: "Remove a package",
- Long: removeCmdHelpText,
- Example: removeCmdHelpEx,
- Run: pkgRemoveCmd,
- }
-
- pkgCmd.AddCommand(removeCmd)
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/project_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/project_cmds.go b/newt/cli/project_cmds.go
deleted file mode 100644
index 89a6eae..0000000
--- a/newt/cli/project_cmds.go
+++ /dev/null
@@ -1,259 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "fmt"
- "os"
- "sort"
- "strings"
-
- "github.com/spf13/cobra"
- "mynewt.apache.org/newt/newt/downloader"
- "mynewt.apache.org/newt/newt/interfaces"
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/newt/project"
- "mynewt.apache.org/newt/util"
-)
-
-func newRunCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd, util.NewNewtError("Must specify "+
- "a project directory to newt new"))
- }
-
- newDir := args[0]
-
- if util.NodeExist(newDir) {
- NewtUsage(cmd, util.NewNewtError("Cannot create new project, "+
- "directory already exists"))
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Downloading "+
- "project skeleton from apache/incubator-mynewt-blinky...\n")
- dl := downloader.NewGithubDownloader()
- dl.User = "apache"
- dl.Repo = "incubator-mynewt-blinky"
-
- dir, err := dl.DownloadRepo(newtutil.NewtBlinkyTag)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Installing "+
- "skeleton in %s...\n", newDir)
-
- if err := util.CopyDir(dir, newDir); err != nil {
- NewtUsage(cmd, err)
- }
-
- if err := os.RemoveAll(newDir + "/" + "/.git/"); err != nil {
- NewtUsage(cmd, err)
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Project %s successfully created.\n", newDir)
-}
-
-func installRunCmd(cmd *cobra.Command, args []string) {
- proj := TryGetProject()
- interfaces.SetProject(proj)
-
- if err := proj.Install(false, newtutil.NewtForce); err != nil {
- NewtUsage(cmd, err)
- }
-}
-
-func upgradeRunCmd(cmd *cobra.Command, args []string) {
- proj := TryGetProject()
- interfaces.SetProject(proj)
-
- if err := proj.Upgrade(newtutil.NewtForce); err != nil {
- NewtUsage(cmd, err)
- }
-}
-
-func infoRunCmd(cmd *cobra.Command, args []string) {
- reqRepoName := ""
- if len(args) >= 1 {
- reqRepoName = strings.TrimPrefix(args[0], "@")
- }
-
- proj := TryGetProject()
-
- repoNames := []string{}
- for repoName, _ := range proj.PackageList() {
- repoNames = append(repoNames, repoName)
- }
- sort.Strings(repoNames)
-
- if reqRepoName == "" {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Repositories in %s:\n",
- proj.Name())
-
- for _, repoName := range repoNames {
- util.StatusMessage(util.VERBOSITY_DEFAULT, " * @%s\n", repoName)
- }
-
- // Now display the packages in the local repository.
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
- reqRepoName = "local"
- }
-
- firstRepo := true
- for _, repoName := range repoNames {
- if reqRepoName == "all" || reqRepoName == repoName {
- packNames := []string{}
- for _, pack := range *proj.PackageList()[repoName] {
- // Don't display the special unittest target; this is used
- // internally by newt, so the user doesn't need to know about
- // it.
- // XXX: This is a hack; come up with a better solution for
- // unit testing.
- if !strings.HasSuffix(pack.Name(), "/unittest") {
- packNames = append(packNames, pack.Name())
- }
- }
-
- sort.Strings(packNames)
- if !firstRepo {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
- } else {
- firstRepo = false
- }
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Packages in @%s:\n",
- repoName)
- for _, pkgName := range packNames {
- util.StatusMessage(util.VERBOSITY_DEFAULT, " * %s\n",
- pkgName)
- }
- }
- }
-}
-
-func syncRunCmd(cmd *cobra.Command, args []string) {
- proj := TryGetProject()
- repos := proj.Repos()
-
- ps, err := project.LoadProjectState()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- var failedRepos []string
- for _, repo := range repos {
- var exists bool
- var updated bool
- if repo.IsLocal() {
- continue
- }
- vers := ps.GetInstalledVersion(repo.Name())
- if vers == nil {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "No installed version of %s found, skipping\n\n",
- repo.Name())
- }
- exists, updated, err = repo.Sync(vers, newtutil.NewtForce)
- if exists && !updated {
- failedRepos = append(failedRepos, repo.Name())
- }
- }
- if len(failedRepos) > 0 {
- var forceMsg string
- if !newtutil.NewtForce {
- forceMsg = " To force resync, add the -f (force) option."
- }
- err = util.NewNewtError(fmt.Sprintf("Failed for repos: %v."+
- forceMsg, failedRepos))
- NewtUsage(nil, err)
- }
-}
-
-func AddProjectCommands(cmd *cobra.Command) {
- installHelpText := ""
- installHelpEx := ""
- installCmd := &cobra.Command{
- Use: "install",
- Short: "Install project dependencies",
- Long: installHelpText,
- Example: installHelpEx,
- Run: installRunCmd,
- }
- installCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Force install of the repositories in project, regardless of what "+
- "exists in repos directory")
-
- cmd.AddCommand(installCmd)
-
- upgradeHelpText := ""
- upgradeHelpEx := ""
- upgradeCmd := &cobra.Command{
- Use: "upgrade",
- Short: "Upgrade project dependencies",
- Long: upgradeHelpText,
- Example: upgradeHelpEx,
- Run: upgradeRunCmd,
- }
- upgradeCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Force upgrade of the repositories to latest state in project.yml")
-
- cmd.AddCommand(upgradeCmd)
-
- syncHelpText := ""
- syncHelpEx := ""
- syncCmd := &cobra.Command{
- Use: "sync",
- Short: "Synchronize project dependencies",
- Long: syncHelpText,
- Example: syncHelpEx,
- Run: syncRunCmd,
- }
- syncCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Force overwrite of existing remote repositories.")
- cmd.AddCommand(syncCmd)
-
- newHelpText := ""
- newHelpEx := ""
- newCmd := &cobra.Command{
- Use: "new <project-dir>",
- Short: "Create a new project",
- Long: newHelpText,
- Example: newHelpEx,
- Run: newRunCmd,
- }
-
- cmd.AddCommand(newCmd)
-
- infoHelpText := "Show information about the current project."
- infoHelpEx := " newt info\n"
-
- infoCmd := &cobra.Command{
- Use: "info",
- Short: "Show project info",
- Long: infoHelpText,
- Example: infoHelpEx,
- Run: infoRunCmd,
- }
-
- cmd.AddCommand(infoCmd)
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/run_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/run_cmds.go b/newt/cli/run_cmds.go
deleted file mode 100644
index 16620dd..0000000
--- a/newt/cli/run_cmds.go
+++ /dev/null
@@ -1,115 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "os"
-
- "github.com/spf13/cobra"
-
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/util"
-)
-
-func runRunCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd, util.NewNewtError("Must specify target"))
- }
-
- TryGetProject()
-
- b, err := TargetBuilderForTargetOrUnittest(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- testPkg := b.GetTestPkg()
- if testPkg != nil {
- b.InjectSetting("TESTUTIL_SYSTEM_ASSERT", "1")
- if err := b.SelfTestCreateExe(); err != nil {
- NewtUsage(nil, err)
- }
- if err := b.SelfTestDebug(); err != nil {
- NewtUsage(nil, err)
- }
- } else {
- if err := b.Build(); err != nil {
- NewtUsage(nil, err)
- }
-
- /*
- * Run create-image if version number is specified. If no version
- * number, remove .img which would'be been created. This so that
- * download script will barf if it needs an image for this type of
- * target, instead of downloading an older version.
- */
- if len(args) > 1 {
- _, _, err = b.CreateImages(args[1], "", 0)
- if err != nil {
- NewtUsage(cmd, err)
- }
- } else {
- os.Remove(b.AppBuilder.AppImgPath())
-
- if b.LoaderBuilder != nil {
- os.Remove(b.LoaderBuilder.AppImgPath())
- }
- }
-
- if err := b.Load(extraJtagCmd); err != nil {
- NewtUsage(nil, err)
- }
-
- if err := b.Debug(extraJtagCmd, true, noGDB_flag); err != nil {
- NewtUsage(nil, err)
- }
- }
-}
-
-func AddRunCommands(cmd *cobra.Command) {
- runHelpText := "Same as running\n" +
- " - build <target>\n" +
- " - create-image <target> <version>\n" +
- " - load <target>\n" +
- " - debug <target>\n\n" +
- "Note if version number is omitted, create-image step is skipped\n"
- runHelpEx := " newt run <target-name> [<version>]\n"
-
- runCmd := &cobra.Command{
- Use: "run",
- Short: "build/create-image/download/debug <target>",
- Long: runHelpText,
- Example: runHelpEx,
- Run: runRunCmd,
- }
-
- runCmd.PersistentFlags().StringVarP(&extraJtagCmd, "extrajtagcmd", "", "",
- "Extra commands to send to JTAG software")
- runCmd.PersistentFlags().BoolVarP(&noGDB_flag, "noGDB", "n", false,
- "Do not start GDB from command line")
- runCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Ignore flash overflow errors during image creation")
-
- cmd.AddCommand(runCmd)
- AddTabCompleteFn(runCmd, func() []string {
- return append(targetList(), unittestList()...)
- })
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/target_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/target_cmds.go b/newt/cli/target_cmds.go
deleted file mode 100644
index 786838c..0000000
--- a/newt/cli/target_cmds.go
+++ /dev/null
@@ -1,809 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "sort"
- "strings"
-
- log "github.com/Sirupsen/logrus"
- "github.com/spf13/cobra"
- "mynewt.apache.org/newt/newt/builder"
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/newt/pkg"
- "mynewt.apache.org/newt/newt/resolve"
- "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
-
-func resolveExistingTargetArg(arg string) (*target.Target, error) {
- t := ResolveTarget(arg)
- if t == nil {
- return nil, util.NewNewtError("Unknown target: " + arg)
- }
-
- return t, nil
-}
-
-// Tells you if a target's directory contains extra user files (i.e., files
-// other than pkg.yml).
-func targetContainsUserFiles(t *target.Target) (bool, error) {
- contents, err := ioutil.ReadDir(t.Package().BasePath())
- if err != nil {
- return false, err
- }
-
- userFiles := false
- for _, node := range contents {
- name := node.Name()
- if name != "." && name != ".." &&
- name != pkg.PACKAGE_FILE_NAME && name != target.TARGET_FILENAME {
-
- userFiles = true
- break
- }
- }
-
- return userFiles, nil
-}
-
-func pkgVarSliceString(pack *pkg.LocalPackage, key string) string {
- features := pack.PkgV.GetStringSlice(key)
- sort.Strings(features)
-
- var buffer bytes.Buffer
- for _, f := range features {
- buffer.WriteString(f)
- buffer.WriteString(" ")
- }
- return buffer.String()
-}
-
-func targetShowCmd(cmd *cobra.Command, args []string) {
- TryGetProject()
- targetNames := []string{}
- if len(args) == 0 {
- for name, _ := range target.GetTargets() {
- // Don't display the special unittest target; this is used
- // internally by newt, so the user doesn't need to know about it.
- // XXX: This is a hack; come up with a better solution for unit
- // testing.
- if !strings.HasSuffix(name, "/unittest") {
- targetNames = append(targetNames, name)
- }
- }
- } else {
- targetSlice, err := ResolveTargets(args...)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- for _, t := range targetSlice {
- targetNames = append(targetNames, t.FullName())
- }
- }
-
- sort.Strings(targetNames)
-
- for _, name := range targetNames {
- kvPairs := map[string]string{}
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, name+"\n")
-
- target := target.GetTargets()[name]
- for k, v := range target.Vars {
- kvPairs[strings.TrimPrefix(k, "target.")] = v
- }
-
- // A few variables come from the base package rather than the target.
- kvPairs["syscfg"] = syscfg.KeyValueToStr(
- 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")
-
- keys := []string{}
- for k, _ := range kvPairs {
- keys = append(keys, k)
- }
- sort.Strings(keys)
- for _, k := range keys {
- val := kvPairs[k]
- if len(val) > 0 {
- util.StatusMessage(util.VERBOSITY_DEFAULT, " %s=%s\n",
- k, kvPairs[k])
- }
- }
- }
-}
-
-func targetSetCmd(cmd *cobra.Command, args []string) {
- if len(args) < 2 {
- NewtUsage(cmd,
- util.NewNewtError("Must specify at least two arguments "+
- "(target-name & k=v) to set"))
- }
-
- TryGetProject()
-
- // Parse target name.
- t, err := resolveExistingTargetArg(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- // Parse series of k=v pairs. If an argument doesn't contain a '='
- // character, display the valid values for the variable and quit.
- vars := [][]string{}
- for i := 1; i < len(args); i++ {
- kv := strings.SplitN(args[i], "=", 2)
- if !strings.HasPrefix(kv[0], "target.") {
- kv[0] = "target." + kv[0]
- }
-
- if len(kv) == 1 {
- // User entered a variable name without a value.
- NewtUsage(cmd, nil)
- }
-
- // Trim trailing slash from value. This is necessary when tab
- // completion is used to fill in the value.
- kv[1] = strings.TrimSuffix(kv[1], "/")
-
- vars = append(vars, kv)
- }
-
- // Set each specified variable in the target.
- 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.syscfg" {
- t.Package().SyscfgV = viper.New()
- kv, err := syscfg.KeyValueFromStr(kv[1])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- t.Package().SyscfgV.Set("syscfg.vals", kv)
- } else if kv[0] == "target.cflags" ||
- kv[0] == "target.lflags" ||
- kv[0] == "target.aflags" {
-
- kv[0] = "pkg." + strings.TrimPrefix(kv[0], "target.")
- if kv[1] == "" {
- // User specified empty value; delete variable.
- t.Package().PkgV.Set(kv[0], nil)
- } else {
- t.Package().PkgV.Set(kv[0], strings.Fields(kv[1]))
- }
- } else {
- if kv[1] == "" {
- // User specified empty value; delete variable.
- delete(t.Vars, kv[0])
- } else {
- // Assign value to specified variable.
- t.Vars[kv[0]] = kv[1]
- }
- }
- }
-
- if err := t.Save(); err != nil {
- NewtUsage(cmd, err)
- }
-
- for _, kv := range vars {
- if kv[1] == "" {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Target %s successfully unset %s\n", t.FullName(), kv[0])
- } else {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Target %s successfully set %s to %s\n", t.FullName(), kv[0],
- kv[1])
- }
- }
-}
-
-func targetCreateCmd(cmd *cobra.Command, args []string) {
- if len(args) != 1 {
- NewtUsage(cmd, util.NewNewtError("Missing target name"))
- }
-
- proj := TryGetProject()
-
- pkgName, err := ResolveNewTargetName(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- repo := proj.LocalRepo()
- pack := pkg.NewLocalPackage(repo, repo.Path()+"/"+pkgName)
- pack.SetName(pkgName)
- pack.SetType(pkg.PACKAGE_TYPE_TARGET)
-
- t := target.NewTarget(pack)
- err = t.Save()
- if err != nil {
- NewtUsage(nil, err)
- } else {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Target %s successfully created\n", pkgName)
- }
-}
-
-func targetDelOne(t *target.Target) error {
- if !targetForce {
- // Determine if the target directory contains extra user files. If it
- // does, a prompt (or force) is required to delete it.
- userFiles, err := targetContainsUserFiles(t)
- if err != nil {
- return err
- }
-
- if userFiles {
- fmt.Printf("Target directory %s contains some extra content; "+
- "delete anyway? (y/N): ", t.Package().BasePath())
- rsp := PromptYesNo(false)
- if !rsp {
- return nil
- }
- }
- }
-
- if err := os.RemoveAll(t.Package().BasePath()); err != nil {
- return util.NewNewtError(err.Error())
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Target %s successfully deleted.\n", t.FullName())
-
- return nil
-}
-
-func targetDelCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd, util.NewNewtError("Must specify at least one "+
- "target to delete"))
- }
-
- TryGetProject()
-
- targets, err := ResolveTargets(args...)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- for _, t := range targets {
- if err := targetDelOne(t); err != nil {
- NewtUsage(cmd, err)
- }
- }
-}
-
-func targetCopyCmd(cmd *cobra.Command, args []string) {
- if len(args) != 2 {
- NewtUsage(cmd, util.NewNewtError("Must specify exactly one "+
- "source target and one destination target"))
- }
-
- proj := TryGetProject()
-
- srcTarget, err := resolveExistingTargetArg(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- dstName, err := ResolveNewTargetName(args[1])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- // Copy the source target's base package and adjust the fields which need
- // to change.
- dstTarget := srcTarget.Clone(proj.LocalRepo(), dstName)
-
- // Save the new target.
- err = dstTarget.Save()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- // Copy syscfg.yml file.
- srcSyscfgPath := fmt.Sprintf("%s/%s",
- srcTarget.Package().BasePath(),
- pkg.SYSCFG_YAML_FILENAME)
- dstSyscfgPath := fmt.Sprintf("%s/%s",
- dstTarget.Package().BasePath(),
- pkg.SYSCFG_YAML_FILENAME)
-
- if err := util.CopyFile(srcSyscfgPath, dstSyscfgPath); err != nil {
- // If there is just no source syscfg.yml file, that is not an error.
- if !util.IsNotExist(err) {
- NewtUsage(nil, err)
- }
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Target successfully copied; %s --> %s\n",
- srcTarget.FullName(), dstTarget.FullName())
-}
-
-func printSetting(entry syscfg.CfgEntry) {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- " * Setting: %s\n", entry.Name)
-
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- " * Description: %s\n", entry.Description)
-
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- " * Value: %s", entry.Value)
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
-
- if len(entry.History) > 1 {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- " * Overridden: ")
- for i := 1; i < len(entry.History); i++ {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "%s, ",
- entry.History[i].Source.Name())
- }
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "default=%s\n", entry.History[0].Value)
- }
-}
-
-func printPkgCfg(pkgName string, cfg syscfg.Cfg, entries []syscfg.CfgEntry) {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "* PACKAGE: %s\n", pkgName)
-
- settingNames := make([]string, len(entries))
- for i, entry := range entries {
- settingNames[i] = entry.Name
- }
- sort.Strings(settingNames)
-
- for _, name := range settingNames {
- printSetting(cfg.Settings[name])
- }
-}
-
-func printCfg(targetName string, cfg syscfg.Cfg) {
- if errText := cfg.ErrorText(); errText != "" {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "!!! %s\n\n", errText)
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "Syscfg for %s:\n", targetName)
- pkgNameEntryMap := syscfg.EntriesByPkg(cfg)
-
- pkgNames := make([]string, 0, len(pkgNameEntryMap))
- for pkgName, _ := range pkgNameEntryMap {
- pkgNames = append(pkgNames, pkgName)
- }
- sort.Strings(pkgNames)
-
- for i, pkgName := range pkgNames {
- if i > 0 {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
- }
- printPkgCfg(pkgName, cfg, pkgNameEntryMap[pkgName])
- }
-}
-
-func yamlPkgCfg(w io.Writer, pkgName string, cfg syscfg.Cfg,
- entries []syscfg.CfgEntry) {
-
- settingNames := make([]string, len(entries))
- for i, entry := range entries {
- settingNames[i] = entry.Name
- }
- sort.Strings(settingNames)
-
- fmt.Fprintf(w, " ### %s\n", pkgName)
- for _, name := range settingNames {
- fmt.Fprintf(w, " %s: '%s'\n", name, cfg.Settings[name].Value)
- }
-}
-
-func yamlCfg(cfg syscfg.Cfg) string {
- if errText := cfg.ErrorText(); errText != "" {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "!!! %s\n\n", errText)
- }
-
- pkgNameEntryMap := syscfg.EntriesByPkg(cfg)
-
- pkgNames := make([]string, 0, len(pkgNameEntryMap))
- for pkgName, _ := range pkgNameEntryMap {
- pkgNames = append(pkgNames, pkgName)
- }
- sort.Strings(pkgNames)
-
- buf := bytes.Buffer{}
-
- fmt.Fprintf(&buf, "syscfg.vals:\n")
- for i, pkgName := range pkgNames {
- if i > 0 {
- fmt.Fprintf(&buf, "\n")
- }
- yamlPkgCfg(&buf, pkgName, cfg, pkgNameEntryMap[pkgName])
- }
-
- return string(buf.Bytes())
-}
-
-func targetBuilderConfigResolve(b *builder.TargetBuilder) *resolve.Resolution {
- res, err := b.Resolve()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- warningText := strings.TrimSpace(res.WarningText())
- if warningText != "" {
- for _, line := range strings.Split(warningText, "\n") {
- log.Warn(line)
- }
- }
-
- return res
-}
-
-func targetConfigShowCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd,
- util.NewNewtError("Must specify target or unittest name"))
- }
-
- for _, arg := range args {
- b, err := TargetBuilderForTargetOrUnittest(arg)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- res := targetBuilderConfigResolve(b)
- printCfg(b.GetTarget().Name(), res.Cfg)
- }
-}
-
-func targetConfigInitCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd,
- util.NewNewtError("Must specify target or unittest name"))
- }
-
- type entry struct {
- lpkg *pkg.LocalPackage
- path string
- b *builder.TargetBuilder
- exists bool
- }
-
- anyExist := false
- entries := make([]entry, len(args))
- for i, pkgName := range args {
- e := &entries[i]
-
- b, err := TargetBuilderForTargetOrUnittest(pkgName)
- if err != nil {
- NewtUsage(cmd, err)
- }
- e.b = b
-
- e.lpkg = b.GetTestPkg()
- if e.lpkg == nil {
- e.lpkg = b.GetTarget().Package()
- }
-
- e.path = builder.PkgSyscfgPath(e.lpkg.BasePath())
-
- if util.NodeExist(e.path) {
- e.exists = true
- anyExist = true
- }
- }
-
- if anyExist && !targetForce {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- "Configuration files already exist:\n")
- for _, e := range entries {
- if e.exists {
- util.StatusMessage(util.VERBOSITY_DEFAULT, " * %s\n",
- e.path)
- }
- }
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
-
- fmt.Printf("Overwrite them? (y/N): ")
- rsp := PromptYesNo(false)
- if !rsp {
- return
- }
- }
-
- for _, e := range entries {
- res := targetBuilderConfigResolve(e.b)
- yaml := yamlCfg(res.Cfg)
-
- if err := ioutil.WriteFile(e.path, []byte(yaml), 0644); err != nil {
- NewtUsage(nil, util.FmtNewtError("Error writing file \"%s\"; %s",
- e.path, err.Error()))
- }
- }
-}
-
-func targetDepCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd,
- util.NewNewtError("Must specify target or unittest name"))
- }
-
- TryGetProject()
-
- b, err := TargetBuilderForTargetOrUnittest(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- res, err := b.Resolve()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- dg, err := b.CreateDepGraph()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- // If user specified any package names, only include specified packages.
- if len(args) > 1 {
- rpkgs, err := ResolveRpkgs(res, args[1:])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- var missingRpkgs []*resolve.ResolvePackage
- dg, missingRpkgs = builder.FilterDepGraph(dg, rpkgs)
- for _, rpkg := range missingRpkgs {
- util.StatusMessage(util.VERBOSITY_QUIET,
- "Warning: Package \"%s\" not included in target \"%s\"\n",
- rpkg.Lpkg.FullName(), b.GetTarget().FullName())
- }
- }
-
- if len(dg) > 0 {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- builder.DepGraphText(dg)+"\n")
- }
-}
-
-func targetRevdepCmd(cmd *cobra.Command, args []string) {
- if len(args) < 1 {
- NewtUsage(cmd, util.NewNewtError("Must specify target name"))
- }
-
- TryGetProject()
-
- b, err := TargetBuilderForTargetOrUnittest(args[0])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- res, err := b.Resolve()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- dg, err := b.CreateRevdepGraph()
- if err != nil {
- NewtUsage(nil, err)
- }
-
- // If user specified any package names, only include specified packages.
- if len(args) > 1 {
- rpkgs, err := ResolveRpkgs(res, args[1:])
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- var missingRpkgs []*resolve.ResolvePackage
- dg, missingRpkgs = builder.FilterDepGraph(dg, rpkgs)
- for _, rpkg := range missingRpkgs {
- util.StatusMessage(util.VERBOSITY_QUIET,
- "Warning: Package \"%s\" not included in target \"%s\"\n",
- rpkg.Lpkg.FullName(), b.GetTarget().FullName())
- }
- }
-
- if len(dg) > 0 {
- util.StatusMessage(util.VERBOSITY_DEFAULT,
- builder.RevdepGraphText(dg)+"\n")
- }
-}
-
-func AddTargetCommands(cmd *cobra.Command) {
- targetHelpText := ""
- targetHelpEx := ""
- targetCmd := &cobra.Command{
- Use: "target",
- Short: "Commands to create, delete, configure, and query targets",
- Long: targetHelpText,
- Example: targetHelpEx,
- Run: func(cmd *cobra.Command, args []string) {
- cmd.Usage()
- },
- }
-
- cmd.AddCommand(targetCmd)
-
- showHelpText := "Show all the variables for the target specified " +
- "by <target-name>."
- showHelpEx := " newt target show <target-name>\n"
- showHelpEx += " newt target show my_target1"
-
- showCmd := &cobra.Command{
- Use: "show",
- Short: "View target configuration variables",
- Long: showHelpText,
- Example: showHelpEx,
- Run: targetShowCmd,
- }
- targetCmd.AddCommand(showCmd)
- AddTabCompleteFn(showCmd, targetList)
-
- setHelpText := "Set a target variable (<var-name>) on target "
- setHelpText += "<target-name> to value <value>.\n\n"
- setHelpText += "Warning: When setting the syscfg variable, a new syscfg.yml file\n"
- setHelpText += "is created and the current settings are deleted. Only the settings\n"
- setHelpText += "specified in the command are saved in the syscfg.yml file."
- setHelpEx := " newt target set my_target1 build_profile=optimized "
- setHelpEx += "cflags=\"-DNDEBUG\"\n"
- setHelpEx += " newt target set my_target1 "
- setHelpEx += "syscfg=LOG_NEWTMGR=1:CONFIG_NEWTMGR=0\n"
-
- setCmd := &cobra.Command{
- Use: "set <target-name> <var-name>=<value>" +
- "[:<var-name>=<value>...]",
- Short: "Set target configuration variable",
- Long: setHelpText,
- Example: setHelpEx,
- Run: targetSetCmd,
- }
- targetCmd.AddCommand(setCmd)
- AddTabCompleteFn(setCmd, targetList)
-
- createHelpText := "Create a target specified by <target-name>."
- createHelpEx := " newt target create <target-name>\n"
- createHelpEx += " newt target create my_target1"
-
- createCmd := &cobra.Command{
- Use: "create",
- Short: "Create a target",
- Long: createHelpText,
- Example: createHelpEx,
- Run: targetCreateCmd,
- }
-
- targetCmd.AddCommand(createCmd)
-
- delHelpText := "Delete the target specified by <target-name>."
- delHelpEx := " newt target delete <target-name>\n"
- delHelpEx += " newt target delete my_target1"
-
- delCmd := &cobra.Command{
- Use: "delete",
- Short: "Delete target",
- Long: delHelpText,
- Example: delHelpEx,
- Run: targetDelCmd,
- }
- delCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Force delete of targets with user files without prompt")
-
- targetCmd.AddCommand(delCmd)
-
- copyHelpText := "Create a new target <dst-target> by cloning <src-target>"
- copyHelpEx := " newt target copy blinky_sim my_target"
-
- copyCmd := &cobra.Command{
- Use: "copy <src-target> <dst-target>",
- Short: "Copy target",
- Long: copyHelpText,
- Example: copyHelpEx,
- Run: targetCopyCmd,
- }
-
- targetCmd.AddCommand(copyCmd)
- AddTabCompleteFn(copyCmd, targetList)
-
- configHelpText := "View or populate a target's system configuration"
-
- configCmd := &cobra.Command{
- Use: "config",
- Short: configHelpText,
- Long: configHelpText,
- Run: func(cmd *cobra.Command, args []string) {
- cmd.Usage()
- },
- }
-
- targetCmd.AddCommand(configCmd)
-
- configShowCmd := &cobra.Command{
- Use: "show <target>",
- Short: "View a target's system configuration",
- Long: "View a target's system configuration",
- Run: targetConfigShowCmd,
- }
-
- configCmd.AddCommand(configShowCmd)
- AddTabCompleteFn(configShowCmd, func() []string {
- return append(targetList(), unittestList()...)
- })
-
- configInitCmd := &cobra.Command{
- Use: "init",
- Short: "Populate a target's system configuration file",
- Long: "Populate a target's system configuration file (syscfg). " +
- "Unspecified settings are given default values.",
- Run: targetConfigInitCmd,
- }
- configInitCmd.PersistentFlags().BoolVarP(&newtutil.NewtForce,
- "force", "f", false,
- "Force overwrite of target configuration")
-
- configCmd.AddCommand(configInitCmd)
- AddTabCompleteFn(configInitCmd, func() []string {
- return append(targetList(), unittestList()...)
- })
-
- depHelpText := "View a target's dependency graph."
-
- depCmd := &cobra.Command{
- Use: "dep <target> [pkg-1] [pkg-2] [...]",
- Short: "View target's dependency graph",
- Long: depHelpText,
- Run: targetDepCmd,
- }
-
- targetCmd.AddCommand(depCmd)
- AddTabCompleteFn(depCmd, func() []string {
- return append(targetList(), unittestList()...)
- })
-
- revdepHelpText := "View a target's reverse-dependency graph."
-
- revdepCmd := &cobra.Command{
- Use: "revdep <target> [pkg-1] [pkg-2] [...]",
- Short: "View target's reverse-dependency graph",
- Long: revdepHelpText,
- Run: targetRevdepCmd,
- }
-
- targetCmd.AddCommand(revdepCmd)
- AddTabCompleteFn(revdepCmd, func() []string {
- return append(targetList(), unittestList()...)
- })
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/util.go
----------------------------------------------------------------------
diff --git a/newt/cli/util.go b/newt/cli/util.go
deleted file mode 100644
index e9c0b4c..0000000
--- a/newt/cli/util.go
+++ /dev/null
@@ -1,353 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "bufio"
- "bytes"
- "fmt"
- "os"
- "path/filepath"
- "regexp"
- "strings"
-
- log "github.com/Sirupsen/logrus"
- "github.com/spf13/cobra"
-
- "mynewt.apache.org/newt/newt/builder"
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/newt/pkg"
- "mynewt.apache.org/newt/newt/project"
- "mynewt.apache.org/newt/newt/resolve"
- "mynewt.apache.org/newt/newt/target"
- "mynewt.apache.org/newt/util"
-)
-
-const TARGET_KEYWORD_ALL string = "all"
-const TARGET_DEFAULT_DIR string = "targets"
-const MFG_DEFAULT_DIR string = "mfgs"
-
-func NewtUsage(cmd *cobra.Command, err error) {
- if err != nil {
- sErr := err.(*util.NewtError)
- log.Debugf("%s", sErr.StackTrace)
- fmt.Fprintf(os.Stderr, "Error: %s\n", sErr.Text)
- }
-
- if cmd != nil {
- fmt.Printf("\n")
- fmt.Printf("%s - ", cmd.Name())
- cmd.Help()
- }
- os.Exit(1)
-}
-
-// Display help text with a max line width of 79 characters
-func FormatHelp(text string) string {
- // first compress all new lines and extra spaces
- words := regexp.MustCompile("\\s+").Split(text, -1)
- linelen := 0
- fmtText := ""
- for _, word := range words {
- word = strings.Trim(word, "\n ") + " "
- tmplen := linelen + len(word)
- if tmplen >= 80 {
- fmtText += "\n"
- linelen = 0
- }
- fmtText += word
- linelen += len(word)
- }
- return fmtText
-}
-
-func ResolveTarget(name string) *target.Target {
- // Trim trailing slash from name. This is necessary when tab
- // completion is used to specify the name.
- name = strings.TrimSuffix(name, "/")
-
- targetMap := target.GetTargets()
-
- // Check for fully-qualified name.
- if t := targetMap[name]; t != nil {
- return t
- }
-
- // Check the local "targets" directory.
- if t := targetMap[TARGET_DEFAULT_DIR+"/"+name]; t != nil {
- return t
- }
-
- // Check each repo alphabetically.
- fullNames := []string{}
- for fullName, _ := range targetMap {
- fullNames = append(fullNames, fullName)
- }
- for _, fullName := range util.SortFields(fullNames...) {
- if name == filepath.Base(fullName) {
- return targetMap[fullName]
- }
- }
-
- return nil
-}
-
-// Resolves a list of target names and checks for the optional "all" keyword
-// among them. Regardless of whether "all" is specified, all target names must
-// be valid, or an error is reported.
-//
-// @return targets, all (t/f), err
-func ResolveTargetsOrAll(names ...string) ([]*target.Target, bool, error) {
- targets := []*target.Target{}
- all := false
-
- for _, name := range names {
- if name == "all" {
- all = true
- } else {
- t := ResolveTarget(name)
- if t == nil {
- return nil, false,
- util.NewNewtError("Could not resolve target name: " + name)
- }
-
- targets = append(targets, t)
- }
- }
-
- return targets, all, nil
-}
-
-func ResolveTargets(names ...string) ([]*target.Target, error) {
- targets, all, err := ResolveTargetsOrAll(names...)
- if err != nil {
- return nil, err
- }
- if all {
- return nil,
- util.NewNewtError("Keyword \"all\" not allowed in thie context")
- }
-
- return targets, nil
-}
-
-func ResolveNewTargetName(name string) (string, error) {
- repoName, pkgName, err := newtutil.ParsePackageString(name)
- if err != nil {
- return "", err
- }
-
- if repoName != "" {
- return "", util.NewNewtError("Target name cannot contain repo; " +
- "must be local")
- }
-
- if pkgName == TARGET_KEYWORD_ALL {
- return "", util.NewNewtError("Target name " + TARGET_KEYWORD_ALL +
- " is reserved")
- }
-
- // "Naked" target names translate to "targets/<name>".
- if !strings.Contains(pkgName, "/") {
- pkgName = TARGET_DEFAULT_DIR + "/" + pkgName
- }
-
- if target.GetTargets()[pkgName] != nil {
- return "", util.NewNewtError("Target already exists: " + pkgName)
- }
-
- return pkgName, nil
-}
-
-func PackageNameList(pkgs []*pkg.LocalPackage) string {
- var buffer bytes.Buffer
- for i, pack := range pkgs {
- if i != 0 {
- buffer.WriteString(" ")
- }
- buffer.WriteString(pack.Name())
- }
-
- return buffer.String()
-}
-
-func ResetGlobalState() error {
- // Make sure the current working directory is at the project base.
- if err := os.Chdir(project.GetProject().Path()); err != nil {
- return util.NewNewtError("Failed to reset global state: " +
- err.Error())
- }
-
- target.ResetTargets()
- project.ResetProject()
-
- return nil
-}
-
-func TryGetProject() *project.Project {
- var p *project.Project
- var err error
-
- if p, err = project.TryGetProject(); err != nil {
- NewtUsage(nil, err)
- }
-
- for _, w := range p.Warnings() {
- util.ErrorMessage(util.VERBOSITY_QUIET, "* Warning: %s\n", w)
- }
-
- return p
-}
-
-func ResolveUnittest(pkgName string) (*target.Target, error) {
- // Each unit test package gets its own target. This target is a copy
- // of the base unit test package, just with an appropriate name. The
- // reason each test needs a unique target is: syscfg and sysinit are
- // target-specific. If each test package shares a target, they will
- // overwrite these generated headers each time they are run. Worse, if
- // two tests are run back-to-back, the timestamps may indicate that the
- // headers have not changed between tests, causing build failures.
- baseTarget := ResolveTarget(TARGET_TEST_NAME)
- if baseTarget == nil {
- return nil, util.FmtNewtError("Can't find unit test target: %s",
- TARGET_TEST_NAME)
- }
-
- targetName := fmt.Sprintf("%s/%s/%s",
- TARGET_DEFAULT_DIR, TARGET_TEST_NAME,
- builder.TestTargetName(pkgName))
-
- t := ResolveTarget(targetName)
- if t == nil {
- targetName, err := ResolveNewTargetName(targetName)
- if err != nil {
- return nil, err
- }
-
- t = baseTarget.Clone(TryGetProject().LocalRepo(), targetName)
- }
-
- return t, nil
-}
-
-// @return Target
-// @return LocalPackage The package under test, if any.
-// @return error
-func ResolveTargetOrUnittest(pkgName string) (
- *target.Target, *pkg.LocalPackage, error) {
-
- // Argument can specify either a target or a unittest package. Determine
- // which type the package is and construct a target builder appropriately.
- if t, err := resolveExistingTargetArg(pkgName); err == nil {
- return t, nil, nil
- }
-
- // Package wasn't a target. Try for a unittest.
- proj := TryGetProject()
- pack, err := proj.ResolvePackage(proj.LocalRepo(), pkgName)
- if err != nil {
- return nil, nil, util.FmtNewtError(
- "Could not resolve target or unittest \"%s\"", pkgName)
- }
-
- if pack.Type() != pkg.PACKAGE_TYPE_UNITTEST {
- return nil, nil, util.FmtNewtError(
- "Package \"%s\" is of type %s; "+
- "must be target or unittest", pkgName,
- pkg.PackageTypeNames[pack.Type()])
- }
-
- t, err := ResolveUnittest(pack.Name())
- if err != nil {
- return nil, nil, err
- }
-
- return t, pack, nil
-}
-
-func ResolvePackages(pkgNames []string) ([]*pkg.LocalPackage, error) {
- proj := TryGetProject()
-
- lpkgs := []*pkg.LocalPackage{}
- for _, pkgName := range pkgNames {
- pack, err := proj.ResolvePackage(proj.LocalRepo(), pkgName)
- if err != nil {
- return nil, err
- }
- lpkgs = append(lpkgs, pack)
- }
-
- return lpkgs, nil
-}
-
-func ResolveRpkgs(res *resolve.Resolution, pkgNames []string) (
- []*resolve.ResolvePackage, error) {
-
- lpkgs, err := ResolvePackages(pkgNames)
- if err != nil {
- return nil, err
- }
-
- rpkgs := []*resolve.ResolvePackage{}
- for _, lpkg := range lpkgs {
- rpkg := res.LpkgRpkgMap[lpkg]
- if rpkg == nil {
- return nil, util.FmtNewtError("Unexpected error; local package "+
- "%s lacks a corresponding resolve package", lpkg.FullName())
- }
-
- rpkgs = append(rpkgs, rpkg)
- }
-
- return rpkgs, nil
-}
-
-func TargetBuilderForTargetOrUnittest(pkgName string) (
- *builder.TargetBuilder, error) {
-
- t, testPkg, err := ResolveTargetOrUnittest(pkgName)
- if err != nil {
- return nil, err
- }
-
- if testPkg == nil {
- return builder.NewTargetBuilder(t)
- } else {
- return builder.NewTargetTester(t, testPkg)
- }
-}
-
-func PromptYesNo(dflt bool) bool {
- scanner := bufio.NewScanner(os.Stdin)
- rc := scanner.Scan()
- if !rc {
- return dflt
- }
-
- if strings.ToLower(scanner.Text()) == "y" {
- return true
- }
-
- if strings.ToLower(scanner.Text()) == "n" {
- return false
- }
-
- return dflt
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/vals_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/vals_cmds.go b/newt/cli/vals_cmds.go
deleted file mode 100644
index 267fc79..0000000
--- a/newt/cli/vals_cmds.go
+++ /dev/null
@@ -1,71 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "strings"
-
- "github.com/spf13/cobra"
-
- "mynewt.apache.org/newt/util"
-)
-
-func valsRunCmd(cmd *cobra.Command, args []string) {
- if len(args) == 0 {
- NewtUsage(cmd, nil)
- }
-
- allVals := [][]string{}
- for _, elemType := range args {
- vals, err := VarValues(elemType)
- if err != nil {
- NewtUsage(cmd, err)
- }
-
- allVals = append(allVals, vals)
- }
-
- for i, vals := range allVals {
- if i != 0 {
- util.StatusMessage(util.VERBOSITY_DEFAULT, "\n")
- }
-
- util.StatusMessage(util.VERBOSITY_DEFAULT, "%s names:\n", args[i])
- for _, val := range vals {
- util.StatusMessage(util.VERBOSITY_DEFAULT, " %s\n", val)
- }
- }
-}
-
-func AddValsCommands(cmd *cobra.Command) {
- valsShortHelp := "Display valid values for the specified element type(s)"
-
- valsLongHelp := valsShortHelp + ".\n\nElement types:\n " +
- strings.Join(VarTypes(), "\n ")
-
- valsCmd := &cobra.Command{
- Use: "vals <element-type> [element-types...]",
- Short: valsShortHelp,
- Long: valsLongHelp,
- Run: valsRunCmd,
- }
-
- cmd.AddCommand(valsCmd)
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/cli/vars.go
----------------------------------------------------------------------
diff --git a/newt/cli/vars.go b/newt/cli/vars.go
deleted file mode 100644
index 7ed25de..0000000
--- a/newt/cli/vars.go
+++ /dev/null
@@ -1,172 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package cli
-
-import (
- "fmt"
- "path/filepath"
- "sort"
- "strings"
-
- "mynewt.apache.org/newt/newt/interfaces"
- "mynewt.apache.org/newt/newt/pkg"
- "mynewt.apache.org/newt/newt/project"
- "mynewt.apache.org/newt/util"
-)
-
-func varsFromPackageType(
- pt interfaces.PackageType, fullPath bool) ([]string, error) {
-
- values := []string{}
-
- packs := project.GetProject().PackagesOfType(pt)
- for _, pack := range packs {
- value := pack.FullName()
- if !fullPath {
- value = filepath.Base(value)
- }
-
- values = append(values, value)
- }
-
- sort.Strings(values)
-
- return values, nil
-}
-
-func settingValues(settingName string) ([]string, error) {
- settingMap := map[string]struct{}{}
-
- packs := project.GetProject().PackagesOfType(-1)
- for _, pack := range packs {
- settings :=
- pack.(*pkg.LocalPackage).PkgV.GetStringSlice(settingName)
-
- for _, setting := range settings {
- settingMap[setting] = struct{}{}
- }
- }
-
- values := make([]string, 0, len(settingMap))
- for f, _ := range settingMap {
- values = append(values, f)
- }
- sort.Strings(values)
-
- return values, nil
-}
-
-func buildProfileValues() ([]string, error) {
- profileMap := map[string]struct{}{}
-
- packs := project.GetProject().PackagesOfType(pkg.PACKAGE_TYPE_COMPILER)
- for _, pack := range packs {
- v, err := util.ReadConfig(pack.(*pkg.LocalPackage).BasePath(),
- "compiler")
- if err != nil {
- return nil, err
- }
-
- settingMap := v.AllSettings()
- for k, _ := range settingMap {
- if strings.HasPrefix(k, "compiler.flags") {
- fields := strings.Split(k, ".")
- if len(fields) >= 3 {
- profileMap[fields[2]] = struct{}{}
- }
- }
- }
- }
-
- values := make([]string, 0, len(profileMap))
- for k, _ := range profileMap {
- values = append(values, k)
- }
-
- sort.Strings(values)
-
- return values, nil
-}
-
-var varsMap = map[string]func() ([]string, error){
- // Package names.
- "app": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_APP, true)
- },
- "bsp": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_BSP, true)
- },
- "compiler": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_COMPILER, true)
- },
- "lib": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_LIB, true)
- },
- "sdk": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_SDK, true)
- },
- "target": func() ([]string, error) {
- return varsFromPackageType(pkg.PACKAGE_TYPE_TARGET, true)
- },
-
- // Package settings.
- "api": func() ([]string, error) {
- return settingValues("pkg.apis")
- },
-
- // Target settings.
- "build_profile": func() ([]string, error) {
- return buildProfileValues()
- },
-}
-
-// Returns a slice of valid values for the target variable with the specified
-// name. If an invalid target variable is specified, an error is returned.
-func VarValues(varName string) ([]string, error) {
- _, err := project.TryGetProject()
- if err != nil {
- return nil, err
- }
-
- fn := varsMap[varName]
- if fn == nil {
- err := util.NewNewtError(fmt.Sprintf("Unknown setting name: \"%s\"",
- varName))
- return nil, err
- }
-
- values, err := fn()
- if err != nil {
- return nil, err
- }
-
- return values, nil
-}
-
-func VarTypes() []string {
- types := make([]string, 0, len(varsMap))
-
- for k, _ := range varsMap {
- types = append(types, k)
- }
-
- sort.Strings(types)
- return types
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/compat/compat.go
----------------------------------------------------------------------
diff --git a/newt/compat/compat.go b/newt/compat/compat.go
deleted file mode 100644
index 2e4355f..0000000
--- a/newt/compat/compat.go
+++ /dev/null
@@ -1,275 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package compat
-
-import (
- "fmt"
- "math"
- "sort"
-
- "github.com/spf13/cast"
-
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/util"
- "mynewt.apache.org/newt/viper"
-)
-
-type NewtCompatCode int
-
-const (
- NEWT_COMPAT_GOOD NewtCompatCode = iota
- NEWT_COMPAT_WARN
- NEWT_COMPAT_ERROR
-)
-
-var NewtCompatCodeNames = map[NewtCompatCode]string{
- NEWT_COMPAT_GOOD: "good",
- NEWT_COMPAT_WARN: "warn",
- NEWT_COMPAT_ERROR: "error",
-}
-
-type NewtCompatEntry struct {
- code NewtCompatCode
- minNewtVer newtutil.Version
-}
-
-// Sorted in ascending order by newt version number.
-type NewtCompatTable []NewtCompatEntry
-
-type NewtCompatMap map[newtutil.Version]NewtCompatTable
-
-func newtCompatCodeToString(code NewtCompatCode) string {
- return NewtCompatCodeNames[code]
-}
-
-func newtCompatCodeFromString(codeStr string) (NewtCompatCode, error) {
- for c, s := range NewtCompatCodeNames {
- if codeStr == s {
- return c, nil
- }
- }
-
- return NewtCompatCode(0),
- util.FmtNewtError("Invalid newt compatibility code: %s", codeStr)
-}
-
-func parseNcEntry(verStr string, codeStr string) (NewtCompatEntry, error) {
- entry := NewtCompatEntry{}
- var err error
-
- entry.minNewtVer, err = newtutil.ParseVersion(verStr)
- if err != nil {
- return entry, err
- }
-
- entry.code, err = newtCompatCodeFromString(codeStr)
- if err != nil {
- return entry, err
- }
-
- return entry, nil
-}
-
-func ParseNcTable(strMap map[string]string) (NewtCompatTable, error) {
- tbl := NewtCompatTable{}
-
- for v, c := range strMap {
- entry, err := parseNcEntry(v, c)
- if err != nil {
- return tbl, err
- }
-
- tbl = append(tbl, entry)
- }
-
- sortEntries(tbl)
-
- return tbl, nil
-}
-
-func ReadNcMap(v *viper.Viper) (NewtCompatMap, error) {
- mp := NewtCompatMap{}
- ncMap := v.GetStringMap("repo.newt_compatibility")
-
- for k, v := range ncMap {
- repoVer, err := newtutil.ParseVersion(k)
- if err != nil {
- return nil, util.FmtNewtError("Newt compatibility table contains " +
- "invalid repo version \"%s\"")
- }
-
- if _, ok := mp[repoVer]; ok {
- return nil, util.FmtNewtError("Newt compatibility table contains "+
- "duplicate version specifier: %s", repoVer.String())
- }
-
- strMap := cast.ToStringMapString(v)
- tbl, err := ParseNcTable(strMap)
- if err != nil {
- return nil, err
- }
-
- mp[repoVer] = tbl
- }
-
- return mp, nil
-}
-
-func (tbl NewtCompatTable) matchIdx(newtVer newtutil.Version) int {
- // Iterate the table backwards. The first entry whose version is less than
- // or equal to the specified version is the match.
- for i := 0; i < len(tbl); i++ {
- idx := len(tbl) - i - 1
- entry := &tbl[idx]
- cmp := newtutil.VerCmp(entry.minNewtVer, newtVer)
- if cmp <= 0 {
- return idx
- }
- }
-
- return -1
-}
-
-func (tbl NewtCompatTable) newIdxRange(i int, j int) []int {
- if i >= len(tbl) {
- return []int{j, i}
- }
-
- if j >= len(tbl) {
- return []int{i, j}
- }
-
- e1 := tbl[i]
- e2 := tbl[j]
-
- if newtutil.VerCmp(e1.minNewtVer, e2.minNewtVer) < 0 {
- return []int{i, j}
- } else {
- return []int{j, i}
- }
-}
-
-func (tbl NewtCompatTable) idxRangesWithCode(c NewtCompatCode) [][]int {
- ranges := [][]int{}
-
- curi := -1
- for i, e := range tbl {
- if curi == -1 {
- if e.code == c {
- curi = i
- }
- } else {
- if e.code != c {
- ranges = append(ranges, tbl.newIdxRange(curi, i))
- curi = -1
- }
- }
- }
-
- if curi != -1 {
- ranges = append(ranges, tbl.newIdxRange(curi, len(tbl)))
- }
- return ranges
-}
-
-func (tbl NewtCompatTable) minMaxTgtVers(goodRange []int) (
- newtutil.Version, newtutil.Version, newtutil.Version) {
-
- minVer := tbl[goodRange[0]].minNewtVer
-
- var maxVer newtutil.Version
- if goodRange[1] < len(tbl) {
- maxVer = tbl[goodRange[1]].minNewtVer
- } else {
- maxVer = newtutil.Version{math.MaxInt64, math.MaxInt64, math.MaxInt64}
- }
-
- targetVer := tbl[goodRange[1]-1].minNewtVer
-
- return minVer, maxVer, targetVer
-}
-
-// @return NewtCompatCode The severity of the newt incompatibility
-// string The warning or error message to display in case
-// of incompatibility.
-func (tbl NewtCompatTable) CheckNewtVer(
- newtVer newtutil.Version) (NewtCompatCode, string) {
-
- var code NewtCompatCode
- idx := tbl.matchIdx(newtVer)
- if idx == -1 {
- // This version of newt is older than every entry in the table.
- code = NEWT_COMPAT_ERROR
- } else {
- code = tbl[idx].code
- if code == NEWT_COMPAT_GOOD {
- return NEWT_COMPAT_GOOD, ""
- }
- }
-
- goodRanges := tbl.idxRangesWithCode(NEWT_COMPAT_GOOD)
- for i := 0; i < len(goodRanges); i++ {
- minVer, maxVer, tgtVer := tbl.minMaxTgtVers(goodRanges[i])
-
- if newtutil.VerCmp(newtVer, minVer) < 0 {
- return code, fmt.Sprintf("Please upgrade your newt tool to "+
- "version %s", tgtVer.String())
- }
-
- if newtutil.VerCmp(newtVer, maxVer) >= 0 {
- return code, fmt.Sprintf("Please upgrade your project "+
- "or downgrade newt to %s", tgtVer.String())
- }
- }
-
- return code, ""
-}
-
-type entrySorter struct {
- entries []NewtCompatEntry
-}
-
-func (s entrySorter) Len() int {
- return len(s.entries)
-}
-func (s entrySorter) Swap(i, j int) {
- s.entries[i], s.entries[j] = s.entries[j], s.entries[i]
-}
-func (s entrySorter) Less(i, j int) bool {
- e1 := s.entries[i]
- e2 := s.entries[j]
-
- cmp := newtutil.VerCmp(e1.minNewtVer, e2.minNewtVer)
- if cmp < 0 {
- return true
- } else if cmp > 0 {
- return false
- }
-
- return false
-}
-
-func sortEntries(entries []NewtCompatEntry) {
- sorter := entrySorter{
- entries: entries,
- }
-
- sort.Sort(sorter)
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/downloader/downloader.go
----------------------------------------------------------------------
diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go
deleted file mode 100644
index c8e13c6..0000000
--- a/newt/downloader/downloader.go
+++ /dev/null
@@ -1,472 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package downloader
-
-import (
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "os"
- "os/exec"
- "path/filepath"
- "strings"
-
- log "github.com/Sirupsen/logrus"
-
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/util"
-)
-
-type Downloader interface {
- FetchFile(name string, dest string) error
- Branch() string
- SetBranch(branch string)
- DownloadRepo(branch string) (string, error)
- CurrentBranch(path string) (string, error)
- UpdateRepo(path string, branchName string) error
- CleanupRepo(path string, branchName string) error
- LocalDiff(path string) ([]byte, error)
-}
-
-type GenericDownloader struct {
- branch string
-}
-
-type GithubDownloader struct {
- GenericDownloader
- Server string
- User string
- Repo string
-
- // Github access token for private repositories.
- Token string
-
- // Basic authentication login and password for private repositories.
- Login string
- Password string
-}
-
-type LocalDownloader struct {
- GenericDownloader
-
- // Path to parent directory of repository.yml file.
- Path string
-}
-
-func executeGitCommand(dir string, cmd []string) ([]byte, error) {
- wd, err := os.Getwd()
- if err != nil {
- return nil, util.NewNewtError(err.Error())
- }
-
- gitPath, err := exec.LookPath("git")
- if err != nil {
- return nil, util.NewNewtError(fmt.Sprintf("Can't find git binary: %s\n",
- err.Error()))
- }
- gitPath = filepath.ToSlash(gitPath)
-
- if err := os.Chdir(dir); err != nil {
- return nil, util.NewNewtError(err.Error())
- }
-
- defer os.Chdir(wd)
-
- gitCmd := []string{gitPath}
- gitCmd = append(gitCmd, cmd...)
- output, err := util.ShellCommand(gitCmd, nil)
- if err != nil {
- return nil, err
- }
-
- return output, nil
-}
-
-func isTag(repoDir string, branchName string) bool {
- cmd := []string{"tag", "--list"}
- output, _ := executeGitCommand(repoDir, cmd)
- return strings.Contains(string(output), branchName)
-}
-
-func branchExists(repoDir string, branchName string) bool {
- cmd := []string{"show-ref", "--verify", "--quiet", "refs/heads/" + branchName}
- _, err := executeGitCommand(repoDir, cmd)
- return err == nil
-}
-
-// checkout does checkout a branch, or create a new branch from a tag name
-// if the commit supplied is a tag. sha1 based commits have no special
-// handling and result in dettached from HEAD state.
-func checkout(repoDir string, commit string) error {
- var cmd []string
- if isTag(repoDir, commit) && !branchExists(repoDir, commit) {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Will create new branch %s"+
- " from tag %s\n", commit, "tags/"+commit)
- cmd = []string{
- "checkout",
- "tags/" + commit,
- "-b",
- commit,
- }
- } else {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Will checkout branch %s\n",
- commit)
- cmd = []string{
- "checkout",
- commit,
- }
- }
- _, err := executeGitCommand(repoDir, cmd)
- return err
-}
-
-// mergeBranches applies upstream changes to the local copy and must be
-// preceeded by a "fetch" to achieve any meaningful result.
-func mergeBranches(repoDir string) {
- branches := []string{"master", "develop"}
- for _, branch := range branches {
- err := checkout(repoDir, branch)
- if err != nil {
- continue
- }
- _, err = executeGitCommand(repoDir, []string{"merge", "origin/" + branch})
- if err != nil {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Merging changes from origin/%s: %s\n",
- branch, err)
- } else {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Merging changes from origin/%s\n",
- branch)
- }
- // XXX: ignore error, probably resulting from a branch not available at
- // origin anymore.
- }
-}
-
-func fetch(repoDir string) error {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Fetching new remote branches/tags\n")
- _, err := executeGitCommand(repoDir, []string{"fetch", "--tags"})
- return err
-}
-
-// stash saves current changes locally and returns if a new stash was
-// created (if there where no changes, there's no need to stash)
-func stash(repoDir string) (bool, error) {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Stashing local changes\n")
- output, err := executeGitCommand(repoDir, []string{"stash"})
- if err != nil {
- return false, err
- }
- return strings.Contains(string(output), "Saved"), nil
-}
-
-func stashPop(repoDir string) error {
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Un-stashing local changes\n")
- _, err := executeGitCommand(repoDir, []string{"stash", "pop"})
- return err
-}
-
-func clean(repoDir string) error {
- _, err := executeGitCommand(repoDir, []string{"clean", "-f"})
- return err
-}
-
-func (gd *GenericDownloader) Branch() string {
- return gd.branch
-}
-
-func (gd *GenericDownloader) SetBranch(branch string) {
- gd.branch = branch
-}
-
-func (gd *GenericDownloader) TempDir() (string, error) {
- dir, err := ioutil.TempDir("", "newt-tmp")
- return dir, err
-}
-
-func (gd *GithubDownloader) FetchFile(name string, dest string) error {
- server := "api.github.com"
- prefix := "repos"
- if gd.Server != "" {
- server = gd.Server
- prefix = "api/v3/repos"
- }
- url := fmt.Sprintf("https://%s/%s/%s/%s/contents/%s?ref=%s",
- server, prefix, gd.User, gd.Repo, name, gd.Branch())
-
- req, err := http.NewRequest("GET", url, nil)
- req.Header.Add("Accept", "application/vnd.github.v3.raw")
-
- if gd.Token != "" {
- // XXX: Add command line option to include token in log.
- log.Debugf("Using authorization token")
- req.Header.Add("Authorization", "token "+gd.Token)
- } else if gd.Login != "" && gd.Password != "" {
- // XXX: Add command line option to include password in log.
- log.Debugf("Using basic auth; login=%s", gd.Login)
- req.SetBasicAuth(gd.Login, gd.Password)
- }
-
- log.Debugf("Fetching file %s (url: %s) to %s", name, url, dest)
- client := &http.Client{}
- rsp, err := client.Do(req)
- if err != nil {
- return util.NewNewtError(err.Error())
- }
- defer rsp.Body.Close()
-
- if rsp.StatusCode != http.StatusOK {
- errMsg := fmt.Sprintf("Failed to download '%s'; status=%s",
- url, rsp.Status)
- switch rsp.StatusCode {
- case http.StatusNotFound:
- errMsg += "; URL incorrect or repository private?"
- case http.StatusUnauthorized:
- errMsg += "; credentials incorrect?"
- }
-
- return util.NewNewtError(errMsg)
- }
-
- handle, err := os.Create(dest)
- if err != nil {
- return util.NewNewtError(err.Error())
- }
- defer handle.Close()
-
- _, err = io.Copy(handle, rsp.Body)
-
- return nil
-}
-
-func (gd *GithubDownloader) CurrentBranch(path string) (string, error) {
- cmd := []string{"rev-parse", "--abbrev-ref", "HEAD"}
- branch, err := executeGitCommand(path, cmd)
- return strings.Trim(string(branch), "\r\n"), err
-}
-
-func (gd *GithubDownloader) UpdateRepo(path string, branchName string) error {
- err := fetch(path)
- if err != nil {
- return err
- }
-
- stashed, err := stash(path)
- if err != nil {
- return err
- }
-
- mergeBranches(path)
-
- err = checkout(path, branchName)
- if err != nil {
- return err
- }
-
- if stashed {
- return stashPop(path)
- }
-
- return nil
-}
-
-func (gd *GithubDownloader) CleanupRepo(path string, branchName string) error {
- _, err := stash(path)
- if err != nil {
- return err
- }
-
- err = clean(path)
- if err != nil {
- return err
- }
-
- // TODO: needs handling of non-tracked files
-
- return gd.UpdateRepo(path, branchName)
-}
-
-func (gd *GithubDownloader) LocalDiff(path string) ([]byte, error) {
- return executeGitCommand(path, []string{"diff"})
-}
-
-func (gd *GithubDownloader) DownloadRepo(commit string) (string, error) {
- // Get a temporary directory, and copy the repository into that directory.
- tmpdir, err := ioutil.TempDir("", "newt-repo")
- if err != nil {
- return "", err
- }
-
- // Currently only the master branch is supported.
- branch := "master"
- server := "github.com"
-
- if gd.Server != "" {
- server = gd.Server
- }
- url := fmt.Sprintf("https://%s/%s/%s.git", server, gd.User, gd.Repo)
- util.StatusMessage(util.VERBOSITY_VERBOSE, "Downloading "+
- "repository %s (branch: %s; commit: %s) at %s\n", gd.Repo, branch,
- commit, url)
-
- gitPath, err := exec.LookPath("git")
- if err != nil {
- os.RemoveAll(tmpdir)
- return "", util.NewNewtError(fmt.Sprintf("Can't find git binary: %s\n",
- err.Error()))
- }
- gitPath = filepath.ToSlash(gitPath)
-
- // Clone the repository.
- cmd := []string{
- gitPath,
- "clone",
- "-b",
- branch,
- url,
- tmpdir,
- }
-
- if util.Verbosity >= util.VERBOSITY_VERBOSE {
- if err := util.ShellInteractiveCommand(cmd, nil); err != nil {
- os.RemoveAll(tmpdir)
- return "", err
- }
- } else {
- if _, err := util.ShellCommand(cmd, nil); err != nil {
- return "", err
- }
- }
-
- // Checkout the specified commit.
- if err := checkout(tmpdir, commit); err != nil {
- return "", err
- }
-
- return tmpdir, nil
-}
-
-func NewGithubDownloader() *GithubDownloader {
- return &GithubDownloader{}
-}
-
-func (ld *LocalDownloader) FetchFile(name string, dest string) error {
- srcPath := ld.Path + "/" + name
-
- log.Debugf("Fetching file %s to %s", srcPath, dest)
- if err := util.CopyFile(srcPath, dest); err != nil {
- return err
- }
-
- return nil
-}
-
-func (ld *LocalDownloader) CurrentBranch(path string) (string, error) {
- cmd := []string{"rev-parse", "--abbrev-ref", "HEAD"}
- branch, err := executeGitCommand(path, cmd)
- return strings.Trim(string(branch), "\r\n"), err
-}
-
-// NOTE: intentionally always error...
-func (ld *LocalDownloader) UpdateRepo(path string, branchName string) error {
- return util.NewNewtError(fmt.Sprintf("Can't pull from a local repo\n"))
-}
-
-func (ld *LocalDownloader) CleanupRepo(path string, branchName string) error {
- os.RemoveAll(path)
- _, err := ld.DownloadRepo(branchName)
- return err
-}
-
-func (ld *LocalDownloader) LocalDiff(path string) ([]byte, error) {
- return executeGitCommand(path, []string{"diff"})
-}
-
-func (ld *LocalDownloader) DownloadRepo(commit string) (string, error) {
- // Get a temporary directory, and copy the repository into that directory.
- tmpdir, err := ioutil.TempDir("", "newt-repo")
- if err != nil {
- return "", err
- }
-
- util.StatusMessage(util.VERBOSITY_VERBOSE,
- "Downloading local repository %s\n", ld.Path)
-
- if err := util.CopyDir(ld.Path, tmpdir); err != nil {
- return "", err
- }
-
- // Checkout the specified commit.
- if err := checkout(tmpdir, commit); err != nil {
- return "", err
- }
-
- return tmpdir, nil
-}
-
-func NewLocalDownloader() *LocalDownloader {
- return &LocalDownloader{}
-}
-
-func LoadDownloader(repoName string, repoVars map[string]string) (
- Downloader, error) {
-
- switch repoVars["type"] {
- case "github":
- gd := NewGithubDownloader()
-
- gd.Server = repoVars["server"]
- gd.User = repoVars["user"]
- gd.Repo = repoVars["repo"]
-
- // The project.yml file can contain github access tokens and
- // authentication credentials, but this file is probably world-readable
- // and therefore not a great place for this.
- gd.Token = repoVars["token"]
- gd.Login = repoVars["login"]
- gd.Password = repoVars["password"]
-
- // Alternatively, the user can put security material in
- // $HOME/.newt/repos.yml.
- newtrc := newtutil.Newtrc()
- privRepo := newtrc.GetStringMapString("repository." + repoName)
- if privRepo != nil {
- if gd.Token == "" {
- gd.Token = privRepo["token"]
- }
- if gd.Login == "" {
- gd.Login = privRepo["login"]
- }
- if gd.Password == "" {
- gd.Password = privRepo["password"]
- }
- }
- return gd, nil
-
- case "local":
- ld := NewLocalDownloader()
- ld.Path = repoVars["path"]
- return ld, nil
-
- default:
- return nil, util.FmtNewtError("Invalid repository type: %s",
- repoVars["type"])
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e31a7d31/newt/flash/flash.go
----------------------------------------------------------------------
diff --git a/newt/flash/flash.go b/newt/flash/flash.go
deleted file mode 100644
index 97114c5..0000000
--- a/newt/flash/flash.go
+++ /dev/null
@@ -1,421 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-package flash
-
-import (
- "bytes"
- "fmt"
- "io"
- "io/ioutil"
- "os"
- "path/filepath"
- "sort"
- "strings"
-
- log "github.com/Sirupsen/logrus"
- "github.com/spf13/cast"
-
- "mynewt.apache.org/newt/newt/newtutil"
- "mynewt.apache.org/newt/util"
-)
-
-const FLASH_AREA_NAME_BOOTLOADER = "FLASH_AREA_BOOTLOADER"
-const FLASH_AREA_NAME_IMAGE_0 = "FLASH_AREA_IMAGE_0"
-const FLASH_AREA_NAME_IMAGE_1 = "FLASH_AREA_IMAGE_1"
-const FLASH_AREA_NAME_IMAGE_SCRATCH = "FLASH_AREA_IMAGE_SCRATCH"
-
-var SYSTEM_AREA_NAME_ID_MAP = map[string]int{
- FLASH_AREA_NAME_BOOTLOADER: 0,
- FLASH_AREA_NAME_IMAGE_0: 1,
- FLASH_AREA_NAME_IMAGE_1: 2,
- FLASH_AREA_NAME_IMAGE_SCRATCH: 3,
-}
-
-const AREA_USER_ID_MIN = 16
-
-const HEADER_PATH = "sysflash/sysflash.h"
-const C_VAR_NAME = "sysflash_map_dflt"
-const C_VAR_COMMENT = `/**
- * This flash map definition is used for two purposes:
- * 1. To locate the meta area, which contains the true flash map definition.
- * 2. As a fallback in case the meta area cannot be read from flash.
- */
-`
-
-type FlashArea struct {
- Name string
- Id int
- Device int
- Offset int
- Size int
-}
-
-type FlashMap struct {
- Areas map[string]FlashArea
- Overlaps [][]FlashArea
- IdConflicts [][]FlashArea
-}
-
-func newFlashMap() FlashMap {
- return FlashMap{
- Areas: map[string]FlashArea{},
- Overlaps: [][]FlashArea{},
- }
-}
-
-func flashAreaErr(areaName string, format string, args ...interface{}) error {
- return util.NewNewtError(
- "failure while parsing flash area \"" + areaName + "\": " +
- fmt.Sprintf(format, args...))
-}
-
-func parseSize(val string) (int, error) {
- lower := strings.ToLower(val)
-
- multiplier := 1
- if strings.HasSuffix(lower, "kb") {
- multiplier = 1024
- lower = strings.TrimSuffix(lower, "kb")
- }
-
- num, err := util.AtoiNoOct(lower)
- if err != nil {
- return 0, err
- }
-
- return num * multiplier, nil
-}
-
-func parseFlashArea(
- name string, ymlFields map[string]interface{}) (FlashArea, error) {
-
- area := FlashArea{
- Name: name,
- }
-
- idPresent := false
- devicePresent := false
- offsetPresent := false
- sizePresent := false
-
- var isSystem bool
- area.Id, isSystem = SYSTEM_AREA_NAME_ID_MAP[name]
-
- var err error
-
- fields := cast.ToStringMapString(ymlFields)
- for k, v := range fields {
- switch k {
- case "user_id":
- if isSystem {
- return area, flashAreaErr(name,
- "system areas cannot specify a user ID")
- }
- userId, err := util.AtoiNoOct(v)
- if err != nil {
- return area, flashAreaErr(name, "invalid user id: %s", v)
- }
- area.Id = userId + AREA_USER_ID_MIN
- idPresent = true
-
- case "device":
- area.Device, err = util.AtoiNoOct(v)
- if err != nil {
- return area, flashAreaErr(name, "invalid device: %s", v)
- }
- devicePresent = true
-
- case "offset":
- area.Offset, err = util.AtoiNoOct(v)
- if err != nil {
- return area, flashAreaErr(name, "invalid offset: %s", v)
- }
- offsetPresent = true
-
- case "size":
- area.Size, err = parseSize(v)
- if err != nil {
- return area, flashAreaErr(name, err.Error())
- }
- sizePresent = true
-
- default:
- util.StatusMessage(util.VERBOSITY_QUIET,
- "Warning: flash area \"%s\" contains unrecognized field: %s",
- name, k)
- }
- }
-
- if !isSystem && !idPresent {
- return area, flashAreaErr(name, "required field \"user_id\" missing")
- }
- if !devicePresent {
- return area, flashAreaErr(name, "required field \"device\" missing")
- }
- if !offsetPresent {
- return area, flashAreaErr(name, "required field \"offset\" missing")
- }
- if !sizePresent {
- return area, flashAreaErr(name, "required field \"size\" missing")
- }
-
- return area, nil
-}
-
-func (flashMap FlashMap) unSortedAreas() []FlashArea {
- areas := make([]FlashArea, 0, len(flashMap.Areas))
- for _, area := range flashMap.Areas {
- areas = append(areas, area)
- }
-
- return areas
-}
-
-func (flashMap FlashMap) SortedAreas() []FlashArea {
- idMap := make(map[int]FlashArea, len(flashMap.Areas))
- ids := make([]int, 0, len(flashMap.Areas))
- for _, area := range flashMap.Areas {
- idMap[area.Id] = area
- ids = append(ids, area.Id)
- }
- sort.Ints(ids)
-
- areas := make([]FlashArea, len(ids))
- for i, id := range ids {
- areas[i] = idMap[id]
- }
-
- return areas
-}
-
-func (flashMap FlashMap) DeviceIds() []int {
- deviceMap := map[int]struct{}{}
-
- for _, area := range flashMap.Areas {
- deviceMap[area.Device] = struct{}{}
- }
-
- devices := make([]int, 0, len(deviceMap))
- for device, _ := range deviceMap {
- devices = append(devices, device)
- }
- sort.Ints(devices)
-
- return devices
-}
-
-func areasDistinct(a FlashArea, b FlashArea) bool {
- var lo FlashArea
- var hi FlashArea
-
- if a.Offset < b.Offset {
- lo = a
- hi = b
- } else {
- lo = b
- hi = a
- }
-
- return lo.Device != hi.Device || lo.Offset+lo.Size <= hi.Offset
-}
-
-func (flashMap *FlashMap) detectOverlaps() {
- flashMap.Overlaps = [][]FlashArea{}
-
- // Convert the map to a slice.
- areas := flashMap.unSortedAreas()
-
- for i := 0; i < len(areas)-1; i++ {
- iarea := areas[i]
- for j := i + 1; j < len(areas); j++ {
- jarea := areas[j]
-
- if !areasDistinct(iarea, jarea) {
- flashMap.Overlaps = append(
- flashMap.Overlaps, []FlashArea{iarea, jarea})
- }
-
- if iarea.Id == jarea.Id {
- flashMap.IdConflicts = append(
- flashMap.IdConflicts, []FlashArea{iarea, jarea})
- }
- }
- }
-}
-
-func (flashMap FlashMap) ErrorText() string {
- str := ""
-
- if len(flashMap.IdConflicts) > 0 {
- str += "Conflicting flash area IDs detected:\n"
-
- for _, pair := range flashMap.IdConflicts {
- str += fmt.Sprintf(" (%d) %s =/= %s\n",
- pair[0].Id-AREA_USER_ID_MIN, pair[0].Name, pair[1].Name)
- }
- }
-
- if len(flashMap.Overlaps) > 0 {
- str += "Overlapping flash areas detected:\n"
-
- for _, pair := range flashMap.Overlaps {
- str += fmt.Sprintf(" %s =/= %s\n", pair[0].Name, pair[1].Name)
- }
- }
-
- return str
-}
-
-func Read(ymlFlashMap map[string]interface{}) (FlashMap, error) {
- flashMap := newFlashMap()
-
- ymlAreas := ymlFlashMap["areas"]
- if ymlAreas == nil {
- return flashMap, util.NewNewtError(
- "\"areas\" mapping missing from flash map definition")
- }
-
- areaMap := cast.ToStringMap(ymlAreas)
- for k, v := range areaMap {
- if _, ok := flashMap.Areas[k]; ok {
- return flashMap, flashAreaErr(k, "name conflict")
- }
-
- ymlArea := cast.ToStringMap(v)
- area, err := parseFlashArea(k, ymlArea)
- if err != nil {
- return flashMap, flashAreaErr(k, err.Error())
- }
-
- flashMap.Areas[k] = area
- }
-
- flashMap.detectOverlaps()
-
- return flashMap, nil
-}
-
-func (flashMap FlashMap) varDecl() string {
- return fmt.Sprintf("const struct flash_area %s[%d]", C_VAR_NAME,
- len(flashMap.Areas))
-}
-
-func (area FlashArea) writeHeader(w io.Writer) {
- fmt.Fprintf(w, "#define %-40s %d\n", area.Name, area.Id)
-}
-
-func (flashMap FlashMap) writeHeader(w io.Writer) {
- fmt.Fprintf(w, newtutil.GeneratedPreamble())
-
- fmt.Fprintf(w, "#ifndef H_MYNEWT_SYSFLASH_\n")
- fmt.Fprintf(w, "#define H_MYNEWT_SYSFLASH_\n")
- fmt.Fprintf(w, "\n")
- fmt.Fprintf(w, "#include \"flash_map/flash_map.h\"\n")
- fmt.Fprintf(w, "\n")
- fmt.Fprintf(w, "%s", C_VAR_COMMENT)
- fmt.Fprintf(w, "extern %s;\n", flashMap.varDecl())
- fmt.Fprintf(w, "\n")
-
- for _, area := range flashMap.SortedAreas() {
- area.writeHeader(w)
- }
-
- fmt.Fprintf(w, "\n#endif\n")
-}
-
-func sizeComment(size int) string {
- if size%1024 != 0 {
- return ""
- }
-
- return fmt.Sprintf(" /* %d kB */", size/1024)
-}
-
-func (area FlashArea) writeSrc(w io.Writer) {
- fmt.Fprintf(w, " /* %s */\n", area.Name)
- fmt.Fprintf(w, " {\n")
- fmt.Fprintf(w, " .fa_id = %d,\n", area.Id)
- fmt.Fprintf(w, " .fa_device_id = %d,\n", area.Device)
- fmt.Fprintf(w, " .fa_off = 0x%08x,\n", area.Offset)
- fmt.Fprintf(w, " .fa_size = %d,%s\n", area.Size,
- sizeComment(area.Size))
- fmt.Fprintf(w, " },\n")
-}
-
-func (flashMap FlashMap) writeSrc(w io.Writer) {
- fmt.Fprintf(w, newtutil.GeneratedPreamble())
-
- fmt.Fprintf(w, "#include \"%s\"\n", HEADER_PATH)
- fmt.Fprintf(w, "\n")
- fmt.Fprintf(w, "%s", C_VAR_COMMENT)
- fmt.Fprintf(w, "%s = {", flashMap.varDecl())
-
- for _, area := range flashMap.SortedAreas() {
- fmt.Fprintf(w, "\n")
- area.writeSrc(w)
- }
-
- fmt.Fprintf(w, "};\n")
-}
-
-func (flashMap FlashMap) ensureWrittenGen(path string, contents []byte) error {
- writeReqd, err := util.FileContentsChanged(path, contents)
- if err != nil {
- return err
- }
- if !writeReqd {
- log.Debugf("flash map unchanged; not writing file (%s).", path)
- return nil
- }
-
- log.Debugf("flash map changed; writing file (%s).", path)
-
- if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
- return util.NewNewtError(err.Error())
- }
-
- if err := ioutil.WriteFile(path, contents, 0644); err != nil {
- return util.NewNewtError(err.Error())
- }
-
- return nil
-}
-
-func (flashMap FlashMap) EnsureWritten(
- srcDir string, includeDir string, targetName string) error {
-
- buf := bytes.Buffer{}
- flashMap.writeSrc(&buf)
- if err := flashMap.ensureWrittenGen(
- fmt.Sprintf("%s/%s-sysflash.c", srcDir, targetName),
- buf.Bytes()); err != nil {
-
- return err
- }
-
- buf = bytes.Buffer{}
- flashMap.writeHeader(&buf)
- if err := flashMap.ensureWrittenGen(
- includeDir+"/"+HEADER_PATH, buf.Bytes()); err != nil {
- return err
- }
-
- return nil
-}