Giter VIP home page Giter VIP logo

cargo-manifmt's Introduction

Cargo Manifmt

Todo

  • retain no crlf
  • table rewrite should happen in a single point eg: foo gets sorted but then reordered after the fact, we ideally should just output the ordering/groups and then later on when we actually apply the ordering, we do it all in one go.
[dependencies]
zoo = "1"
[dependencies.foo]
v = "1"

Rust Stable

A tool to check that your Cargo.toml dependencies are sorted alphabetically. Project created as a solution to @dtolnay's request for implementation #29. Cross platform implementation, windows compatible. Terminal coloring works on both cmd.exe and powershell. Checks/sorts by key in tables and also nested table headers (does not sort the items in a nested header, sorts the table itself). cargo sort uses toml-edit to parse the toml file into something useful.

The --format option may result in improperly formatted toml; please file an issue.

Use

There are three modes cargo-sort can be used in:

  • default
    • No flags set cargo-sort will write the sorted result over the input Cargo.toml file.
  • -c or --check
    • Will fail with a non-zero exit code if the file is unsorted.
  • -n or --no-format
    • Will NOT format the sorted toml. This option only has an effect if writing or printing out.
  • -g or --grouped
    • When sorting keep table key value spacing. If you have dependency groups they will stick but be sorted within the grouping. The key_value_newlines config option needs to be true for this to have any effect.
  • -p or --print
    • Write the sorted toml file to stdout.
  • -w or --workspace
    • Checks every crate in the workspace based on flags. Only one root may be given.
  • -o or --order
    • Specify an ordering of tables. All nested tables will be sorted and appear after the specified table. Any unspecified table will be after specified.

Config

cargo sort uses a config file when formatting called tomlfmt.toml. This is optional and defaults will be used if not found in the current working dir.

Here are the defaults when no tomlfmt.toml is found

# trailing comma in arrays
always_trailing_comma = false
# trailing comma when multi-line
multiline_trailing_comma = true
space_around_eq = true
# remove all the spacing inside the array
compact_arrays = false
# remove all the spacing inside the object
compact_inline_tables = false
trailing_newline = true
# is it ok to have blank lines inside of a table
# this option needs to be true for the --grouped flag
key_value_newlines = true
allowed_blank_lines = 1
# windows style line endings
crlf = false
# The user specified ordering of tables in a document.
# All unspecified tables will come after these.
table_order = []

included in sort check is:

["dependencies"]
["dev-dependencies"]
["build-dependencies"]
["workspace.members"]
["workspace.exclude"]

If you have a header to add open a PR, they are welcome.

Install

cargo install cargo-sort

pre-commit

If you use pre-commit in your project, you can add cargo-sort as hook by adding the following entry to your .pre-commit0-config.yaml configuration:

repos:
- repo: https://github.com/DevinR528/cargo-sort
  rev: v1.0.4
  hooks:
  - id: cargo-sort

Please make sure to set rev to the latest tag of this repo as the tag shown here might not always be updated to the latest version.

Run

Thanks to @dspicher for issue #4 you can now invoke cargo sort check as a cargo subcommand

cargo sort [FLAGS] [path]

Wildcard expansion is supported so you can do this

cargo-sort [FLAGS] [path/to/*/Cargo.toml | path/to/*]

or any other pattern that is supported by your terminal. This also means multiple paths work.

cargo-sort [FLAGS] path/to/a path/to/b path/to/c/Cargo.toml

Finally cargo sort has the --workspace flag and will sort each Cargo.toml file in a workspace

cargo-sort -w/--workspace

These are all valid. File names and extensions can be used on some of the paths but not others, if left off the tool will default to Cargo.toml.

cargo sort 1.0.0
Devin R <[email protected]>
Ensure Cargo.toml dependency tables are sorted.

USAGE:
    cargo-sort [FLAGS] [CWD]

FLAGS:
    -c, --check        exit with non-zero if Cargo.toml is unsorted, overrides default behavior
    -f, --format       formats the given Cargo.toml according to tomlfmt.toml
    -g, --grouped      when sorting groups of key value pairs blank lines are kept
    -h, --help         Prints help information
    -p, --print        prints Cargo.toml, lexically sorted, to stdout
    -V, --version      Prints version information
    -w, --workspace    checks every crate in a workspace

ARGS:
    <CWD>...    sets cwd, must contain a Cargo.toml file

Docker

Build the image:

docker build -t cargo-sort .

Run the container:

docker run -it --rm -v "$(pwd)/Cargo.toml":/app/Cargo.toml cargo-sort

Image is also available on Docker Hub:

docker run -it --rm -v "$(pwd)/Cargo.toml":/app/Cargo.toml devinr528/cargo-sort:latest

Examples

[dependencies]
a="0.1.1"
# comments will stay with the item
c="0.1.1"

# If --grouped is used the blank line will stay.
b="0.1.1"

[dependencies.alpha]
version="0"

[build-dependencies]
foo="0"
bar="0"

# comments will also stay with header
[dependencies.zed]
version="0"

[dependencies.beta]
version="0"

[dev-dependencies]
bar="0"
foo="0"

Will sort to, or fail until organized like so

[dependencies]
a="0.1.1"

# If --grouped is used the blank line will stay
b="0.1.1"
# comments will stay with the item
c="0.1.1"

[dependencies.alpha]
version="0"

[dependencies.beta]
version="0"

# comments will also stay with header
[dependencies.zed]
version="0"

# Tables are ordered by their appearance so
# if dev-dependencies was before build it would be
# sorted that way unless --order is specified
[build-dependencies]
bar="0"
foo="0"

[dev-dependencies]
bar="0"
foo="0"

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

cargo-manifmt's People

Contributors

devinr528 avatar dnaka91 avatar dspicher avatar estk avatar jose-acevedoflores avatar jplatte avatar matze avatar omid avatar orhun avatar quarticcat avatar

Watchers

 avatar

cargo-manifmt's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.