-
Notifications
You must be signed in to change notification settings - Fork 95
/
project_cmds.go
247 lines (208 loc) · 6.57 KB
/
project_cmds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* 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"
"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/newt/repo"
"mynewt.apache.org/newt/util"
)
var infoRemote bool
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/mynewt-blinky...\n")
dl := downloader.NewGithubDownloader()
dl.User = "apache"
dl.Repo = "mynewt-blinky"
tmpdir, err := newtutil.MakeTempRepoDir()
if err != nil {
NewtUsage(nil, err)
}
defer os.RemoveAll(tmpdir)
/* For new command don't use shallow copy by default
* as release tag may not be present on tip of master
* branch.
*/
if util.ShallowCloneDepth < 0 {
util.ShallowCloneDepth = 0
}
if err := dl.Clone("master", tmpdir); err != nil {
NewtUsage(nil, err)
}
commit := newtutil.NewtBlinkyTag
err = dl.Checkout(tmpdir, commit)
/* If checkout with final tag fails, try to find latest rc tag */
if err != nil {
commit, err = dl.LatestRc(tmpdir, newtutil.NewtBlinkyTag)
if err != nil {
NewtUsage(nil, err)
}
err = dl.Checkout(tmpdir, commit)
if err != nil {
NewtUsage(nil, err)
}
}
util.StatusMessage(util.VERBOSITY_DEFAULT, "Installing "+
"skeleton in %s (commit: %s)\n", newDir, commit)
if err := util.CopyDir(tmpdir, 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)
}
// 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 the user didn't specify any repo names, apply the operation to all
// repos in `project.yml`.
if len(repoNames) == 0 {
proj := project.GetProject()
return func(r *repo.Repo) bool { return proj.RepoIsRoot(r.Name()) }
}
return func(r *repo.Repo) bool {
if !r.IsLocal() {
for _, arg := range repoNames {
if strings.TrimPrefix(r.Name(), "@") == arg {
return true
}
}
}
return false
}
}
func upgradeRunCmd(cmd *cobra.Command, args []string) {
proj := TryGetOrDownloadProject()
interfaces.SetProject(proj)
proj.GetPkgRepos()
proj.SetGitEnvVariables()
pred := makeRepoPredicate(args)
if err := proj.UpgradeIf(
newtutil.NewtForce, newtutil.NewtAsk, pred); err != nil {
NewtUsage(nil, err)
}
}
func infoRunCmd(cmd *cobra.Command, args []string) {
newtutil.PrintNewtVersion()
proj := TryGetProject()
// If no arguments specified, print status of all installed repos.
if len(args) == 0 {
pred := func(r *repo.Repo) bool { return true }
if err := proj.InfoIf(pred, infoRemote); err != nil {
NewtUsage(nil, err)
}
return
}
// Otherwise, list packages specified repo contains.
reqRepoName := strings.TrimPrefix(args[0], "@")
repoNames := []string{}
for repoName, _ := range proj.PackageList() {
repoNames = append(repoNames, repoName)
}
sort.Strings(repoNames)
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 AddProjectCommands(cmd *cobra.Command) {
upgradeHelpText := ""
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 [repo-1] [repo-2] [...]",
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")
upgradeCmd.PersistentFlags().BoolVarP(&newtutil.NewtAsk,
"ask", "a", false, "Prompt user before upgrading any repos")
cmd.AddCommand(upgradeCmd)
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,
}
infoCmd.PersistentFlags().BoolVarP(&infoRemote,
"remote", "r", false,
"Fetch latest repos to determine if upgrades are required")
cmd.AddCommand(infoCmd)
}