diff --git a/README.md b/README.md index 07064ed1..ee6dbe43 100644 --- a/README.md +++ b/README.md @@ -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 ./...` diff --git a/regolith/profiles.go b/regolith/profiles.go index a06534e8..faeaabab 100644 --- a/regolith/profiles.go +++ b/regolith/profiles.go @@ -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{}) @@ -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) } @@ -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) } diff --git a/regolith/project.go b/regolith/project.go index cc1d4b00..4e857985 100644 --- a/regolith/project.go +++ b/regolith/project.go @@ -17,7 +17,6 @@ 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"` @@ -25,16 +24,18 @@ type Config struct { 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 { @@ -43,7 +44,7 @@ func LoadConfig() (*Config, error) { } } } - return result, nil + return result } type Packs struct { diff --git a/regolith/remote_filters.go b/regolith/remote_filters.go index 672d04d0..e63f79fd 100644 --- a/regolith/remote_filters.go +++ b/regolith/remote_filters.go @@ -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 { diff --git a/regolith/utils.go b/regolith/utils.go index eb085d94..3ef56cfe 100644 --- a/regolith/utils.go +++ b/regolith/utils.go @@ -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") @@ -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 { @@ -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 { @@ -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, } } diff --git a/test/export_test.go b/test/export_test.go index ed8bdfa4..5a7f1005 100644 --- a/test/export_test.go +++ b/test/export_test.go @@ -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)