Skip to content

Commit

Permalink
add just and setup codegen cli
Browse files Browse the repository at this point in the history
  • Loading branch information
psteinroe committed Dec 10, 2024
1 parent 64737cd commit 3d682bb
Show file tree
Hide file tree
Showing 12 changed files with 573 additions and 3 deletions.
10 changes: 9 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
members = [
"crates/*",
"lib/*",
"xtask/"
"xtask/codegen"
]
resolver = "2"

Expand Down
2 changes: 2 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allow-dbg-in-tests = true

152 changes: 152 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
_default:
just --list -u

alias f := format
alias t := test
# alias r := ready
# alias l := lint
# alias qt := test-quick

# Installs the tools needed to develop
# install-tools:
# cargo install cargo-binstall
# cargo binstall cargo-insta taplo-cli wasm-pack wasm-tools knope

# Upgrades the tools needed to develop
# upgrade-tools:
# cargo install cargo-binstall --force
# cargo binstall cargo-insta taplo-cli wasm-pack wasm-tools knope --force

# Generate all files across crates and tools. You rarely want to use it locally.
# gen-all:
# cargo run -p xtask_codegen -- all
# cargo codegen-configuration
# cargo codegen-migrate
# just gen-bindings
# just format

# Generates TypeScript types and JSON schema of the configuration
# gen-bindings:
# cargo codegen-schema
# cargo codegen-bindings

# Generates code generated files for the linter
# gen-lint:
# cargo run -p xtask_codegen -- analyzer
# cargo codegen-configuration
# cargo codegen-migrate
# just gen-bindings
# cargo run -p rules_check
# just format

# Generates the initial files for all formatter crates
# gen-formatter:
# cargo run -p xtask_codegen -- formatter

# Generates the Tailwind CSS preset for utility class sorting (requires Bun)
# gen-tw:
# bun packages/tailwindcss-config-analyzer/src/generate-tailwind-preset.ts

# Generates the code of the grammars available in Biome
# gen-grammar *args='':
# cargo run -p xtask_codegen -- grammar {{args}}

# Generates the linter documentation and Rust documentation
# documentation:
# RUSTDOCFLAGS='-D warnings' cargo documentation

# Creates a new lint rule in the given path, with the given name. Name has to be camel case.
# new-js-lintrule rulename:
# cargo run -p xtask_codegen -- new-lintrule --kind=js --category=lint --name={{rulename}}
# just gen-lint
# just documentation

# Creates a new lint rule in the given path, with the given name. Name has to be camel case.
# new-js-assistrule rulename:
# cargo run -p xtask_codegen -- new-lintrule --kind=js --category=assist --name={{rulename}}
# just gen-lint
# just documentation

# Promotes a rule from the nursery group to a new group
# promote-rule rulename group:
# cargo run -p xtask_codegen -- promote-rule --name={{rulename}} --group={{group}}
# just gen-lint
# just documentation
# -cargo test -p biome_js_analyze -- {{snakecase(rulename)}}
# cargo insta accept


# Format Rust files and TOML files
format:
cargo format
# taplo format

[unix]
_touch file:
touch {{file}}

[windows]
_touch file:
(gci {{file}}).LastWriteTime = Get-Date

# Run tests of all crates
test:
cargo test run --no-fail-fast

# Run tests for the crate passed as argument e.g. just test-create pg_cli
test-crate name:
cargo test run -p {{name}} --no-fail-fast

# Run doc tests
test-doc:
cargo test --doc

# Tests a lint rule. The name of the rule needs to be camel case
# test-lintrule name:
# just _touch crates/biome_js_analyze/tests/spec_tests.rs
# just _touch crates/biome_json_analyze/tests/spec_tests.rs
# just _touch crates/biome_css_analyze/tests/spec_tests.rs
# just _touch crates/biome_graphql_analyze/tests/spec_tests.rs
# cargo test -p biome_js_analyze -- {{snakecase(name)}} --show-output
# cargo test -p biome_json_analyze -- {{snakecase(name)}} --show-output
# cargo test -p biome_css_analyze -- {{snakecase(name)}} --show-output
# cargo test -p biome_graphql_analyze -- {{snakecase(name)}} --show-output

# Tests a lint rule. The name of the rule needs to be camel case
# test-transformation name:
# just _touch crates/biome_js_transform/tests/spec_tests.rs
# cargo test -p biome_js_transform -- {{snakecase(name)}} --show-output

