OSDN Git Service

feat: add fileIO components
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / CheckVersion.java
1 package com.ranfa.lib;
2
3 import java.io.IOException;
4 import java.net.URL;
5
6 import javax.swing.JOptionPane;
7
8 import org.slf4j.LoggerFactory;
9
10 import com.fasterxml.jackson.databind.JsonNode;
11 import com.fasterxml.jackson.databind.ObjectMapper;
12 import com.ranfa.main.DelesteRandomSelector;
13
14 public class CheckVersion {
15
16         private final static String URI = "https://raw.githubusercontent.com/hizumiaoba/DelesteRandomSelector/master/version.json";
17         private final static String RELEASE_STRING = "https://github.com/hizumiaoba/DelesteRandomSelector/releases";
18
19         public static void needToBeUpdated() {
20                 if(!getSuffix().equals(Suffix.STABLE))
21                         return;
22                 int latestMajor = 0;
23                 int latestMinor = 0;
24                 int latestPatch = 0;
25                 try {
26                         JsonNode node = new ObjectMapper().readTree(new URL(URI));
27                         latestMajor = node.get("major").asInt();
28                         latestMinor = node.get("minor").asInt();
29                         latestPatch = node.get("patch").asInt();
30                 } catch (IOException e) {
31                         LoggerFactory.getLogger(CheckVersion.class).error("Error while processing version tag.", e);
32                 }
33                 String newVersion = String.format("v%d.%d.%d", latestMajor, latestMinor, latestPatch);
34                 if(latestMajor > getMajorVersion()) {
35                         JOptionPane.showInputDialog(null, String.format("大規模なソフトウェアの更新が公開されています。速やかにアップデートをお願いします。\n最新バージョン:%s", newVersion), RELEASE_STRING);
36                 } else if(latestMinor > getMinorVersion()) {
37                         JOptionPane.showInputDialog(null, String.format("ソフトウェアの軽微な機能改修が公開されています。こちらから最新バージョンをダウンロードしてください。\n最新バージョン:%s", newVersion), RELEASE_STRING);
38                 } else if(latestPatch > getPatchVersion()) {
39                         JOptionPane.showInputDialog(null, String.format("ソフトウェアのバグ修正が公開されています。こちらから最新バージョンをダウンロードしてください。\n最新バージョン:%s", newVersion), RELEASE_STRING);
40                 }
41         }
42
43
44         /**
45          * アノテーションで記載されているバージョンを取得します
46          * @since v1.0.0
47          * @return アノテーションで定義されているバージョン
48          */
49         public static String getVersion() {
50                 String value = "v"
51                                 + getMajorVersion() + "."
52                                 + getMinorVersion() + "."
53                                 + getPatchVersion() + "-"
54                                 + getSuffix().toString();
55                 return value;
56         }
57
58         public static int getMajorVersion() {
59                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
60                 return version.major();
61         }
62
63         public static int getMinorVersion() {
64                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
65                 return version.minor();
66         }
67
68         public static int getPatchVersion() {
69                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
70                 return version.patch();
71         }
72         
73         public static Suffix getSuffix() {
74                 Version version = DelesteRandomSelector.class.getAnnotation(Version.class);
75                 return version.suffix();
76         }
77
78 }