Skip to content

Commit

Permalink
Merge pull request #29 from Glow-Project/enhance-code
Browse files Browse the repository at this point in the history
Enhance code
  • Loading branch information
Tch1b0 authored Aug 1, 2022
2 parents 16f4f96 + 2ab9814 commit 15703ba
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 71 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ jobs:
with:
go-version: "1.18"

# compile windows executable
- name: Compile to ${{ matrix.os-name }}
if: ${{ startsWith(matrix.os-name, 'windows') }}
env:
GOARCH: ${{ matrix.go-arch }}
GOOS: ${{ matrix.go-os }}
run: go build -o ./build/ppm_${{ matrix.os-name }}.exe -ldflags "-X main.BuildVersion=${{ github.event.release.tag_name }}"

# compile linux/darwin executable
- name: Compile to ${{ matrix.os-name }}
if: ${{ !startsWith(matrix.os-name, 'windows') }}
env:
Expand Down
8 changes: 4 additions & 4 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ func Commands() []*cli.Command {
{
Name: "install",
Aliases: []string{"i"},
Usage: "install a certain plugin or dependencies",
Usage: "install one or more dependencies",
Action: install,
},
{
Name: "uninstall",
Usage: "uninstall a certain plugin or dependencies",
Usage: "uninstall one or more dependencies",
Action: uninstall,
Flags: []cli.Flag{
&cli.BoolFlag{
Expand All @@ -25,7 +25,7 @@ func Commands() []*cli.Command {
{
Name: "reinstall",
Aliases: []string{"ri"},
Usage: "reinstall all dependencies",
Usage: "delete and reinstall all dependencies",
Action: reinstall,
},
{
Expand All @@ -45,7 +45,7 @@ func Commands() []*cli.Command {
},
{
Name: "tidy",
Usage: "tidy the config",
Usage: "clean up the config",
Action: tidy,
},
}
Expand Down
25 changes: 9 additions & 16 deletions pkg/commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ func install(ctx *cli.Context) error {
installAllDependencies(&config, paths)
}

for i := 0; i < dependencies.Len(); i++ {
repo := dependencies.Get(i)

if config.HasDependency(repo) {
alreadyInstalled(repo)
} else if err = installDependency(&config, paths, repo, false); err != nil {
for _, repo := range dependencies.Slice() {
if err = installDependency(&config, paths, repo, false); err != nil {
return err
}
}
Expand All @@ -40,39 +36,36 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro
return err
}
}
utility.PrintDone()
return nil
}

func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error {
dependency, version := utility.GetVersionOrNot(dependency)
if !isSubDependency {
fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginName(dependency)))
fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency)))
} else {
fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginName(dependency)))
fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency)))
}
loadAnim := utility.StartLoading()

err := utility.Clone(paths.Addons, dependency, version)
loadAnim.Stop()

var addDependency bool

if err != nil {
if err.Error() == "repository already exists" {
alreadyInstalled(dependency)
return nil
} else {
installError(dependency)
return err
}
}

addDependency = (!isSubDependency && !config.HasDependency(dependency)) || (isSubDependency && !config.HasSubDependency(dependency))
shouldAddDep := (!isSubDependency && !config.HasDependency(dependency)) ||
(isSubDependency && !config.HasSubDependency(dependency))

