Commit db1b6887 authored by Bruno's avatar Bruno

Merge pull request #82 from derekstavis/install-as-fish

Migrate install script from bash to fish
parents e40f2d3c 8c394771
...@@ -9,7 +9,7 @@ env: ...@@ -9,7 +9,7 @@ env:
- OMF_REPO_BRANCH="$TRAVIS_BRANCH" - OMF_REPO_BRANCH="$TRAVIS_BRANCH"
before_install: before_install:
- export OMF_REPO_BRANCH=`tools/branch-name.sh` - source tools/travis-github-pr-integration.sh
- docker build -t fish . < Dockerfile - docker build -t fish . < Dockerfile
before_script: before_script:
......
#!/bin/sh #!/usr/bin/env fish
# #
# USAGE # USAGE
# #1: curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | sh # #1: curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | source -
# #2: curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install > install && chmod +x install && ./install # #2: curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install > install; and source install
# #3: OMF_CONFIG=~/.omf curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | sh # #3: env OMF_CONFIG=~/.omf curl -L github.com/oh-my-fish/oh-my-fish/raw/master/bin/install | source -
# #
# ENV # ENV
# XDG_DATA_HOME Base directory (~/.local/share) # XDG_DATA_HOME Base directory (~/.local/share)
# XDG_CONFIG_HOME Base configuration directory (~/.config) # XDG_CONFIG_HOME Base configuration directory (~/.config)
# #
# ↑ See XDG Base Directory Specification # - See XDG Base Directory Specification:
# https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html # - https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
# #
# OMF_PATH Oh My Fish directory # FISH_CONFIG Fish shell configuration file
# OMF_CONFIG Oh My Fish configuration
# #
# OMF_REPO_URI Source git repository # OMF_PATH Oh My Fish installation directory
# OMF_REPO_BRANCH Source repository default branch (master) # OMF_CONFIG Oh My Fish configuration directory
# #
# FUNCTIONS # OMF_REPO_URI Oh My Fish source git repository
# die # OMF_REPO_BRANCH Oh My Fish source repository branch
# is_installed
# omf_create_fish_config <path/to/fish.config>
# omf_install set -q XDG_DATA_HOME; or set XDG_DATA_HOME "$HOME/.local/share"
set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME "$HOME/.config"
test -z ${XDG_DATA_HOME+_} && XDG_DATA_HOME="${HOME}/.local/share"
test -z ${XDG_CONFIG_HOME+_} && XDG_CONFIG_HOME="${HOME}/.config" set -q FISH_CONFIG; or set FISH_CONFIG "$XDG_CONFIG_HOME/fish"
test -z ${OMF_PATH+_} && OMF_PATH="${XDG_DATA_HOME}/omf" set -q OMF_PATH; or set OMF_PATH "$XDG_DATA_HOME/omf"
test -z ${OMF_CONFIG+_} && OMF_CONFIG="${XDG_CONFIG_HOME}/omf" set -q OMF_CONFIG; or set OMF_CONFIG "$XDG_CONFIG_HOME/omf"
test -z ${OMF_REPO_URI+_} && OMF_REPO_URI="https://github.com/oh-my-fish/oh-my-fish" set -q OMF_REPO_URI; or set OMF_REPO_URI "https://github.com/oh-my-fish/oh-my-fish"
test -z ${OMF_REPO_BRANCH+_} && OMF_REPO_BRANCH="master" set -q OMF_REPO_BRANCH; or set OMF_REPO_BRANCH "master"
die() { set OMF_FISH_MIN_VER 2 1 0
echo "$1" && exit 1
} function available -a name
type "$name" >/dev/null 2>&1
is_installed() { end
type "$1" >/dev/null 2>&1
}
omf_create_fish_config() {
local fish_config_file=$1
mkdir -p $(dirname "${fish_config_file}")
touch "${fish_config_file}"
}
omf_install() {
echo "Resolving Oh My Fish path → ${OMF_PATH}"
test -d "${OMF_PATH}" && die "Existing installation detected, aborting"
local git_uri="$(echo ${OMF_REPO_URI} | sed 's/\.git//').git"
echo "Cloning Oh My Fish → ${git_uri}"
if ! git clone -q --depth 1 -b "${OMF_REPO_BRANCH}" "${git_uri}" "${OMF_PATH}"; then
echo "Is 'git' installed?"
die "Could not clone the repository → ${OMF_PATH}:${OMF_REPO_BRANCH}"
fi
local git_rev=$(git --git-dir ${OMF_PATH}/.git --work-tree ${OMF_PATH} rev-parse HEAD) >/dev/null 2>&1
local git_upstream=$(git --git-dir ${OMF_PATH}/.git --work-tree ${OMF_PATH} config remote.upstream.url)
if [ -z "${git_upstream}" ]; then
git --git-dir ${OMF_PATH}/.git --work-tree ${OMF_PATH} remote add upstream ${git_uri}
else
git --git-dir ${OMF_PATH}/.git --work-tree ${OMF_PATH} remote set-url upstream ${git_uri}
fi
echo "Oh My Fish revision id → ${git_rev}" function report -a what message
test -z ${FISH_CONFIG+_} && FISH_CONFIG="${XDG_CONFIG_HOME}/fish" switch $what
case 'progress'
set_color yellow
case 'success'
set_color green
case 'error'
set_color red
printf "$message\n"
exit 1
end
local fish_config_file="${FISH_CONFIG}/config.fish" printf "$message\n"
end
if [ -e "${FISH_CONFIG}/config.fish" ]; then function fish_version_compatible
local timestamp=$(date +%s) set -l major (echo $version | cut -d. -f1)
local fish_config_bk="${FISH_CONFIG}/config.${timestamp}.copy" set -l minor (echo $version | cut -d. -f2)
echo "Found existing 'fish' configuration → ${fish_config_file}" return (test $major = $OMF_FISH_MIN_VER[1] -a $minor -ge $OMF_FISH_MIN_VER[2])
echo "Writing back-up copy → ${fish_config_bk}" end
cp "${fish_config_file}" "${fish_config_bk}" >/dev/null 2>&1 function install_omf
test $? -ne 0 && die "Writing back-up copy failed, error code → ${?}" # Grant repository URL ends with .git
set git_uri (echo $OMF_REPO_URI | sed 's/\.git//').git
report progress "Cloning $OMF_REPO_BRANCH from $git_uri..."
if not git clone -q --depth 1 -b $OMF_REPO_BRANCH $git_uri "$OMF_PATH"
report error "Error cloning repository!"
end
set git_upstream (git --git-dir "$OMF_PATH/.git" --work-tree "$OMF_PATH" config remote.upstream.url)
if test -z "$git_upstream"
git --git-dir "$OMF_PATH/.git" --work-tree "$OMF_PATH" remote add upstream $git_uri
else else
omf_create_fish_config $fish_config_file git --git-dir "$OMF_PATH/.git" --work-tree "$OMF_PATH" remote set-url upstream $git_uri
fi end
echo "Adding Oh My Fish bootstrap → ${fish_config_file}" set fish_config_file "$FISH_CONFIG/config.fish"
touch ${fish_config_file} >/dev/null 2>&1
test ! -w ${fish_config_file} && die "Fish configuration file is not writable, aborting." if test -e "$fish_config_file"
set -l timestamp (date +%s)
sed "s|{{OMF_PATH}}|$(echo "${OMF_PATH}" | sed -e "s|$HOME|\$HOME|")|" \ set -l original_fish_config_file "$FISH_CONFIG/config.$timestamp.copy"
"${OMF_PATH}/templates/config.fish" > "${fish_config_file}"
report progress "Existent fish config file found at $fish_config_file"
if [ ! -d "${OMF_CONFIG}" ]; then report progress "↳ Moving file to $original_fish_config_file"
echo "Writing Oh My Fish configuration → ${OMF_CONFIG}"
mkdir -p "${OMF_CONFIG}" if not mv "$fish_config_file" "$original_fish_config_file" 2>/dev/null
test -f "${OMF_CONFIG}/bundle" || echo "theme default" > "${OMF_CONFIG}/bundle" report error "Aborting: Could not backup fish config file"
test -f "${OMF_CONFIG}/theme" || echo default > "${OMF_CONFIG}/theme" end
test -f "${OMF_CONFIG}/revision" || echo ${git_rev} > "${OMF_CONFIG}/revision"
else else
fish -c "omf install" mkdir -p "$FISH_CONFIG"
fi end
}
report progress "Adding startup code to fish config file..."
echo "Installing Oh My Fish..."
! is_installed "fish" && die "Please install fish to continue → http://fishshell.com/" set template "templates/config.fish"
if omf_install; then set replacements "s|{{OMF_PATH}}|$OMF_PATH|;s|{{OMF_CONFIG}}|$OMF_CONFIG|"
echo "Oh My Fish successfully installed."
cd $HOME if test "$OMF_CONFIG" != "$XDG_CONFIG_HOME/omf"
# Do not swap process if running in a CI environment. set replacements "$replacements;s|#set|set|"
[ -z ${CI+_} ] || exit 0 && exec "fish" < /dev/tty end
else
die "Oh My Fish couldn't install, but you can complain here → github.com/oh-my-fish/oh-my-fish/issues" sed "$replacements" "$OMF_PATH/$template" > "$fish_config_file"
fi
report progress "Building Oh My Fish configuration..."
if not test -d "$OMF_CONFIG"
mkdir -p "$OMF_CONFIG"
end
test -f "$OMF_CONFIG/bundle"; or echo "theme default" > "$OMF_CONFIG/bundle"
test -f "$OMF_CONFIG/theme"; or echo "default" > "$OMF_CONFIG/theme"
source "$OMF_PATH/init.fish"
omf.bundle.install
end
function main
report progress "Installing Oh My Fish to $OMF_PATH..."
if test -d "$OMF_PATH"
report error "Aborting: Existing installation detected."
end
if not available git
report error "Aborting: Installation requires Git."
end
if not fish_version_compatible
set -l minimum_version_string (echo $OMF_FISH_MIN_VER | sed 's/ /./g')
report error "Aborting: Detected fish $version, but Oh My Fish requires fish $minimum_version_string or greater."
end
if install_omf
report success "Installation successful! Reload your shell to start using Oh My Fish."
else
report error "Oh My Fish installation failed.\n\nIf you think that it's a bug, please open an\nissue with the complete installation log here:\n\nhttp://github.com/oh-my-fish/oh-my-fish/issues"
end
exit 0
end
main
# Path to your oh-my-fish. # Path to Oh My Fish install.
set -g OMF_PATH {{OMF_PATH}} set -gx OMF_PATH {{OMF_PATH}}
### Configuration required to load oh-my-fish ### # Customize Oh My Fish configuration path.
# Note: Only add configurations that are required to be set before oh-my-fish is loaded. #set -gx OMF_CONFIG {{OMF_CONFIG}}
# For common configurations, we advise you to add them to your $OMF_CONFIG/init.fish
# file or to create a custom plugin instead.
# Load oh-my-fish configuration. # Load oh-my-fish configuration.
source $OMF_PATH/init.fish source $OMF_PATH/init.fish
#!/usr/bin/env sh
URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST
BRANCH_NAME=`curl -s $URL ^/dev/null \
| grep -C 2 "head" \
| grep \"ref\" \
| cut -d':' -f2- \
| sed -e 's/["|,]//g;s/^[ \t]//g'
`
if [ -n "$BRANCH_NAME" ]; then
echo $BRANCH_NAME
else
echo $TRAVIS_BRANCH
fi
# Return if we are not in a Pull Request
[[ "$TRAVIS_PULL_REQUEST" = "false" ]] && return
GITHUB_PR_URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST
GITHUB_PR_BODY=$(curl -s $GITHUB_PR_URL 2>/dev/null)
if [[ $GITHUB_PR_BODY =~ \"ref\":\ *\"([a-zA-Z0-9_-]*)\" ]]; then
export OMF_REPO_BRANCH=${BASH_REMATCH[1]}
fi
if [[ $GITHUB_PR_BODY =~ \"repo\":.*\"clone_url\":\ *\"(https://github\.com/[a-zA-Z0-9_-]*/[a-zA-Z0-9_-]*\.git).*\"base\" ]]; then
export OMF_REPO_URI=${BASH_REMATCH[1]}
fi
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment