Skip to content

Commit

Permalink
feat: Upsearch for project files up the tree
Browse files Browse the repository at this point in the history
  • Loading branch information
denysdovhan committed May 13, 2022
1 parent 9b97651 commit 08f6d70
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 21 deletions.
15 changes: 15 additions & 0 deletions lib/utils.zsh
Expand Up @@ -126,20 +126,35 @@ spaceship::union() {
# $ spaceship::upsearch package.json node_modules
# > /home/username/path/to/project/node_modules
spaceship::upsearch() {
# Parse CLI options
zparseopts -E -D \
s=silent -silent=silent

local files=("$@")
local root="$(pwd -P)"

# Go up to the root
while [ "$root" ]; do
# For every file as an argument
for file in "${files[@]}"; do
local filepath="$root/$file"
if [[ -e "$filepath" ]]; then
if [[ -z "$silent" ]]; then
echo "$filepath"
fi
return 0
elif [[ -d .git || -d .hg ]]; then
# If we reached the root of repo, return non-zero
return 1
fi
done

# Go one level up
root="${root%/*}"
done

# If we reached the root, return non-zero
return 1
}
return 1
}
5 changes: 3 additions & 2 deletions sections/docker.zsh
Expand Up @@ -56,11 +56,12 @@ spaceship_docker() {
local docker_context="$(spaceship_docker_context)"

# Show Docker status only for Docker-specific folders or when connected to external host
[[ "$compose_exists" == true || -f Dockerfile || -f docker-compose.yml || -f /.dockerenv || -n $docker_context ]] || return
local is_docker_project="$(spaceship::upsearch Dockerfile docker-compose.yml)"
[[ "$compose_exists" == true || -n "$is_docker_project" || -f /.dockerenv || -n $docker_context ]] || return

# if docker daemon isn't running you'll get an error saying it can't connect
# Note: Declaration and assignment is separated for correctly getting the exit code
local 'docker_version'
local docker_version
docker_version=$(docker version -f "{{.Server.Version}}" 2>/dev/null)
[[ $? -ne 0 || -z $docker_version ]] && return

Expand Down
3 changes: 2 additions & 1 deletion sections/dotnet.zsh
Expand Up @@ -25,7 +25,8 @@ SPACESHIP_DOTNET_COLOR="${SPACESHIP_DOTNET_COLOR="128"}"
spaceship_dotnet() {
[[ $SPACESHIP_DOTNET_SHOW == false ]] && return

[[ -f project.json || -f global.json || -f paket.dependencies || -n *.(cs|fs|x)proj(#qN^/) || -n *.sln(#qN^/) ]] || return
local is_dotnet_project="$(spaceship::upsearch project.json global.json paket.dependencies)"
[[ -n "$is_dotnet_project" || -n *.(cs|fs|x)proj(#qN^/) || -n *.sln(#qN^/) ]] || return

spaceship::exists dotnet || return

Expand Down
3 changes: 2 additions & 1 deletion sections/elm.zsh
Expand Up @@ -23,7 +23,8 @@ SPACESHIP_ELM_COLOR="${SPACESHIP_ELM_COLOR="cyan"}"
spaceship_elm() {
[[ $SPACESHIP_ELM_SHOW == false ]] && return

[[ -f elm.json || -f elm-package.json || -d elm-stuff || -n *.elm(#qN^/) ]] || return
local is_elm_project="$(spaceship::upsearch elm.json elm-package.json elm-stuff)"
[[ -n "$is_elm_project" || -n *.elm(#qN^/) ]] || return

spaceship::exists elm || return

Expand Down
4 changes: 2 additions & 2 deletions sections/golang.zsh
Expand Up @@ -24,8 +24,8 @@ spaceship_golang() {
[[ $SPACESHIP_GOLANG_SHOW == false ]] && return

# If there are Go-specific files in current directory, or current directory is under the GOPATH
[[ -f go.mod || -d Godeps || -f glide.yaml || -n *.go(#qN^/) || -f Gopkg.toml || -f Gopkg.lock \
|| ( $GOPATH && "$PWD/" =~ "$GOPATH/" ) ]] || return
local is_go_project="$(spaceship::upsearch go.mod Godeps glide.yaml Gopkg.toml Gopkg.lock)"
[[ -n "$is_go_project" || ( $GOPATH && "$PWD/" =~ "$GOPATH/" ) || -n *.go(#qN^/) ]] || return

spaceship::exists go || return

Expand Down
4 changes: 2 additions & 2 deletions sections/haskell.zsh
Expand Up @@ -23,8 +23,8 @@ SPACESHIP_HASKELL_COLOR="${SPACESHIP_HASKELL_COLOR="red"}"
spaceship_haskell() {
[[ $SPACESHIP_HASKELL_SHOW == false ]] && return

# If there are stack files in current directory
[[ -f stack.yaml ]] || return
# If there are stack files in the context
spaceship::upsearch stack.yaml &>/dev/null || return

# The command is stack, so do not change this to haskell.
spaceship::exists stack || return
Expand Down
5 changes: 3 additions & 2 deletions sections/node.zsh
Expand Up @@ -25,9 +25,10 @@ spaceship_node() {
[[ $SPACESHIP_NODE_SHOW == false ]] && return

# Show NODE status only for JS-specific folders
[[ -f package.json || -d node_modules || -n *.js(#qN^/) ]] || return
local is_node_project="$(spaceship::upsearch package.json node_modules)"
[[ -n "$is_node_project" || -n *.js(#qN^/) ]] || return

local 'node_version'
local node_version

if spaceship::exists fnm; then
node_version=$(fnm current 2>/dev/null)
Expand Down
3 changes: 2 additions & 1 deletion sections/php.zsh
Expand Up @@ -24,7 +24,8 @@ spaceship_php() {
[[ $SPACESHIP_PHP_SHOW == false ]] && return

# Show only if php files or composer.json exist in current directory
[[ -n *.php(#qN^/) || -f composer.json ]] || return
local is_php_project="$(spaceship::upsearch composer.json)"
[[ -n "$is_php_project" || -n *.php(#qN^/) ]] || return

spaceship::exists php || return

Expand Down
5 changes: 3 additions & 2 deletions sections/python.zsh
Expand Up @@ -24,9 +24,10 @@ spaceship_python() {
[[ $SPACESHIP_PYTHON_SHOW == false ]] && return

# Show python version only in directories with relevant files
[[ -f requirements.txt || -f Pipfile || -f pyproject.toml || -n *.py(#qN^/) ]] || return
local is_python_project="$(spaceship::upsearch requirements.txt Pipfile pyproject.toml)"
[[ -n "$is_python_project" || -n *.py(#qN^/) ]] || return

local 'py_version'
local py_version

if [[ -n "$VIRTUAL_ENV" ]] || [[ $SPACESHIP_PYTHON_SHOW == always ]]; then
py_version=${(@)$(python -V 2>&1)[2]}
Expand Down
5 changes: 3 additions & 2 deletions sections/ruby.zsh
Expand Up @@ -24,9 +24,10 @@ spaceship_ruby() {
[[ $SPACESHIP_RUBY_SHOW == false ]] && return

# Show versions only for Ruby-specific folders
[[ -f Gemfile || -f Rakefile || -n *.rb(#qN^/) ]] || return
local is_ruby_project="$(spaceship::upsearch Gemfile Rakefile)"
[[ -n "$is_ruby_project" || -n *.rb(#qN^/) ]] || return

local 'ruby_version'
local ruby_version

if spaceship::exists rvm-prompt; then
ruby_version=$(rvm-prompt i v g)
Expand Down
3 changes: 2 additions & 1 deletion sections/rust.zsh
Expand Up @@ -25,7 +25,8 @@ spaceship_rust() {
[[ $SPACESHIP_RUST_SHOW == false ]] && return

# If there are Rust-specific files in current directory
[[ -f Cargo.toml || -n *.rs(#qN^/) ]] || return
local is_rust_project="$(spaceship::upsearch Cargo.toml)"
[[ -n "$is_rust_project" || -n *.rs(#qN^/) ]] || return

spaceship::exists rustc || return

Expand Down
2 changes: 1 addition & 1 deletion sections/swift.zsh
Expand Up @@ -24,7 +24,7 @@ SPACESHIP_SWIFT_COLOR="${SPACESHIP_SWIFT_COLOR="yellow"}"
spaceship_swift() {
spaceship::exists swiftenv || return

local 'swift_version'
local swift_version

if [[ $SPACESHIP_SWIFT_SHOW_GLOBAL == true ]] ; then
swift_version=$(swiftenv version | sed 's/ .*//')
Expand Down
2 changes: 1 addition & 1 deletion sections/terraform.zsh
Expand Up @@ -28,7 +28,7 @@ spaceship_terraform() {
spaceship::exists terraform || return

# Show Terraform Workspaces when exists
[[ -f .terraform/environment ]] || return
spaceship::upsearch .terraform/environment || return

local terraform_workspace=$(<.terraform/environment)
[[ -z $terraform_workspace ]] && return
Expand Down
2 changes: 1 addition & 1 deletion sections/user.zsh
Expand Up @@ -33,7 +33,7 @@ spaceship_user() {
|| [[ $UID == 0 ]] \
|| [[ $SPACESHIP_USER_SHOW == true && -n $SSH_CONNECTION ]]
then
local 'user_color'
local user_color

if [[ $USER == 'root' ]]; then
user_color=$SPACESHIP_USER_COLOR_ROOT
Expand Down
2 changes: 1 addition & 1 deletion sections/venv.zsh
Expand Up @@ -28,7 +28,7 @@ spaceship_venv() {
# Check if the current directory running via Virtualenv
[ -n "$VIRTUAL_ENV" ] || return

local 'venv'
local venv

if [[ "${SPACESHIP_VENV_GENERIC_NAMES[(i)$VIRTUAL_ENV:t]}" -le \
"${#SPACESHIP_VENV_GENERIC_NAMES}" ]]
Expand Down
2 changes: 1 addition & 1 deletion sections/xcode.zsh
Expand Up @@ -24,7 +24,7 @@ SPACESHIP_XCODE_COLOR="${SPACESHIP_XCODE_COLOR="blue"}"
spaceship_xcode() {
spaceship::exists xcenv || return

local 'xcode_path'
local xcode_path

if [[ $SPACESHIP_XCODE_SHOW_GLOBAL == true ]] ; then
xcode_path=$(xcenv version | sed 's/ .*//')
Expand Down

0 comments on commit 08f6d70

Please sign in to comment.