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

Commit 47ba978

Browse files
authored
Merge branch 'master' into docker
2 parents 5b843b0 + e4c1698 commit 47ba978

74 files changed

Lines changed: 1525 additions & 1134 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
</goals>
141141
<configuration>
142142
<annotationProcessors>
143-
<arg>fi.helsinki.cs.tmc.cli.command.core.CommandAnnotationProcessor</arg>
143+
<arg>fi.helsinki.cs.tmc.cli.core.CommandAnnotationProcessor</arg>
144144
</annotationProcessors>
145145
<fork>true</fork> <!-- bug; non-forked compiler has basedir set to user.dir or sth -->
146146
<excludes>

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package fi.helsinki.cs.tmc.cli;
22

3-
import fi.helsinki.cs.tmc.cli.command.core.AbstractCommand;
4-
import fi.helsinki.cs.tmc.cli.command.core.CommandFactory;
3+
import fi.helsinki.cs.tmc.cli.core.AbstractCommand;
4+
import fi.helsinki.cs.tmc.cli.core.CliContext;
5+
import fi.helsinki.cs.tmc.cli.core.CommandFactory;
56
import fi.helsinki.cs.tmc.cli.io.Color;
7+
import fi.helsinki.cs.tmc.cli.io.ColorUtil;
68
import fi.helsinki.cs.tmc.cli.io.EnvironmentUtil;
79
import fi.helsinki.cs.tmc.cli.io.HelpGenerator;
810
import fi.helsinki.cs.tmc.cli.io.Io;
911
import fi.helsinki.cs.tmc.cli.io.ShutdownHandler;
10-
import fi.helsinki.cs.tmc.cli.updater.TmcCliUpdater;
12+
import fi.helsinki.cs.tmc.cli.updater.AutoUpdater;
1113

1214
import org.apache.commons.cli.CommandLine;
1315
import org.apache.commons.cli.GnuParser;
@@ -58,13 +60,13 @@ public Application(CliContext context) {
5860
}
5961

6062
private boolean runCommand(String name, String[] args) {
61-
AbstractCommand command = CommandFactory.createCommand(this.context, name);
63+
AbstractCommand command = CommandFactory.createCommand(name);
6264
if (command == null) {
6365
io.println("Command " + name + " doesn't exist.");
6466
return false;
6567
}
6668

67-
command.execute(args, io);
69+
command.execute(context, args);
6870
return true;
6971
}
7072

