Skip to content

Commit 95709ff

Browse files
authored
Merge pull request #16 from nexB/12-swiftpm
Support generation of lockfile and manifest JSON dump for Swift project
2 parents 85d9b48 + 2fabe3e commit 95709ff

8 files changed

Lines changed: 104 additions & 7 deletions

File tree

Makefile

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,25 @@ BUILD_DIR=./build
2020
GOOS := $(shell $(GOCMD) env GOOS)
2121
GOARCH := $(shell $(GOCMD) env GOARCH)
2222
BINARY_OUTPUT=$(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH)
23+
BUILD_FLAGS=
24+
STRIP_FLAG=
25+
26+
strip ?= no
27+
28+
# Omit symbol table, debug info and DWARF symbol table
29+
# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123
30+
ifeq ($(strip), yes)
31+
BUILD_FLAGS=-s -w
32+
STRIP_FLAG=--strip
33+
endif
2334

2435

2536
build:
2637
@echo "Building binary to $(BINARY_OUTPUT)"
27-
$(GOCMD) build -o $(BINARY_OUTPUT) -v
38+
$(GOCMD) build -ldflags "$(BUILD_FLAGS)" -o $(BINARY_OUTPUT)
2839

2940
build-all:
30-
./scripts/build-all.sh
41+
./scripts/build-all.sh $(STRIP_FLAG)
3142

3243
clean:
3344
$(GOCMD) clean

cmd/npm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
3232
lockFiles,
3333
args,
3434
lockGenCommand,
35+
"",
3536
forced,
3637
)
3738
},

cmd/pnpm.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
3232
lockFiles,
3333
args,
3434
lockGenCommand,
35+
"",
3536
forced,
3637
)
3738
},

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var ecosystems = []func() *cobra.Command{
2222
pnpmCmd,
2323
npmCmd,
2424
yarnCmd,
25+
swiftCmd,
2526
}
2627

2728
func NewRootCmd() *cobra.Command {

cmd/swift.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 swiftCmd() *cobra.Command {
20+
lockFiles := [][]string{
21+
{"Package.resolved", ".package.resolved"},
22+
{"Package.swift.json"},
23+
}
24+
lockGenCommands := [][]string{
25+
{"swift", "package", "resolve"},
26+
{"swift", "package", "dump-package"},
27+
}
28+
commandOutput := []string{
29+
"",
30+
"Package.swift.json",
31+
}
32+
forced := false
33+
34+
swiftCmd := &cobra.Command{
35+
Use: "swift [path]",
36+
Short: "Generate lockfile for swift project",
37+
Long: `Create lockfile and JSON dump of manifest for swift project
38+
if they doesn't already exist in the specified [path].
39+
If no path is provided, the command defaults to the current directory.`,
40+
Args: cobra.MaximumNArgs(1),
41+
42+
Run: func(cmd *cobra.Command, args []string) {
43+
44+
for i := range lockFiles {
45+
internal.CreateLockFile(
46+
lockFiles[i],
47+
args,
48+
lockGenCommands[i],
49+
commandOutput[i],
50+
forced,
51+
)
52+
}
53+
54+
},
55+
}
56+
57+
swiftCmd.Flags().BoolVarP(&forced, "force", "f", false, "Generate lockfile forcibly, ignoring existing lockfiles")
58+
59+
return swiftCmd
60+
}

cmd/yarn.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
3232
lockFiles,
3333
args,
3434
lockGenCommand,
35+
"",
3536
forced,
3637
)
3738
},

internal/utils.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"path/filepath"
1919
)
2020

21-
func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, forced bool) {
21+
func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, outputFileName string, forced bool) {
2222

2323
path := "."
2424
if len(cmdArgs) > 0 {
@@ -42,7 +42,7 @@ func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, f
4242

4343
}
4444
}
45-
genLock(lockGenCmd, absPath)
45+
genLock(lockGenCmd, absPath, outputFileName)
4646

4747
}
4848

@@ -67,14 +67,28 @@ func DoesFileExists(absPath string) bool {
6767
return false
6868
}
6969

70-
func genLock(lockGenCmd []string, absPath string) {
70+
func genLock(lockGenCmd []string, absPath string, outputFileName string) {
7171
fmt.Printf("Generating lockfile using '%s'\n", lockGenCmd)
7272

7373
// #nosec G204
7474
command := exec.Command(lockGenCmd[0], lockGenCmd[1:]...)
7575
command.Dir = absPath
76-
command.Stdout = os.Stdout
7776
command.Stderr = os.Stderr
77+
command.Stdout = os.Stdout
78+
if outputFileName != "" {
79+
80+
outputPath := filepath.Join(absPath, outputFileName)
81+
82+
// #nosec G304
83+
outputFile, err := os.Create(outputPath)
84+
if err != nil {
85+
fmt.Fprintln(os.Stderr, "Error: failed to create output file: ", err)
86+
os.Exit(1)
87+
}
88+
defer outputFile.Close()
89+
90+
command.Stdout = outputFile
91+
}
7892

7993
if err := command.Run(); err != nil {
8094
fmt.Fprintln(os.Stderr, "Error: Failed to generate lockfile: ", err)

scripts/build-all.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ platforms=(
1717
"windows/amd64"
1818
)
1919

20+
build_flags=
21+
22+
# Omit symbol table, debug info and DWARF symbol table
23+
# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123
24+
if [ "$1" == "--strip" ]; then
25+
build_flags="-s -w"
26+
fi
27+
2028
for platform in "${platforms[@]}"
2129
do
2230
platform_split=(${platform//\// })
@@ -28,7 +36,7 @@ do
2836
output_name+='.exe'
2937
fi
3038

31-
env GOOS=$GOOS GOARCH=$GOARCH go build -o ./build/$output_name
39+
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "$build_flags" -o ./build/$output_name
3240

3341
if [ $? -ne 0 ]; then
3442
echo "An error occurred during the build process!"

0 commit comments

Comments
 (0)