fix plugins reset

Signed-off-by: pengzhile <pengzhile@gmail.com>
pull/8/head
pengzhile 2020-11-11 18:31:00 +08:00
parent 0110c6868a
commit f3c9630a7d
14 changed files with 212 additions and 151 deletions

View File

@ -2,5 +2,6 @@
1. Download and install plugin from [Release Page](https://gitee.com/pengzhile/ide-eval-resetter/attach_files/516690/download/ide-eval-resetter-2.0.3.zip).
2. Click `Help` or `Get Help` -> `Eval Reset` menu.
3. Restart your IDE.
4. Now you have another 30 days eval time :)
3. Click `Reset` -> `Yes` button.
4. Restart your IDE.
5. Now you have another 30 days eval time :)

View File

@ -4,10 +4,10 @@ plugins {
}
group 'io.zhile.research.intellij'
version '2.0.3'
version '2.0.4'
sourceCompatibility = 1.7
targetCompatibility = 1.7
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
@ -29,6 +29,9 @@ intellij {
patchPluginXml {
changeNotes """<pre>
Release v2.0.4
1. fix plugins reset
2. reset more gracefully
Release v2.0.3
1. more friendly "Reload" icon
Release v2.0.2

View File

@ -14,6 +14,6 @@ public class RestartAction extends AnAction implements DumbAware {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
ApplicationManager.getApplication().restart();
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().restart());
}
}

View File

@ -0,0 +1,5 @@
package io.zhile.research.intellij.ier.common;
public interface EvalRecord {
void reset() throws Exception;
}

View File

@ -0,0 +1,39 @@
package io.zhile.research.intellij.ier.common;
import com.intellij.openapi.util.io.FileUtil;
import io.zhile.research.intellij.ier.helper.DateTime;
import java.io.*;
import java.util.Date;
public class LicenseFileRecord implements EvalRecord {
private final String type = "LICENSE";
private final File file;
private final Date expireDate;
public LicenseFileRecord(File file) {
this.file = file;
try (DataInputStream dis = new DataInputStream(new FileInputStream(file))) {
expireDate = new Date(~dis.readLong() + 2592000000L);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void reset() throws Exception {
if (!FileUtil.delete(file)) {
throw new Exception("Remove " + type + " failed: " + file.getAbsolutePath());
}
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream(file))) {
dos.writeLong(~System.currentTimeMillis());
}
}
@Override
public String toString() {
return type + ": " + file.getName() + ", UNTIL: " + DateTime.DF_DATETIME.format(expireDate);
}
}

View File

@ -0,0 +1,26 @@
package io.zhile.research.intellij.ier.common;
import com.intellij.openapi.util.io.FileUtil;
import java.io.File;
public class NormalFileRecord implements EvalRecord {
private final String type = "FILE";
private final File file;
public NormalFileRecord(File file) {
this.file = file;
}
@Override
public void reset() throws Exception {
if (!FileUtil.delete(file)) {
throw new Exception("Remove " + type + " failed: " + file.getAbsolutePath());
}
}
@Override
public String toString() {
return type + ": " + file.getName();
}
}

View File

@ -0,0 +1,48 @@
package io.zhile.research.intellij.ier.common;
import com.intellij.ide.Prefs;
import java.util.prefs.Preferences;
public class PreferenceRecord implements EvalRecord {
private static final String DEFAULT_VALUE = null;
private final String type = "PREFERENCE";
private final String key;
private final String value;
private final boolean isRaw;
public PreferenceRecord(String key) {
this(key, false);
}
public PreferenceRecord(String key, boolean isRaw) {
this.key = key;
this.isRaw = isRaw;
this.value = isRaw ? Preferences.userRoot().get(key, DEFAULT_VALUE) : Prefs.get(key, DEFAULT_VALUE);
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public void reset() throws Exception {
if (isRaw) {
Preferences.userRoot().remove(key);
} else {
Prefs.remove(key);
}
Resetter.syncPrefs();
}
@Override
public String toString() {
return type + ": " + key + " = " + (null == value ? "" : value);
}
}

View File

@ -0,0 +1,35 @@
package io.zhile.research.intellij.ier.common;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.PropertiesComponentImpl;
public class PropertyRecord implements EvalRecord {
public static final PropertiesComponentImpl PROPS = (PropertiesComponentImpl) PropertiesComponent.getInstance();
private final String type = "PROPERTY";
private final String key;
private final String value;
public PropertyRecord(String key) {
this.key = key;
this.value = PROPS.getValue(key);
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
@Override
public void reset() throws Exception {
PROPS.unsetValue(key);
}
@Override
public String toString() {
return type + ": " + key + " = " + (null == value ? "" : value);
}
}

View File

@ -1,29 +0,0 @@
package io.zhile.research.intellij.ier.common;
public class RecordItem {
private final RecordType type;
private final String key;
private final String value;
public RecordItem(RecordType type, String key, String value) {
this.type = type;
this.key = key;
this.value = value;
}
public RecordItem(RecordType type, String key) {
this(type, key, null);
}
public RecordType getType() {
return type;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}

View File

@ -1,17 +0,0 @@
package io.zhile.research.intellij.ier.common;
public enum RecordType {
FILE("FILE"),
PREFERENCE("PREFERENCE"),
PROPERTY("PROPERTY");
private final String value;
RecordType(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}

View File

@ -3,21 +3,18 @@ package io.zhile.research.intellij.ier.common;
import com.intellij.ide.Prefs;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.ide.util.PropertiesComponent;
import com.intellij.ide.util.PropertiesComponentImpl;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.io.FileUtil;
import io.zhile.research.intellij.ier.helper.Constants;
import io.zhile.research.intellij.ier.helper.NotificationHelper;
import io.zhile.research.intellij.ier.helper.Reflection;
import org.jdom.Attribute;
import org.jdom.Element;
import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
@ -31,92 +28,73 @@ public class Resetter {
private static final String EVAL_KEY = "evlsprt";
private static final String AUTO_RESET_KEY = Constants.PLUGIN_PREFS_PREFIX + ".auto_reset." + Constants.IDE_NAME_LOWER + "." + Constants.IDE_HASH;
private static final PropertiesComponentImpl PROPS = (PropertiesComponentImpl) PropertiesComponent.getInstance();
public static List<RecordItem> getEvalRecords() {
List<RecordItem> list = new ArrayList<>();
public static List<EvalRecord> getEvalRecords() {
List<EvalRecord> list = new ArrayList<>();
File evalDir = getEvalDir();
if (evalDir.exists()) {
for (File file : evalDir.listFiles()) {
list.add(new RecordItem(RecordType.FILE, file.getAbsolutePath()));
File[] files = evalDir.listFiles((dir, name) -> name.endsWith(".key"));
if (files == null) {
NotificationHelper.showError(null, "List eval license file failed!");
} else {
Arrays.stream(files).forEach(file -> list.add(new LicenseFileRecord(file)));
}
}
Element state = PROPS.getState();
Element state = PropertyRecord.PROPS.getState();
if (state != null) {
Attribute attrName, attrValue;
String key, value;
for (Element element : state.getChildren()) {
state.getChildren().stream().filter(element -> {
if (!element.getName().equals("property")) {
continue;
return false;
}
attrName = element.getAttribute("name");
attrValue = element.getAttribute("value");
if (attrName == null || attrValue == null) {
continue;
if (element.getAttribute("name") == null || element.getAttribute("value") == null) {
return false;
}
key = attrName.getValue();
value = attrValue.getValue();
if (key.startsWith(EVAL_KEY)) {
list.add(new RecordItem(RecordType.PROPERTY, key, value));
}
}
return element.getAttribute("name").getValue().startsWith(EVAL_KEY);
}).forEach(element -> list.add(new PropertyRecord(element.getAttribute("name").getValue())));
}
RecordItem[] prefsValue = new RecordItem[]{
new RecordItem(RecordType.PREFERENCE, OLD_MACHINE_ID_KEY, Preferences.userRoot().get(OLD_MACHINE_ID_KEY, null)),
new RecordItem(RecordType.PREFERENCE, NEW_MACHINE_ID_KEY, Prefs.get(NEW_MACHINE_ID_KEY, null)),
new RecordItem(RecordType.PREFERENCE, DEVICE_ID_KEY, Prefs.get(DEVICE_ID_KEY, null)),
PreferenceRecord[] prefsValue = new PreferenceRecord[]{
new PreferenceRecord(OLD_MACHINE_ID_KEY, true),
new PreferenceRecord(NEW_MACHINE_ID_KEY),
new PreferenceRecord(DEVICE_ID_KEY),
};
for (RecordItem item : prefsValue) {
if (null == item.getValue()) {
continue;
}
list.add(item);
}
Arrays.stream(prefsValue).filter(record -> record.getValue() != null).forEach(list::add);
try {
List<String> prefsList = new ArrayList<>();
getAllPrefsKeys(Preferences.userRoot().node(IDE_EVAL_PREFIX), prefsList);
Method methodGetProductCode = Reflection.getMethod(IdeaPluginDescriptor.class, "getProductCode");
Method methodGetReleaseVersion = Reflection.getMethod(IdeaPluginDescriptor.class, "getReleaseVersion");
if (null != methodGetProductCode && null != methodGetReleaseVersion) {
if (null != methodGetProductCode) {
for (IdeaPluginDescriptor descriptor : PluginManager.getPlugins()) {
String productCode = (String) methodGetProductCode.invoke(descriptor);
if (null == productCode || productCode.isEmpty()) {
continue;
}
int releaseVersion = (int) methodGetReleaseVersion.invoke(descriptor);
getAllPrefsKeys(Preferences.userRoot().node(DEFAULT_VENDOR + "/" + productCode.toLowerCase() + "/" + releaseVersion), prefsList);
getAllPrefsKeys(Preferences.userRoot().node(DEFAULT_VENDOR + "/" + productCode.toLowerCase()), prefsList);
}
}
for (String key : prefsList) {
if (!key.contains(EVAL_KEY)) {
continue;
}
prefsList.stream().filter(key -> key.contains(EVAL_KEY)).forEach(key -> {
if (key.startsWith("/")) {
key = key.substring(1).replace('/', '.');
}
list.add(new RecordItem(RecordType.PREFERENCE, key, Prefs.get(key, null)));
}
list.add(new PreferenceRecord(key));
});
} catch (Exception e) {
NotificationHelper.showError(null, "List eval preferences failed!");
}
if (SystemInfo.isWindows) {
String[] names = new String[]{"PermanentUserId", "PermanentDeviceId"};
for (String name : names) {
for (String name : new String[]{"PermanentUserId", "PermanentDeviceId"}) {
File file = getSharedFile(name);
if (null != file && file.exists()) {
list.add(new RecordItem(RecordType.FILE, file.getAbsolutePath()));
list.add(new NormalFileRecord(file));
}
}
}
@ -124,33 +102,15 @@ public class Resetter {
return list;
}
public static void reset(List<RecordItem> recordItems) {
for (RecordItem item : recordItems) {
reset(item);
}
public static void reset(List<EvalRecord> records) {
records.forEach(Resetter::reset);
}
public static void reset(RecordItem recordItem) {
RecordType type = recordItem.getType();
String key = recordItem.getKey();
switch (type) {
case FILE:
if (!FileUtil.delete(new File(key))) {
NotificationHelper.showError(null, "Remove " + type.getValue() + " failed: " + key);
}
break;
case PREFERENCE:
if (OLD_MACHINE_ID_KEY.equals(key)) {
Preferences.userRoot().remove(key);
} else {
Prefs.remove(key);
}
syncPrefs();
break;
case PROPERTY:
PROPS.unsetValue(key);
break;
public static void reset(EvalRecord record) {
try {
record.reset();
} catch (Exception e) {
NotificationHelper.showError(null, e.getMessage());
}
}

View File

@ -12,6 +12,7 @@ import io.zhile.research.intellij.ier.helper.Constants;
import io.zhile.research.intellij.ier.helper.DateTime;
import io.zhile.research.intellij.ier.helper.NotificationHelper;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;
@ -77,13 +78,11 @@ public class ResetTimer {
if (projects.length == 0) {
notification.notify(null);
} else {
for (Project project : projects) {
notification.notify(project);
}
Arrays.stream(projects).forEach(notification::notify);
}
} while (false);
new Timer().schedule(new ResetTimerTask(lastResetTime, resetAction), 3600000); // 60 min
new Timer().schedule(new ResetTimerTask(lastResetTime, resetAction), 600000); // 10 min
}
}
}

View File

@ -5,11 +5,11 @@ import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTime {
public static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static final DateFormat DF_DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static String getStringFromTimestamp(long timestamp) {
Date date = new Date(timestamp);
return df.format(date);
return DF_DATETIME.format(date);
}
}

View File

@ -4,7 +4,7 @@ import com.intellij.icons.AllIcons;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import io.zhile.research.intellij.ier.common.RecordItem;
import io.zhile.research.intellij.ier.common.EvalRecord;
import io.zhile.research.intellij.ier.common.Resetter;
import io.zhile.research.intellij.ier.component.ResetTimer;
import io.zhile.research.intellij.ier.helper.Constants;
@ -79,33 +79,24 @@ public class MainForm {
private void reloadRecordItems() {
listModel.clear();
List<RecordItem> recordItemList = Resetter.getEvalRecords();
for (RecordItem item : recordItemList) {
listModel.addElement(item.getType().getValue() + ": \t" + item.getKey() + (null == item.getValue() ? "" : " = \t" + item.getValue()));
}
List<EvalRecord> recordItemList = Resetter.getEvalRecords();
recordItemList.forEach(record -> listModel.addElement(record.toString()));
}
private void resetEvalItems() {
if (Messages.YES != Messages.showYesNoDialog("Your IDE will restart after reset!\nAre your sure to reset?", Constants.PLUGIN_NAME, AllIcons.General.Reset)) {
return;
}
Resetter.reset(Resetter.getEvalRecords());
ResetTimer.resetLastResetTime();
listModel.clear();
if (Messages.YES == Messages.showYesNoDialog("Reset successfully!\nWould like to restart your IDE?", Constants.PLUGIN_NAME, AllIcons.General.Reset)) {
if (null != dialogWrapper) {
dialogWrapper.close(0);
}
ApplicationManager.getApplication().invokeLater(new Runnable() {
@Override
public void run() {
ApplicationManager.getApplication().restart();
}
});
return;
if (null != dialogWrapper) {
dialogWrapper.close(0);
}
reloadRecordItems();
ApplicationManager.getApplication().invokeLater(() -> ApplicationManager.getApplication().restart());
}
private static void boldFont(Component component) {