Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: script for updating languages in readme #85

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ tempfile = "3.10.1"

[dev-dependencies]
test-with = { version = "0.12.6", default-features = false, features = [
"executable"
"executable",
] }
79 changes: 42 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,40 +58,45 @@ mdsf init
>
> Only formatters that are already installed will be used.

| Language | Formatters |
| ----------- | ------------------------------------------------------------- |
| Blade | `blade-formatter` |
| C | `clang-format` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| CSharp | `clang-format` |
| CSS | `prettier` |
| Dart | `dart_format` |
| Elm | `elm-format` |
| Elixir | `mix_format` |
| Gleam | `gleam_format` |
| Go | `gofmt`, `gofumpt` |
| GraphQL | `prettier` |
| HTML | `prettier` |
| Java | `clang-format` |
| JavaScript | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| JSON | `prettier`, `biome`, `clang-format`, `deno_fmt` |
| Just | `just_fmt` |
| Lua | `stylua` |
| Nim | `nimpretty` |
| Objective C | `clang-format` |
| OCaml | `ocamlformat` |
| Protobuf | `clang-format` |
| Python | `ruff`, `black`, `blue`, `yapf`, `autopep8`, `isort`, `usort` |
| ReScript | `rescript_format` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| Shell | `shfmt` |
| SQL | `sqlfluff`, `sql-formatter` |
| TOML | `taplo` |
| TypeScript | `prettier`, `biome`, `deno_fmt` |
| Vue | `prettier` |
| Xml | `xmllint` |
| YAML | `prettier` |
| Zig | `zigfmt` |
<!-- START_SECTION:supported-languages -->

| Language | Formatters |
| ---------- | ------------------------------------------------------------- |
| Blade | `blade-formatter` |
| C | `clang-format` |
| CSharp | `clang-format` |
| Cpp | `clang-format` |
| Crystal | `crystal_format` |
| Css | `prettier` |
| Dart | `dart_format` |
| Elixir | `mix_format` |
| Elm | `elm-format` |
| Gleam | `gleam_format` |
| Go | `gofmt`, `gofumpt` |
| GraphQL | `prettier` |
| Html | `prettier` |
| Java | `clang-format` |
| JavaScript | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Json | `biome`, `clang-format`, `deno_fmt`, `prettier` |
| Just | `just_fmt` |
| Lua | `stylua` |
| Markdown | `prettier` |
| Nim | `nimpretty` |
| OCaml | `ocamlformat` |
| ObjectiveC | `clang-format` |
| Protobuf | `clang-format` |
| Python | `autopep8`, `black`, `blue`, `isort`, `ruff`, `usort`, `yapf` |
| ReScript | `rescript_format` |
| Roc | `roc_format` |
| Ruby | `rubocop` |
| Rust | `rustfmt` |
| Shell | `shfmt` |
| Sql | `sql-formatter`, `sqlfluff` |
| Toml | `taplo` |
| TypeScript | `biome`, `deno_fmt`, `prettier` |
| Vue | `prettier` |
| Xml | `xmllint` |
| Yaml | `prettier` |
| Zig | `zigfmt` |

<!-- END_SECTION:supported-languages -->
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ precommit:
just lint
just test
cargo run -- schema
node scripts/update-supported-languages.mjs
npx --yes prettier --write .
git restore tests/
typos .
Expand Down
112 changes: 112 additions & 0 deletions scripts/update-supported-languages.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import fs from "fs/promises";

async function getVersion() {
const content = await fs
.readFile("Cargo.toml")
.then((data) => data.toString());

const line = content.split("\n").find((l) => l.startsWith("version ="));

return line.split('"')[1];
}

/**
* @param version {string}
*/
function loadSchema(version) {
return fs
.readFile(`schemas/v${version}/mdsf.schema.json`)
.then((data) => data.toString())
.then(JSON.parse)
.then((data) => {
if (!data || typeof data !== "object") {
throw new Error("Expected schema to be an object");
}

return data;
});
}

