Skip to content

Commit 15703ba

Browse files
authored
Merge pull request #29 from Glow-Project/enhance-code
Enhance code
2 parents 16f4f96 + 2ab9814 commit 15703ba

File tree

11 files changed

+98
-71
lines changed

11 files changed

+98
-71
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ jobs:
3737
with:
3838
go-version: "1.18"
3939

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

48+
# compile linux/darwin executable
4749
- name: Compile to ${{ matrix.os-name }}
4850
if: ${{ !startsWith(matrix.os-name, 'windows') }}
4951
env:

pkg/commands/commands.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ func Commands() []*cli.Command {
77
{
88
Name: "install",
99
Aliases: []string{"i"},
10-
Usage: "install a certain plugin or dependencies",
10+
Usage: "install one or more dependencies",
1111
Action: install,
1212
},
1313
{
1414
Name: "uninstall",
15-
Usage: "uninstall a certain plugin or dependencies",
15+
Usage: "uninstall one or more dependencies",
1616
Action: uninstall,
1717
Flags: []cli.Flag{
1818
&cli.BoolFlag{
@@ -25,7 +25,7 @@ func Commands() []*cli.Command {
2525
{
2626
Name: "reinstall",
2727
Aliases: []string{"ri"},
28-
Usage: "reinstall all dependencies",
28+
Usage: "delete and reinstall all dependencies",
2929
Action: reinstall,
3030
},
3131
{
@@ -45,7 +45,7 @@ func Commands() []*cli.Command {
4545
},
4646
{
4747
Name: "tidy",
48-
Usage: "tidy the config",
48+
Usage: "clean up the config",
4949
Action: tidy,
5050
},
5151
}

pkg/commands/install.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ func install(ctx *cli.Context) error {
2121
installAllDependencies(&config, paths)
2222
}
2323

24-
for i := 0; i < dependencies.Len(); i++ {
25-
repo := dependencies.Get(i)
26-
27-
if config.HasDependency(repo) {
28-
alreadyInstalled(repo)
29-
} else if err = installDependency(&config, paths, repo, false); err != nil {
24+
for _, repo := range dependencies.Slice() {
25+
if err = installDependency(&config, paths, repo, false); err != nil {
3026
return err
3127
}
3228
}
@@ -40,39 +36,36 @@ func installAllDependencies(config *utility.PpmConfig, paths utility.Paths) erro
4036
return err
4137
}
4238
}
43-
utility.PrintDone()
4439
return nil
4540
}
4641

4742
func installDependency(config *utility.PpmConfig, paths utility.Paths, dependency string, isSubDependency bool) error {
4843
dependency, version := utility.GetVersionOrNot(dependency)
4944
if !isSubDependency {
50-
fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginName(dependency)))
45+
fmt.Printf("\rinstalling %s\n", color.YellowString(utility.GetPluginIdentifier(dependency)))
5146
} else {
52-
fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginName(dependency)))
47+
fmt.Printf("\t -> installing %s\n", color.YellowString(utility.GetPluginIdentifier(dependency)))
5348
}
5449
loadAnim := utility.StartLoading()
5550

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

59-
var addDependency bool
60-
6154
if err != nil {
6255
if err.Error() == "repository already exists" {
6356
alreadyInstalled(dependency)
64-
return nil
6557
} else {
6658
installError(dependency)
6759
return err
6860
}
6961
}
7062

71-
addDependency = (!isSubDependency && !config.HasDependency(dependency)) || (isSubDependency && !config.HasSubDependency(dependency))
63+
shouldAddDep := (!isSubDependency && !config.HasDependency(dependency)) ||
64+
(isSubDependency && !config.HasSubDependency(dependency))
7265

73-
if addDependency && isSubDependency {
66+
if shouldAddDep && isSubDependency {
7467
config.AddSubDependency(dependency)
75-
} else if addDependency {
68+
} else if shouldAddDep {
7669
config.AddDependency(dependency)
7770
}
7871

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

87-
// Iterate over dependencies and install them if needed
80+
// iterate over dependencies and install them if needed
8881
for _, dep := range subConfig.Dependencies {
8982
if !config.HasSubDependency(dep) {
9083
installDependency(config, paths, dep, true)

pkg/commands/uninstall.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func uninstallAllDependencies(config *utility.PpmConfig, paths utility.Paths, ha
5454
}
5555

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

7575
// path: root/addons/dependency
76-
err = os.RemoveAll(path.Join(paths.Addons, dep))
76+
err = os.RemoveAll(path.Join(paths.Addons, utility.GetPluginName(dep)))
7777
loadAnim.Stop()
7878
if err != nil {
7979
return err

pkg/commands/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/urfave/cli/v2"
88
)
99

10-
// The update method is called by the cli
11-
// It updates all dependencies
10+
// the update method is called by the cli
11+
// it updates all dependencies
1212
func update(ctx *cli.Context) error {
1313
paths, config, err := utility.GetPathsAndConfig()
1414
if err != nil {

pkg/utility/git.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99
git "github.com/go-git/go-git/v5"
1010
)
1111

12-
// Clone a certain github repository into a certain folder
12+
// clone a certain github repository into a certain folder
1313
func Clone(path string, repoName string, version string) error {
14-
// The clonable name of the repository
14+
// the clonable name of the repository
1515
var repository string
16-
// The name of the repository
16+
// the name of the repository
1717
var name string
1818

1919
if IsUrl(repoName) {
@@ -37,7 +37,7 @@ func Clone(path string, repoName string, version string) error {
3737
return err
3838
}
3939

40-
// Update a certain repository
40+
// update a certain repository
4141
func Update(path string) error {
4242
repo, err := git.PlainOpen(path)
4343
if err != nil {

pkg/utility/loading.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ type LoadingVisualization struct {
1111
finished chan any
1212
}
1313

14-
// Create and start a new LoadingVisualziation instance
14+
// create and start a new LoadingVisualziation instance
1515
func StartLoading() *LoadingVisualization {
1616
l := LoadingVisualization{}
1717
l.Start()
1818
return &l
1919
}
2020

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

31-
// Run the loading animation
31+
// run the loading animation
3232
func (l *LoadingVisualization) Run(speed time.Duration) {
3333
index := 0
3434
for {
@@ -48,7 +48,7 @@ func (l *LoadingVisualization) Run(speed time.Duration) {
4848
}
4949
}
5050

51-
// Stop the loading animation
51+
// stop the loading animation
5252
func (l *LoadingVisualization) Stop() {
5353
l.finished <- nil
5454
}

pkg/utility/paths.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ import (
66
"strings"
77
)
88

9-
// Access different important paths in the GD project
9+
// access different important paths in the GD project
1010
type Paths struct {
1111
/*
12-
The root of the project.
12+
the root of the project.
1313
14-
If the project is a game, the root is on the same level
14+
if the project is a game, the root is on the same level
1515
as the godot config files.
1616
17-
If the project is a plugin, the root is somewhere under /addons
17+
if the project is a plugin, the root is somewhere under /addons
1818
*/
1919
Root string
2020

21-
// The path to the addons folder
21+
// the path to the addons folder
2222
Addons string
2323

24-
// The path to the ppm.json config file
24+
// the path to the ppm.json config file
2525
ConfigFile string
2626
}
2727

28-
// Create a new paths instance from the root path
28+
// create a new paths instance from the root path
2929
func CreatePaths(rootPath string) Paths {
3030
var addons string
3131
if strings.HasSuffix(filepath.Dir(rootPath), "addons") {
@@ -41,7 +41,7 @@ func CreatePaths(rootPath string) Paths {
4141
}
4242
}
4343

44-
// Create a new paths instance from the current working directory
44+
// create a new paths instance from the current working directory
4545
func CreatePathsFromCwd() (Paths, error) {
4646
rootPath, err := os.Getwd()
4747
if err != nil {

pkg/utility/ppmconfig.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,57 @@ import (
1111
"strings"
1212
)
1313

14-
// Representing a ppm.json configuration file
14+
// representing a ppm.json configuration file
1515
type PpmConfig struct {
1616
IsPlugin bool `json:"plugin"`
1717
Dependencies []string `json:"dependencies"`
1818
SubDependencies []string `json:"sub-dependencies"`
1919
filePath string
2020
}
2121

22-
// Add an item safely to the Dependencies property
22+
// add an item safely to the Dependencies property
2323
func (ppm *PpmConfig) AddDependency(dependency string) {
2424
ppm.Dependencies = append(ppm.Dependencies, dependency)
2525
ppm.Write()
2626
}
2727

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

34-
// Remove ALL (sub)dependencies
34+
// remove ALL (sub)dependencies
3535
func (ppm *PpmConfig) RemoveAllDependencies() {
3636
ppm.Dependencies = []string{}
3737
ppm.SubDependencies = []string{}
3838
ppm.Write()
3939
}
4040

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

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

59-
// Check wether the config file has a certain dependency
59+
// check wether the config file has a certain dependency
6060
func (ppm PpmConfig) HasDependency(dependency string) bool {
6161
return SliceContains(dependency, ppm.Dependencies)
6262
}
6363

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

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

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

104104
content, err := ioutil.ReadFile(file.Name())
105105
if err != nil {
106-
fmt.Println(err)
107106
return PpmConfig{}, err
108107
}
109108

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

117-
// Create a new ppm.json file
116+
// create a new ppm.json file
118117
func CreateNewPpmConfig(path string) error {
119118
configPath := filepath.Join(path, "ppm.json")
120119

0 commit comments

Comments
 (0)