Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: multi-runtime (take two) #150

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ jobs:
with:
check_together: 'yes'
ignore_paths: test/_helpers/bats-assert test/_helpers/bats-file test/_helpers/bats-support
# unsure why action picks up zsh scripts, but it shouldn't
ignore_names: runtime.zsh

- name: Build compa and environment
- name: Build milpa and environment
run: |
version="test-$(date -u "+%s")"
echo "TEST_MILPA_VERSION=$version" >> $GITHUB_ENV
echo "MILPA_ROOT=$(pwd)" >> $GITHUB_ENV
echo "MILPA_PATH=$(pwd)/internal" >> $GITHUB_ENV
echo "MILPA_UPDATE_CHECK_DISABLED=1" >> $GITHUB_ENV
echo "BATS_LIB_PATH=$(pwd)/test/_helpers" >> $GITHUB_ENV
go build -ldflags "-s -w -X main.version=${version}" -o compa -cover
go build -ldflags "-X main.version=${version}" -tags coverage -o milpa -cover

- name: Unit tests
run: |
Expand All @@ -53,6 +55,7 @@ jobs:

- name: Integration tests
run: |
sudo apt-get install zsh fish
$(pwd)/milpa dev test integration --coverage

- name: milpa doctor
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
sudo mv htmlq /usr/local/bin/htmlq
echo "::endgroup::"

echo "::group::build compa"
go build -ldflags "-s -w -X main.version=CI" -o compa
echo "::group::build milpa locally"
go build -ldflags "-s -w -X main.version=CI" -o milpa
echo "::endgroup::"

echo "::group::Release notes"
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
test/coverage.*
test/coverage
!.milpa
milpa
2 changes: 1 addition & 1 deletion .milpa/commands/itself/command-tree.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function get_tree () {
args=( "--template=${2}" )
fi
args+=( "${MILPA_ARG_PREFIX[@]}" )
"$MILPA_COMPA" __command_tree \
"$MILPA" __command_tree \
--depth "$MILPA_OPT_DEPTH" \
--format "$1" \
"${args[@]}"
Expand Down
2 changes: 1 addition & 1 deletion .milpa/commands/itself/command-tree.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ arguments:
variadic: true
values:
suggest-only: true
script: 'compa __command_tree --format autocomplete{{ if (index .Args "prefix") }} {{ Arg "prefix" }}{{ end }}'
script: 'milpa __command_tree --format autocomplete{{ if (index .Args "prefix") }} {{ Arg "prefix" }}{{ end }}'
options:
depth:
default: "15"
Expand Down
44 changes: 32 additions & 12 deletions .milpa/commands/itself/create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@ fi
command_name=${MILPA_ARG_NAME[*]}
command_path="$repo_path/commands/${command_name// //}"

[[ "${MILPA_OPT_EXECUTABLE}" ]] || command_path="$command_path.sh"
function createOrExit() {
[[ -f "$1" ]] && @milpa.fail "Command already exists at $1"

[[ -f "$command_path" ]] && @milpa.fail "Command already exists at $command_path"
@milpa.log info "Creating command $(@milpa.fmt bold "${command_name}") at $1"
mkdir -p "$(dirname "$1")"
}

@milpa.log info "Creating command $(@milpa.fmt bold "${command_name}") at $command_path"
mkdir -p "$(dirname "$command_path")"

if [[ "${MILPA_OPT_EXECUTABLE}" ]]; then
touch "$command_path" || @milpa.fail "could not create $command_path"
chmod +x "$command_path"
else
echo "#!/usr/bin/env bash" >> "$command_path" || @milpa.fail "could not create $command_path"
fi
case "${MILPA_OPT_KIND}" in
bash)
spec="${command_path}.yaml"
command_path="$command_path.sh"
createOrExit "$command_path"
echo "#!/usr/bin/env bash" >> "$command_path" || @milpa.fail "could not create $command_path"
;;
zsh)
spec="${command_path}.yaml"
command_path="$command_path.zsh"
createOrExit "$command_path"
echo "#!/usr/bin/env zsh" >> "$command_path" || @milpa.fail "could not create $command_path"
;;
fish)
spec="${command_path}.yaml"
command_path="$command_path.fish"
createOrExit "$command_path"
echo "#!/usr/bin/env fish" >> "$command_path" || @milpa.fail "could not create $command_path"
;;
executable)
createOrExit "$command_path"
touch "$command_path" || @milpa.fail "could not create $command_path"
chmod +x "$command_path"
spec="${command_path}.yaml"
;;
esac

