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

Commit 1da0c82

Browse files
authored
Merge branch 'master' into new-update-command-juha
2 parents 0d51e8a + 3e73d34 commit 1da0c82

File tree

6 files changed

+90
-31
lines changed

6 files changed

+90
-31
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>fi.helsinki.cs.tmc.cli</groupId>
55
<artifactId>tmc-cli</artifactId>
6-
<version>0.4.2</version>
6+
<version>0.5.0</version>
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

scripts/autocompletion.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# SCRIPT_PATH will be filled by stub script
44
alias tmc="$SCRIPT_PATH/tmc"
55

6-
tmcCommands=\$(tmc shell-helpper -c)
6+
tmcCommands=\$(tmc shell-helper -c)
77

88
_tmc_opts()
99
{

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

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.nio.file.Path;
3030
import java.nio.file.Paths;
3131
import java.util.Arrays;
32+
import java.util.Date;
3233
import java.util.HashMap;
3334
import java.util.Properties;
3435

@@ -39,12 +40,18 @@
3940
*/
4041
public class Application {
4142
private static final Logger logger = LoggerFactory.getLogger(Application.class);
43+
private static final String previousUpdateDateKey = "update-date";
44+
private static final long defaultUpdateInterval = 60 * 60 * 1000;
45+
4246
private CommandFactory commandFactory;
47+
private HashMap<String, String> properties;
4348
private TmcCore tmcCore;
4449
private Settings settings;
45-
private Io io;
4650
private WorkDir workDir;
47-
private HashMap<String, String> properties;
51+
private Io io;
52+
53+
private boolean inTest;
54+
private ShutdownHandler shutdownHandler;
4855

4956
private Options options;
5057
private GnuParser parser;
@@ -55,6 +62,15 @@ public Application(Io io) {
5562
this.commandFactory = new CommandFactory();
5663
options.addOption("h", "help", false, "Display help information about tmc-cli");
5764
options.addOption("v", "version", false, "Give the version of the tmc-cli");
65+
66+
inTest = true;
67+
if (io == null) {
68+
inTest = false;
69+
io = new TerminalIo();
70+
shutdownHandler = new ShutdownHandler(io);
71+
Runtime.getRuntime().addShutdownHook(shutdownHandler);
72+
}
73+
5874
this.io = io;
5975
this.workDir = new WorkDir();
6076
this.properties = SettingsIo.loadProperties();
@@ -119,12 +135,15 @@ public void printHelp() {
119135
}
120136

121137
public void run(String[] args) {
138+
if (!inTest) {
139+
versionCheck();
140+
}
141+
122142
String[] tmcArgs;
123143
String[] commandArgs;
124144
String commandName;
125-
int commandIndex;
126145

127-
commandIndex = findCommand(args);
146+
int commandIndex = findCommand(args);
128147

129148
if (commandIndex != -1) {
130149
commandName = args[commandIndex];
@@ -133,7 +152,6 @@ public void run(String[] args) {
133152
tmcArgs = Arrays.copyOfRange(args, 0, commandIndex);
134153
commandArgs = Arrays.copyOfRange(args, commandIndex + 1, args.length);
135154

136-
137155
} else {
138156
commandName = "help";
139157
tmcArgs = args;
@@ -145,6 +163,10 @@ public void run(String[] args) {
145163
}
146164

147165
runCommand(commandName, commandArgs);
166+
167+
if (!inTest) {
168+
Runtime.getRuntime().removeShutdownHook(shutdownHandler);
169+
}
148170
}
149171

150172
public void createTmcCore(Settings settings) {
@@ -207,15 +229,8 @@ public void setSettings(Settings settings) {
207229
}
208230

209231
public static void main(String[] args) {
210-
Io io = new TerminalIo();
211-
ShutdownHandler shutdownHandler = new ShutdownHandler(io);
212-
Runtime.getRuntime().addShutdownHook(shutdownHandler);
213-
214-
new TmcCliUpdater(io, getVersion(), isWindows()).run();
215-
216-
Application app = new Application(io);
232+
Application app = new Application(null);
217233
app.run(args);
218-
Runtime.getRuntime().removeShutdownHook(shutdownHandler);
219234
}
220235

221236
public static String getVersion() {
@@ -263,4 +278,33 @@ public static boolean isWindows() {
263278
public CourseInfo createCourseInfo(Course course) {
264279
return new CourseInfo(settings, course);
265280
}
281+
282+
private void versionCheck() {
283+
String previousTimestamp = properties.get(previousUpdateDateKey);
284+
Date previous = null;
285+
286+
if (previousTimestamp != null) {
287+
long time;
288+
try {
289+
time = Long.parseLong(previousTimestamp);
290+
} catch (NumberFormatException ex) {
291+
io.println("The previous update date isn't number.");
292+
logger.warn("The previous update date isn't number.", ex);
293+
return;
294+
}
295+
previous = new Date(time);
296+
}
297+
298+
Date now = new Date();
299+
if (previous != null && previous.getTime() + defaultUpdateInterval > now.getTime()) {
300+
return;
301+
}
302+
303+
TmcCliUpdater update = new TmcCliUpdater(io, getVersion(), isWindows());
304+
update.run();
305+
306+
long timestamp = now.getTime();
307+
properties.put(previousUpdateDateKey, Long.toString(timestamp));
308+
setProperties(properties);
309+
}
266310
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,17 @@ private List<String> getCommandStrings() {
4747
if ((Class)commandClass == (Class)TestCommand.class) {
4848
continue;
4949
}
50+
if ((Class)commandClass == (Class)ShellHelperCommand.class) {
51+
continue;
52+
}
5053
strings.add(createCommandString(command));
5154
}
5255
return strings;
5356
}
5457

5558
private String createCommandString(Command command) {
5659
StringBuilder builder = new StringBuilder();
57-
builder.append(" " + command.name());
60+
builder.append(" ").append(command.name());
5861
for (int i = 0; i < longestName - command.name().length() + 2; i++) {
5962
builder.append(" ");
6063
}

src/main/java/fi/helsinki/cs/tmc/cli/command/ShellHelpperCommand.java renamed to src/main/java/fi/helsinki/cs/tmc/cli/command/ShellHelperCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import org.apache.commons.cli.CommandLine;
99
import org.apache.commons.cli.Options;
1010

11-
@Command(name = "shell-helpper", desc = "Submit exercises")
12-
public class ShellHelpperCommand extends AbstractCommand {
11+
@Command(name = "shell-helper", desc = "Used by autocomplete to extract internal values.")
12+
public class ShellHelperCommand extends AbstractCommand {
1313

1414
@Override
1515
public void getOptions(Options options) {
@@ -25,6 +25,8 @@ public void run(CommandLine args, Io io) {
2525
Command command = CommandFactory.getCommand(commandClass);
2626
io.println(command.name());
2727
}
28+
} else {
29+
io.println("This is only for internal usage.");
2830
}
2931
}
3032

src/main/java/fi/helsinki/cs/tmc/cli/updater/TmcCliUpdater.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.File;
2222
import java.io.InputStream;
2323
import java.io.IOException;
24+
import java.util.logging.Level;
2425

2526
public class TmcCliUpdater {
2627

@@ -87,38 +88,47 @@ public void run() {
8788
runNewTmcCliBinary(destination.getAbsolutePath());
8889
}
8990

90-
protected InputStream fetchHttpEntity(String url) {
91+
protected byte[] fetchHttpEntity(String url) {
9192
CloseableHttpClient httpClient = HttpClients.createDefault();
9293
HttpGet httpGet = new HttpGet(url);
9394
httpGet.addHeader("User-Agent", "tmc-cli (https://github.com/tmc-cli/tmc-cli)");
9495

95-
InputStream stream = null;
96+
HttpEntity entity;
97+
byte[] content;
9698
try {
9799
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
98-
HttpEntity entity = httpResponse.getEntity();
99-
if (entity != null) {
100-
stream = entity.getContent();
100+
entity = httpResponse.getEntity();
101+
if (entity == null) {
102+
logger.warn("Failed to get http request content.");
103+
httpGet.releaseConnection();
104+
return null;
101105
}
102106

103107
} catch (IOException ex) {
104108
logger.warn("Failed to create http connection to github.", ex);
109+
return null;
110+
}
111+
try {
112+
content = IOUtils.toByteArray(entity.getContent());
113+
} catch (IOException ex) {
114+
logger.warn("Failed to fetch data from github", ex);
115+
content = null;
105116
}
106-
107117
httpGet.releaseConnection();
108-
return stream;
118+
return content;
109119
}
110120

111121
/**
112122
* Downloads a JSON string that contains information about the latest
113123
* tmc-cli release.
114124
*/
115125
protected String fetchLatestReleaseJson() {
116-
InputStream stream = fetchHttpEntity(LATEST_RELEASE_URL);
117-
if (stream == null) {
126+
byte[] content = fetchHttpEntity(LATEST_RELEASE_URL);
127+
if (content == null) {
118128
return null;
119129
}
120130
try {
121-
return IOUtils.toString(stream, "UTF-8");
131+
return new String(content, "UTF-8");
122132
} catch (IOException ex) {
123133
logger.warn("Failed to fetch JSON data for the latest release", ex);
124134
}
@@ -130,13 +140,13 @@ protected String fetchLatestReleaseJson() {
130140
* file.
131141
*/
132142
protected void fetchTmcCliBinary(String downloadUrl, File destination) {
133-
InputStream stream = fetchHttpEntity(downloadUrl);
134-
if (stream == null) {
143+
byte[] content = fetchHttpEntity(downloadUrl);
144+
if (content == null) {
135145
io.println("Failed to download tmc-cli.");
136146
return;
137147
}
138148
try {
139-
FileUtils.copyInputStreamToFile(stream, destination);
149+
FileUtils.writeByteArrayToFile(destination, content);
140150
} catch (IOException ex) {
141151
io.println("Failed to write the new version into \'" + destination + "\'.");
142152
}

0 commit comments

Comments
 (0)