Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit a427dd9

Browse files
author
Aleksi Salmela
committed
Lazy load the course info object in context.
1 parent 144f432 commit a427dd9

10 files changed

Lines changed: 49 additions & 31 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/CliContext.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ public class CliContext {
2222
private Application application;
2323
private TmcCore tmcCore;
2424
private Settings settings;
25+
26+
/* cached values */
2527
private boolean hasLogin;
2628
private CourseInfo courseInfo;
27-
2829
private HashMap<String, String> properties;
2930
private final boolean inTest;
3031

@@ -60,6 +61,11 @@ public CliContext(Io io, WorkDir workDir, TmcCore core) {
6061
this.courseInfo = null;
6162
}
6263

64+
/*TODO create reset method for removing all cached data that is called
65+
* when working directory is changed or by users demand also use it in
66+
* constructor.
67+
*/
68+
6369
protected void setApp(Application app) {
6470
this.application = app;
6571
}
@@ -109,6 +115,24 @@ public Boolean saveProperties() {
109115
return SettingsIo.saveProperties(properties);
110116
}
111117

118+
public CourseInfo getCourseInfo() {
119+
if (courseInfo != null) {
120+
return courseInfo;
121+
}
122+
if (workDir.getConfigFile() == null) {
123+
return null;
124+
}
125+
courseInfo = CourseInfoIo.load(workDir.getConfigFile());
126+
if (courseInfo == null) {
127+
io.println("Course configuration file "
128+
+ workDir.getConfigFile().toString()
129+
+ "is invalid.");
130+
//TODO add a way to rewrite the course config file.
131+
return null;
132+
}
133+
return this.courseInfo;
134+
}
135+
112136
// Method is used to help testing
113137
public void setTmcCore(TmcCore tmcCore) {
114138
this.tmcCore = tmcCore;
@@ -168,23 +192,18 @@ private void createTmcCore(Settings settings) {
168192
}
169193

170194
private boolean createTmcCore() {
171-
Settings settings;
195+
Settings cachedSettings = null;
172196

173197
if (workDir.getConfigFile() != null) {
174198
// If we're in a course directory, we load settings matching the course
175199
// Otherwise we just load the last used settings
176-
courseInfo = CourseInfoIo.load(workDir.getConfigFile());
177-
if (courseInfo == null) {
178-
io.println("Course configuration file "
179-
+ workDir.getConfigFile().toString()
180-
+ "is invalid.");
181-
//TODO add a way to rewrite the course config file.
182-
return false;
200+
courseInfo = getCourseInfo();
201+
if (courseInfo != null) {
202+
cachedSettings = SettingsIo.load(courseInfo.getUsername(),
203+
courseInfo.getServerAddress());
183204
}
184-
settings = SettingsIo.load(courseInfo.getUsername(),
185-
courseInfo.getServerAddress());
186205
} else {
187-
settings = SettingsIo.load();
206+
cachedSettings = SettingsIo.load();
188207
}
189208

190209
hasLogin = true;
@@ -193,7 +212,7 @@ private boolean createTmcCore() {
193212
settings = new Settings();
194213
}
195214

196-
createTmcCore(settings);
215+
createTmcCore(cachedSettings);
197216
return true;
198217
}
199218
}

src/main/java/fi/helsinki/cs/tmc/cli/command/CourseInfoCommand.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ public void run(CommandLine args, Io io) {
4444
this.ctx = getContext();
4545
workDir = ctx.getWorkDir();
4646

47-
if (!args.hasOption("i")) {
47+
boolean fetchFromInternet = args.hasOption("i");
48+
49+
if (! ctx.loadBackend()) {
50+
return;
51+
}
52+
53+
if (!fetchFromInternet) {
4854
printLocalCourseOrExercise(args);
4955
return;
5056
}
@@ -55,9 +61,6 @@ public void run(CommandLine args, Io io) {
5561
return;
5662
}
5763

58-
if (! ctx.loadBackend()) {
59-
return;
60-
}
6164
course = TmcUtil.findCourse(ctx, stringArgs[0]);
6265
if (course == null) {
6366
io.println("The course " + stringArgs[0] + " doesn't exist on this server.");
@@ -67,8 +70,8 @@ public void run(CommandLine args, Io io) {
6770
}
6871

6972
private void printLocalCourseOrExercise(CommandLine args) {
70-
if (workDir.getConfigFile() != null) {
71-
info = CourseInfoIo.load(workDir.getConfigFile());
73+
info = ctx.getCourseInfo();
74+
if (info != null) {
7275
course = info.getCourse();
7376
printCourseOrExercise(args);
7477
} else {

src/main/java/fi/helsinki/cs/tmc/cli/command/ListCoursesCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import fi.helsinki.cs.tmc.cli.io.Io;
66
import fi.helsinki.cs.tmc.cli.tmcstuff.TmcUtil;
77

8-
import fi.helsinki.cs.tmc.core.TmcCore;
98
import fi.helsinki.cs.tmc.core.domain.Course;
109

1110
import org.apache.commons.cli.CommandLine;

src/main/java/fi/helsinki/cs/tmc/cli/command/ListExercisesCommand.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void getOptions(Options options) {
3535
public void run(CommandLine args, Io io) {
3636
this.ctx = getContext();
3737
this.io = io;
38-
38+
3939
String courseName = getCourseName(args);
4040
if (courseName == null) {
4141
return;
@@ -77,10 +77,7 @@ private String getCourseNameFromCurrentDirectory() {
7777
private CourseInfo getCourseInfoFromCurrentDirectory() {
7878
WorkDir workDir = ctx.getWorkDir();
7979
workDir.addPath();
80-
if (workDir.getConfigFile() != null) {
81-
return CourseInfoIo.load(workDir.getConfigFile());
82-
}
83-
return null;
80+
return ctx.getCourseInfo();
8481
}
8582

8683
private List<Exercise> getExercisesFromServer(String courseName) {

src/main/java/fi/helsinki/cs/tmc/cli/command/PasteCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ public void run(CommandLine args, Io io) {
8888
}
8989

9090
String exerciseName = exercisenames.get(0);
91-
CourseInfo courseinfo = CourseInfoIo.load(ctx.getWorkDir().getConfigFile());
92-
Exercise exercise = courseinfo.getExercise(exerciseName);
91+
CourseInfo info = ctx.getCourseInfo();
92+
Exercise exercise = info.getExercise(exerciseName);
9393
URI uri = TmcUtil.sendPaste(ctx, exercise, message);
9494
if (uri == null) {
9595
io.println("Unable to send the paste");

src/main/java/fi/helsinki/cs/tmc/cli/command/RunTestsCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void run(CommandLine args, Io io) {
6767
io.println("You have to be in a course directory to run tests");
6868
return;
6969
}
70-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
70+
CourseInfo info = ctx.getCourseInfo();
7171

7272
ResultPrinter resultPrinter
7373
= new ResultPrinter(io, this.showDetails, this.showPassed);

src/main/java/fi/helsinki/cs/tmc/cli/command/SubmitCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void run(CommandLine args, Io io) {
8888
return;
8989
}
9090

91-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
91+
CourseInfo info = ctx.getCourseInfo();
9292
Course currentCourse = info.getCourse();
9393
if (currentCourse == null) {
9494
return;

src/main/java/fi/helsinki/cs/tmc/cli/command/UpdateCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void run(CommandLine args, Io io) {
5353
return;
5454
}
5555

56-
CourseInfo info = CourseInfoIo.load(workDir.getConfigFile());
56+
CourseInfo info = ctx.getCourseInfo();
5757
updateExercises(info, workDir.getConfigFile());
5858
}
5959

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/SettingsHolder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package fi.helsinki.cs.tmc.cli.tmcstuff;
22

33
import java.util.ArrayList;
4-
import java.util.HashMap;
54

65
/**
76
* This is a class for storing all different login settings as a single array.

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/WorkDir.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public List<String> getExerciseNames(
8181
return new ArrayList<>();
8282
}
8383
}
84+
/*TODO somehow use the ctx.getCourseInfo */
8485
CourseInfo courseinfo = CourseInfoIo.load(getConfigFile());
8586
if (courseinfo == null) {
8687
return new ArrayList<>();

0 commit comments

Comments
 (0)