Skip to content

Commit

Permalink
Feature/add plugin support (#484)
Browse files Browse the repository at this point in the history
* add plugin support

* update doc-generator

* update doc generator

* update dependencies

* format code

* update dockerfile

* fix tests
  • Loading branch information
stefanvacareanu7 authored Oct 17, 2024
1 parent 9b0f2a7 commit abbf266
Show file tree
Hide file tree
Showing 134 changed files with 1,474 additions and 2,061 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2.1

orbs:
codacy: codacy/[email protected].1
codacy: codacy/[email protected].3
codacy_plugins_test: codacy/[email protected]

workflows:
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ WORKDIR /workdir

COPY package*.json ./

RUN npm install
RUN npm install --legacy-peer-deps --omit=dev &&\
npm cache clean --force

COPY docs /docs
COPY target/universal/stage/ ./
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Run docker:
docker run -it -v $srcDir:/src <DOCKER_NAME>:<DOCKER_VERSION>
```

## Add a new plugin
In DocGenerator, listOfPlugins method, add a new plugin with all the necessary details and run npm install {plugin} and doc generator.

## Generate Docs

```sh
Expand Down
148 changes: 99 additions & 49 deletions doc-generator/src/main/scala/codacy/stylelint/DocGenerator.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package codacy.stylelint

import java.nio.charset.Charset

import better.files.{File, Resource}
import com.codacy.plugins.api._
import com.codacy.plugins.api.results.{Parameter, Pattern, Result, Tool}
Expand All @@ -12,25 +11,65 @@ import scala.sys.process.Process
object DocGenerator {

def main(args: Array[String]): Unit = {
val tmpDirectory = File.newTemporaryDirectory()

val version = File("package.json").inputStream() { file =>
Json.parse(file)("dependencies")("stylelint").as[String].stripPrefix("^")
}
val pluginsList = processPlugins()

initializePatternsFile(pluginsList)
initializeDescriptionFile(pluginsList)
copyDescriptionFiles(pluginsList)
}

def processPlugins(): List[Plugin] = {

val pluginsList = listOfPlugins()
var tempList: List[Plugin] = List()

cloneFromGitToTmpDir(tmpDirectory, version)
pluginsList.map { plugin =>
val version = File("package.json").inputStream() { file =>
Json.parse(file)("dependencies")(plugin.pluginName).as[String].stripPrefix("^")
}
val tmpDirectory = File.newTemporaryDirectory()
cloneFromGitToTmpDir(tmpDirectory, version, plugin.url)
val rulesdir = tmpDirectory + "/" + plugin.relativeRulesDir
val patterns = getListOfSubDirectories(rulesdir)
val tempPlugin =
Plugin(plugin.pluginName, plugin.relativeRulesDir, plugin.url, plugin.tree, patterns, version, tmpDirectory)

val rulesdir = tmpDirectory + "/lib/rules"
val filePathForDocs = "docs/"
tempList = tempList :+ tempPlugin
}
(tempList)
}

val patterns = getListOfSubDirectories(rulesdir)
initializePatternsFile(patterns, version, filePathForDocs)
initializeDescriptionFile(patterns, rulesdir, filePathForDocs)
copyDescriptionFiles(patterns, rulesdir, tmpDirectory, filePathForDocs, version)
case class Plugin(pluginName: String,
relativeRulesDir: String,
url: String,
tree: String,
patterns: List[String],
version: String,
tempDirectory: better.files.File)

def listOfPlugins(): List[Plugin] = {
List(
Plugin(
"stylelint",
"lib/rules",
"https://github.com/stylelint/stylelint.git",
"https://github.com/stylelint/stylelint",
null,
null,
null),
Plugin(
"stylelint-a11y",
"src/rules",
"https://github.com/YozhikM/stylelint-a11y.git",
"https://github.com/YozhikM/stylelint-a11y",
null,
null,
null))
}

def cloneFromGitToTmpDir(tmpDirectory: better.files.File, version: String): Int = {
Process(Seq("git", "clone", "https://github.com/stylelint/stylelint.git", tmpDirectory.pathAsString)).!
def cloneFromGitToTmpDir(tmpDirectory: better.files.File, version: String, url: String): Int = {
Process(Seq("git", "clone", url, tmpDirectory.pathAsString)).!
Process(Seq("git", "reset", "--hard", version), tmpDirectory.toJava).!
}

Expand All @@ -45,14 +84,20 @@ object DocGenerator {
.toList
}

def initializePatternsFile(patterns: List[String], version: String, filePathForDocs: String): File = {
def initializePatternsFile(plugins: List[Plugin]): File = {
val default = PatternsFromDefaultConfig()

val version = File("package.json").inputStream() { file =>
Json.parse(file)("dependencies")("stylelint").as[String].stripPrefix("^")
}

val patterns: List[String] = plugins.flatMap(_.patterns)

val toolpatterns: Set[Pattern.Specification] = patterns.view.map { patternid =>
addNewPattern(patternid, default.getOrElse(patternid, Parameter.Value(JsNull)))
}.to(Set)
val tool = Tool.Specification(Tool.Name("stylelint"), Option(Tool.Version(version)), toolpatterns)
File(filePathForDocs + "/patterns.json").write(Json.prettyPrint(Json.toJson(tool)))
File("docs/patterns.json").write(Json.prettyPrint(Json.toJson(tool)))
}

def addNewPattern(patternName: String, default: Parameter.Value): Pattern.Specification = {
Expand All @@ -69,18 +114,24 @@ object DocGenerator {
Json.parse(stylelintConfigStandard).as[Map[String, Parameter.Value]]
}

def initializeDescriptionFile(patterns: List[String], rulesdir: String, filePathForDocs: String): File = {
def initializeDescriptionFile(plugins: List[Plugin]): File = {

val patternsDescription: Set[Pattern.Description] = patterns.view.map { patternid =>
val patternDescription =
ParseMarkupRule.parseForDescriptions(File(rulesdir + "/" + patternid + "/README.md"))
var finalPatternsDescription: Set[Pattern.Description] = Set()
plugins.map { plugin =>
val patternsDescription: Set[Pattern.Description] = plugin.patterns.map { pattern =>
val patternDescription =
ParseMarkupRule.parseForDescriptions(
File(plugin.tempDirectory + "/" + plugin.relativeRulesDir + "/" + pattern + "/README.md"))

// looking for markdown links, e.g., [text](https://www.example.com)
val urlRegex = """\[(.+?)\]\((.+?)\)""".r
val descriptionWithoutUrl = urlRegex.replaceAllIn(patternDescription, m => m.group(1)).trim
addNewDescription(patternid, descriptionWithoutUrl)
}.to(Set)
File(filePathForDocs + "/description/description.json").write(Json.prettyPrint(Json.toJson(patternsDescription)))
// looking for markdown links, e.g., [text](https://www.example.com)
val urlRegex = """\[(.+?)\]\((.+?)\)""".r
val descriptionWithoutUrl = urlRegex.replaceAllIn(patternDescription, m => m.group(1)).trim
addNewDescription(pattern, descriptionWithoutUrl)
}.to(Set)

finalPatternsDescription ++= patternsDescription
}
File("docs/description/description.json").write(Json.prettyPrint(Json.toJson(finalPatternsDescription)))
}

def addNewDescription(patternName: String, patternDescription: String): Pattern.Description = {
Expand All @@ -89,30 +140,29 @@ object DocGenerator {
Pattern.Description(Pattern.Id(patternName), Pattern.Title(patternDescription), None, None, param)
}

def copyDescriptionFiles(folderNames: List[String],
rulesDirectory: String,
mainDirectory: File,
docsDirectory: String,
version: String): Unit = {
val descriptionDir = File(docsDirectory + "/description/")
def copyDescriptionFiles(plugins: List[Plugin]): Unit = {
val descriptionDir = File("docs/description/")
descriptionDir.createDirectories()
folderNames.foreach { patternName =>
val documentationFile = File(s"$rulesDirectory/$patternName/README.md")
val fileContent = documentationFile.contentAsString

// looking for markdown link to local resources, e.g, [`fix` option](../../../docs/user-guide/usage/options.md#fix)
// assuming local URLs start with "../" this is the pattern used at the time of this solution
val localUrlRegex = """\[(.+?)\]\((\.\./.+?)\)""".r

val contentWithReplacedUrls = localUrlRegex.replaceAllIn(fileContent, m => {
val linkText = m.group(1)
val localUrl = m.group(2)
val absoluteFilePath = documentationFile.parent / localUrl
val relativePath = mainDirectory.relativize(absoluteFilePath).toString
s"[$linkText](https://github.com/stylelint/stylelint/tree/$version/$relativePath)"
})

File(s"$descriptionDir/$patternName.md").write(contentWithReplacedUrls)
plugins.map { plugin =>
plugin.patterns.foreach { patternName =>
val documentationFile = File(s"${plugin.tempDirectory}/${plugin.relativeRulesDir}/$patternName/README.md")
val fileContent = documentationFile.contentAsString

// looking for markdown link to local resources, e.g, [`fix` option](../../../docs/user-guide/usage/options.md#fix)
// assuming local URLs start with "../" this is the pattern used at the time of this solution
val localUrlRegex = """\[(.+?)\]\((\.\./.+?)\)""".r

val contentWithReplacedUrls = localUrlRegex.replaceAllIn(fileContent, m => {
val linkText = m.group(1)
val localUrl = m.group(2)
val absoluteFilePath = documentationFile.parent / localUrl
val relativePath = plugin.tempDirectory.relativize(absoluteFilePath).toString
s"[$linkText](${plugin.tree}/${plugin.version}/$relativePath)"
})

File(s"$descriptionDir/$patternName.md").write(contentWithReplacedUrls)
}
}

}
}
4 changes: 2 additions & 2 deletions docs/description/alpha-value-notation.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Specify percentage or number notation for alpha-values.
* This notation */
```

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/annotation-no-unknown.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ a { color: green !imprtant; }

This rule considers annotations defined in the CSS Specifications, up to and including Editor's Drafts, to be known.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/at-rule-allowed-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Specify a list of allowed at-rules.
* At-rules like this */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/at-rule-disallowed-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Specify a list of disallowed at-rules.
* At-rules like this */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/at-rule-empty-line-before.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This rule ignores:
- at-rules that are the very first node in the source
- `@import` in Less.

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/at-rule-no-unknown.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Disallow unknown at-rules.

This rule considers at-rules defined in the CSS Specifications, up to and including Editor's Drafts, to be known.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
4 changes: 2 additions & 2 deletions docs/description/at-rule-no-vendor-prefix.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Disallow vendor prefixes for at-rules.

This rule ignores non-standard vendor-prefixed at-rules that aren't handled by [Autoprefixer](https://github.com/postcss/autoprefixer).

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule. However, it will not remove duplicate at-rules produced when the prefixes are removed. You can use [Autoprefixer](https://github.com/postcss/autoprefixer) itself, with the [`add` option off and the `remove` option on](https://github.com/postcss/autoprefixer#options), in these situations.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule. However, it will not remove duplicate at-rules produced when the prefixes are removed. You can use [Autoprefixer](https://github.com/postcss/autoprefixer) itself, with the [`add` option off and the `remove` option on](https://github.com/postcss/autoprefixer#options), in these situations.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/at-rule-property-required-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Specify a list of required properties for an at-rule.
* At-rule and required property names */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
4 changes: 2 additions & 2 deletions docs/description/color-function-notation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Modern color-functions use a comma-free syntax because functions in CSS are used

For legacy reasons, `rgb()` and `hsl()` also supports an alternate syntax that separates all of its arguments with commas. Also for legacy reasons, the `rgba()` and `hsla()` functions exist using the same comma-based syntax.

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix some of the problems reported by this rule when the primary option is `"modern"`.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix some of the problems reported by this rule when the primary option is `"modern"`.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/color-hex-alpha.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a { color: #fffa }
* This alpha channel */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
4 changes: 2 additions & 2 deletions docs/description/color-hex-length.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ a { color: #fff }
* This hex color */
```

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/color-no-hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ a { color: #333 }
* This hex color */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/color-no-invalid-hex.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ a { color: #y3 }

Longhand hex colors can be either 6 or 8 (with alpha channel) hexadecimal characters. And their shorthand variants are 3 and 4 characters respectively.

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/comment-empty-line-before.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This rule ignores:
- single-line comments with `//` (when you're using a custom syntax that supports them)
- comments within selector and value lists

The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/comment-pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Specify a pattern for comments.
* The pattern of this */
```

The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/comment-whitespace-inside.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Any number of asterisks are allowed at the beginning or end of the comment. So `
> [!WARNING]
> Comments within _selector and value lists_ are currently ignored.
The [`fix` option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.
The [`fix` option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/options.md#fix) can automatically fix all of the problems reported by this rule.

## Options

Expand Down
2 changes: 1 addition & 1 deletion docs/description/comment-word-disallowed-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Specify a list of disallowed words within comments.
> [!WARNING]
> Comments within _selector and value lists_ are currently ignored.
The [`message` secondary option](https://github.com/stylelint/stylelint/tree/16.6.1/docs/user-guide/configure.md#message) can accept the arguments of this rule.
The [`message` secondary option](https://github.com/stylelint/stylelint/16.10.0/docs/user-guide/configure.md#message) can accept the arguments of this rule.

## Options

Expand Down
Loading

0 comments on commit abbf266

Please sign in to comment.