@@ -19,22 +19,22 @@ import java.nio.file.NoSuchFileException
1919import kotlin.io.path.bufferedReader
2020
2121/* *
22- * Manages default commit types and scopes .
22+ * Manages bundled and custom commit message tokens .
2323 *
2424 * @author Edoardo Luppi
2525 */
2626@Suppress(" LightServiceMigrationCode" )
27- internal class CCDefaultTokensService (private val project : Project ) {
27+ internal class CCTokensService (private val project : Project ) {
2828 private companion object {
29- private val logger = logger<CCDefaultTokensService >()
29+ private val logger = logger<CCTokensService >()
3030 }
3131
3232 private val configService = project.service<CCConfigService >()
3333
3434 /* *
3535 * JSON Schema used to validate the default commit types and scopes JSON file.
3636 */
37- private val defaultsSchema : Schema by lazy {
37+ private val tokensSchema : Schema by lazy {
3838 val bufferedReader = getResourceAsStream(" /defaults/${CC .File .Schema } " ).bufferedReader()
3939 val schemaJson = bufferedReader.use {
4040 JsonParser (bufferedReader).parse()
@@ -44,53 +44,56 @@ internal class CCDefaultTokensService(private val project: Project) {
4444 }
4545
4646 /* *
47- * Built-in default commit types and scopes.
48- *
49- * @throws SchemaValidationException When the JSON object does not respect the schema
47+ * Bundled commit message tokens.
5048 */
51- private val builtInDefaultTokens : JsonDefaults by lazy {
49+ private val bundledTokensModel : TokensModel by lazy {
5250 val jsonStr = getResourceAsStream(" /defaults/${CC .File .Defaults } " ).bufferedReader().use(Reader ::readText)
5351 parseJsonStr(jsonStr)
5452 }
5553
56- fun getDefaultsFromCustomFile (filePath : String? = null): JsonDefaults {
57- val path = filePath ? : findDefaultFilePathFromProjectRoot()
58- return path?.let (::readDefaultsFromFile) ? : builtInDefaultTokens
54+ /* *
55+ * Returns commit message tokens defined in a custom `conventionalcommit.json` file,
56+ * or falls back to the bundled defaults if no custom file is specified.
57+ *
58+ * @see getBundledTokens
59+ */
60+ fun getTokens (): TokensModel {
61+ val path = configService.customFilePath ? : findTokensFileInProjectRoot()
62+ return path?.let (::readTokensFromFile) ? : getBundledTokens()
5963 }
6064
6165 /* *
62- * Returns the built- in commit types and scopes .
66+ * Returns commit message tokens defined in the bundled `conventionalcommit.json` file .
6367 */
64- fun getBuiltInDefaults (): JsonDefaults =
65- builtInDefaultTokens
68+ fun getBundledTokens (): TokensModel =
69+ bundledTokensModel
6670
6771 /* *
6872 * Validates a file via the inputted absolute path.
6973 *
7074 * @throws SchemaValidationException When the JSON object does not respect the schema
7175 */
72- fun validateDefaultsFile (filePath : String ) {
76+ fun validateTokensFile (filePath : String ) {
7377 val path = FileSystems .getDefault().getPath(filePath)
7478
7579 if (Files .notExists(path)) {
7680 throw NoSuchFileException (filePath)
7781 }
7882
7983 val jsonStr = path.bufferedReader().use(Reader ::readText)
80- defaultsSchema .validateJson(jsonStr)
84+ tokensSchema .validateJson(jsonStr)
8185 }
8286
8387 /* *
8488 * Returns the user-defined co-authors.
8589 */
8690 fun getCoAuthors (): Collection <String > {
8791 val customCoAuthorsFilePath = configService.customCoAuthorsFilePath
88- val fileSystem = FileSystems .getDefault()
8992 val filePath = if (customCoAuthorsFilePath == null ) {
9093 val projectBasePath = project.basePath ? : return emptySet()
91- fileSystem .getPath(projectBasePath, CC .File .CoAuthors )
94+ FileSystems .getDefault() .getPath(projectBasePath, CC .File .CoAuthors )
9295 } else {
93- fileSystem .getPath(customCoAuthorsFilePath)
96+ FileSystems .getDefault() .getPath(customCoAuthorsFilePath)
9497 }
9598
9699 try {
@@ -132,10 +135,9 @@ internal class CCDefaultTokensService(private val project: Project) {
132135 }
133136
134137 /* *
135- * Returns the full path of the default tokens file located
136- * in the project root directory, or `null`.
138+ * Returns the path of the tokens file in the project root directory, or `null` if there is not.
137139 */
138- private fun findDefaultFilePathFromProjectRoot (): String? {
140+ private fun findTokensFileInProjectRoot (): String? {
139141 val projectBasePath = project.basePath ? : return null
140142 return LocalFileSystem .getInstance().refreshAndFindFileByPath(projectBasePath)
141143 ?.findChild(CC .File .Defaults )
@@ -147,7 +149,7 @@ internal class CCDefaultTokensService(private val project: Project) {
147149 *
148150 * @throws SchemaValidationException When the JSON object does not respect the schema
149151 */
150- private fun readDefaultsFromFile (filePath : String ): JsonDefaults {
152+ private fun readTokensFromFile (filePath : String ): TokensModel {
151153 val path = FileSystems .getDefault().getPath(filePath)
152154 val jsonStr = path.bufferedReader().use(Reader ::readText)
153155 return parseJsonStr(jsonStr)
@@ -156,10 +158,10 @@ internal class CCDefaultTokensService(private val project: Project) {
156158 /* *
157159 * @throws SchemaValidationException When the JSON object does not respect the schema
158160 */
159- private fun parseJsonStr (jsonStr : String ): JsonDefaults {
161+ private fun parseJsonStr (jsonStr : String ): TokensModel {
160162 // If the inputted JSON isn't valid, an exception is thrown.
161163 // The exception contains the validation errors which can be used to notify the user
162- defaultsSchema .validateJson(jsonStr)
164+ tokensSchema .validateJson(jsonStr)
163165
164166 val rootJsonObject = JSONObject (jsonStr)
165167 val commonScopes = buildScopes(rootJsonObject.optJSONObject(" commonScopes" ) ? : JSONObject ())
@@ -171,63 +173,63 @@ internal class CCDefaultTokensService(private val project: Project) {
171173 else -> error(" Should never get here" )
172174 }
173175
174- return JsonDefaults (types = types, footerTypes = footerTypes)
176+ return TokensModel (types = types, footerTypes = footerTypes)
175177 }
176178
177- private fun buildTypes (jsonObject : JSONObject , commonScopes : List <JsonCommitScope >): Map <String , JsonCommitType > =
179+ private fun buildTypes (jsonObject : JSONObject , commonScopes : List <CommitScopeModel >): Map <String , CommitTypeModel > =
178180 jsonObject.keySet().associateWith {
179181 val descriptor = jsonObject.getJSONObject(it)
180182 val description = descriptor.optString(" description" , " " )
181183 val scopes = buildScopes(descriptor.optJSONObject(" scopes" ) ? : JSONObject ())
182- JsonCommitType (
184+ CommitTypeModel (
183185 name = it,
184186 description = description,
185187 scopes = scopes + commonScopes,
186188 )
187189 }
188190
189- private fun buildScopes (jsonObject : JSONObject ): List <JsonCommitScope > =
191+ private fun buildScopes (jsonObject : JSONObject ): List <CommitScopeModel > =
190192 jsonObject.keySet().map {
191193 val descriptor = jsonObject.getJSONObject(it)
192194 val description = descriptor.optString(" description" , " " )
193- JsonCommitScope (
195+ CommitScopeModel (
194196 name = it,
195197 description = description,
196198 )
197199 }
198200
199- private fun buildFooterTypes (jsonObject : JSONObject ): Map <String , JsonCommitFooterType > =
201+ private fun buildFooterTypes (jsonObject : JSONObject ): Map <String , CommitFooterTypeModel > =
200202 jsonObject.keySet().associateWith {
201203 val descriptor = jsonObject.getJSONObject(it)
202204 val description = descriptor.optString(" description" , " " )
203205 val values = buildFooterValues(descriptor.optJSONObject(" values" ) ? : JSONObject ())
204- JsonCommitFooterType (
206+ CommitFooterTypeModel (
205207 name = it,
206208 description = description,
207209 values = values,
208210 )
209211 }
210212
211- private fun buildFooterValues (jsonObject : JSONObject ): List <JsonCommitFooterValue > =
213+ private fun buildFooterValues (jsonObject : JSONObject ): List <CommitFooterValueModel > =
212214 jsonObject.keySet().map {
213215 val descriptor = jsonObject.getJSONObject(it)
214216 val description = descriptor.optString(" description" , " " )
215- JsonCommitFooterValue (
217+ CommitFooterValueModel (
216218 name = it,
217219 description = description,
218220 )
219221 }
220222
221223 // TODO(Edoardo): will have to remove it as point, as keeping footer values
222224 // as an array does not make sense anymore
223- private fun buildFooterTypesArray (jsonArray : JSONArray ): Map <String , JsonCommitFooterType > {
224- val map = LinkedHashMap <String , JsonCommitFooterType >()
225+ private fun buildFooterTypesArray (jsonArray : JSONArray ): Map <String , CommitFooterTypeModel > {
226+ val map = LinkedHashMap <String , CommitFooterTypeModel >()
225227
226228 for (i in 0 .. < jsonArray.length()) {
227229 val descriptor = jsonArray.getJSONObject(i)
228230 val name = descriptor.optString(" name" )
229231 val description = descriptor.optString(" description" , " " )
230- map[name] = JsonCommitFooterType (
232+ map[name] = CommitFooterTypeModel (
231233 name = name,
232234 description = description,
233235 values = emptyList(),
@@ -237,29 +239,29 @@ internal class CCDefaultTokensService(private val project: Project) {
237239 return map
238240 }
239241
240- class JsonDefaults (
241- val types : Map <String , JsonCommitType >,
242- val footerTypes : Map <String , JsonCommitFooterType >,
242+ class TokensModel (
243+ val types : Map <String , CommitTypeModel >,
244+ val footerTypes : Map <String , CommitFooterTypeModel >,
243245 )
244246
245- class JsonCommitType (
247+ class CommitTypeModel (
246248 val name : String ,
247249 val description : String ,
248- val scopes : List <JsonCommitScope >,
250+ val scopes : List <CommitScopeModel >,
249251 )
250252
251- class JsonCommitScope (
253+ class CommitScopeModel (
252254 val name : String ,
253255 val description : String ,
254256 )
255257
256- class JsonCommitFooterType (
258+ class CommitFooterTypeModel (
257259 val name : String ,
258260 val description : String ,
259- val values : List <JsonCommitFooterValue >,
261+ val values : List <CommitFooterValueModel >,
260262 )
261263
262- class JsonCommitFooterValue (
264+ class CommitFooterValueModel (
263265 val name : String ,
264266 val description : String ,
265267 )
0 commit comments