You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by st...@apache.org on 2016/07/14 21:31:31 UTC

incubator-mynewt-newt git commit: add sync command to newt to resynchronize directories based on project.state file.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop 224eb6997 -> 847641b85


add sync command to newt to resynchronize directories based on project.state file.


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/847641b8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/847641b8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/847641b8

Branch: refs/heads/develop
Commit: 847641b85ab1235d6b75a6e6601ad6b38f5a3349
Parents: 224eb69
Author: Sterling Hughes <st...@apache.org>
Authored: Thu Jul 14 14:31:14 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Jul 14 14:31:27 2016 -0700

----------------------------------------------------------------------
 newt/cli/project_cmds.go | 46 ++++++++++++++++++++++++
 newt/repo/repo.go        | 84 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 113 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/847641b8/newt/cli/project_cmds.go
----------------------------------------------------------------------
diff --git a/newt/cli/project_cmds.go b/newt/cli/project_cmds.go
index abe7e3d..8b4dc43 100644
--- a/newt/cli/project_cmds.go
+++ b/newt/cli/project_cmds.go
@@ -33,6 +33,7 @@ import (
 )
 
 var projectForce bool = false
+var syncForce bool = false
 
 func newRunCmd(cmd *cobra.Command, args []string) {
 	if len(args) < 1 {
@@ -158,6 +159,38 @@ func infoRunCmd(cmd *cobra.Command, args []string) {
 	}
 }
 
+func syncRunCmd(cmd *cobra.Command, args []string) {
+	proj := project.GetProject()
+	repos := proj.Repos()
+
+	ps, err := project.LoadProjectState()
+	if err != nil {
+		NewtUsage(nil, err)
+	}
+
+	for rName, repo := range repos {
+		var exists bool
+		if rName == "local" {
+			continue
+		}
+		vers := ps.GetInstalledVersion(repo.Name())
+		if vers == nil {
+			util.StatusMessage(util.VERBOSITY_DEFAULT,
+				"No installed version of %s found, skipping\n",
+				repo.Name())
+		}
+		if err, exists = repo.Sync(vers, syncForce); err != nil {
+			NewtUsage(nil, err)
+		}
+
+		if exists && !syncForce {
+			util.StatusMessage(util.VERBOSITY_DEFAULT,
+				"Skipping resync of %s because directory exists.  To "+
+					"force resync, add the -f (force) option.\n", repo.Name())
+		}
+	}
+}
+
 func AddProjectCommands(cmd *cobra.Command) {
 	installHelpText := ""
 	installHelpEx := ""
@@ -188,6 +221,19 @@ func AddProjectCommands(cmd *cobra.Command) {
 
 	cmd.AddCommand(upgradeCmd)
 
+	syncHelpText := ""
+	syncHelpEx := ""
+	syncCmd := &cobra.Command{
+		Use:     "sync",
+		Short:   "Synchronize project dependencies",
+		Long:    syncHelpText,
+		Example: syncHelpEx,
+		Run:     syncRunCmd,
+	}
+	syncCmd.PersistentFlags().BoolVarP(&syncForce, "force", "f", false,
+		"Force overwrite of existing remote repositories.")
+	cmd.AddCommand(syncCmd)
+
 	newHelpText := ""
 	newHelpEx := ""
 	newCmd := &cobra.Command{

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/847641b8/newt/repo/repo.go
----------------------------------------------------------------------
diff --git a/newt/repo/repo.go b/newt/repo/repo.go
index 9b3fada..4d55631 100644
--- a/newt/repo/repo.go
+++ b/newt/repo/repo.go
@@ -274,16 +274,50 @@ func (r *Repo) repoFilePath() string {
 		".configs/" + r.name + "/"
 }
 
-func (r *Repo) Install(force bool) (*Version, error) {
+func (r *Repo) downloadRepo(branchName string) error {
+	dl := r.downloader
+
+	// Download the git repo, returns the git repo, checked out to that branch
+	tmpdir, err := dl.DownloadRepo(branchName)
+	if err != nil {
+		return util.NewNewtError(fmt.Sprintf("Error download repository %s, : %s",
+			r.Name(), err.Error()))
+	}
+
+	// Copy the Git repo into the the desired local path of the repo
+	if err := util.CopyDir(tmpdir, r.Path()); err != nil {
+		// Cleanup any directory that might have been created if we error out
+		// here.
+		os.RemoveAll(r.Path())
+		return err
+	}
+
+	return nil
+}
+
+func (r *Repo) checkForceInstall(force bool) (error, bool) {
 	// Copy the git repo into /repos/, error'ing out if the repo already exists
 	if util.NodeExist(r.Path()) {
 		if force {
 			if err := os.RemoveAll(r.Path()); err != nil {
-				return nil, util.NewNewtError(err.Error())
+				return util.NewNewtError(err.Error()), true
+			}
+		}
+		return nil, true
+	}
+	return nil, false
+}
+
+func (r *Repo) Install(force bool) (*Version, error) {
+	if err, exists := r.checkForceInstall(force); err != nil || exists {
+		if err == nil {
+			if !force {
+				return nil, util.NewNewtError(fmt.Sprintf(
+					"Repository %s already exists, provide the -f option "+
+						"to overwrite", r.Name()))
 			}
 		} else {
-			return nil, util.NewNewtError(fmt.Sprintf("Repository %s already "+
-				"exists in local tree, cannot install.  Provide -f to override.", r.Path()))
+			return nil, err
 		}
 	}
 
@@ -293,24 +327,40 @@ func (r *Repo) Install(force bool) (*Version, error) {
 			r.rdesc.String()))
 	}
 
-	dl := r.downloader
+	if err := r.downloadRepo(branchName); err != nil {
+		return nil, err
+	}
 
-	// Download the git repo, returns the git repo, checked out to that branch
-	tmpdir, err := dl.DownloadRepo(branchName)
-	if err != nil {
-		return nil, util.NewNewtError(fmt.Sprintf("Error download repository %s, : %s",
-			r.Name(), err.Error()))
+	return vers, nil
+}
+
+func (r *Repo) Sync(vers *Version, force bool) (error, bool) {
+	var exists bool
+	var err error
+
+	if err, exists = r.checkForceInstall(force); err != nil {
+		return err, exists
+	}
+	if exists && !force {
+		return nil, exists
 	}
 
-	// Copy the Git repo into the the desired local path of the repo
-	if err := util.CopyDir(tmpdir, r.Path()); err != nil {
-		// Cleanup any directory that might have been created if we error out
-		// here.
-		os.RemoveAll(r.Path())
-		return nil, err
+	// Update the repo description
+	if _, updated, err := r.UpdateDesc(); updated != true || err != nil {
+		return util.NewNewtError("Cannot update repository description."), exists
 	}
 
-	return vers, nil
+	branchName, vers, found := r.rdesc.MatchVersion(vers)
+	if found == false {
+		return util.NewNewtError(fmt.Sprintf("Branch description for %s not found",
+			r.Name())), exists
+	}
+
+	if err := r.downloadRepo(branchName); err != nil {
+		return err, exists
+	}
+
+	return nil, exists
 }
 
 func (r *Repo) UpdateDesc() ([]*Repo, bool, error) {