# Run the quick_test for the given package.
# test-quick package:
# cargo test -p {{package}} --test quick_test -- quick_test --nocapture --ignored


# Alias for `cargo clippy`, it runs clippy on the whole codebase
lint:
cargo clippy

# When you finished coding, run this command to run the same commands in the CI.
# ready:
# git diff --exit-code --quiet
# just gen-all
# just documentation
# #just format # format is already run in `just gen-all`
# just lint
# just test
# just test-doc
# git diff --exit-code --quiet

# Creates a new crate
new-crate name:
cargo new --lib crates/{{snakecase(name)}}
cargo run -p xtask_codegen -- new-crate --name={{snakecase(name)}}

# Creates a new changeset for the final changelog
# new-changeset:
# knope document-change

# Dry-run of the release
# dry-run-release *args='':
# knope release --dry-run {{args}}

2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
newline_style = "Unix"

2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xtask"
version = "0.1.0"
version = "0.0.0"
publish = false
license = "MIT OR Apache-2.0"
edition = "2021"
Expand Down
11 changes: 11 additions & 0 deletions xtask/codegen/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
edition = "2021"
name = "xtask_codegen"
publish = false
version = "0.0.0"

[dependencies]
bpaf = { workspace = true, features = ["derive"] }
xtask = { path = '../', version = "0.0" }


64 changes: 64 additions & 0 deletions xtask/codegen/src/generate_crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::fs;
use xtask::*;

fn cargo_template(name: &str) -> String {
format!(
r#"
[package]
authors.workspace = true
categories.workspace = true
description = "<DESCRIPTION>"
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
name = "{name}"
repository.workspace = true
version = "0.0.0"
[lints]
workspace = true
"#
)
}

// fn knope_template(name: &str) -> String {
// format!(
// r#"
// [packages.{name}]
// versioned_files = ["crates/{name}/Cargo.toml"]
// changelog = "crates/{name}/CHANGELOG.md"
// "#
// )
// }

pub fn generate_crate(crate_name: String) -> Result<()> {
let crate_root = project_root().join("crates").join(crate_name.as_str());
let cargo_file = crate_root.join("Cargo.toml");
// let knope_config = project_root().join("knope.toml");

// let mut knope_contents = fs::read_to_string(&knope_config)?;
fs::write(cargo_file, cargo_template(crate_name.as_str()))?;
// let start_content = "## Rust crates. DO NOT CHANGE!\n";
// let end_content = "\n## End of crates. DO NOT CHANGE!";
// debug_assert!(
// knope_contents.contains(start_content),
// "The file knope.toml must contains `{start_content}`"
// );
// debug_assert!(
// knope_contents.contains(end_content),
// "The file knope.toml must contains `{end_content}`"
// );

// let file_start_index = knope_contents.find(start_content).unwrap() + start_content.len();
// let file_end_index = knope_contents.find(end_content).unwrap();
// let crates_text = &knope_contents[file_start_index..file_end_index];
// let template = knope_template(crate_name.as_str());
// let new_crates_text: Vec<_> = crates_text.lines().chain(Some(&template[..])).collect();
// let new_crates_text = new_crates_text.join("\n");
//
// knope_contents.replace_range(file_start_index..file_end_index, &new_crates_text);
// fs::write(knope_config, knope_contents)?;
Ok(())
}

20 changes: 20 additions & 0 deletions xtask/codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Codegen tools. Derived from Biome's codegen
mod generate_crate;

use bpaf::Bpaf;
pub use self::generate_crate::generate_crate;

#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub enum TaskCommand {
/// Creates a new crate
#[bpaf(command, long("new-crate"))]
NewCrate {
/// The name of the crate
#[bpaf(long("name"), argument("STRING"))]
name: String,
},
}


19 changes: 19 additions & 0 deletions xtask/codegen/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use xtask::{project_root, pushd, Result};

use xtask_codegen::{
generate_crate, task_command, TaskCommand,
};

fn main() -> Result<()> {
let _d = pushd(project_root());
let result = task_command().fallback_to_usage().run();

match result {
TaskCommand::NewCrate { name } => {
generate_crate(name)?;
}
}

Ok(())
}

Loading

0 comments on commit 3d682bb

Please sign in to comment.