# shellcheck disable=2001
cat > "${command_path%.sh}.yaml" <<YAML
cat > "$spec" <<YAML
# see \`milpa help docs command spec\` for all the options
summary: ${MILPA_OPT_SUMMARY}
description: |
Expand Down
11 changes: 7 additions & 4 deletions .milpa/commands/itself/create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ arguments:
variadic: true
values:
suggest-only: true
script: compa __command_tree --format autocomplete{{ if (index .Args "name") }} {{ Arg "name" }}{{ end }}
script: milpa __command_tree --format autocomplete{{ if (index .Args "name") }} {{ Arg "name" }}{{ end }}
options:
summary:
description: a short summary of what this command does
Expand All @@ -19,9 +19,12 @@ options:
open:
type: bool
description: Open the script file in your current $EDITOR after creation
executable:
type: bool
description: Create an empty, executable command. Useful when you'd like using something other than bash
kind:
type: string
description: The kind of command to create
default: bash
values:
static: [executable, bash, fish, zsh]
repo:
type: string
description: a path to the milpa repo to create this command in. By default, the nearest .milpa directory from `pwd` and up
Expand Down
10 changes: 5 additions & 5 deletions .milpa/commands/itself/install-autocomplete.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ case "$SHELL" in
if [[ -d "$dst" ]]; then
@milpa.log info "Found completion dir at $dst"
if [[ -w "$dst" ]]; then
"$MILPA_COMPA" __generate_completions bash > "$dst/milpa" || @milpa.fail "Could not install completion script"
"$MILPA" __generate_completions bash > "$dst/milpa" || @milpa.fail "Could not install completion script"
else
@milpa.log warning "$dst does not look writeable for user $USER, using sudo"
"$MILPA_COMPA" __generate_completions bash | sudo tee "$dst/milpa" || @milpa.fail "Could not install completion script"
"$MILPA" __generate_completions bash | sudo tee "$dst/milpa" || @milpa.fail "Could not install completion script"
fi
@milpa.log success "Installed completion script to $dst/milpa"
installed="true"
Expand All @@ -42,11 +42,11 @@ case "$SHELL" in
@milpa.log info "Installing completions to $dst"
if [[ -w "$dst" ]]; then
[[ -f "$dst" ]] || mkdir -pv "$dst"
"$MILPA_COMPA" __generate_completions zsh > "${dst}/_milpa" || @milpa.fail "Could not install completions"
"$MILPA" __generate_completions zsh > "${dst}/_milpa" || @milpa.fail "Could not install completions"
else
@milpa.log warning "$dst does not look writeable for user $USER, using sudo"
[[ -f "$dst" ]] || sudo mkdir -pv "$dst"
"$MILPA_COMPA" __generate_completions zsh | sudo tee "${dst}/_milpa" >/dev/null || @milpa.fail "Could not install completions"
"$MILPA" __generate_completions zsh | sudo tee "${dst}/_milpa" >/dev/null || @milpa.fail "Could not install completions"
fi

if ! zsh -c "source ~/.zshrc 2>/dev/null; command -v compinit >/dev/null" >/dev/null 2>&1; then
Expand All @@ -60,7 +60,7 @@ then reloading your shell'
*fish)
@milpa.log info "Fish detected"
dst="$HOME/.config/fish/completions/milpa.fish"
"$MILPA_COMPA" __generate_completions fish > "$dst" || @milpa.fail "Could not install completions"
"$MILPA" __generate_completions fish > "$dst" || @milpa.fail "Could not install completions"
;;
*)
@milpa.fail "No completion script found for shell $SHELL"
Expand Down
2 changes: 1 addition & 1 deletion .milpa/commands/itself/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Roberto Hidalgo <milpa@un.rob.mx>

