aboutsummaryrefslogtreecommitdiff
path: root/stddirs.go
blob: 581af87d7620b57c4b13d5e194712d8a4fface85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package planr

import (
  "os"
  "log"
  "path"
  "path/filepath"
)

// Standard Directories:
//
// Planr relies on a standard directory structure to resolve paths to source code
// These directories can be overridden via environmental variables //
// Directories:
// - `.`            root directory
// - `src`          contains source code
// - `planr`        contains test metadata (also called configuration directory)
// - `planr/rubric` contains test case configuration
// - `planr/tests`  contains source code for test cases
// - `planr/build`  contains all files written during the build process (ephemeral)

// Env overrides
const (
  ENV_CONFIG_DIR="PLANR_CONFIG_DIR"
  ENV_SRC_DIR="PLANR_SRC_DIR"
  ENV_BUILD_DIR="PLANR_BUILD_DIR"
)

// Try these search directories
var CONFIG_SEARCH_DIRS = [] string {
  "planr",
  ".planr",
}

// Path are relative to the "planr" project config directory
const (
  DEFAULT_PATH_SRC="../src"
  DEFAULT_PATH_BUILD="build"
  DEFAULT_PATH_RUBRIC="rubric"
  DEFAULT_PATH_TESTS="tests"
)

type DirConfig struct {
  src        string
  config     string
  build      string
  
  // Config falls back to the config found in the parent directory if the env variable hasn't been overridden
  pdFallback string
}

func dieDirAbsent(name, path string) {
  if !directoryExists(path) {
    log.Fatalf("Could not find %s directory tried %s", name, path)
  }
}

func dirFromEnv(name, env string) *string {
  if dir, isSet := os.LookupEnv(env); isSet {
    
    dieDirAbsent(name, dir)

    return &dir
  }

  return nil
}

func (c *DirConfig) SetSrc(srcDir string) {
  dieDirAbsent("src", srcDir)
  c.src = abs(srcDir)
}

func (c *DirConfig) SetConfig(configDir string) {
  dieDirAbsent("planr (config)", configDir)
  c.config = abs(configDir)
}

func (c *DirConfig) SetBuild(buildDir string) {
  dieDirAbsent("build", buildDir)
  c.build = abs(buildDir)
}

func (c *DirConfig) SetConfigFromTree(cdir string) {
  var configDir string

  found := traverseUp(cdir, func (path string) bool {

    for _, dir := range CONFIG_SEARCH_DIRS {
      configDir = filepath.Join(path, dir)

      if directoryExists(configDir) {
        return true
      }
    }

    return false 
  });


  if found {
    c.pdFallback = configDir
  }
}

func (c DirConfig) Config() string {
  if c.config != "" {
    return c.config
  }

  if dir := dirFromEnv("config", ENV_CONFIG_DIR); dir != nil {
    c.config = abs(*dir)
    return c.config
  }

  if c.pdFallback == "" {
    log.Fatal("Could not find planr directory");
  }

  c.config = abs(c.pdFallback);
  return c.config
}

func (c DirConfig) Src() string {
  if c.src != "" {
    return c.src
  }

  if dir := dirFromEnv("src", ENV_SRC_DIR); dir != nil {
    c.src = *dir
    return c.src
  }

  // set path relative to config
  dir := c.Config()
  return path.Join(dir, DEFAULT_PATH_SRC)
}

func abs(path string) string {
  apath, err := filepath.Abs(path)

  if err != nil {
    log.Fatalf("Could not find path %s", path)
  }

  return apath
}

func (c DirConfig) Build() string {
  if c.build != "" {
    return c.build
  }

  if dir := dirFromEnv("build", ENV_BUILD_DIR); dir != nil {
    c.build = *dir
    return c.build
  }

  dir := c.Config()
  return path.Join(dir, DEFAULT_PATH_BUILD)
}

func (c DirConfig) CleanBuild() {
  build := c.Build()

  if err := os.RemoveAll(build); err != nil {
    log.Fatalf("Cannot build directory %v\n", err)
  }
  
}

func (c DirConfig) MkBuild() {
  build := c.Build()

  if err := os.Mkdir(build, 0755); err != nil {
    log.Fatalf("Could not create build directory %v\n", err)
  }
}

func (c DirConfig) Rubric() string {
  rubric := path.Join(c.Config(), DEFAULT_PATH_RUBRIC)
  dieDirAbsent("rubric", rubric)
  return rubric
}

func (c DirConfig) Tests() string {
  tests := path.Join(c.Config(), DEFAULT_PATH_TESTS)
  dieDirAbsent("tests", tests)
  return tests
}