From 0064d2d381bc2238fa8960e685da28e05e92d2b2 Mon Sep 17 00:00:00 2001 From: Tiffany Le-Nguyen Date: Fri, 8 Mar 2019 12:04:02 -0500 Subject: [PATCH] feat: add cargo package manager and refactor existing package section (#171) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit โœจ๐Ÿ”จ Refactor package to include cargo, and use jq and python for node --- docs/Options.md | 7 ++- functions/__sf_lib_section.fish | 2 +- functions/__sf_section_package.fish | 40 +++++++++++++--- tests/__sf_section_package.test.fish | 72 ++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 9 deletions(-) create mode 100644 tests/__sf_section_package.test.fish diff --git a/docs/Options.md b/docs/Options.md index fc8e4a2..26b82a9 100644 --- a/docs/Options.md +++ b/docs/Options.md @@ -139,9 +139,12 @@ Git status indicators is shown only when you have dirty repository. ### Package version \(`package`\) -> Works only for [npm](https://www.npmjs.com/) at the moment. Please, help [spaceship](https://github.com/denysdovhan/spaceship-prompt) improve this section! +> Works only for [npm](https://www.npmjs.com/) and [cargo](https://crates.io/) at the moment. Please, help [spaceship](https://github.com/denysdovhan/spaceship-prompt) improve this section! -Package version is shown when repository is a package (e.g. contains a `package.json` file). If no version information is found in `package.json`, the `โš ` symbol will be shown. +Package version is shown when repository is a package (e.g. contains a `package.json` or `Cargo.toml` file). If no version information is found in `package.json` or there is an error parsing `Cargo.toml`, the `โš ` symbol will be shown. + +* **npm** โ€” `npm` package contains a `package.json` file. We use `jq`, `python` to parse package version for improving performance and `node` as a fallback. Install [jq](https://stedolan.github.io/jq/) for **improved performance** of this section ([Why?](./Troubleshooting.md#why-is-my-prompt-slow)) +* **cargo** โ€” `cargo` package contains a `Cargo.toml` file. Currently, we use `cargo pkgid`, it depends on `Cargo.lock`. So if package version isn't shown, you may need to run some command like `cargo build` which can generate `Cargo.lock` file. > **Note:** This is the version of the package you are working on, not the version of package manager itself. diff --git a/functions/__sf_lib_section.fish b/functions/__sf_lib_section.fish index 4fbf756..819b68f 100644 --- a/functions/__sf_lib_section.fish +++ b/functions/__sf_lib_section.fish @@ -1,5 +1,5 @@ function __sf_lib_section -a color prefix content suffix - # If there are only 2 args, they're content and prefix + # If there are only 2 args, they are $content and $prefix if test (count $argv) -eq 2 set content $argv[2] set prefix diff --git a/functions/__sf_section_package.fish b/functions/__sf_section_package.fish index 2edc7de..b613b87 100644 --- a/functions/__sf_section_package.fish +++ b/functions/__sf_section_package.fish @@ -4,6 +4,7 @@ # Current package version. # These package managers supported: # * NPM +# * Cargo function __sf_section_package -d "Display the local package version" # ------------------------------------------------------------------------------ @@ -22,13 +23,40 @@ function __sf_section_package -d "Display the local package version" [ $SPACEFISH_PACKAGE_SHOW = false ]; and return - # Show package version only when repository is a package - [ -f ./package.json ]; or return - # Show package version only if npm is installed - type -q npm; or return + # Exit if there is no package.json or Cargo.toml + if not test -e ./package.json; and not test -e ./Cargo.toml + return + end + + set -l package_version + + # Check if package.json exists AND npm exists locally while supressing output to just exit code (-q) + if type -q npm; and test -f ./package.json + # Check if jq (json handler) exists locally. If yes, check in package.json version + if type -q jq + set package_version (jq -r '.version' package.json 2>/dev/null) + # Check if python exists locally, use json to check version in package.json + else if type -q python + set package_version (python -c "import json; print(json.load(open('package.json'))['version'])" 2>/dev/null) + # Check if node exists locally, use it to check version of package.json + else if type -q node + set package_version (node -p "require('./package.json').version" 2>/dev/null) + end + end + + # Check if Cargo.toml exists and cargo command exists + # and use cargo pkgid to figure out the package + if type -q cargo; and test -f ./Cargo.toml + # Handle missing field `version` in Cargo.toml. + # `cargo pkgid` needs Cargo.lock to exists too. If + # it doesn't, do not show package version + set -l pkgid (cargo pkgid 2>&1) + # Early return on error + echo $pkgid | grep -q "error:"; and return - set -l version_line (grep -E '"version": "v?([0-9]+\.){1,}' package.json) - set -l package_version (string split \" $version_line)[4] + # Example input: abc#1.0.0. Example output: 1.0.1 + set package_version (string match -r '#(.*)' $pkgid)[2] + end if test -z "$package_version" set package_version โš  diff --git a/tests/__sf_section_package.test.fish b/tests/__sf_section_package.test.fish new file mode 100644 index 0000000..0666073 --- /dev/null +++ b/tests/__sf_section_package.test.fish @@ -0,0 +1,72 @@ +source $DIRNAME/spacefish_test_setup.fish + +function setup + spacefish_test_setup + mock cargo pkgid 0 "echo \"file:///Users/sirMerr/Development/test-rust#0.1.0\"" + mkdir -p /tmp/tmp-spacefish + cd /tmp/tmp-spacefish +end + +function teardown + rm -rf /tmp/tmp-spacefish +end + +test "Prints section when Cargo.toml is present" + ( + touch /tmp/tmp-spacefish/Cargo.toml + + set_color --bold + echo -n "is " + set_color normal + set_color --bold red + echo -n "๐Ÿ“ฆ v0.1.0" + set_color normal + set_color --bold + echo -n " " + set_color normal + ) = (__sf_section_package) +end + +test "Prints section when package.json is present" + ( + echo "{\"version\": \"1.0\"}" > /tmp/tmp-spacefish/package.json + + set_color --bold + echo -n "is " + set_color normal + set_color --bold red + echo -n "๐Ÿ“ฆ v1.0" + set_color normal + set_color --bold + echo -n " " + set_color normal + ) = (__sf_section_package) +end + +test "Changing SPACEFISH_PACKAGE_SUFFIX changes the character suffix" + ( + touch /tmp/tmp-spacefish/Cargo.toml + set SPACEFISH_PACKAGE_SUFFIX ยท + + set_color --bold + echo -n "is " + set_color normal + set_color --bold red + echo -n "๐Ÿ“ฆ v0.1.0" + set_color normal + set_color --bold + echo -n "ยท" + set_color normal + ) = (__sf_section_package) +end + +test "Does not print section when Cargo.toml or package.json is not present" + () = (__sf_section_package) +end + +test "Doesn't display the section when SPACEFISH_PACKAGE_SHOW is set to \"false\"" + ( + touch /tmp/tmp-spacefish/Cargo.toml + set SPACEFISH_PACKAGE_SHOW false + ) = (__sf_section_package) +end