/**
* @param schema {Awaited<ReturnType<typeof loadSchema>>}
*/
function createLanguageTable(schema) {
const languageHeading = "Language";
let languageWidth = languageHeading.length;

const formatterHeading = "Formatters";
let formatterWidth = formatterHeading.length;

/** @type {Map<string, string>} */
const languages = new Map();

for (const [key, value] of Object.entries(schema.definitions)) {
if (key.startsWith("MdsfFormatter_") || key.startsWith("Lang_")) {
continue;
}

const formatterLine = value.enum
.sort()
.map((f) => "`" + f + "`")
.join(", ");

languageWidth = Math.max(languageWidth, key.length);
formatterWidth = Math.max(formatterWidth, formatterLine.length);

languages.set(key, formatterLine);
}

/** @type {string[]} */
const lines = [];

for (const [key, value] of languages) {
const line = `| ${key.padEnd(languageWidth, " ")} | ${value.padEnd(
formatterWidth,
" ",
)} |`;

lines.push(line);
}

lines.sort();

const filler = `| ${"".padEnd(languageWidth, "-")} | ${"".padEnd(
formatterWidth,
"-",
)} |`;

lines.unshift(filler);

const heading = `| ${languageHeading.padEnd(
languageWidth,
" ",
)} | ${formatterHeading.padEnd(formatterWidth, " ")} |`;

lines.unshift(heading);

return lines.join("\n");
}

/**
* @param table {string}
*/
async function updateReadme(table) {
const content = await fs
.readFile("README.md")
.then((data) => data.toString());

const re =
/(<!-- START_SECTION:supported-languages -->)[^{}]*<!-- END_SECTION:supported-languages -->/gm;
const update = `<!-- START_SECTION:supported-languages -->\n\n${table}\n\n<!-- END_SECTION:supported-languages -->`;

await fs.writeFile("README.md", content.replace(re, update));
}

