Skip to content

Commit cd2a1b1

Browse files
authored
Merge pull request #21 from nexB/11-nuget
Support lockfile generation for NuGet project
2 parents 5326acc + 9611efc commit cd2a1b1

6 files changed

Lines changed: 102 additions & 13 deletions

File tree

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Supported Ecosystems
3333
- **swift**: https://www.swift.org/documentation/package-manager/
3434
- **cocoapods**: https://cocoapods.org/
3535
- **pypi**: https://pypi.org/
36+
- **nuget**: https://www.nuget.org/
3637

3738

3839
Installation

cmd/nuget.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
3+
Copyright (c) nexB Inc. and others. All rights reserved.
4+
ScanCode is a trademark of nexB Inc.
5+
SPDX-License-Identifier: Apache-2.0
6+
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
See https://github.com/nexB/dependency-inspector for support or download.
8+
See https://aboutcode.org for more information about nexB OSS projects.
9+
10+
*/
11+
12+
package cmd
13+
14+
import (
15+
"github.com/nexB/dependency-inspector/internal"
16+
"github.com/spf13/cobra"
17+
)
18+
19+
func nugetCmd() *cobra.Command {
20+
forced := false
21+
22+
nugetCmd := &cobra.Command{
23+
Use: "nuget [path]",
24+
Short: "Generate lockfile for NuGet project",
25+
Long: `Create lockfile for NuGet project if it doesn't exist in the specified [path].
26+
If no path is provided, the command defaults to the current directory.`,
27+
Args: cobra.MaximumNArgs(1),
28+
Run: func(cmd *cobra.Command, args []string) {
29+
internal.CreateLockFileNuGet(
30+
args,
31+
forced,
32+
)
33+
},
34+
}
35+
36+
nugetCmd.Flags().BoolVarP(&forced, "force", "f", false, "Generate lockfile forcibly, ignoring existing lockfiles")
37+
38+
return nugetCmd
39+
}

cmd/root.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ var ecosystems = []func() *cobra.Command{
2525
swiftCmd,
2626
cocoapodsCmd,
2727
pypiCmd,
28+
nugetCmd,
2829
}
2930

30-
func NewRootCmd() *cobra.Command {
31+
func rootCmd() *cobra.Command {
3132
rootCmd := &cobra.Command{
3233
Use: "deplock",
3334
Short: "DepLock: Dependency Locker CLI",
@@ -48,7 +49,7 @@ func initConfig(rootCmd *cobra.Command) {
4849
}
4950

5051
func Execute() {
51-
rootCmd := NewRootCmd()
52+
rootCmd := rootCmd()
5253

5354
if err := rootCmd.Execute(); err != nil {
5455
fmt.Fprintln(os.Stderr, "Error:", err)

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ module github.com/nexB/dependency-inspector
22

33
go 1.22.3
44

5-
require github.com/spf13/cobra v1.8.0
5+
require (
6+
github.com/bmatcuk/doublestar/v4 v4.6.1
7+
github.com/spf13/cobra v1.8.0
8+
)
69

710
require (
811
github.com/inconshreveable/mousetrap v1.1.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/bmatcuk/doublestar/v4 v4.6.1 h1:FH9SifrbvJhnlQpztAx++wlkk70QBf0iBWDwNy7PA4I=
2+
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
13
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
24
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
35
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=

internal/utils.go

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,18 @@ import (
1616
"os"
1717
"os/exec"
1818
"path/filepath"
19+
20+
"github.com/bmatcuk/doublestar/v4"
1921
)
2022

2123
func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, outputFileName string, forced bool) {
22-
23-
path := "."
24-
if len(cmdArgs) > 0 {
25-
path = cmdArgs[0]
26-
}
27-
28-
absPath, err := filepath.Abs(path)
29-
if err != nil {
30-
fmt.Fprintln(os.Stderr, "Error: Failed to retrieve absolute path: ", err)
24+
absPath := getAbsPath(cmdArgs)
25+
if absPath == "" {
3126
return
3227
}
3328

3429
if !forced {
30+
// If any lockfile is present already then skip lockfile generation.
3531
for _, lockFile := range lockFiles {
3632
lockFileAbsPath := filepath.Join(absPath, lockFile)
3733

@@ -66,8 +62,23 @@ func DoesFileExists(absPath string) bool {
6662
return false
6763
}
6864

65+
func getAbsPath(cmdArgs []string) string {
66+
path := "."
67+
if len(cmdArgs) > 0 {
68+
path = cmdArgs[0]
69+
}
70+
71+
absPath, err := filepath.Abs(path)
72+
if err != nil {
73+
fmt.Fprintln(os.Stderr, "Error: Failed to retrieve absolute path: ", err)
74+
return ""
75+
}
76+
77+
return absPath
78+
}
79+
6980
func genLock(lockGenCmd []string, absPath string, outputFileName string) {
70-
fmt.Printf("Generating lockfile using '%s'\n", lockGenCmd)
81+
fmt.Printf("Generating lockfile at '%s' using '%s'\n", absPath, lockGenCmd)
7182

7283
// #nosec G204
7384
command := exec.Command(lockGenCmd[0], lockGenCmd[1:]...)
@@ -96,3 +107,35 @@ func genLock(lockGenCmd []string, absPath string, outputFileName string) {
96107

97108
fmt.Println("Lock file generated successfully.")
98109
}
110+
111+
func CreateLockFileNuGet(cmdArgs []string, force bool) {
112+
nuGetLockFileName := "packages.lock.json"
113+
nuGetLockFileGenCmd := []string{"dotnet", "restore", "--use-lock-file"}
114+
115+
project_path := getAbsPath(cmdArgs)
116+
if project_path == "" {
117+
return
118+
}
119+
120+
fs := os.DirFS(project_path)
121+
csproj_pattern := "**/*.csproj"
122+
123+
csproj_files, _ := doublestar.Glob(fs, csproj_pattern)
124+
if len(csproj_files) == 0 {
125+
fmt.Fprintln(os.Stderr, "Error: Path does not contain a NuGet project")
126+
return
127+
}
128+
129+
// Generate lockfile for all NuGet projects
130+
for _, file := range csproj_files {
131+
fullPath := filepath.Join(project_path, file)
132+
dir := filepath.Dir(fullPath)
133+
134+
lockFile := filepath.Join(dir, nuGetLockFileName)
135+
if force || !DoesFileExists(lockFile) {
136+
genLock(nuGetLockFileGenCmd, dir, "")
137+
}
138+
139+
}
140+
141+
}

0 commit comments

Comments
 (0)