Skip to content

Commit

Permalink
dir preview extracts challenge paths from config
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrunde committed Jul 19, 2023
1 parent 7545761 commit e3e185c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
43 changes: 0 additions & 43 deletions app/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"sort"
"strings"

"github.com/gSchool/glearn-cli/mdresourceparser"
yaml "gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -377,48 +376,6 @@ func anyMatchingPrefix(target string, prefixes []string) bool {
return false
}

func parseConfigAndGatherLinkedPaths(target string) ([]string, error) {
ret := []string{}
config := ConfigYaml{}

configYaml, _ := findConfig(target)
data, err := ioutil.ReadFile(configYaml)
if err != nil {
return ret, err
}

err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
return ret, err
}

for _, std := range config.Standards {
for _, cf := range std.ContentFiles {
contents, err := ioutil.ReadFile(target + cf.Path)
if err != nil {
return []string{}, fmt.Errorf("Failure to read file '%s'. Err: %s", string(contents), err)
}

m := mdresourceparser.New([]rune(string(contents)))
m.ParseResources()
for _, link := range m.Links {
var pathSplits = strings.Split(cf.Path, "/")
pathSplits = pathSplits[:len(pathSplits)-1]
if !strings.HasPrefix(link, "/") {
link = "/" + link
}
var linkRelativePath = target + strings.Join(pathSplits, "/") + link
linkAbsPath, _ := filepath.Abs(linkRelativePath)
ret = append(ret, linkAbsPath)
}
cfPath, _ := filepath.Abs(target + cf.Path)
ret = append(ret, cfPath)
}
}

return ret, nil
}

// tries to find the config yaml or autoconfig yaml
func findConfig(target string) (string, error) {
configPath := ""
Expand Down
57 changes: 55 additions & 2 deletions app/cmd/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/briandowns/spinner"
"github.com/spf13/cobra"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"

"github.com/gSchool/glearn-cli/api/learn"
di "github.com/gSchool/glearn-cli/ignorematcher"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (p *previewBuilder) setConfigYaml() error {
return fmt.Errorf("Failed to find or create a config file for: (%s).\nErr: %v", p.target, err)
}

configYamlPaths, err := parseConfigAndGatherLinkedPaths(p.target)
configYamlPaths, err := p.parseConfigAndGatherPaths()
if err != nil {
return fmt.Errorf("Failed to parse config/autoconfig yaml for: (%s).\nErr: %v", p.target, err)
}
Expand Down Expand Up @@ -178,7 +179,8 @@ func (p *previewBuilder) compressDirectory(zipTarget string) error {
}
}
for _, d := range resourcePaths {
if strings.Contains(path, d) {
if strings.Contains(path, d) || strings.Contains(path, trimFirstRune(d)) {
fmt.Println("file included", d)
fileIsIncluded = true
}
}
Expand Down Expand Up @@ -416,6 +418,7 @@ preview and return/open the preview URL when it is complete.
previewCmdError(fmt.Sprintf("%v", err), tmpZipFile)
return
}
//previewer.addPathsFromConfigFiles()
}

err = previewer.compressDirectory(tmpZipFile)
Expand Down Expand Up @@ -892,6 +895,56 @@ func CopyDirectoryContents(src, dst string, ignorePatterns []string) error {
return nil
}

// parseConfigAndGatherPaths
func (p *previewBuilder) parseConfigAndGatherPaths() ([]string, error) {
ret := []string{}
config := ConfigYaml{}

configYaml, _ := findConfig(p.target)
data, err := ioutil.ReadFile(configYaml)
if err != nil {
return ret, err
}

err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
return ret, err
}

for _, std := range config.Standards {
for _, cf := range std.ContentFiles {
fmt.Println("parsing content file:", cf)
contents, err := ioutil.ReadFile(p.target + cf.Path)
if err != nil {
return []string{}, fmt.Errorf("Failure to read file '%s'. Err: %s", string(contents), err)
}

m := mdresourceparser.New([]rune(string(contents)))
// TODO include all files in dockerDirPaths
dataPaths, _, testFilePaths, setupFilePaths := m.ParseResources()
p.challengePaths = append(append(append(p.challengePaths, dataPaths...), testFilePaths...), setupFilePaths...)

// add links
for _, link := range m.Links {
var pathSplits = strings.Split(cf.Path, "/")
pathSplits = pathSplits[:len(pathSplits)-1]
if !strings.HasPrefix(link, "/") {
link = "/" + link
}
var linkRelativePath = p.target + strings.Join(pathSplits, "/") + link
linkAbsPath, _ := filepath.Abs(linkRelativePath)
ret = append(ret, linkAbsPath)
}

cfPath, _ := filepath.Abs(p.target + cf.Path)
ret = append(ret, cfPath)

}
}

return ret, nil
}

// fileFromParents checks for a file from the given filePath, and if not found it will
// move up a parent and check again. Checking stops once the root of the file system is hit
func fileFromParents(target, filePath string) (file os.FileInfo, path string) {
Expand Down

0 comments on commit e3e185c

Please sign in to comment.