aboutsummaryrefslogtreecommitdiff
path: root/fs.go
diff options
context:
space:
mode:
Diffstat (limited to 'fs.go')
-rw-r--r--fs.go108
1 files changed, 97 insertions, 11 deletions
diff --git a/fs.go b/fs.go
index 905d6d1..978c291 100644
--- a/fs.go
+++ b/fs.go
@@ -6,7 +6,7 @@ import (
"io"
"path"
"path/filepath"
- // "fmt"
+ "strings"
)
/* CONFIG DIRECTORY */
@@ -50,7 +50,7 @@ func directoryExists(path string) bool {
// 1. PlANR_DIRECTORY env if set
// 2. planr
// 3. .planr
-func configDir() string {
+func ConfigDir() string {
// Return environmental override if set
if dir, isSet := os.LookupEnv("PLANR_DIRECTORY"); isSet {
@@ -95,9 +95,22 @@ func configDir() string {
return rubricDir
}
+func JoinConfigDir(path_ string, file string) string {
+ if path.IsAbs(path_) {
+ return path.Join(path_, file)
+ }
+
+ return path.Join(ConfigDir(), path_, file)
+}
+
+
+func RootDir() string {
+ return path.Join(ConfigDir(), "..")
+}
+
// Find rubric directory at PLANR_DIR/rubric
func RubricDir() string {
- rubricDir := path.Join(configDir(), "rubric");
+ rubricDir := path.Join(ConfigDir(), "rubric");
if !directoryExists(rubricDir) {
log.Fatal("Could not find the rubric directory inside of planr")
@@ -106,21 +119,95 @@ func RubricDir() string {
return rubricDir
}
+func BuildDir() string {
+ buildDir := path.Join(ConfigDir(), "build")
+
+ if !directoryExists(buildDir) {
+ err := os.Mkdir(buildDir, 0755)
+
+ if err != nil {
+ log.Fatalf("Cannot create build directory %v\n", err)
+ }
+ }
+
+ return buildDir
+}
+
+func CleanBuildDir() {
+ buildDir := path.Join(ConfigDir(), "build")
+ if err := os.RemoveAll(buildDir); err != nil {
+ log.Fatalf("Cannot clean (removeAll) in build directory %v\n", err)
+ }
+}
+
+func (ac AdapterConfig) ConfigDir() string {
+ dir := BuildDir()
+ dir = path.Join(dir, ac.Name)
+
+ if !directoryExists(dir) {
+ err := os.Mkdir(dir, 0755)
+
+ if err != nil {
+ log.Fatalf("Cannot create build/%s directory %v\n", ac.Name, err)
+ }
+ }
+
+ return dir
+}
+
+func basename(path string) string {
+ ext := filepath.Ext(path)
+ return path[0:len(path) - len(ext)]
+}
+
+func cname(root string, path string) string {
+ rel, err := filepath.Rel(root, path)
+
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ rel = filepath.ToSlash(rel)
+ parts := strings.Split(rel, "/")
+ n := len(parts)
+
+ if n == 0 {
+ return ""
+ }
+
+ parts[n-1] = basename(parts[n-1])
+
+ return strings.Join(parts, ".")
+}
+
+func collectUnits(root string, cfgs []AdapterConfig) []TestCase {
+ tcs := make([]TestCase, 0)
+
+ collectFromDir(root, nil, cfgs, &tcs)
+
+ for i := range tcs {
+ tcs[i].Cname = cname(root, tcs[i].Path)
+ }
+
+ return tcs
+}
+
+const DEFAULTS = "defaults.toml"
// Collects the units from the configuration tree
// TODO: Cleanup
-func collectUnits(
- name string,
+func collectFromDir(
+ dir string,
defaults *Defaults,
cfgs []AdapterConfig,
units *[]TestCase,
) {
- fp, err := os.Open(name)
+ fp, err := os.Open(dir)
if err != nil {
log.Fatal(err)
}
// Process defaults for this directory if a defaults.toml is found
- defaultsPath := path.Join(name, "defaults.toml")
+ defaultsPath := path.Join(dir, DEFAULTS)
if info, err := os.Stat(defaultsPath); err == nil && !info.IsDir() {
d := DecodeDefaults(defaultsPath, cfgs)
@@ -143,13 +230,13 @@ func collectUnits(
for _, ent := range dirs {
- child := path.Join(name, ent.Name())
+ child := path.Join(dir, ent.Name())
nm := ent.Name()
if ent.IsDir() {
- collectUnits(child, defaults, cfgs, units)
+ collectFromDir(child, defaults, cfgs, units)
} else {
- if nm == "defaults.toml" {
+ if nm == DEFAULTS {
continue
}
@@ -159,7 +246,6 @@ func collectUnits(
tc := TestCase {
Path: child,
- Cname: nm,
Config: config,
}