Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

Commit

Permalink
Cleaning up #149
Browse files Browse the repository at this point in the history
  • Loading branch information
Smarticles101 committed Feb 28, 2019
1 parent 7e58c7b commit 5aa30dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 60 deletions.
101 changes: 44 additions & 57 deletions cmd/implementation_status.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package cmd

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"

"github.com/exercism/configlet/track"
"github.com/exercism/configlet/ui"
"github.com/spf13/cobra"
)

var pathExampleProblemSpecifications = "<path/to/problemspecifications>"
var pathExampleProblemSpecifications = "<path/to/problem-specifications>"

// implementationStatusCmd gives information about exercises that should be
// worked on (e.g., new version or not implemented yet).
Expand All @@ -35,100 +34,88 @@ should be made available soon.
},
}

const (
implementationStatusOK = iota
implementationStatusDeprecated
implementationStatusForegone
)

// listProblemSpecs gives a map of exercise name and whether the exercise is deprecated.
func listProblemSpecs(path string) (map[string]int, error) {
func listProblemSpecs(path string) (map[string]bool, error) {
fis, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}

ret := make(map[string]int)
ret := make(map[string]bool)
for _, f := range fis {
_, err := os.Stat(filepath.Join(path, f.Name(), ".deprecated"))
if os.IsNotExist(err) {
ret[f.Name()] = implementationStatusOK
ret[f.Name()] = true
} else if err == nil {
ret[f.Name()] = false
} else {
ret[f.Name()] = implementationStatusDeprecated
return nil, err
}
}

return ret, nil
}

func trackConfigExercises(track string) (map[string]int, error) {
var trackConfig struct {
Foregone []string `json:"foregone"`
Exercises []struct {
Slug string `json:"slug"`
Deprecated bool `json:"deprecated"`
} `json:"exercises"`
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

f, err := os.Open(filepath.Join(track, "config.json"))
if err != nil {
return nil, err
}
defer f.Close()
if err := json.NewDecoder(f).Decode(&trackConfig); err != nil {
return nil, err
}
func runImplementationStatus(specPath, trackPath string) {

names := make(map[string]int)
for _, x := range trackConfig.Exercises {
if x.Deprecated {
names[x.Slug] = implementationStatusDeprecated
} else {
names[x.Slug] = implementationStatusOK
}
}
for _, f := range trackConfig.Foregone {
names[f] = implementationStatusForegone
trackConfigPath := filepath.Join(trackPath, "config.json")
track.ProblemSpecificationsPath = filepath.Join(filepath.Dir(trackConfigPath), track.ProblemSpecificationsDir)
if specPath != "" {
track.ProblemSpecificationsPath = specPath
}
return names, nil
}

func runImplementationStatus(problemSpecifications, track string) {
fmt.Println(track.ProblemSpecificationsPath)
if _, err := os.Stat(track.ProblemSpecificationsPath); os.IsNotExist(err) {
ui.PrintError("path not found:", track.ProblemSpecificationsPath)
os.Exit(1)
}

trackConfig, err := trackConfigExercises(track)
trackConfig, err := track.NewConfig(trackConfigPath)
if err != nil {
ui.PrintError(err.Error())
return
}

namesPS, err := listProblemSpecs(filepath.Join(problemSpecifications, "exercises"))
namesPS, err := listProblemSpecs(filepath.Join(track.ProblemSpecificationsPath, "exercises"))
if err != nil {
ui.PrintError(err.Error())
return
}

results := make([]string, 0)
trackExerciseSlugs := make(map[string]bool)

for k, v := range namesPS {
if _, ok := trackConfig[k]; !ok {
results = append(results, fmt.Sprintf("The exercise with slug '%s' is not implemented in this track.", k))
for _, exercise := range trackConfig.Exercises {
trackExerciseSlugs[exercise.Slug] = true
}

for slug, active := range namesPS {
if trackExerciseSlugs[slug] {
if stringInSlice(slug, trackConfig.DeprecatedSlugs) || !active {
ui.Print(fmt.Sprintf("The exercise '%s' exists in this track but is deprecated.", slug))
} else if stringInSlice(slug, trackConfig.ForegoneSlugs) {
ui.Print(fmt.Sprintf("The exercise '%s' exists in this track but is forgone.", slug))
}
} else {
if v == implementationStatusDeprecated && trackConfig[k] == implementationStatusOK {
results = append(results, fmt.Sprintf("The exercise with slug '%s' exists in this track, but has been deprecated in the specifications repository.", k))
if !stringInSlice(slug, trackConfig.ForegoneSlugs) {
ui.Print(fmt.Sprintf("The exercise '%s' does not exist in this track.", slug))
}
}
}

for k := range trackConfig {
if _, ok := namesPS[k]; !ok {
results = append(results, fmt.Sprintf("The exercise with slug '%s' exists in this track, but has been removed from the specifications repository.", k))
for slug := range trackExerciseSlugs {
if _, ok := namesPS[slug]; !ok {
ui.Print(fmt.Sprintf("The exercise '%s' exists in the track but is not in the problem-specifications repository.", slug))
}
}

sort.Strings(results)
for _, r := range results {
ui.Print(r)
}
}

func init() {
Expand Down
6 changes: 3 additions & 3 deletions cmd/implementation_status_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func ExampleImplementationStatus() {
filepath.FromSlash("../fixtures/implementation-status/problem-specifications"),
filepath.FromSlash("../fixtures/implementation-status/track"))
// Output:
// -> The exercise with slug 'not-implemented' is not implemented in this track.
// -> The exercise with slug 'old-but-gold' exists in this track, but has been removed from the specifications repository.
// -> The exercise with slug 'old-but-present' exists in this track, but has been deprecated in the specifications repository.
// -> The exercise 'not-implemented' does not exist in this track.
// -> The exercise 'old-but-present' exists in this track but is deprecated.
// -> The exercise 'old-but-gold' exists in the track but is not in the problem-specifications repository.
}

0 comments on commit 5aa30dd

Please sign in to comment.