@@ -16,22 +16,18 @@ import (
1616 "os"
1717 "os/exec"
1818 "path/filepath"
19+
20+ "github.com/bmatcuk/doublestar/v4"
1921)
2022
2123func 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+
6980func 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