(async () => {
const version = await getVersion();

const schema = await loadSchema(version);
const table = createLanguageTable(schema);

await updateReadme(table);
})();
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ impl Default for MdsfConfig {
csharp: Lang::<CSharp>::default(),
css: Lang::<Css>::default(),
dart: Lang::<Dart>::default(),
elm: Lang::<Elm>::default(),
elixir: Lang::<Elixir>::default(),
elm: Lang::<Elm>::default(),
gleam: Lang::<Gleam>::default(),
go: Lang::<Go>::default(),
graphql: Lang::<GraphQL>::default(),
Expand Down
8 changes: 4 additions & 4 deletions src/formatters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Crystal => config.crystal.format(snippet_path),
Language::Css => config.css.format(snippet_path),
Language::Dart => config.dart.format(snippet_path),
Language::Elm => config.elm.format(snippet_path),
Language::Elixir => config.elixir.format(snippet_path),
Language::Elm => config.elm.format(snippet_path),
Language::Gleam => config.gleam.format(snippet_path),
Language::Go => config.go.format(snippet_path),
Language::GraphQL => config.graphql.format(snippet_path),
Expand All @@ -120,10 +120,11 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Lua => config.lua.format(snippet_path),
Language::Markdown => config.markdown.format(snippet_path),
Language::Nim => config.nim.format(snippet_path),
Language::ObjectiveC => config.objective_c.format(snippet_path),
Language::OCaml => config.ocaml.format(snippet_path),
Language::ObjectiveC => config.objective_c.format(snippet_path),
Language::Protobuf => config.protobuf.format(snippet_path),
Language::Python => config.python.format(snippet_path),
Language::ReScript => config.rescript.format(snippet_path),
Language::Roc => config.roc.format(snippet_path),
Language::Ruby => config.ruby.format(snippet_path),
Language::Rust => config.rust.format(snippet_path),
Expand All @@ -132,10 +133,9 @@ pub fn format_snippet(config: &MdsfConfig, language: &Language, code: &str) -> S
Language::Toml => config.toml.format(snippet_path),
Language::TypeScript => config.typescript.format(snippet_path),
Language::Vue => config.vue.format(snippet_path),
Language::Xml => config.xml.format(snippet_path),
Language::Yaml => config.yaml.format(snippet_path),
Language::Zig => config.zig.format(snippet_path),
Language::ReScript => config.rescript.format(snippet_path),
Language::Xml => config.xml.format(snippet_path),
} {
let mut f = formatted_code.trim().to_owned();

Expand Down
38 changes: 18 additions & 20 deletions src/languages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use crate::formatters::MdsfFormatter;
pub enum Language {
Blade,
C,
Crystal,
Cpp,
CSharp,
Cpp,
Crystal,
Css,
Dart,
Elm,
Elixir,
Elm,
Gleam,
GraphQL,
Go,
GraphQL,
Html,
Java,
JavaScript,
Expand All @@ -23,8 +23,8 @@ pub enum Language {
Lua,
Markdown,
Nim,
ObjectiveC,
OCaml,
ObjectiveC,
Protobuf,
Python,
ReScript,
Expand Down Expand Up @@ -110,8 +110,10 @@ impl Language {
"css" | "scss" => Some(Self::Css),
"dart" => Some(Self::Dart),
"elixir" => Some(Self::Elixir),
"elm" => Some(Self::Elm),
"gleam" => Some(Self::Gleam),
"go" | "golang" => Some(Self::Go),
"graphql" | "gql" => Some(Self::GraphQL),
"html" => Some(Self::Html),
"java" => Some(Self::Java),
"javascript" | "js" | "jsx" => Some(Self::JavaScript),
Expand All @@ -124,51 +126,52 @@ impl Language {
"ocaml" => Some(Self::OCaml),
"profobuf" | "profo" => Some(Self::Protobuf),
"python" => Some(Self::Python),
"rescript" => Some(Self::ReScript),
"roc" => Some(Self::Roc),
"ruby" => Some(Self::Ruby),
"rust" | "rb" => Some(Self::Rust),
"ruby" | "rb" => Some(Self::Ruby),
"rust" | "rs" => Some(Self::Rust),
"shell" | "sh" | "bash" | "zsh" => Some(Self::Shell),
"sql" | "bigquery" | "db2" | "db2i" | "hive" | "mariadb" | "mysql" | "n1ql"
| "plsql" | "postgresql" | "redshift" | "singlestoredb" | "snowflake" | "spark"
| "sqlite" | "transactsql" | "trino" | "tsql" => Some(Self::Sql),
"toml" => Some(Self::Toml),
"typescript" | "ts" | "tsx" => Some(Self::TypeScript),
"vue" => Some(Self::Vue),
"xml" => Some(Self::Xml),
"yml" | "yaml" => Some(Self::Yaml),
"zig" => Some(Self::Zig),
"graphql" | "gql" => Some(Self::GraphQL),
"elm" => Some(Self::Elm),
"rescript" => Some(Self::ReScript),
"xml" => Some(Self::Xml),
_ => None,
}
}

#[inline]
pub const fn to_file_ext(&self) -> &'static str {
match self {
// NOTE: since scss is a superset of css we might as well support both at the same time
Self::Blade => ".blade.php",
Self::C => ".c",
Self::CSharp => ".cs",
Self::Cpp => ".cpp",
Self::Crystal => ".cr",
Self::CSharp => ".cs",
Self::Css => ".scss",
Self::Dart => ".dart",
Self::Elixir => ".ex",
Self::Elm => ".elm",
Self::Gleam => ".gleam",
Self::Go => ".go",
Self::GraphQL => ".gql",
Self::Html => ".html",
Self::Java => ".java",
Self::JavaScript => ".js",
Self::Json => ".jsonc",
// NOTE: just does not have any file ext
Self::Just => ".justfile",
Self::Lua => ".lua",
Self::Markdown => ".md",
Self::Nim => ".nim",
Self::OCaml => ".ml",
Self::ObjectiveC => ".m",
Self::Protobuf => ".proto",
Self::Python => ".py",
Self::ReScript => ".res",
Self::Roc => ".roc",
Self::Ruby => ".rb",
Self::Rust => ".rs",
Expand All @@ -177,14 +180,9 @@ impl Language {
Self::Toml => ".toml",
Self::TypeScript => ".ts",
Self::Vue => ".vue",
Self::Xml => ".xml",
Self::Yaml => ".yml",
Self::Zig => ".zig",
Self::GraphQL => ".gql",
Self::Elm => ".elm",
Self::Blade => ".blade.php",
Self::OCaml => ".ml",
Self::ReScript => ".res",
Self::Xml => ".xml",
}
}
}
Expand Down
Loading