Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit c1507d1

Browse files
committed
Added presets and enhanced requirement classes. Finished API requirements generator.
1 parent 732328d commit c1507d1

File tree

2 files changed

+78
-95
lines changed

2 files changed

+78
-95
lines changed

project/APIUtility.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class APIUtility(logger: ManagedLogger) {
3939
val filesWithRequirements =
4040
for (sourceFilesOfAKind <- sourceFiles) yield {
4141
for (sourceFile <- sourceFilesOfAKind) yield {
42-
val content = Source.fromFile(sourceFile).mkString
42+
val file = Source.fromFile(sourceFile)
43+
val content = file.mkString
44+
file.close()
4345

4446
// This is (especially the group ids) highly dependent of the regex string and the IsRequirement-Annotation
4547
requirementRegex.findFirstMatchIn(content) match {

project/RequirementsFile.scala

Lines changed: 75 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
import java.io.{File, PrintWriter}
22

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+
*/
310
class RequirementsFile(requirementsDirectory: File, requirementType: String, files: Seq[AnnotatedRequirement]) {
411

12+
/**
13+
* Creates the requirement file and saves it into the api.
14+
*/
515
def createFile(): Unit = {
616
val fileContent = generateContent()
717

8-
val writer = new PrintWriter(new File(requirementsDirectory, s"${requirementType}Gen.java"))
18+
val writer = new PrintWriter(new File(requirementsDirectory, s"$requirementType.java"))
919
writer.print(fileContent)
1020
writer.close()
1121
}
1222

1323
private def generateContent(): String =
1424
s"""package org.codeoverflow.chatoverflow.api.plugin.configuration;
25+
|
26+
|// THIS FILE IS GENERATED WHILE COMPILING. DO NOT CHANGE ANYTHING HERE!
1527
|
1628
|$generateImportStatements
1729
|
30+
|// THIS FILE IS GENERATED WHILE COMPILING. DO NOT CHANGE ANYTHING HERE!
31+
|
1832
|/**
1933
| * Select a $requirementType Requirement to get access to the desired platform or values.
2034
| */
@@ -27,103 +41,70 @@ class RequirementsFile(requirementsDirectory: File, requirementType: String, fil
2741
| }
2842
|
2943
|$generateRequireMethods
30-
|
3144
|}
3245
|""".stripMargin
3346

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", "")
4271

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+
}
127108
}
128109

129110
object RequirementsFile {

0 commit comments

Comments
 (0)