Skip to content

Commit f924461

Browse files
authored
style: Apply new formatting and add a CONTRIBUTING.md which explains it. (#381)
Co-authored-by: Dylan Anthony <[email protected]>
1 parent 757770f commit f924461

37 files changed

+266
-167
lines changed

.github/workflows/tests.yml

+23
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,29 @@ jobs:
3939
- name: Test building the book
4040
run: cargo make book
4141

42+
check-format:
43+
runs-on: ubuntu-latest
44+
steps:
45+
- uses: actions/checkout@v3
46+
- run: |
47+
rustup override set nightly
48+
rustup update nightly
49+
rustup component add rustfmt
50+
51+
- name: Install cargo-binstall
52+
run: |
53+
wget https://github.com/ryankurte/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz
54+
tar -xvf cargo-binstall-x86_64-unknown-linux-musl.tgz
55+
cp cargo-binstall $HOME/.cargo/bin
56+
57+
- name: Install cargo-make
58+
run: cargo-binstall --no-confirm cargo-make --force
59+
60+
- name: Install taplo
61+
run: cargo-binstall --no-confirm taplo-cli --force
62+
63+
- run: cargo make check-format
64+
4265
validate-config:
4366
name: Validate Knope Config
4467
runs-on: ubuntu-latest

CONTRIBUTING.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Contributing
2+
3+
Knope is open to all kinds of contributions—if you want to contribute code there are a few helpful hints.
4+
5+
## cargo-make
6+
7+
We use [`cargo-make`](https://sagiegurari.github.io/cargo-make/) to make running some development tasks easier. You do not _need_ `cargo-make`, but it is recommended. Here are the common tasks you'll want to run:
8+
9+
1. `cargo make` will run all the development tasks, reformatting code, linting, and running tests.
10+
2. `cargo make serve-book` builds the docs, watches for changes, and starts a local webserver to view them.
11+
3. `cargo make ci-flow` will run _most_ of what CI does, excluding building the docs and checking formatting.
12+
4. `cargo make check-format` checks the formatting of the code the same way that CI will.
13+
14+
## Formatting
15+
16+
We use `rustfmt` to format Rust code, but we depend on unstable features (e.g., sorting imports). You need to install the nightly toolchain (e.g., `rustup toolchain install nightly`) before formatting the code.
17+
18+
We also use [Prettier](https://prettier.io) to format Markdown (via [`npx`](https://docs.npmjs.com/cli/v7/commands/npx)) and [Taplo](https://crates.io/crates/taplo-cli) for formatting TOML. Taplo will be installed automatically if using `cargo-make`, but `npx` must be available to run Prettier.
19+
20+
## Snapshot Tests
21+
22+
We use [snapbox](https://crates.io/crates/snapbox) for most of the integration tests in the `tests` dir. This allows us to run commands end-to-end and compare the output to what we expect (making it much clearer when things change).
23+
24+
The general workflow for a snapshot test is:
25+
26+
1. Create a new directory in `tests` (optionally nested in a subdirectory relevant to what you're testing) which contains all the setup files you need (e.g., a `knope.toml` and a `Cargo.toml`).
27+
2. Create a temp directory and copy all the source files over.
28+
3. Use the functions from `tests/git_repo_helpers` to set up a git repo and add any commits/tags you need (for testing releases).
29+
4. Run the command and verify the output using snapbox.
30+
31+
A good example of this is the `prerelease_after_release` test in `tests/prepare_release.rs`.

Makefile.toml

+13-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ args = ["prettier", "**/*.md", "--list-different", "--prose-wrap=never"]
4242
command = "cargo"
4343
args = ["deny", "--all-features", "check"]
4444

45+
[tasks.check-format-toml]
46+
install_crate = "taplo-cli"
47+
command = "taplo"
48+
args = ["format", "--check"]
49+
50+
[tasks.check-format]
51+
toolchain = "nightly"
52+
dependencies = ["prettier-check", "check-format-toml"]
53+
54+
[tasks.format]
55+
toolchain = "nightly"
56+
4557
[tasks.dev-test-flow]
4658
dependencies = [
4759
"deny",
@@ -56,4 +68,4 @@ dependencies = [
5668
]
5769

5870
[tasks.pre-ci-flow]
59-
dependencies = ["deny", "clippy-flow", "check-format"]
71+
dependencies = ["deny", "clippy-flow"]

deny.toml

+10-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@ unlicensed = "deny"
6969
# List of explicitly allowed licenses
7070
# See https://spdx.org/licenses/ for list of possible licenses
7171
# [possible values: any SPDX 3.11 short identifier (+ optional exception)].
72-
allow = ["MIT", "Apache-2.0", "ISC", "OpenSSL", "MPL-2.0", "BSD-3-Clause", "Unicode-DFS-2016"]
72+
allow = [
73+
"MIT",
74+
"Apache-2.0",
75+
"ISC",
76+
"OpenSSL",
77+
"MPL-2.0",
78+
"BSD-3-Clause",
79+
"Unicode-DFS-2016",
80+
]
7381
# List of explicitly disallowed licenses
7482
# See https://spdx.org/licenses/ for list of possible licenses
7583
# [possible values: any SPDX 3.11 short identifier (+ optional exception)].
@@ -163,7 +171,7 @@ skip = [
163171
# by default infinite
164172
skip-tree = [
165173
{ name = "windows", version = "0.40.0", depth = 10 }, # Waiting on update to git-index
166-
{ name = "ahash", version = "0.7.6", depth = 10 }, # Waiting on update to hashbrown
174+
{ name = "ahash", version = "0.7.6", depth = 10 }, # Waiting on update to hashbrown
167175
]
168176

169177
# This section is considered when running `cargo deny check sources`.

docs/src/default_workflows.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ Without any config, you can run `knope release` to create a new release from [co
2424

2525
[conventional commits]: https://www.conventionalcommits.org/en/v1.0.0/
2626
[semantic version]: https://semver.org
27-
[`PrepareRelease`]: config/step/PrepareRelease.md
28-
[`Release`]: config/step/Release.md
27+
[`preparerelease`]: config/step/PrepareRelease.md
28+
[`release`]: config/step/Release.md

rustfmt.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
unstable_features = true
2+
group_imports = "StdExternalCrate"
3+
imports_granularity = "Crate"

src/app_config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use platform_dirs::AppDirs;
22

3-
use crate::prompt::get_input;
4-
use crate::step::StepError;
3+
use crate::{prompt::get_input, step::StepError};
54

65
/// For managing configuration of knope globally
76

src/command.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ use std::collections::HashMap;
33
use execute::shell;
44
use serde::{Deserialize, Serialize};
55

6-
use crate::git::branch_name_from_issue;
7-
use crate::releases::get_version;
8-
use crate::state::Release;
9-
use crate::step::StepError;
10-
use crate::{state, RunType, State};
6+
use crate::{
7+
git::branch_name_from_issue, releases::get_version, state, state::Release, step::StepError,
8+
RunType, State,
9+
};
1110

1211
/// Describes a value that you can replace an arbitrary string with when running a command.
1312
#[derive(Debug, Deserialize, Serialize)]
@@ -99,9 +98,8 @@ fn replace_variables(
9998
mod test_run_command {
10099
use tempfile::NamedTempFile;
101100

102-
use crate::State;
103-
104101
use super::*;
102+
use crate::State;
105103

106104
#[test]
107105
fn test() {
@@ -128,12 +126,14 @@ mod test_run_command {
128126

129127
#[cfg(test)]
130128
mod test_replace_variables {
131-
use crate::issues::Issue;
132-
use crate::releases::{semver::Version, Package, Release};
133-
use crate::state;
134129
use std::path::PathBuf;
135130

136131
use super::*;
132+
use crate::{
133+
issues::Issue,
134+
releases::{semver::Version, Package, Release},
135+
state,
136+
};
137137

138138
fn packages() -> Vec<Package> {
139139
vec![Package {

src/config.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
use std::collections::BTreeMap;
2-
use std::fs;
3-
use std::path::PathBuf;
1+
use std::{collections::BTreeMap, fs, path::PathBuf};
42

53
use miette::{IntoDiagnostic, Result, WrapErr};
64
use serde::{Deserialize, Serialize};
75
use velcro::{hash_map, vec};
86

9-
use crate::releases::find_packages;
10-
use crate::step::{PrepareRelease, Step, StepError};
11-
use crate::workflow::Workflow;
12-
use crate::{command, git, releases};
7+
use crate::{
8+
command, git, releases,
9+
releases::find_packages,
10+
step::{PrepareRelease, Step, StepError},
11+
workflow::Workflow,
12+
};
1313

1414
#[derive(Deserialize, Debug, Serialize)]
1515
pub(crate) struct Config {

src/git.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use std::path::PathBuf;
2-
use std::str::FromStr;
1+
use std::{path::PathBuf, str::FromStr};
32

4-
use git2::build::CheckoutBuilder;
5-
use git2::{Branch, BranchType, Repository};
3+
use git2::{build::CheckoutBuilder, Branch, BranchType, Repository};
64
use log::{debug, error, trace, warn};
75

8-
use crate::issues::Issue;
9-
use crate::prompt::select;
10-
use crate::releases::{get_current_versions_from_tag, tag_name};
11-
use crate::state;
12-
use crate::step::StepError;
13-
use crate::RunType;
6+
use crate::{
7+
issues::Issue,
8+
prompt::select,
9+
releases::{get_current_versions_from_tag, tag_name},
10+
state,
11+
step::StepError,
12+
RunType,
13+
};
1414

1515
/// Based on the selected issue, either checks out an existing branch matching the name or creates
1616
/// a new one, prompting for which branch to base it on.

src/issues/github.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use crate::app_config::get_or_prompt_for_github_token;
2-
use crate::issues::Issue;
3-
use crate::step::StepError;
4-
use crate::{config, state};
1+
use crate::{
2+
app_config::get_or_prompt_for_github_token, config, issues::Issue, state, step::StepError,
3+
};
54

65
const ISSUES_QUERY: &str = r##"
76
query($repo: String!, $owner: String!, $labels: [String!]) {

src/issues/jira.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use base64::{prelude::BASE64_STANDARD as base64, Engine};
22
use serde::{Deserialize, Serialize};
33

4-
use crate::app_config::{get_or_prompt_for_email, get_or_prompt_for_jira_token};
5-
use crate::config::Jira;
6-
use crate::issues::Issue;
7-
use crate::step::StepError;
4+
use crate::{
5+
app_config::{get_or_prompt_for_email, get_or_prompt_for_jira_token},
6+
config::Jira,
7+
issues::Issue,
8+
step::StepError,
9+
};
810

911
#[derive(Serialize, Debug)]
1012
struct SearchParams {

src/issues/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::fmt;
22

3-
use crate::prompt::select;
4-
use crate::state::{self, RunType, State};
5-
use crate::step::StepError;
3+
use crate::{
4+
prompt::select,
5+
state::{self, RunType, State},
6+
step::StepError,
7+
};
68

79
mod github;
810
mod jira;

src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use std::io::stdout;
88

99
use clap::Parser;
1010
use miette::{miette, Result};
11-
1211
use prompt::select;
1312

14-
use crate::config::Config;
15-
use crate::state::{RunType, State};
13+
use crate::{
14+
config::Config,
15+
state::{RunType, State},
16+
};
1617

1718
mod app_config;
1819
mod command;

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
#![allow(clippy::multiple_crate_versions)] // Let cargo-deny handle this
55
#![forbid(unsafe_code)]
66

7-
use clap::Parser;
8-
use miette::Result;
97
use std::env::var;
108

9+
use clap::Parser;
1110
use knope::{run, Cli};
11+
use miette::Result;
1212

1313
fn main() -> Result<()> {
1414
if var("RUST_LOG").is_ok() {

src/prompt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::fmt::Display;
22

33
use console::Term;
4-
use dialoguer::theme::ColorfulTheme;
5-
use dialoguer::{Input, Select};
4+
use dialoguer::{theme::ColorfulTheme, Input, Select};
65
use miette::Result;
76

87
use crate::step::StepError;

src/releases/changelog.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::iter::Map;
2-
use std::slice::Iter;
1+
use std::{iter::Map, slice::Iter};
32

43
use itertools::Itertools;
54

src/releases/conventional_commits.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@ use std::io::Write;
33
use git_conventional::{Commit, Type};
44
use log::debug;
55

6-
use crate::git::{add_files, get_commit_messages_after_last_stable_version};
7-
use crate::releases::semver::{Label, PackageVersion};
8-
use crate::releases::Package;
9-
use crate::step::StepError;
10-
use crate::{state, step, RunType};
11-
12-
use super::changelog::{add_version_to_changelog, new_changelog_lines};
13-
use super::semver::{bump_version, ConventionalRule, Rule};
14-
use super::Release;
6+
use super::{
7+
changelog::{add_version_to_changelog, new_changelog_lines},
8+
semver::{bump_version, ConventionalRule, Rule},
9+
Release,
10+
};
11+
use crate::{
12+
git::{add_files, get_commit_messages_after_last_stable_version},
13+
releases::{
14+
semver::{Label, PackageVersion},
15+
Package,
16+
},
17+
state, step,
18+
step::StepError,
19+
RunType,
20+
};
1521

1622
#[derive(Debug)]
1723
struct ConventionalCommits {

src/releases/git.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use std::env::current_dir;
2-
use std::io::Write;
3-
use std::str::FromStr;
1+
use std::{env::current_dir, io::Write, str::FromStr};
42

5-
use git_repository::object::Kind;
6-
use git_repository::open;
7-
use git_repository::refs::transaction::PreviousValue;
3+
use git_repository::{object::Kind, open, refs::transaction::PreviousValue};
84

9-
use crate::releases::semver::Version;
10-
use crate::releases::{CurrentVersions, Release};
11-
use crate::step::StepError;
5+
use crate::{
6+
releases::{semver::Version, CurrentVersions, Release},
7+
step::StepError,
8+
};
129

1310
pub(crate) fn tag_name(version: &Version, package_name: &Option<String>) -> String {
1411
let prefix = package_name

src/releases/github.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use std::io::Write;
22

33
use serde::Serialize;
44

5-
use crate::app_config::get_or_prompt_for_github_token;
6-
use crate::config::GitHub;
7-
use crate::releases::git::tag_name;
8-
use crate::releases::Release;
9-
use crate::state;
10-
use crate::state::GitHub::{Initialized, New};
11-
use crate::step::StepError;
5+
use crate::{
6+
app_config::get_or_prompt_for_github_token,
7+
config::GitHub,
8+
releases::{git::tag_name, Release},
9+
state,
10+
state::GitHub::{Initialized, New},
11+
step::StepError,
12+
};
1213

1314
pub(crate) fn release(
1415
release: &Release,

src/releases/go.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::releases::semver::Version;
2-
use crate::step::StepError;
1+
use crate::{releases::semver::Version, step::StepError};
32

43
pub(crate) fn set_version(go_mod: String, new_version: &Version) -> Result<String, StepError> {
54
let new_major = new_version.stable_component().major;

0 commit comments

Comments
 (0)