Skip to content

Commit

Permalink
feat: add command to print urls from deps file (#17)
Browse files Browse the repository at this point in the history
Addt'l
- chore: add ibazel run output file
  This allows ibazel to run another command if the output matches
  • Loading branch information
kreempuff authored Jan 31, 2023
2 parents 8cb2739 + f9df8ff commit 2bd82d6
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 56 deletions.
7 changes: 7 additions & 0 deletions .bazel_fix_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"regex": "^.*\\.go.*undefined:.*$",
"command": "bazel",
"args": [ "run", "//:gazelle" ]
}
]
1 change: 1 addition & 0 deletions cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"exit.go",
"gitDeps.go",
"printUrls.go",
"root.go",
],
importpath = "kreempuff.dev/rules-unreal-engine/cmd",
Expand Down
57 changes: 6 additions & 51 deletions cmd/gitDeps.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
package cmd

import (
"bytes"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"kreempuff.dev/rules-unreal-engine/pkg/gitDeps"
"net/http"
"os"
"path/filepath"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var gitDepsCmd = &cobra.Command{
Expand All @@ -21,56 +18,13 @@ var gitDepsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
logrus.SetLevel(logrus.DebugLevel)

input, err := cmd.Flags().GetString("input")

input, err := cmd.Flags().GetString(gitDeps.InputFlag)
if err != nil {
logrus.Error(err)
logrus.Exit(UnknownExitCode)
}

// Expand full path from relative path
abs, err := filepath.Abs(input)

if err != nil {
logrus.Error(err)
logrus.Exit(UnknownExitCode)
}

// Check if directory exists
stat, err := os.Stat(abs)

if err != nil && os.IsNotExist(err) {
logrus.Error("Directory does not exist")
logrus.Exit(UnknownExitCode)
}

// Find dependency file
var depFile string

if stat.IsDir() {
depFile = filepath.Join(abs, ".ue4dependencies")
} else {
depFile = input
}
_, err = os.Stat(depFile)

if os.IsNotExist(err) {
logrus.Errorf("dependency file does not exist: %s", err)
logrus.Exit(UnknownExitCode)
}

// TODO
// check if file is a file

f, err := os.ReadFile(depFile)

if err != nil {
logrus.Errorf("error opening dependency file: %s", err)
logrus.Exit(UnknownExitCode)
}
buf := bytes.NewBuffer(f)

manifest, err := gitDeps.ParseFile(buf)
manifest, err := gitDeps.GetManifestFromInput(input)
if err != nil {
logrus.Errorf("error decoding dependency file: %s", err)
logrus.Exit(UnknownExitCode)
Expand Down Expand Up @@ -102,8 +56,9 @@ func init() {
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// gitDepsCmd.PersistentFlags().String("foo", "", "A help for foo")
gitDepsCmd.PersistentFlags().StringP("input", "i", ".", "The dependency file or directory to parse dependencies from")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
gitDepsCmd.Flags().StringP("input", "i", ".", "The dependency file or directory to parse dependencies from")
//gitDepsCmd.Flags().StringP("input", "i", ".", "The dependency file or directory to parse dependencies from")
}
85 changes: 85 additions & 0 deletions cmd/printUrls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cmd

import (
"fmt"
"github.com/sirupsen/logrus"
"kreempuff.dev/rules-unreal-engine/pkg/gitDeps"

"github.com/spf13/cobra"
)

// printUrlsCmd represents the printUrls command
var printUrlsCmd = &cobra.Command{
Use: "printUrls",
Short: "Prints the urls of the packs in the dependency file",
Long: `Prints the urls of the packs in the dependency file`,
Run: func(cmd *cobra.Command, args []string) {
input, err := cmd.Flags().GetString(gitDeps.InputFlag)
if err != nil {
logrus.Error(err)
logrus.Exit(UnknownExitCode)
}

output, err := cmd.Flags().GetString("output")
if err != nil {
logrus.Error(err)
logrus.Exit(UnknownExitCode)
}

manifest, err := gitDeps.GetManifestFromInput(input)
if err != nil {
logrus.Errorf("error decoding dependency file: %s", err)
logrus.Exit(UnknownExitCode)
}

fmt.Println(len(manifest.Files))
fmt.Println(len(manifest.Packs))
fmt.Println(len(manifest.Blobs))
urls := gitDeps.GetPackUrls(*manifest)

out := ""
switch output {
case "json":
out = formatUrlsAsJson(urls)
case "bazel":
out = formatUrlsAsBazel(urls)
}

fmt.Println(out)
},
}

// formatUrlsAsBazel formats the urls as a list of bazel urls
func formatUrlsAsBazel(urls []string) string {
var output string
for _, url := range urls {
output += fmt.Sprintf("urls = [\"%s\"],", url)
}
return output
}

// formatUrlsAsJson formats the urls as an array of json objects
// with the key 'url' and the value being the url
func formatUrlsAsJson(urls []string) string {
var output string
output += "["
for _, url := range urls {
output += fmt.Sprintf("{\"url\": \"%s\"},", url)
}
output += "]"
return output
}

func init() {
gitDepsCmd.AddCommand(printUrlsCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// printUrlsCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
printUrlsCmd.Flags().StringP("output", "o", "json", "How the urls should be printed. Valid values are 'json' and 'bazel'.")
}
7 changes: 2 additions & 5 deletions pkg/gitDeps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "gitDeps",
srcs = [
"constants.go",
"gitDeps.go",
"xml.go",
],
Expand All @@ -19,9 +20,5 @@ go_test(
],
embed = [":gitDeps"],
embedsrcs = ["working-manifest-test.xml"],
deps = [
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//http",
"@com_github_stretchr_testify//mock",
],
deps = ["@com_github_stretchr_testify//assert"],
)
6 changes: 6 additions & 0 deletions pkg/gitDeps/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gitDeps

const (
// Input flag
InputFlag = "input"
)
55 changes: 55 additions & 0 deletions pkg/gitDeps/gitDeps.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package gitDeps

import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
)

func ParseDir(dir fs.FS) []WorkingManifest {
Expand Down Expand Up @@ -102,3 +105,55 @@ func GetPackfromFileName(filename string, w WorkingManifest) *Pack {

return pack
}

// GetPackUrls returns a list of urls for all the packs in a manifest file
func GetPackUrls(w WorkingManifest) []string {
var urls []string
for _, p := range w.Packs {
urls = append(urls, fmt.Sprintf("%s/%s/%s", w.BaseUrl, p.RemotePath, p.Hash))
}
return urls
}

// GetManifestFromInput takes a string that represents a path or directory to a manifest file and returns
// a data structure representing the file for further processing
func GetManifestFromInput(input string) (*WorkingManifest, error) {
// Expand full path from relative path
abs, err := filepath.Abs(input)

if err != nil {
return nil, err
}

// Check if directory exists
stat, err := os.Stat(abs)

if err != nil && os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %w", err)
}

// Find dependency file
var depFile string

if stat.IsDir() {
depFile = filepath.Join(abs, ".ue4dependencies")
} else {
depFile = input
}
_, err = os.Stat(depFile)

if os.IsNotExist(err) {
return nil, fmt.Errorf("file does not exist: %w", err)
}

// TODO
// check if file is a file

f, err := os.ReadFile(depFile)

if err != nil {
return nil, err
}
buf := bytes.NewBuffer(f)
return ParseFile(buf)
}
34 changes: 34 additions & 0 deletions pkg/gitDeps/gitDeps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,37 @@ func TestDownloadPack(t *testing.T) {
})
}
}

func TestGetPackUrls(t *testing.T) {
type args struct {
w WorkingManifest
}
tests := []struct {
name string
args args
want []string
}{
{
name: "happy path",
args: args{
w: WorkingManifest{
BaseUrl: "https://some-base-url",
Packs: []Pack{
{
Hash: "some-hash",
Size: 0,
CompressedSize: 0,
RemotePath: "some-remote-path",
},
},
},
},
want: []string{"https://some-base-url/some-remote-path/some-hash"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, GetPackUrls(tt.args.w), "GetPackUrls(%v)", tt.args.w)
})
}
}

0 comments on commit 2bd82d6

Please sign in to comment.