Skip to content

Commit 60b57f1

Browse files
committed
Add legacy solution handling
Adds compatibility for already downloaded exercises
1 parent cef955f commit 60b57f1

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

workspace/solution.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workspace
22

33
import (
44
"encoding/json"
5+
"errors"
56
"fmt"
67
"io/ioutil"
78
"os"
@@ -71,7 +72,9 @@ func (s *Solution) Write(dir string) error {
7172
}
7273

7374
path := filepath.Join(dir, ignoreSubdir)
74-
_ = os.Mkdir(path, os.FileMode(0755))
75+
if err := createIgnoreSubdir(path); err != nil {
76+
return err
77+
}
7578

7679
// Hack because ioutil.WriteFile fails on hidden files
7780
visibility.ShowFile(path)
@@ -92,3 +95,22 @@ func (s *Solution) PathToParent() string {
9295
}
9396
return filepath.Join(dir, s.Track)
9497
}
98+
99+
func createIgnoreSubdir(path string) error {
100+
if _, err := os.Stat(path); os.IsNotExist(err) {
101+
if err := os.Mkdir(path, os.FileMode(0755)); err != nil {
102+
return fmt.Errorf("failed to create directory: %s", path)
103+
}
104+
}
105+
return nil
106+
}
107+
108+
func migrateLegacySolutionFile(path string, legacySolutionPath string, solutionPath string) error {
109+
if err := createIgnoreSubdir(filepath.Join(path, ignoreSubdir)); err != nil {
110+
return err
111+
}
112+
if err := os.Rename(legacySolutionPath, solutionPath); err != nil {
113+
return errors.New("failed migrating legacy solution file")
114+
}
115+
return nil
116+
}

workspace/workspace.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,29 @@ func (ws Workspace) SolutionDir(s string) (string, error) {
186186
path := s
187187
for {
188188
if path == ws.Dir {
189-
return "", errors.New("couldn't find it")
189+
return "", fmt.Errorf("couldn't find %s", solutionFilename)
190190
}
191191
if _, err := os.Lstat(path); os.IsNotExist(err) {
192192
return "", err
193193
}
194-
if _, err := os.Lstat(filepath.Join(path, solutionRelPath)); err == nil {
194+
if err := checkSolutionFile(path); err == nil {
195195
return path, nil
196196
}
197197
path = filepath.Dir(path)
198198
}
199199
}
200+
201+
func checkSolutionFile(path string) error {
202+
legacySolutionPath := filepath.Join(path, ".solution.json")
203+
solutionPath := filepath.Join(path, solutionRelPath)
204+
205+
if _, legacyErr := os.Lstat(legacySolutionPath); legacyErr == nil {
206+
return migrateLegacySolutionFile(path, legacySolutionPath, solutionPath)
207+
} else if _, err := os.Lstat(solutionPath); err != nil {
208+
return err
209+
} else if legacyErr != nil && err == nil {
210+
return nil
211+
} else {
212+
return legacyErr
213+
}
214+
}

0 commit comments

Comments
 (0)