Skip to content

Commit

Permalink
feat: add cargo package manager and refactor existing package section (
Browse files Browse the repository at this point in the history
…matchai#171)

✨🔨 Refactor package to include cargo, and use jq and python for node
  • Loading branch information
tiffafoo authored and adamsimp committed May 6, 2019
1 parent 628eab6 commit 22c4250
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
7 changes: 5 additions & 2 deletions docs/Options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion functions/__sf_lib_section.fish
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 34 additions & 6 deletions functions/__sf_section_package.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# Current package version.
# These package managers supported:
# * NPM
# * Cargo

function __sf_section_package -d "Display the local package version"
# ------------------------------------------------------------------------------
Expand All @@ -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 ⚠
Expand Down
72 changes: 72 additions & 0 deletions tests/__sf_section_package.test.fish
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 22c4250

Please sign in to comment.