"$MILPA_COMPA" __version 2>&1 || {
"$MILPA" __version 2>&1 || {
if [[ "$?" != 42 ]]; then
@milpa.fail "could not get version"
fi
Expand Down
2 changes: 1 addition & 1 deletion .milpa/docs/milpa/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ There's a few environment variables that control the behavior of `milpa`.

### `MILPA_ROOT`

`MILPA_ROOT` points to the installed milpa _kernel_, by default `/usr/local/lib/milpa`. This folder contains a milpa repo, the `milpa` executable, and a helper binary named `compa`, along a copy of the license and the source repo's README.
`MILPA_ROOT` points to the installed milpa _kernel_, by default `/usr/local/lib/milpa`. This folder contains a the built-in milpa repo, the `milpa` executable, and a copy of the license and the source repo's README.

You can set it to a local installation, like a fork, and run `$MILPA_ROOT/milpa` to use that fork's scripts instead of an installed version.

Expand Down
42 changes: 42 additions & 0 deletions .milpa/runtime.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Roberto Hidalgo <milpa@un.rob.mx>

function @milpa.load_util () {
# shell scripts can call @milpa.load_util to load utils from MILPA_ROOT
# or the current MILPA_COMMAND_REPO
local env_name
for util_name in "$@"; do
env_name="_MILPA_UTIL_${util_name//-/_}"
if [[ "${!env_name}" == "1" ]]; then
# util already loaded
continue
fi

global="$MILPA_ROOT/.milpa"
util_path="${global}/util/$util_name.sh"
if [[ ! -f "$util_path" ]] && [[ "$MILPA_COMMAND_REPO" != "" ]]; then
util_path="$MILPA_COMMAND_REPO/util/$util_name.sh"
fi

if ! [[ -f "$util_path" ]]; then
# util not found
>&2 echo "Missing util named $util_name in ${MILPA_COMMAND_REPO}"
exit 70 # programmer error
fi

set -o allexport
# shellcheck disable=1090
source "$util_path"
set +o allexport
export "${env_name?}=1"
break
done
}

@milpa.load_util log
function @milpa.fail () {
# print an error, then exit
@milpa.log error "$*"
exit 2
}
34 changes: 34 additions & 0 deletions .milpa/runtime.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env fish
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Roberto Hidalgo <milpa@un.rob.mx>

function @milpa.load_util
for util_name in $argv
set env_name "_MILPA_UTIL_$(string replace --all "-" "_" "$util_name")"
if set -q "$env_name"
echo "util $util_name already loaded" >&2
continue
end

set --local util_path "$MILPA_ROOT/.milpa/util/$util_name.fish"
if test ! -f "$util_path" -a "$MILPA_COMMAND_REPO" != ""
set util_path "$MILPA_COMMAND_REPO/util/$util_name.fish"
end

if ! test -f "$util_path"
echo "Missing util named $util_name, add to $util_path" >&2
exit 70
end

source "$util_path"
set -g "$env_name" 1
end
end

@milpa.load_util log

function @milpa.fail
# print an error, then exit
@milpa.log error $argv
exit 2
end
42 changes: 42 additions & 0 deletions .milpa/runtime.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0
# Copyright © 2021 Roberto Hidalgo <milpa@un.rob.mx>

function @milpa.load_util () {
# shell scripts can call @milpa.load_util to load utils from MILPA_ROOT
# or the current MILPA_COMMAND_REPO
local env_name
for util_name in "$@"; do
env_name="_MILPA_UTIL_${util_name//-/_}"
if [[ "${(P)env_name}" == "1" ]]; then
# util already loaded
continue
fi

global="$MILPA_ROOT/.milpa"
util_path="${global}/util/$util_name.sh"
if [[ ! -f "$util_path" ]] && [[ "$MILPA_COMMAND_REPO" != "" ]]; then
util_path="$MILPA_COMMAND_REPO/util/$util_name.sh"
fi

if ! [[ -f "$util_path" ]]; then
# util not found
>&2 echo "Missing util named $util_name in ${MILPA_COMMAND_REPO}"
exit 70 # programmer error
fi

set -o allexport
# shellcheck disable=1090
source "$util_path"
set +o allexport
export "${env_name}"="1"
break
done
}

@milpa.load_util log
function @milpa.fail () {
# print an error, then exit
@milpa.log error "$*"
exit 2
}
Loading
Loading