if addDependency && isSubDependency {
if shouldAddDep && isSubDependency {
config.AddSubDependency(dependency)
} else if addDependency {
} else if shouldAddDep {
config.AddDependency(dependency)
}

Expand All @@ -84,7 +77,7 @@ func installDependency(config *utility.PpmConfig, paths utility.Paths, dependenc
return nil
}

// Iterate over dependencies and install them if needed
// iterate over dependencies and install them if needed
for _, dep := range subConfig.Dependencies {
if !config.HasSubDependency(dep) {
installDependency(config, paths, dep, true)
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, ha
}

func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error {
dep := utility.GetPluginName(dependency)
dep := utility.GetPluginIdentifier(dependency)
if !isSubDependency {
fmt.Println("\runinstalling", color.YellowString(dep))
} else {
Expand All @@ -73,7 +73,7 @@ func uninstallDependency(config *utility.PpmConfig, paths utility.Paths, depende
}

// path: root/addons/dependency
err = os.RemoveAll(path.Join(paths.Addons, dep))
err = os.RemoveAll(path.Join(paths.Addons, utility.GetPluginName(dep)))
loadAnim.Stop()
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/urfave/cli/v2"
)

// The update method is called by the cli
// It updates all dependencies
// the update method is called by the cli
// it updates all dependencies
func update(ctx *cli.Context) error {
paths, config, err := utility.GetPathsAndConfig()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/utility/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
git "github.com/go-git/go-git/v5"
)

// Clone a certain github repository into a certain folder
// clone a certain github repository into a certain folder
func Clone(path string, repoName string, version string) error {
// The clonable name of the repository
// the clonable name of the repository
var repository string
// The name of the repository
// the name of the repository
var name string

if IsUrl(repoName) {
Expand All @@ -37,7 +37,7 @@ func Clone(path string, repoName string, version string) error {
return err
}

// Update a certain repository
// update a certain repository
func Update(path string) error {
repo, err := git.PlainOpen(path)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/utility/loading.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type LoadingVisualization struct {
finished chan any
}

// Create and start a new LoadingVisualziation instance
// create and start a new LoadingVisualziation instance
func StartLoading() *LoadingVisualization {
l := LoadingVisualization{}
l.Start()
return &l
}

// Prepare and run the loading animation in a new goroutine
// prepare and run the loading animation in a new goroutine
func (l *LoadingVisualization) Start() {
if l.AnimationFrames == nil {
l.AnimationFrames = []string{"|", "/", "-", "\\", "|"}
Expand All @@ -28,7 +28,7 @@ func (l *LoadingVisualization) Start() {
go l.Run(10)
}

// Run the loading animation
// run the loading animation
func (l *LoadingVisualization) Run(speed time.Duration) {
index := 0
for {
Expand All @@ -48,7 +48,7 @@ func (l *LoadingVisualization) Run(speed time.Duration) {
}
}

// Stop the loading animation
// stop the loading animation
func (l *LoadingVisualization) Stop() {
l.finished <- nil
}
16 changes: 8 additions & 8 deletions pkg/utility/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ import (
"strings"
)

// Access different important paths in the GD project
// access different important paths in the GD project
type Paths struct {
/*
The root of the project.
the root of the project.
If the project is a game, the root is on the same level
if the project is a game, the root is on the same level
as the godot config files.
If the project is a plugin, the root is somewhere under /addons
if the project is a plugin, the root is somewhere under /addons
*/
Root string

// The path to the addons folder
// the path to the addons folder
Addons string

// The path to the ppm.json config file
// the path to the ppm.json config file
ConfigFile string
}

// Create a new paths instance from the root path
// create a new paths instance from the root path
func CreatePaths(rootPath string) Paths {
var addons string
if strings.HasSuffix(filepath.Dir(rootPath), "addons") {
Expand All @@ -41,7 +41,7 @@ func CreatePaths(rootPath string) Paths {
}
}

// Create a new paths instance from the current working directory
// create a new paths instance from the current working directory
func CreatePathsFromCwd() (Paths, error) {
rootPath, err := os.Getwd()
if err != nil {
Expand Down
39 changes: 19 additions & 20 deletions pkg/utility/ppmconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,57 @@ import (
"strings"
)

// Representing a ppm.json configuration file
// representing a ppm.json configuration file
type PpmConfig struct {
IsPlugin bool `json:"plugin"`
Dependencies []string `json:"dependencies"`
SubDependencies []string `json:"sub-dependencies"`
filePath string
}

// Add an item safely to the Dependencies property
// add an item safely to the Dependencies property
func (ppm *PpmConfig) AddDependency(dependency string) {
ppm.Dependencies = append(ppm.Dependencies, dependency)
ppm.Write()
}

// Add an item safely to the sub-dependencies property
// add an item safely to the sub-dependencies property
func (ppm *PpmConfig) AddSubDependency(dependency string) {
ppm.SubDependencies = append(ppm.SubDependencies, dependency)
ppm.Write()
}

// Remove ALL (sub)dependencies
// remove ALL (sub)dependencies
func (ppm *PpmConfig) RemoveAllDependencies() {
ppm.Dependencies = []string{}
ppm.SubDependencies = []string{}
ppm.Write()
}

// Remove an item safely from the Dependencies property by its name
// remove an item safely from the Dependencies property by its name
func (ppm *PpmConfig) RemoveSubDependency(dependency string) {
index := IndexOf(dependency, ppm.SubDependencies)
ppm.SubDependencies = append(ppm.SubDependencies[:index], ppm.SubDependencies[index+1:]...)
dependency = GetPluginIdentifier(dependency)
ppm.SubDependencies = Filter(ppm.SubDependencies, func(item string, _ int) bool {
return item != dependency
})
ppm.Write()
}

// Remove an item safely from the sub-dependencies property by its name
// remove an item safely from the sub-dependencies property by its name
func (ppm *PpmConfig) RemoveDependency(dependency string) {
if len(ppm.Dependencies) == 1 {
ppm.Dependencies = []string{}
} else {
index := IndexOf(dependency, ppm.Dependencies)
ppm.Dependencies = append(ppm.Dependencies[:index], ppm.Dependencies[index+1:]...)
}
dependency = GetPluginIdentifier(dependency)
ppm.Dependencies = Filter(ppm.Dependencies, func(item string, _ int) bool {
return item != dependency
})
ppm.Write()
}

// Check wether the config file has a certain dependency
// check wether the config file has a certain dependency
func (ppm PpmConfig) HasDependency(dependency string) bool {
return SliceContains(dependency, ppm.Dependencies)
}

// Check wether the config file has a certain sub-dependency
// check wether the config file has a certain sub-dependency
func (ppm PpmConfig) HasSubDependency(dependency string) bool {
return SliceContains(dependency, ppm.SubDependencies)
}
Expand All @@ -78,7 +78,7 @@ func (ppm PpmConfig) PrettyPrint() {
fmt.Printf("this project is a %s\ndependencies: %v\nsubdependencies: %v", ppmType, dependencies, subDependencies)
}

// Write the current state of the configuartion to the config file
// write the current state of the configuartion to the config file
func (ppm PpmConfig) Write() error {
content, err := json.MarshalIndent(ppm, "", " ")
if err != nil {
Expand All @@ -93,7 +93,7 @@ func (ppm PpmConfig) Write() error {
return nil
}

// Parse the ppm.json file to an object
// parse the ppm.json file to an object
func ParsePpmConfig(filePath string) (PpmConfig, error) {
file, err := os.Open(filePath)
if err != nil {
Expand All @@ -103,7 +103,6 @@ func ParsePpmConfig(filePath string) (PpmConfig, error) {

content, err := ioutil.ReadFile(file.Name())
if err != nil {
fmt.Println(err)
return PpmConfig{}, err
}

Expand All @@ -114,7 +113,7 @@ func ParsePpmConfig(filePath string) (PpmConfig, error) {
return config, nil
}

// Create a new ppm.json file
// create a new ppm.json file
func CreateNewPpmConfig(path string) error {
configPath := filepath.Join(path, "ppm.json")

Expand Down
Loading

0 comments on commit 15703ba

Please sign in to comment.