ccollins476ad closed pull request #139: Selective install
URL: https://github.com/apache/mynewt-newt/pull/139
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/newt/cli/project_cmds.go b/newt/cli/project_cmds.go
index e900985e..3ec2f2f0 100644
--- a/newt/cli/project_cmds.go
+++ b/newt/cli/project_cmds.go
@@ -29,6 +29,7 @@ import (
        "mynewt.apache.org/newt/newt/interfaces"
        "mynewt.apache.org/newt/newt/newtutil"
        "mynewt.apache.org/newt/newt/project"
+       "mynewt.apache.org/newt/newt/repo"
        "mynewt.apache.org/newt/util"
 )
 
@@ -76,11 +77,32 @@ func newRunCmd(cmd *cobra.Command, args []string) {
                "Project %s successfully created.\n", newDir)
 }
 
+// Builds a repo selection predicate based on the specified names.  If no names
+// are specified, the resulting function selects all non-local repos.
+// Otherwise, the function selects each non-local repo whose name is specified.
+func makeRepoPredicate(repoNames []string) func(r *repo.Repo) bool {
+       if len(repoNames) == 0 {
+               return func(r *repo.Repo) bool { return !r.IsLocal() }
+       }
+
+       return func(r *repo.Repo) bool {
+               if !r.IsLocal() {
+                       for _, arg := range repoNames {
+                               if r.Name() == arg {
+                                       return true
+                               }
+                       }
+               }
+               return false
+       }
+}
+
 func installRunCmd(cmd *cobra.Command, args []string) {
        proj := TryGetProject()
        interfaces.SetProject(proj)
 
-       if err := proj.Install(false, newtutil.NewtForce); err != nil {
+       pred := makeRepoPredicate(args)
+       if err := proj.InstallIf(false, newtutil.NewtForce, pred); err != nil {
                NewtUsage(nil, err)
        }
 }
@@ -89,7 +111,8 @@ func upgradeRunCmd(cmd *cobra.Command, args []string) {
        proj := TryGetProject()
        interfaces.SetProject(proj)
 
-       if err := proj.Upgrade(newtutil.NewtForce); err != nil {
+       pred := makeRepoPredicate(args)
+       if err := proj.InstallIf(true, newtutil.NewtForce, pred); err != nil {
                NewtUsage(nil, err)
        }
 }
@@ -154,7 +177,8 @@ func infoRunCmd(cmd *cobra.Command, args []string) {
 
 func syncRunCmd(cmd *cobra.Command, args []string) {
        proj := TryGetProject()
-       repos := proj.Repos()
+       pred := makeRepoPredicate(args)
+       repos := proj.SelectRepos(pred)
 
        ps, err := project.LoadProjectState()
        if err != nil {
@@ -163,9 +187,6 @@ func syncRunCmd(cmd *cobra.Command, args []string) {
 
        var anyFails bool
        for _, repo := range repos {
-               if repo.IsLocal() {
-                       continue
-               }
                vers := ps.GetInstalledVersion(repo.Name())
                if vers == nil {
                        util.StatusMessage(util.VERBOSITY_DEFAULT,
@@ -192,9 +213,12 @@ func syncRunCmd(cmd *cobra.Command, args []string) {
 
 func AddProjectCommands(cmd *cobra.Command) {
        installHelpText := ""
-       installHelpEx := ""
+       installHelpEx := "  newt install\n"
+       installHelpEx += "    Installs all repositories specified in 
project.yml.\n\n"
+       installHelpEx += "  newt install apache-mynewt-core\n"
+       installHelpEx += "    Installs the apache-mynewt-core repository."
        installCmd := &cobra.Command{
-               Use:     "install",
+               Use:     "install [repo-1] [repo-2] [...]",
                Short:   "Install project dependencies",
                Long:    installHelpText,
                Example: installHelpEx,
@@ -208,9 +232,12 @@ func AddProjectCommands(cmd *cobra.Command) {
        cmd.AddCommand(installCmd)
 
        upgradeHelpText := ""
-       upgradeHelpEx := ""
+       upgradeHelpEx := "  newt upgrade\n"
+       upgradeHelpEx += "    Upgrades all repositories specified in 
project.yml.\n\n"
+       upgradeHelpEx += "  newt upgrade apache-mynewt-core\n"
+       upgradeHelpEx += "    Upgrades the apache-mynewt-core repository."
        upgradeCmd := &cobra.Command{
-               Use:     "upgrade",
+               Use:     "upgrade [repo-1] [repo-2] [...]",
                Short:   "Upgrade project dependencies",
                Long:    upgradeHelpText,
                Example: upgradeHelpEx,
@@ -223,9 +250,12 @@ func AddProjectCommands(cmd *cobra.Command) {
        cmd.AddCommand(upgradeCmd)
 
        syncHelpText := ""
-       syncHelpEx := ""
+       syncHelpEx := "  newt sync\n"
+       syncHelpEx += "    Syncs all repositories specified in project.yml.\n\n"
+       syncHelpEx += "  newt sync apache-mynewt-core\n"
+       syncHelpEx += "    Syncs the apache-mynewt-core repository."
        syncCmd := &cobra.Command{
-               Use:     "sync",
+               Use:     "sync [repo-1] [repo-2] [...]",
                Short:   "Synchronize project dependencies",
                Long:    syncHelpText,
                Example: syncHelpEx,
diff --git a/newt/downloader/downloader.go b/newt/downloader/downloader.go
index 5746bea2..90640d2c 100644
--- a/newt/downloader/downloader.go
+++ b/newt/downloader/downloader.go
@@ -625,9 +625,9 @@ func (ld *LocalDownloader) CurrentBranch(path string) 
(string, error) {
        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"))
+       // Nothing to update in a local repo.
+       return nil
 }
 
 func (ld *LocalDownloader) CleanupRepo(path string, branchName string) error {
diff --git a/newt/project/project.go b/newt/project/project.go
index 6a00ef88..ae37329c 100644
--- a/newt/project/project.go
+++ b/newt/project/project.go
@@ -318,7 +318,7 @@ func (proj *Project) checkDeps(r *repo.Repo) error {
                curRepo, ok := proj.repos[newRepo.Name()]
                if !ok {
                        proj.repos[newRepo.Name()] = newRepo
-                       return proj.UpdateRepos()
+                       return proj.updateRepos(proj.SortedRepos())
                } else {
                        // Add any dependencies we might have found here.
                        for _, dep := range newRepo.Deps() {
@@ -332,9 +332,23 @@ func (proj *Project) checkDeps(r *repo.Repo) error {
        return nil
 }
 
-func (proj *Project) UpdateRepos() error {
-       repoList := proj.Repos()
-       for _, r := range repoList {
+// Selects repositories from the global state that satisfy the specified
+// predicate.
+func (proj *Project) SelectRepos(pred func(r *repo.Repo) bool) []*repo.Repo {
+       all := proj.SortedRepos()
+       var filtered []*repo.Repo
+
+       for _, r := range all {
+               if pred(r) {
+                       filtered = append(filtered, r)
+               }
+       }
+
+       return filtered
+}
+
+func (proj *Project) updateRepos(repos []*repo.Repo) error {
+       for _, r := range repos {
                if r.IsLocal() {
                        continue
                }
@@ -347,8 +361,8 @@ func (proj *Project) UpdateRepos() error {
        return nil
 }
 
-func (proj *Project) Install(upgrade bool, force bool) error {
-       repoList := proj.SortedRepos()
+func (proj *Project) InstallIf(upgrade bool, force bool, predicate func(r 
*repo.Repo) bool) error {
+       repoList := proj.SelectRepos(predicate)
 
        var verb string
        if upgrade {
@@ -370,9 +384,9 @@ func (proj *Project) Install(upgrade bool, force bool) 
error {
                }
        }
 
-       // Fetch "origin" for all repos and copy the current version of
+       // Fetch "origin" for specified repos and copy the current version of
        // `repository.yml`.
-       if err := proj.UpdateRepos(); err != nil {
+       if err := proj.updateRepos(repoList); err != nil {
                return err
        }
 
@@ -380,7 +394,7 @@ func (proj *Project) Install(upgrade bool, force bool) 
error {
        // install procedure locally.
 
        // Get repository list and print every repo and its dependencies.
-       if err := repo.CheckDeps(proj.Repos()); err != nil {
+       if err := repo.CheckDeps(repoList); err != nil {
                return err
        }
 
@@ -435,10 +449,6 @@ func (proj *Project) Install(upgrade bool, force bool) 
error {
        return nil
 }
 
-func (proj *Project) Upgrade(force bool) error {
-       return proj.Install(true, force)
-}
-
 func (proj *Project) loadRepo(rname string, yc ycfg.YCfg) error {
        varName := fmt.Sprintf("repository.%s", rname)
 
diff --git a/newt/repo/repo.go b/newt/repo/repo.go
index 760cd756..55021aaf 100644
--- a/newt/repo/repo.go
+++ b/newt/repo/repo.go
@@ -206,13 +206,18 @@ func pickVersion(repo *Repo, versions []*Version) 
([]*Version, error) {
        }
 }
 
-func CheckDeps(checkRepos map[string]*Repo) error {
+func CheckDeps(repos []*Repo) error {
+       repoMap := map[string]*Repo{}
+       for _, r := range repos {
+               repoMap[r.Name()] = r
+       }
+
        // For each dependency, get it's version
        depArray := map[string][]*Version{}
 
-       for _, checkRepo := range checkRepos {
+       for _, checkRepo := range repoMap {
                for _, rd := range checkRepo.Deps() {
-                       lookupRepo := checkRepos[rd.Name()]
+                       lookupRepo := repoMap[rd.Name()]
 
                        _, vers, ok := lookupRepo.rdesc.Match(rd.Storerepo)
                        if !ok {
@@ -242,7 +247,7 @@ func CheckDeps(checkRepos map[string]*Repo) error {
                        }
                }
                if pickVer {
-                       newArray, err := pickVersion(checkRepos[repoName],
+                       newArray, err := pickVersion(repoMap[repoName],
                                depArray[repoName])
                        depArray[repoName] = newArray
                        if err != nil {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to