1
1
import java .io .{File , PrintWriter }
2
2
3
+ /**
4
+ * Represents a requirements class which is used by plugin devs to require inputs / outputs and parameters.
5
+ *
6
+ * @param requirementsDirectory the requirements directory in the api project
7
+ * @param requirementType the type of the requirements class (input / output / parameter)
8
+ * @param files all found annotated source files which can be required
9
+ */
3
10
class RequirementsFile (requirementsDirectory : File , requirementType : String , files : Seq [AnnotatedRequirement ]) {
4
11
12
+ /**
13
+ * Creates the requirement file and saves it into the api.
14
+ */
5
15
def createFile (): Unit = {
6
16
val fileContent = generateContent()
7
17
8
- val writer = new PrintWriter (new File (requirementsDirectory, s " ${ requirementType} Gen .java" ))
18
+ val writer = new PrintWriter (new File (requirementsDirectory, s " $requirementType.java " ))
9
19
writer.print(fileContent)
10
20
writer.close()
11
21
}
12
22
13
23
private def generateContent (): String =
14
24
s """ package org.codeoverflow.chatoverflow.api.plugin.configuration;
25
+ |
26
+ |// THIS FILE IS GENERATED WHILE COMPILING. DO NOT CHANGE ANYTHING HERE!
15
27
|
16
28
| $generateImportStatements
17
29
|
30
+ |// THIS FILE IS GENERATED WHILE COMPILING. DO NOT CHANGE ANYTHING HERE!
31
+ |
18
32
|/**
19
33
| * Select a $requirementType Requirement to get access to the desired platform or values.
20
34
| */
@@ -27,103 +41,70 @@ class RequirementsFile(requirementsDirectory: File, requirementType: String, fil
27
41
| }
28
42
|
29
43
| $generateRequireMethods
30
- |
31
44
|}
32
45
| """ .stripMargin
33
46
34
- private def generateImportStatements : String =
35
- """ import org.codeoverflow.chatoverflow.api.io.input.FileInput;
36
- |import org.codeoverflow.chatoverflow.api.io.input.SampleInput;
37
- |import org.codeoverflow.chatoverflow.api.io.input.SerialInput;
38
- |import org.codeoverflow.chatoverflow.api.io.input.chat.DiscordChatInput;
39
- |import org.codeoverflow.chatoverflow.api.io.input.chat.MockUpChatInput;
40
- |import org.codeoverflow.chatoverflow.api.io.input.event.TipeeestreamEventInput;
41
- |import org.codeoverflow.chatoverflow.api.io.input.chat.TwitchChatInput;"""
47
+ private def generateImportStatements : String = {
48
+ val startingPoint = " main.java."
49
+ val filesToImport = files.map(annotateReq => annotateReq.file.getAbsolutePath.replace(File .separatorChar, '.' ))
50
+ val classesToImport = filesToImport.map(filePath => filePath.substring(filePath.lastIndexOf(startingPoint) + startingPoint.length))
51
+
52
+ classesToImport.map(_.replace(" .java" , " " )).map(cls => s " import $cls; " ).mkString(" \n |" )
53
+ }
54
+
55
+ private def generateRequireMethods : String = {
56
+ files.map(file => generateRequireMethod(file, shortVersion = false )
57
+ ++ generateRequireMethod(file, shortVersion = true )).mkString
58
+ }
59
+
60
+ private def generateRequireMethod (requirement : AnnotatedRequirement , shortVersion : Boolean ): String = {
61
+
62
+ val className = requirement.file.getAbsolutePath.substring(
63
+ requirement.file.getAbsolutePath.lastIndexOf(File .separator) + 1 ).replace(" .java" , " " )
64
+
65
+ val requiresValue = if (requirement.requires != " " ) requirement.requires else className
66
+
67
+ val generatedName = className
68
+ .replace(" Input" , " " )
69
+ .replace(" Output" , " " )
70
+ .replace(" Parameter" , " " )
42
71
43
- private def generateRequireMethods : String =
44
- """ /**
45
- | * Requires a twitch chat login that has to be created by the framework user.
46
- | *
47
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
48
- | * @param displayName Is displayed to the framework user to tell him what to enter
49
- | * @param isOptional true if this requirement is optional, false if mandatory
50
- | * @return the requirement object
51
- | */
52
- | public Requirement<TwitchChatInput> twitchChat(String uniqueRequirementId, String displayName, boolean isOptional) {
53
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, TwitchChatInput.class);
54
- | }
55
- |
56
- | /**
57
- | * Requires a connected mockup chat that has to be submitted by the user.
58
- | *
59
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
60
- | * @param displayName Is displayed to the framework user and to tell him what to enter
61
- | * @param isOptional true if this requirement is optional, false if mandatory
62
- | * @return the requirement object
63
- | */
64
- | public Requirement<MockUpChatInput> mockupChat(String uniqueRequirementId, String displayName, boolean isOptional) {
65
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, MockUpChatInput.class);
66
- | }
67
- |
68
- | /**
69
- | * Demonstration requirement for the sample input, to get a idea how requirements work
70
- | *
71
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
72
- | * @param displayName Is displayed to the framework user and to tell him what to enter
73
- | * @param isOptional true if this requirement is optional, false if mandatory
74
- | * @return the requirement object
75
- | */
76
- | public Requirement<SampleInput> sampleInput(String uniqueRequirementId, String displayName, boolean isOptional) {
77
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, SampleInput.class);
78
- | }
79
- |
80
- | /**
81
- | * Requires a discord chat bot that has to be created by the framework user.
82
- | *
83
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
84
- | * @param displayName Is displayed to the framework user to tell him what to enter
85
- | * @param isOptional true if this requirement is optional, false if mandatory
86
- | * @return the requirement object
87
- | */
88
- | public Requirement<DiscordChatInput> discordChat(String uniqueRequirementId, String displayName, boolean isOptional) {
89
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, DiscordChatInput.class);
90
- | }
91
- |
92
- | /**
93
- | * Requires a connection with a device connected to a serial port (an Arduino for example)
94
- | *
95
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
96
- | * @param displayName Is displayed to the framework user and to tell him what to enter
97
- | * @param isOptional true if this requirement is optional, false if mandatory
98
- | * @return the requirement object
99
- | */
100
- | public Requirement<SerialInput> serial(String uniqueRequirementId, String displayName, boolean isOptional) {
101
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, SerialInput.class);
102
- | }
103
- |
104
- | /**
105
- | * Requires a login for the TipeeeStream api that has to be created by the framework user.
106
- | *
107
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
108
- | * @param displayName Is displayed to the framework user and to tell him what to enter
109
- | * @param isOptional true if this requirement is optional, false if mandatory
110
- | * @return the requirement object
111
- | */
112
- | public Requirement<TipeeestreamEventInput> tipeeeStream(String uniqueRequirementId, String displayName, boolean isOptional) {
113
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, TipeeestreamEventInput.class);
114
- | }
115
- |
116
- | /**
117
- | * @param uniqueRequirementId any unique id by which your plugin can identify the requirement
118
- | * @param displayName Is displayed to the framework user and to tell him what to enter
119
- | * @param isOptional true if this requirement is optional, false if mandatory
120
- | * @return the requirement object
121
- | */
122
- | public Requirement<FileInput> file(String uniqueRequirementId, String displayName, boolean isOptional) {
123
- | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, FileInput.class);
124
- | }
125
- |
126
- | // Add more inputs here"""
72
+ val methodNameValue = if (requirement.methodName != " " ) {
73
+ requirement.methodName.replace(" " , " " )
74
+ } else {
75
+ generatedName.head.toLower + generatedName.tail
76
+ }
77
+
78
+ if (! shortVersion) {
79
+ s """ /**
80
+ | * Requires a $requiresValue which has to be specified by the user.
81
+ | *
82
+ | * @param uniqueRequirementId a plugin unique identifier which is stored for your plugin
83
+ | * @param displayName a string to display to the user while setting your requirement
84
+ | * @param isOptional true if this requirement is optional, false if mandatory
85
+ | * @return the requirement object. Use the get() method only at runtime!
86
+ | */
87
+ | public Requirement< $className> $methodNameValue(String uniqueRequirementId, String displayName, boolean isOptional) {
88
+ | return requirements.requireInput(uniqueRequirementId, displayName, isOptional, $className.class);
89
+ | }
90
+ |
91
+ | """
92
+ } else {
93
+ val displayName = " \" " + s " ${generatedName.map(c => if (c.isUpper) " " + c else c).mkString.trim}" + " \" "
94
+
95
+ s """ /**
96
+ | * Requires a $requiresValue which has to be specified by the user.
97
+ | *
98
+ | * @param uniqueRequirementId a plugin unique identifier which is stored for your plugin
99
+ | * @return the requirement object. Use the get() method only at runtime!
100
+ | */
101
+ | public Requirement< $className> $methodNameValue(String uniqueRequirementId) {
102
+ | return requirements.requireInput(uniqueRequirementId, $displayName, false, $className.class);
103
+ | }
104
+ |
105
+ | """
106
+ }
107
+ }
127
108
}
128
109
129
110
object RequirementsFile {
0 commit comments