浏览代码

Migrate action aliases to real aliases.

Change-Id: I2d5f263df1c1e7c36e3c8a392aa2d35c8730a36a
Reviewed-on: http://gerrit.dmdirc.com/3547
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
pull/1/head
Chris Smith 10 年前
父节点
当前提交
a42a254096

+ 12
- 0
src/com/dmdirc/Main.java 查看文件

@@ -32,6 +32,7 @@ import com.dmdirc.events.ClientOpenedEvent;
32 32
 import com.dmdirc.events.FeedbackNagEvent;
33 33
 import com.dmdirc.events.FirstRunEvent;
34 34
 import com.dmdirc.interfaces.CommandController.CommandDetails;
35
+import com.dmdirc.interfaces.Migrator;
35 36
 import com.dmdirc.interfaces.SystemLifecycleComponent;
36 37
 import com.dmdirc.interfaces.config.IdentityController;
37 38
 import com.dmdirc.interfaces.ui.UIController;
@@ -86,6 +87,8 @@ public class Main {
86 87
     private final ColourActionComparison colourActionComparison;
87 88
     /** The set of known lifecycle components. */
88 89
     private final Set<SystemLifecycleComponent> lifecycleComponents;
90
+    /** The set of migrators to execute on startup. */
91
+    private final Set<Migrator> migrators;
89 92
     /** The event bus to dispatch events on. */
90 93
     private final EventBus eventBus;
91 94
     /** The commands to load into the command manager. */
@@ -104,6 +107,7 @@ public class Main {
104 107
      * @param globalWindowManager    Global window manager to use.
105 108
      * @param colourActionComparison The colour-based action comparisons.
106 109
      * @param lifecycleComponents    The set of known lifecycle components.
110
+     * @param migrators              The set of migrators to execute on startup.
107 111
      * @param eventBus               The event bus to dispatch events on.
108 112
      * @param commands               The commands to be loaded into the command manager.
109 113
      */
@@ -119,6 +123,7 @@ public class Main {
119 123
             final GlobalWindowManager globalWindowManager,
120 124
             final ColourActionComparison colourActionComparison,
121 125
             final Set<SystemLifecycleComponent> lifecycleComponents,
126
+            final Set<Migrator> migrators,
122 127
             final EventBus eventBus,
123 128
             final Set<CommandDetails> commands) {
124 129
         this.identityManager = identityManager;
@@ -131,6 +136,7 @@ public class Main {
131 136
         this.globalWindowManager = globalWindowManager;
132 137
         this.colourActionComparison = colourActionComparison;
133 138
         this.lifecycleComponents = lifecycleComponents;
139
+        this.migrators = migrators;
134 140
         this.eventBus = eventBus;
135 141
         this.commands = commands;
136 142
     }
@@ -161,6 +167,12 @@ public class Main {
161 167
      * Initialises the client.
162 168
      */
163 169
     public void init() {
170
+        for (Migrator migrator : migrators) {
171
+            if (migrator.needsMigration()) {
172
+                migrator.migrate();
173
+            }
174
+        }
175
+
164 176
         for (CommandDetails command : commands) {
165 177
             commandManager.registerCommand(command.getCommand(), command.getInfo());
166 178
         }

+ 145
- 0
src/com/dmdirc/commandparser/aliases/ActionAliasMigrator.java 查看文件

@@ -0,0 +1,145 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.commandparser.aliases;
24
+
25
+import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
26
+import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
27
+import com.dmdirc.interfaces.Migrator;
28
+import com.dmdirc.util.io.ConfigFile;
29
+import com.dmdirc.util.io.InvalidConfigFileException;
30
+
31
+import com.google.common.base.Joiner;
32
+
33
+import java.io.File;
34
+import java.io.IOException;
35
+import java.util.Map;
36
+
37
+import javax.inject.Inject;
38
+import javax.inject.Singleton;
39
+
40
+/**
41
+ * Migrates "alias" actions into proper aliases.
42
+ */
43
+@Singleton
44
+public class ActionAliasMigrator implements Migrator {
45
+
46
+    private final File directory;
47
+    private final AliasFactory aliasFactory;
48
+    private final AliasManager aliasManager;
49
+
50
+    /**
51
+     * Creates a new alias migrator.
52
+     *
53
+     * @param directory    The base directory to read alias actions from.
54
+     * @param aliasFactory The factory to use to create new aliases.
55
+     * @param aliasManager The manager to add aliases to.
56
+     */
57
+    @Inject
58
+    public ActionAliasMigrator(
59
+            @Directory(DirectoryType.ACTIONS) final String directory,
60
+            final AliasFactory aliasFactory,
61
+            final AliasManager aliasManager) {
62
+        this.directory = new File(directory, "aliases");
63
+        this.aliasFactory = aliasFactory;
64
+        this.aliasManager = aliasManager;
65
+    }
66
+
67
+    @Override
68
+    public boolean needsMigration() {
69
+        return directory.exists();
70
+    }
71
+
72
+    @Override
73
+    public void migrate() {
74
+        for (File child : directory.listFiles()) {
75
+            if (migrate(child)) {
76
+                child.delete();
77
+            }
78
+        }
79
+        directory.delete();
80
+    }
81
+
82
+    /**
83
+     * Migrates the specified file.
84
+     *
85
+     * @param file The file to be migrated.
86
+     *
87
+     * @return True if the file was migrated successfully, false otherwise.
88
+     */
89
+    private boolean migrate(final File file) {
90
+        try {
91
+            final ConfigFile configFile = new ConfigFile(file);
92
+            configFile.read();
93
+
94
+            final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));
95
+            final String name = getTrigger(configFile);
96
+            final int minArguments = getArguments(configFile);
97
+
98
+            aliasManager.addAlias(aliasFactory.createAlias(name, minArguments, response));
99
+            return true;
100
+        } catch (IOException | InvalidConfigFileException | NumberFormatException ex) {
101
+            return false;
102
+        }
103
+    }
104
+
105
+    /**
106
+     * Finds and returns the trigger for an alias.
107
+     *
108
+     * @param configFile The config file to read triggers from.
109
+     *
110
+     * @return The trigger for the alias.
111
+     *
112
+     * @throws InvalidConfigFileException If the config file is missing a trigger
113
+     */
114
+    private String getTrigger(final ConfigFile configFile) throws InvalidConfigFileException {
115
+        for (Map<String, String> section : configFile.getKeyDomains().values()) {
116
+            if (section.containsKey("comparison")
117
+                    && section.containsKey("target")
118
+                    && "STRING_EQUALS".equals(section.get("comparison"))) {
119
+                return section.get("target");
120
+            }
121
+        }
122
+        throw new InvalidConfigFileException("No trigger found");
123
+    }
124
+
125
+    /**
126
+     * Finds and returns the minimum number of arguments an alias requires.
127
+     *
128
+     * @param configFile The config file to read minimum arguments from.
129
+     *
130
+     * @return The minimum number of arguments if present, or <code>0</code> otherwise.
131
+     *
132
+     * @throws NumberFormatException If the config file contains an invalid number of args.
133
+     */
134
+    private int getArguments(final ConfigFile configFile) throws NumberFormatException {
135
+        for (Map<String, String> section : configFile.getKeyDomains().values()) {
136
+            if (section.containsKey("comparison")
137
+                    && section.containsKey("target")
138
+                    && "INT_GREATER".equals(section.get("comparison"))) {
139
+                return 1 + Integer.valueOf(section.get("target"));
140
+            }
141
+        }
142
+        return 0;
143
+    }
144
+
145
+}

+ 6
- 0
src/com/dmdirc/commandparser/aliases/AliasesModule.java 查看文件

@@ -22,6 +22,7 @@
22 22
 
23 23
 package com.dmdirc.commandparser.aliases;
24 24
 
25
+import com.dmdirc.interfaces.Migrator;
25 26
 import com.dmdirc.interfaces.SystemLifecycleComponent;
26 27
 
27 28
 import javax.inject.Singleton;
@@ -46,4 +47,9 @@ public class AliasesModule {
46 47
         return manager;
47 48
     }
48 49
 
50
+    @Provides(type = Provides.Type.SET)
51
+    public Migrator getMigrator(final ActionAliasMigrator migrator) {
52
+        return migrator;
53
+    }
54
+
49 55
 }

+ 42
- 0
src/com/dmdirc/interfaces/Migrator.java 查看文件

@@ -0,0 +1,42 @@
1
+/*
2
+ * Copyright (c) 2006-2014 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.interfaces;
24
+
25
+/**
26
+ * A class that can perform one-off migration after a client upgrade.
27
+ */
28
+public interface Migrator {
29
+
30
+    /**
31
+     * Determines whether migration is needed or not.
32
+     *
33
+     * @return True if migration is required, false otherwise.
34
+     */
35
+    boolean needsMigration();
36
+
37
+    /**
38
+     * Performs the actual migration.
39
+     */
40
+    void migrate();
41
+
42
+}

正在加载...
取消
保存