Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ BUILD_DIR=./build
GOOS := $(shell $(GOCMD) env GOOS)
GOARCH := $(shell $(GOCMD) env GOARCH)
BINARY_OUTPUT=$(BUILD_DIR)/$(BINARY_NAME)-$(GOOS)-$(GOARCH)
BUILD_FLAGS=
STRIP_FLAG=

strip ?= no

# Omit symbol table, debug info and DWARF symbol table
# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123
ifeq ($(strip), yes)
BUILD_FLAGS=-s -w
STRIP_FLAG=--strip
endif


build:
@echo "Building binary to $(BINARY_OUTPUT)"
$(GOCMD) build -o $(BINARY_OUTPUT) -v
$(GOCMD) build -ldflags "$(BUILD_FLAGS)" -o $(BINARY_OUTPUT)

build-all:
./scripts/build-all.sh
./scripts/build-all.sh $(STRIP_FLAG)

clean:
$(GOCMD) clean
Expand Down
1 change: 1 addition & 0 deletions cmd/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
lockFiles,
args,
lockGenCommand,
"",
forced,
)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/pnpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
lockFiles,
args,
lockGenCommand,
"",
forced,
)
},
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ecosystems = []func() *cobra.Command{
pnpmCmd,
npmCmd,
yarnCmd,
swiftCmd,
}

func NewRootCmd() *cobra.Command {
Expand Down
60 changes: 60 additions & 0 deletions cmd/swift.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*

Copyright (c) nexB Inc. and others. All rights reserved.
ScanCode is a trademark of nexB Inc.
SPDX-License-Identifier: Apache-2.0
See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
See https://github.com/nexB/dependency-inspector for support or download.
See https://aboutcode.org for more information about nexB OSS projects.

*/

package cmd

import (
"github.com/nexB/dependency-inspector/internal"
"github.com/spf13/cobra"
)

func swiftCmd() *cobra.Command {
lockFiles := [][]string{
{"Package.resolved", ".package.resolved"},
{"Package.swift.json"},
}
lockGenCommands := [][]string{
{"swift", "package", "resolve"},
{"swift", "package", "dump-package"},
}
commandOutput := []string{
"",
"Package.swift.json",
}
forced := false

swiftCmd := &cobra.Command{
Use: "swift [path]",
Short: "Generate lockfile for swift project",
Long: `Create lockfile and JSON dump of manifest for swift project
if they doesn't already exist in the specified [path].
If no path is provided, the command defaults to the current directory.`,
Args: cobra.MaximumNArgs(1),

Run: func(cmd *cobra.Command, args []string) {

for i := range lockFiles {
internal.CreateLockFile(
lockFiles[i],
args,
lockGenCommands[i],
commandOutput[i],
forced,
)
}

},
}

swiftCmd.Flags().BoolVarP(&forced, "force", "f", false, "Generate lockfile forcibly, ignoring existing lockfiles")

return swiftCmd
}
1 change: 1 addition & 0 deletions cmd/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ If no path is provided, the command defaults to the current directory.`,
lockFiles,
args,
lockGenCommand,
"",
forced,
)
},
Expand Down
22 changes: 18 additions & 4 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"path/filepath"
)

func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, forced bool) {
func CreateLockFile(lockFiles []string, cmdArgs []string, lockGenCmd []string, outputFileName string, forced bool) {

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

}
}
genLock(lockGenCmd, absPath)
genLock(lockGenCmd, absPath, outputFileName)

}

Expand All @@ -67,14 +67,28 @@ func DoesFileExists(absPath string) bool {
return false
}

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

// #nosec G204
command := exec.Command(lockGenCmd[0], lockGenCmd[1:]...)
command.Dir = absPath
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Stdout = os.Stdout
if outputFileName != "" {

outputPath := filepath.Join(absPath, outputFileName)

// #nosec G304
outputFile, err := os.Create(outputPath)
if err != nil {
fmt.Fprintln(os.Stderr, "Error: failed to create output file: ", err)
os.Exit(1)
}
defer outputFile.Close()

command.Stdout = outputFile
}

if err := command.Run(); err != nil {
fmt.Fprintln(os.Stderr, "Error: Failed to generate lockfile: ", err)
Expand Down
10 changes: 9 additions & 1 deletion scripts/build-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ platforms=(
"windows/amd64"
)

build_flags=

# Omit symbol table, debug info and DWARF symbol table
# https://github.com/golang/go/blob/ca5ba146da7a9d4e2a8cbe1715a78be42b45a745/src/cmd/link/doc.go#L115-L123
if [ "$1" == "--strip" ]; then
build_flags="-s -w"
fi

for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
Expand All @@ -28,7 +36,7 @@ do
output_name+='.exe'
fi

env GOOS=$GOOS GOARCH=$GOARCH go build -o ./build/$output_name
env GOOS=$GOOS GOARCH=$GOARCH go build -ldflags "$build_flags" -o ./build/$output_name

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