Skip to content

Commit

Permalink
Merge pull request #127 from Bedrock-OSS/debt/code-cleanup-p1
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
SirLich committed Jan 2, 2022
2 parents b032c00 + ea03b43 commit 09b5446
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 45 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@

### 2. Build

`./scripts/build-local.sh`
There are a few ways to build:
- `./scripts/build-local.sh` (no clue)
- `go build` (creates exe)
- `go install` (installs to gopath)

#### or:
## Running Tests

`go build`

#### or:

`go install`
`go test ./...`
12 changes: 5 additions & 7 deletions regolith/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ func SetupTmpFiles(config Config, profile Profile) error {
// is the name of the profile which should be loaded from the configuration.
func RunProfile(profileName string) error {
Logger.Info("Running profile: ", profileName)
project, err := LoadConfig()
if err != nil {
return wrapError("Failed to load project config", err)
}
profile := project.Profiles[profileName]
config := LoadConfig()

profile := config.Profiles[profileName]

// Check whether every filter, uses a supported filter type
checked := make(map[string]struct{})
Expand All @@ -112,7 +110,7 @@ func RunProfile(profileName string) error {
}

// Prepare tmp files
err = SetupTmpFiles(*project, profile)
err := SetupTmpFiles(*config, profile)
if err != nil {
return wrapError("Unable to setup profile", err)
}
Expand All @@ -130,7 +128,7 @@ func RunProfile(profileName string) error {
// Export files
Logger.Info("Moving files to target directory")
start := time.Now()
err = ExportProject(profile, project.Name)
err = ExportProject(profile, config.Name)
if err != nil {
return wrapError("Exporting project failed", err)
}
Expand Down
11 changes: 6 additions & 5 deletions regolith/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,25 @@ const ManifestName = "config.json"
const GitIgnore = `/build
/.regolith`

// TODO implement the rest of the standard config spec
type Config struct {
Name string `json:"name,omitempty"`
Author string `json:"author,omitempty"`
Packs `json:"packs,omitempty"`
RegolithProject `json:"regolith,omitempty"`
}

func LoadConfig() (*Config, error) {
func LoadConfig() *Config {
file, err := ioutil.ReadFile(ManifestName)
if err != nil {
return nil, wrapError(fmt.Sprintf("Couldn't find %s! Consider running 'regolith init'", ManifestName), err)
Logger.Fatal("Couldn't find %s! Consider running 'regolith init'.", ManifestName, err)
}

var result *Config
err = json.Unmarshal(file, &result)
if err != nil {
return nil, wrapError(fmt.Sprintf("Couldn't load %s: ", ManifestName), err)
Logger.Fatal("Couldn't load %s! Does the file contain correct json?", ManifestName, err)
}

// If settings is nil replace it with empty map
for _, profile := range result.Profiles {
for fk := range profile.Filters {
Expand All @@ -43,7 +44,7 @@ func LoadConfig() (*Config, error) {
}
}
}
return result, nil
return result
}

type Packs struct {
Expand Down
17 changes: 3 additions & 14 deletions regolith/remote_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,10 @@ func IsRemoteFilterCached(url string) bool {
func InstallDependencies(isForced bool) error {
Logger.Infof("Installing dependencies...")

project, err := LoadConfig()
if err != nil {
return wrapError("Failed to load project config", err)
}
project := LoadConfig()

err = os.MkdirAll(".regolith/cache/filters", 0666)
if err != nil {
return wrapError("Could not create .regolith/cache/filters", err)
}

// Special path for virtual environments for python
err = os.MkdirAll(".regolith/cache/venvs", 0666)
if err != nil {
return wrapError("Could not create .regolith/cache/venvs", err)
}
CreateDirectoryIfNotExists(".regolith/cache/filters", true)
CreateDirectoryIfNotExists(".regolith/cache/venvs", true)

wd, err := os.Getwd()
if err != nil {
Expand Down
23 changes: 18 additions & 5 deletions regolith/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ func wrapError(text string, err error) error {
return errors.New(text)
}

func CreateDirectoryIfNotExists(directory string, mustSucceed bool) {
if _, err := os.Stat(directory); os.IsNotExist(err) {
err = os.MkdirAll(directory, 0666)
if err != nil {
if mustSucceed {
Logger.Fatalf("Failed to create directory %s: %s", directory, err.Error())
} else {
Logger.Warnf("Failed to create directory %s: %s", directory, err.Error())
}
}
}
}

// GetAbsoluteWorkingDirectory returns an absolute path to .regolith/tmp
func GetAbsoluteWorkingDirectory() string {
absoluteWorkingDir, _ := filepath.Abs(".regolith/tmp")
Expand Down Expand Up @@ -59,7 +72,7 @@ func LogStd(in io.ReadCloser, logFunc func(template string, args ...interface{})
}
}

func parseSemver(semver string) (major int, minor int, patch int) {
func ParseSemanticVersion(semver string) (major int, minor int, patch int) {
split := strings.Split(semver, ".")
length := len(split)
if length > 0 {
Expand All @@ -75,9 +88,9 @@ func parseSemver(semver string) (major int, minor int, patch int) {
}

// Returns 1 if first version is newer, -1 if older, 0 if the same
func compareSemver(ver1 string, ver2 string) int {
major1, minor1, patch1 := parseSemver(ver1)
major2, minor2, patch2 := parseSemver(ver2)
func CompareSemanticVersion(ver1 string, ver2 string) int {
major1, minor1, patch1 := ParseSemanticVersion(ver1)
major2, minor2, patch2 := ParseSemanticVersion(ver2)
if major1 > major2 {
return 1
} else if major1 < major2 {
Expand Down Expand Up @@ -114,7 +127,7 @@ func CheckUpdate(version string, status chan UpdateStatus) {
return
}
status <- UpdateStatus{
ShouldUpdate: compareSemver(*release.TagName, version) == 1,
ShouldUpdate: CompareSemanticVersion(*release.TagName, version) == 1,
Url: release.HTMLURL,
}
}
12 changes: 5 additions & 7 deletions test/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,13 @@ func TestMoveFilesAcl(t *testing.T) {
defer os.Chdir(wd)
// Switch wd to wrokingDir
os.Chdir(workingDir)
// Get the name of the project from config
project, err := regolith.LoadConfig()
if err != nil {
t.Fatalf("Failed to load project config: %s", err)
}
// Get the name of the config from config
config := regolith.LoadConfig()

bpPath := filepath.Join(
mojangDir, "development_behavior_packs", project.Name+"_bp")
mojangDir, "development_behavior_packs", config.Name+"_bp")
rpPath := filepath.Join(
mojangDir, "development_resource_packs", project.Name+"_rp")
mojangDir, "development_resource_packs", config.Name+"_rp")
// Run regolith project
os.Chdir(workingDir)
regolith.InitLogging(true)
Expand Down

0 comments on commit 09b5446

Please sign in to comment.