sync: rehandle dependency, trivial processing

This commit is contained in:
nemunaire 2018-09-09 22:50:06 +02:00 committed by Pierre-Olivier Mercier
parent 2a6fbd4e32
commit 3146e75ead

View File

@ -36,7 +36,7 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
if exercices, err := getExercices(i, theme); err != nil { if exercices, err := getExercices(i, theme); err != nil {
errs = append(errs, err.Error()) errs = append(errs, err.Error())
} else { } else {
dmap := map[int]fic.Exercice{} dmap := map[int64]fic.Exercice{}
emap := map[string]int{} emap := map[string]int{}
for _, edir := range exercices { for _, edir := range exercices {
edir_splt := strings.SplitN(edir, "-", 2) edir_splt := strings.SplitN(edir, "-", 2)
@ -68,6 +68,7 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
// Handle score gain // Handle score gain
var gain int64 var gain int64
var depend *fic.Exercice
if p, err := parseExerciceParams(i, path.Join(theme.Path, edir)); err != nil { if p, err := parseExerciceParams(i, path.Join(theme.Path, edir)); err != nil {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: challenge.txt: %s", edir, err))
continue continue
@ -75,6 +76,27 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
errs = append(errs, fmt.Sprintf("%q: challenge.txt: Undefined gain for challenge", edir)) errs = append(errs, fmt.Sprintf("%q: challenge.txt: Undefined gain for challenge", edir))
} else { } else {
gain = p.Gain gain = p.Gain
// Handle dependency
if len(p.Dependencies) > 0 {
if len(p.Dependencies[0].Theme) > 0 && p.Dependencies[0].Theme != theme.Name {
errs = append(errs, fmt.Sprintf("%q: unable to treat dependency to another theme: not implemented.", edir))
} else {
for ed, e := range dmap {
if ed == p.Dependencies[0].Id {
depend = &e
break
}
}
if depend == nil {
dmap_keys := []string{}
for k, _ := range dmap {
dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k))
}
errs = append(errs, fmt.Sprintf("%q: Unable to find required dependancy %q (available at time of processing: %s)", edir, p.Dependencies[0].Id, strings.Join(dmap_keys, ",")))
}
}
}
} }
// Handle video // Handle video
@ -89,11 +111,11 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
overview = string(blackfriday.Run([]byte(overview))) overview = string(blackfriday.Run([]byte(overview)))
if e, err := theme.GetExerciceByTitle(ename); err != nil { if e, err := theme.GetExerciceByTitle(ename); err != nil {
if ex, err := theme.AddExercice(ename, fic.ToURLid(ename), path.Join(theme.Path, edir), statement, overview, nil, gain, videoURI); err != nil { if ex, err := theme.AddExercice(ename, fic.ToURLid(ename), path.Join(theme.Path, edir), statement, overview, depend, gain, videoURI); err != nil {
errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: error on exercice add: %s", edir, err))
continue continue
} else { } else {
dmap[eid] = ex dmap[int64(eid)] = ex
} }
} else if e.Title != ename || e.URLId == "" || e.Statement != statement || e.Overview != overview || e.Gain != gain || e.VideoURI != videoURI { } else if e.Title != ename || e.URLId == "" || e.Statement != statement || e.Overview != overview || e.Gain != gain || e.VideoURI != videoURI {
e.Title = ename e.Title = ename
@ -106,10 +128,10 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
errs = append(errs, fmt.Sprintf("%q: error on exercice update: %s", edir, err)) errs = append(errs, fmt.Sprintf("%q: error on exercice update: %s", edir, err))
continue continue
} else { } else {
dmap[eid] = e dmap[int64(eid)] = e
} }
} else { } else {
dmap[eid] = e dmap[int64(eid)] = e
} }
} }
@ -121,35 +143,6 @@ func SyncExercices(i Importer, theme fic.Theme) []string {
} }
} }
} }
dmap_keys := []string{}
for k, _ := range dmap {
dmap_keys = append(dmap_keys, fmt.Sprintf("%d", k))
}
for _, e := range dmap {
// No error if the file doesn't exist
if ! i.exists(path.Join(e.Path, "depends.txt")) {
continue
}
// Treat depends.txt
if depends, err := getFileContent(i, path.Join(e.Path, "depends.txt")); err != nil {
errs = append(errs, fmt.Sprintf("%q: depends.txt: %s", path.Base(e.Path), err))
} else {
for nline, dep := range strings.Split(depends, "\n") {
if did, err := strconv.Atoi(dep); err != nil {
errs = append(errs, fmt.Sprintf("%q: depends.txt:%d: %s", path.Base(e.Path), nline + 1, err))
continue
} else if exdep, exist := dmap[did]; !exist {
errs = append(errs, fmt.Sprintf("%q: depends.txt:%d: Unable to find required dependancy %q (%d ; available: %s)", path.Base(e.Path), nline + 1, dep, did, strings.Join(dmap_keys, ",")))
continue
} else {
e.Depend = &exdep.Id
e.Update()
}
}
}
}
} }
return errs return errs
} }