Skip to content

Commit

Permalink
feat: support generating gitlab hosted plugins (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
hickey authored Jul 5, 2021
1 parent 0fefe82 commit 2e64f8d
Show file tree
Hide file tree
Showing 9 changed files with 291 additions and 10 deletions.
127 changes: 117 additions & 10 deletions setup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -euo pipefail
HELP="
Usage:
bash $0 PLUGIN_NAME TOOL_TEST GH_USER AUTHOR_NAME TOOL_GH TOOL_PAGE LICENSE
bash [--github | --gitlab] $0 PLUGIN_NAME TOOL_TEST GH_USER AUTHOR_NAME TOOL_GH TOOL_PAGE LICENSE
All arguments are optional and will be interactively prompted when not given.
Expand All @@ -17,7 +17,7 @@ TOOL_TEST.
Normallly this command is something taking \`--version\` or \`--help\`.
GH_USER.
Your GitHub username.
Your GitHub/GitLab username.
AUTHOR_NAME.
Your name, used for licensing.
Expand Down Expand Up @@ -110,8 +110,8 @@ set_placeholder() {
done
}

setup() {
local cwd out tool_name tool_repo check_command author_name github_username tool_homepage ok
setup_github() {
local cwd out tool_name tool_repo check_command author_name github_username tool_homepage ok primary_branch

cwd="$PWD"
out="$cwd/out"
Expand All @@ -129,6 +129,8 @@ setup() {
license_keyword="${7:-$(ask_license)}"
license_keyword="$(echo "$license_keyword" | tr '[:upper:]' '[:lower:]')"

primary_branch="$(git rev-parse --abbrev-ref HEAD)"

cat <<-EOF
Setting up plugin: asdf-$tool_name
Expand All @@ -141,7 +143,7 @@ $tool_name github: $tool_repo
$tool_name docs: $tool_homepage
$tool_name test: \`$check_command\`
After confirmation, the \`master\` will be replaced with the generated
After confirmation, the \`$primary_branch\` will be replaced with the generated
template using the above information. Please ensure all seems correct.
EOF

Expand Down Expand Up @@ -173,31 +175,136 @@ EOF
set_placeholder "<TOOL CHECK>" "$check_command" "$out"
set_placeholder "<YOUR NAME>" "$author_name" "$out"
set_placeholder "<YOUR GITHUB USERNAME>" "$github_username" "$out"
set_placeholder "<PRIMARY BRANCH>" "$primary_branch" "$out"

git add "$out"
# remove GitLab specific files
git rm -rf "$out/.gitlab" "$out/.gitlab-ci.yml" "$out/README-gitlab.md" "$out/contributing-gitlab.md"
# rename GitHub specific files to final filenames
git mv "$out/README-github.md" "$out/README.md"
git mv "$out/contributing-github.md" "$out/contributing.md"
git commit -m "Generate asdf-$tool_name plugin from template."

cd "$cwd"
git branch -M out master
git branch -M out "$primary_branch"
git worktree remove -f out
git checkout -f master
git checkout -f "$primary_branch"

echo "All done."
echo "Your master branch has been reset to an initial commit."
echo "You might want to push using \`--force-with-lease\` to origin/master"
echo "Your $primary_branch branch has been reset to an initial commit."
echo "You might want to push using \`--force-with-lease\` to origin/$primary_branch"

echo "Showing pending TODO tags that you might want to review"
git grep -P -n -C 3 "TODO"
) || cd "$cwd"
fi
}

setup_gitlab() {
local cwd out tool_name tool_repo check_command author_name github_username gitlab_username tool_homepage ok primary_branch

cwd="$PWD"
out="$cwd/out"

# ask for arguments not given via CLI
tool_name="${1:-$(ask_for "$HELP_PLUGIN_NAME")}"
tool_name="${tool_name/asdf-/}"
check_command="${2:-$(ask_for "$HELP_TOOL_CHECK" "$tool_name --help")}"

gitlab_username="$(ask_for "Your GitLab username")"
author_name="${4:-$(ask_for "Your name" "$(git config user.name 2>/dev/null)")}"

github_username="${3:-$(ask_for "Tool GitHub username")}"
tool_repo="${5:-$(ask_for "$HELP_TOOL_REPO" "https://github.com/$github_username/$tool_name")}"
tool_homepage="${6:-$(ask_for "$HELP_TOOL_HOMEPAGE" "https://github.com/$github_username/$tool_name")}"
license_keyword="${7:-$(ask_license)}"
license_keyword="$(echo "$license_keyword" | tr '[:upper:]' '[:lower:]')"

primary_branch="$(git rev-parse --abbrev-ref HEAD)"

cat <<-EOF
Setting up plugin: asdf-$tool_name
author: $author_name
plugin repo: https://gitlab.com/$gitlab_username/asdf-$tool_name
license: https://choosealicense.com/licenses/$license_keyword/
$tool_name github: $tool_repo
$tool_name docs: $tool_homepage
$tool_name test: \`$check_command\`
After confirmation, the \`$primary_branch\` will be replaced with the generated
template using the above information. Please ensure all seems correct.
EOF

ok="${8:-$(ask_for "Type \`yes\` if you want to continue.")}"
if [ "yes" != "$ok" ]; then
echo "Nothing done."
else
(
set -e
# previous cleanup to ensure we can run this program many times
git branch template 2>/dev/null || true
git checkout -f template
git worktree remove -f out 2>/dev/null || true
git branch -D out 2>/dev/null || true

# checkout a new worktree and replace placeholders there
git worktree add --detach out

cd "$out"
git checkout --orphan out
git rm -rf "$out" >/dev/null
git read-tree --prefix=/ -u template:template/

download_license "$license_keyword" "$out/LICENSE"

set_placeholder "<YOUR TOOL>" "$tool_name" "$out"
set_placeholder "<TOOL HOMEPAGE>" "$tool_homepage" "$out"
set_placeholder "<TOOL REPO>" "$tool_repo" "$out"
set_placeholder "<TOOL CHECK>" "$check_command" "$out"
set_placeholder "<YOUR NAME>" "$author_name" "$out"
set_placeholder "<YOUR GITHUB USERNAME>" "$github_username" "$out"
set_placeholder "<YOUR GITLAB USERNAME>" "$gitlab_username" "$out"
set_placeholder "<PRIMARY BRANCH>" "$primary_branch" "$out"

git add "$out"
# remove GitHub specific files
git rm -rf "$out/.github" "$out/README-github.md" "$out/contributing-github.md"
# rename GitLab specific files to final filenames
git mv "$out/README-gitlab.md" "$out/README.md"
git mv "$out/contributing-gitlab.md" "$out/contributing.md"
git commit -m "Generate asdf-$tool_name plugin from template."

cd "$cwd"
git branch -M out "$primary_branch"
git worktree remove -f out
git checkout -f "$primary_branch"

echo "All done."
echo "Your $primary_branch branch has been reset to an initial commit."
echo "You might want to push using \`--force-with-lease\` to origin/$primary_branch"

echo "Showing pending TODO tags that you might want to review"
git grep -P -n -C 3 "TODO"
) || cd "$cwd"
fi
}
case "${1:-}" in
"-h" | "--help" | "help")
echo "$HELP"
exit 0
;;
"--gitlab")
shift
setup_gitlab "$@"
;;
"--github")
shift
setup_github "$@"
;;
*)
setup "$@"
setup_github "$@"
;;
esac
30 changes: 30 additions & 0 deletions template/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
image: debian:10

stages:
- lint
- test

variables:
PLUGIN_NAME: asdf-<YOUR TOOL>
TOOL_CHECK: <TOOL CHECK>
SHELLCHECK_VERSION: stable

asdf-plugin-lint:
stage: lint
before_script:
- apt update && apt install -y xz-utils wget
- wget -qO- https://github.com/koalaman/shellcheck/releases/download/$SHELLCHECK_VERSION/shellcheck-$SHELLCHECK_VERSION.linux.x86_64.tar.xz | tar xJv
- cp shellcheck-$SHELLCHECK_VERSION/shellcheck /usr/bin
script:
- shellcheck -x bin/* -P lib/

asdf-plugin-test:
stage: test
before_script:
- apt update && apt install -y git curl bsdmainutils file
- git clone https://github.com/asdf-vm/asdf.git
- . asdf/asdf.sh
script:
- asdf plugin-add $PLUGIN_NAME $CI_REPOSITORY_URL
- asdf list-all $PLUGIN_NAME
- asdf plugin test $PLUGIN_NAME $CI_REPOSITORY_URL --asdf-plugin-gitref $CI_COMMIT_REF_NAME "$TOOL_CHECK" || exit 1
21 changes: 21 additions & 0 deletions template/.gitlab/issue_templates/Bug_Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Describe the bug

<!-- A clear and concise description of what the bug is. -->

## Steps to reproduce

<!-- Steps to reproduce the behavior: -->

## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->

## Screenshots

<!-- If applicable, add screenshots to help explain your problem. -->

## Additional context

<!-- Add any other context about the problem here. -->

/label ~bug
18 changes: 18 additions & 0 deletions template/.gitlab/issue_templates/Feature_Request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Feature description

- [ ] Is this feature the result of a problem?
<!-- A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] -->

## Desired solution

<!-- A clear and concise description of what you want to happen. -->

## Alternatives considered

<!-- A clear and concise description of any alternative solutions or features you've considered. -->

## Additional context

<!-- Add any other context or screenshots about the feature request here. -->

/label ~"feature request"
34 changes: 34 additions & 0 deletions template/.gitlab/merge_request_templates/Merge_Request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Usage examples

<!--- Provide examples of intended usage -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] I have updated the documentation accordingly.
- [ ] I have added tests to cover my changes.
File renamed without changes.
59 changes: 59 additions & 0 deletions template/README-gitlab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<div align="center">

# asdf-<YOUR TOOL> ![Build Status](https://gitlab.com/<YOUR GITLAB USERNAME>/asdf-<YOUR TOOL>/badges/<PRIMARY BRANCH>/pipeline.svg)

[<YOUR TOOL>](<TOOL HOMEPAGE>) plugin for the [asdf version manager](https://asdf-vm.com).

</div>

# Contents

- [Dependencies](#dependencies)
- [Install](#install)
- [Why?](#why)
- [Contributing](#contributing)
- [License](#license)

# Dependencies

- `bash`, `curl`, `tar`: generic POSIX utilities.
- `SOME_ENV_VAR`: set this environment variable in your shell config to load the correct version of tool x.

# Install

Plugin:

```shell
asdf plugin add <YOUR TOOL>
# or
asdf plugin add https://gitlab.com/<YOUR GITLAB USERNAME>/asdf-<YOUR TOOL>.git
```

<YOUR TOOL>:

```shell
# Show all installable versions
asdf list-all <YOUR TOOL>

# Install specific version
asdf install <YOUR TOOL> latest

# Set a version globally (on your ~/.tool-versions file)
asdf global <YOUR TOOL> latest

# Now <YOUR TOOL> commands are available
<TOOL CHECK>
```

Check [asdf](https://github.com/asdf-vm/asdf) readme for more instructions on how to
install & manage versions.

# Contributing

Contributions of any kind welcome! See the [contributing guide](contributing.md).

[Thanks goes to these contributors](https://gitlab.com/<YOUR GITLAB USERNAME>/asdf-<YOUR TOOL>/-/graphs/<PRIMARY BRANCH>)!

# License

See [LICENSE](LICENSE) © [<YOUR NAME>](https://gitlab.com/<YOUR GITLAB USERNAME>/)
File renamed without changes.
12 changes: 12 additions & 0 deletions template/contributing-gitlab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Contributing

Testing Locally:

```shell
asdf plugin test <plugin-name> <plugin-url> [--asdf-tool-version <version>] [--asdf-plugin-gitref <git-ref>] [test-command*]

#
asdf plugin test <YOUR TOOL> https://gitlab.com/<YOUR GITLAB USERNAME>/asdf-<YOUR TOOL>.git "<TOOL CHECK>"
```

Tests are automatically run in GitLab CI on push and merge request.

0 comments on commit 2e64f8d

Please sign in to comment.