@@ -169,7 +171,7 @@ private boolean versionCheck() {
169171
public boolean runAutoUpdate() {
170172
Map<String, String> properties = context.getProperties();
171173
Date now = new Date();
172-
TmcCliUpdater update = TmcCliUpdater.createUpdater(io,
174+
AutoUpdater update = AutoUpdater.createUpdater(io,
173175
EnvironmentUtil.getVersion(), EnvironmentUtil.isWindows());
174176
boolean updated = update.run();
175177

@@ -181,16 +183,16 @@ public boolean runAutoUpdate() {
181183
}
182184

183185
//TODO rename this as getColorProperty and move it somewhere else
184-
public Color.AnsiColor getColor(String propertyName) {
186+
public Color getColor(String propertyName) {
185187
String propertyValue = context.getProperties().get(propertyName);
186-
Color.AnsiColor color = Color.getColor(propertyValue);
188+
Color color = ColorUtil.getColor(propertyValue);
187189
if (color == null) {
188190
switch (propertyName) {
189-
case "progressbar-left": return Color.AnsiColor.ANSI_CYAN;
190-
case "progressbar-right": return Color.AnsiColor.ANSI_CYAN;
191-
case "testresults-left": return Color.AnsiColor.ANSI_GREEN;
192-
case "testresults-right": return Color.AnsiColor.ANSI_RED;
193-
default: return Color.AnsiColor.ANSI_NONE;
191+
case "progressbar-left": return Color.CYAN;
192+
case "progressbar-right": return Color.CYAN;
193+
case "testresults-left": return Color.GREEN;
194+
case "testresults-right": return Color.RED;
195+
default: return Color.NONE;
194196
}
195197
}
196198
return color;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fi.helsinki.cs.tmc.cli.backend;
2+
3+
/**
4+
* This object stores all login info.
5+
*/
6+
public class Account {
7+
8+
static Account NULL_ACCOUNT = new Account(null, null, null);
9+
10+
private String serverAddress;
11+
private String username;
12+
private String password;
13+
14+
// for gson
15+
public Account() { }
16+
17+
public Account(String serverAddress, String username, String password) {
18+
this.serverAddress = serverAddress;
19+
this.username = username;
20+
this.password = password;
21+
}
22+
23+
public String getServerAddress() {
24+
return serverAddress;
25+
}
26+
27+
public String getPassword() {
28+
return password;
29+
}
30+
31+
public String getUsername() {
32+
return username;
33+
}
34+
35+
private static boolean stringEquals(String str1, String str2) {
36+
return (str1 == null ? str2 == null : str1.equals(str2));
37+
}
38+
39+
@Override
40+
public boolean equals(Object obj) {
41+
if (obj == null) {
42+
return false;
43+
}
44+
if (!(obj instanceof Account)) {
45+
return false;
46+
}
47+
Account another = (Account) obj;
48+
return stringEquals(this.serverAddress, another.serverAddress)
49+
&& stringEquals(this.username, another.username)
50+
&& stringEquals(this.password, another.password);
51+
}
52+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package fi.helsinki.cs.tmc.cli.backend;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
/**
9+
* This is a class for storing all different accounts as a single array.
10+
*/
11+
public class AccountList implements Iterable<Account> {
12+
13+
private List<Account> accountArray;
14+
15+
public AccountList() {
16+
this.accountArray = new ArrayList<>();
17+
}
18+
19+
//TODO This should not be used
20+
public Account getAccount() {
21+
if (this.accountArray.size() > 0) {
22+
// Get last used account by default
23+
return this.accountArray.get(0);
24+
}
25+
return null;
26+
}
27+
28+
public Account getAccount(String username, String server) {
29+
if (username == null || server == null) {
30+
return getAccount();
31+
}
32+
for (Account account : this.accountArray) {
33+
if (account.getUsername().equals(username)
34+
&& account.getServerAddress().equals(server)) {
35+
// Move account to index 0 so we can always use the last used account by default
36+
this.accountArray.remove(account);
37+
this.accountArray.add(0, account);
38+
return account;
39+
}
40+
}
41+
return null;
42+
}
43+
44+
public void addAccount(Account newSettings) {
45+
for (Account account : this.accountArray) {
46+
if (account.getUsername().equals(newSettings.getUsername())
47+
&& account.getServerAddress().equals(newSettings.getServerAddress())) {
48+
// Replace old account if username and server match
49+
this.accountArray.remove(account);
50+
break;
51+
}
52+
}
53+
this.accountArray.add(0, newSettings);
54+
}
55+
56+
public void deleteAccount(String username, String server) {
57+
Account remove = null;
58+
for (Account account : this.accountArray) {
59+
if (account.getUsername().equals(username)
60+
&& account.getServerAddress().equals(server)) {
61+
remove = account;
62+
break;
63+
}
64+
}
65+
if (remove != null) {
66+
this.accountArray.remove(remove);
67+
}
68+
}
69+
70+
public void deleteAllAccounts() {
71+
this.accountArray = new ArrayList<>();
72+
}
73+
74+
public int getAccountCount() {
75+
return this.accountArray.size();
76+
}
77+
78+
public List<Account> getAccountList() {
79+
return Collections.unmodifiableList(this.accountArray);
80+
}
81+
82+
@Override
83+
public Iterator<Account> iterator() {
84+
return getAccountList().iterator();
85+
}
86+
}

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/CourseInfo.java renamed to src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfo.java

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

3-
import fi.helsinki.cs.tmc.core.configuration.TmcSettings;
43
import fi.helsinki.cs.tmc.core.domain.Course;
54
import fi.helsinki.cs.tmc.core.domain.Exercise;
65

@@ -19,12 +18,12 @@ public class CourseInfo {
1918
private List<String> localCompletedExercises;
2019
private HashMap<String, String> properties;
2120

22-
public CourseInfo(TmcSettings settings, Course course) {
23-
this.username = settings.getUsername();
24-
this.serverAddress = settings.getServerAddress();
21+
public CourseInfo(Account account, Course course) {
22+
this.username = account.getUsername();
23+
this.serverAddress = account.getServerAddress();
2524
this.course = course;
2625
this.properties = new HashMap<>();
27-
this.localCompletedExercises = new ArrayList<String>();
26+
this.localCompletedExercises = new ArrayList<>();
2827
}
2928

3029
public String getUsername() {
@@ -47,7 +46,7 @@ public List<String> getLocalCompletedExercises() {
4746
// Check for null pointer in case of old .tmc.json files
4847
// Remove this when we are sure nobody's using 0.5.1 anymore
4948
if (this.localCompletedExercises == null) {
50-
this.localCompletedExercises = new ArrayList<String>();
49+
this.localCompletedExercises = new ArrayList<>();
5150
}
5251
return this.localCompletedExercises;
5352
}
@@ -78,6 +77,7 @@ public List<String> getExerciseNames() {
7877
return names;
7978
}
8079

80+
//TODO This is exactly same method as TmcUtil.findExercise(course, name)
8181
public Exercise getExercise(String name) {
8282
for (Exercise exercise : this.course.getExercises()) {
8383
if (exercise.getName().equals(name)) {

src/main/java/fi/helsinki/cs/tmc/cli/tmcstuff/CourseInfoIo.java renamed to src/main/java/fi/helsinki/cs/tmc/cli/backend/CourseInfoIo.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
package fi.helsinki.cs.tmc.cli.tmcstuff;
1+
package fi.helsinki.cs.tmc.cli.backend;
2+
3+
import fi.helsinki.cs.tmc.core.domain.Course;
24

35
import com.google.gson.Gson;
46
import org.slf4j.Logger;
@@ -50,4 +52,14 @@ public static CourseInfo load(Path courseInfoFile) {
5052
}
5153
return gson.fromJson(reader, CourseInfo.class);
5254
}
55+
56+
public static void createNewCourse(Course course, Account account, Path parentDir) {
57+
Path configFile = parentDir
58+
.resolve(course.getName())
59+
.resolve(CourseInfoIo.COURSE_CONFIG);
60+
61+
CourseInfo info = new CourseInfo(account, course);
62+
info.setExercises(course.getExercises());
63+
CourseInfoIo.save(info, configFile);
64+
}
5365
}

0 commit comments

Comments
 (0)