Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
m-o-e committed Jan 7, 2023
0 parents commit a803574
Show file tree
Hide file tree
Showing 103 changed files with 2,041 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*.cr]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
125 changes: 125 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
name: CI

on:
push:
pull_request:
branches:
- 'master'

jobs:
prepare:
name: Prepare
runs-on: ubuntu-latest
outputs:
trusted: ${{ steps.contains_tag.outputs.retval }}
matrix_json: ${{ steps.crystal_action.outputs.matrix_json }}
crystal_version: ${{ steps.crystal_action.outputs.crystal_version }}
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Lint
uses: crystal-ameba/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Determine if tag is on trusted branch
uses: rickstaa/action-contains-tag@0f592a0dd54a67d9af4545f6b6687ee01853d9a7
id: contains_tag
with:
frail: false
reference: "master"
tag: "${{ github.ref }}"

- name: Crystal Action
id: crystal_action
run: .github/workflows/crystal_action.rb

build:
name: Test & Build
needs: prepare
strategy:
fail-fast: true
matrix:
include: ${{ fromJson(needs.prepare.outputs.matrix_json) }}
runs-on: ${{ matrix.platform }}
permissions:
contents: write

steps:
- name: Checkout
uses: actions/checkout@v3

- if: matrix.platform != 'ubuntu-latest' && !endsWith(matrix.platform, '-arm')
name: Install Crystal
uses: crystal-lang/install-crystal@v1
with:
crystal: ${{ matrix.crystal }}

- if: matrix.platform != 'ubuntu-latest' && !endsWith(matrix.platform, '-arm')
name: Test & Build (on runner host)
run: |
make clean ci release
- if: matrix.platform == 'ubuntu-latest' || endsWith(matrix.platform, '-arm')
name: Test & Build (in alpine)
uses: addnab/docker-run-action@v3
with:
image: 84codes/crystal:${{ matrix.crystal }}-alpine
options: -v ${{ github.workspace }}:/workspace
run: |
cd /workspace && make clean ci release
- if: startsWith(github.ref, 'refs/tags/') && matrix.crystal == needs.prepare.outputs.crystal_version && needs.prepare.outputs.trusted == 'true'
name: Compute checksum
run: |
shasum -a 256 build/* >checksums.txt
- if: startsWith(github.ref, 'refs/tags/') && matrix.crystal == needs.prepare.outputs.crystal_version && needs.prepare.outputs.trusted == 'true'
name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: sha256-${{ matrix.platform }}
path: checksums.txt

- if: startsWith(github.ref, 'refs/tags/') && matrix.crystal == needs.prepare.outputs.crystal_version && needs.prepare.outputs.trusted == 'true'
name: Draft release
uses: ncipollo/release-action@v1
with:
artifacts: "build/*"
allowUpdates: true
draft: true
updateOnlyUnreleased: false
generateReleaseNotes: false
omitBody: true

release:
name: Release
needs: [prepare, build]
permissions:
contents: write
runs-on: ubuntu-latest

if: startsWith(github.ref, 'refs/tags/') && needs.prepare.outputs.trusted == 'true'
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Download artifacts
uses: actions/download-artifact@v3

- name: Generate Release Notes
run: .github/workflows/release_notes.sh >release.txt

- name: Finish Release
uses: ncipollo/release-action@v1
with:
allowUpdates: true
draft: false
updateOnlyUnreleased: false
generateReleaseNotes: false
bodyFile: release.txt
omitBody: false

26 changes: 26 additions & 0 deletions .github/workflows/crystal_action.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env ruby

# crystal_action.rb v1.0.0
# (c)2023 [email protected] - MIT License

require 'json'
require 'yaml'

SHARD_YML = YAML.load_file('shard.yml')

ENTRIES={}
ENTRIES['platform'] = SHARD_YML['support_matrix']['platforms']
ENTRIES['crystal'] = SHARD_YML['support_matrix']['crystal_versions'] # + %w[latest]

def product_hash(hsh)
keys = hsh.keys
attrs = keys.map { |key| hsh[key] }
product = attrs[0].product(*attrs[1..-1])
product.map{ |p| Hash[keys.zip p] }
end

print "::set-output name=matrix_json::"
puts product_hash(ENTRIES).to_json

print "::set-output name=crystal_version::"
puts SHARD_YML['crystal']
55 changes: 55 additions & 0 deletions .github/workflows/release_notes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash

set -e

VERSION=$(git describe --tags | cut -c 2-)

cat <<EOF
# Installation
## OSX
\`\`\`bash
wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${VERSION}.darwin-x86_64
sudo chmod +x envcat-${VERSION}.darwin-x86_64
sudo mv envcat-${VERSION}.darwin-x86_64 /usr/bin
sudo ln -sf /usr/bin/envcat-${VERSION}.darwin-x86_64 /usr/bin/envcat
\`\`\`
## Linux
\`\`\`bash
wget https://github.com/busyloop/envcat/releases/download/v${VERSION}/envcat-${VERSION}.linux-x86_64
sudo chmod +x envcat-${VERSION}.linux-x86_64
sudo mv envcat-${VERSION}.linux-x86_64 /usr/bin
sudo ln -sf /usr/bin/envcat-${VERSION}.linux-x86_64 /usr/bin/envcat
\`\`\`
## Dockerfile :whale:
EOF

for ARCH in linux-x86_64 linux-aarch64; do
CHECKSUM=$(grep -hrE "build/envcat-${VERSION}.${ARCH}$" sha256-*/checksums.txt | cut -d ' ' -f 1)

if [ -z "$CHECKSUM" ]; then
ls -R
cat sha256-*/checksums.txt
echo "*** CHECKSUM ERROR OR BUILD VERSION MISMATCH ***"
exit 1
fi

cat <<EOF
#### ${ARCH}
\`\`\`Dockerfile
# Install envcat (${ARCH})
ARG envcat_version=${VERSION}
ARG envcat_sha256=${CHECKSUM}
ADD --checksum=sha256:\${envcat_sha256} https://github.com/busyloop/envcat/releases/download/v\${envcat_version}/envcat-\${envcat_version}.${ARCH} /envcat
RUN chmod +x /envcat
\`\`\`
EOF
done
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/docs/
/lib/
/bin/
/build/
/.shards/
*.dwarf
.DS_Store
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crystal 1.6.2
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 moe <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
export UNAME := $(shell uname -sm | sed 's/ /-/' | tr '[:upper:]' '[:lower:]')
export MAKE_UNAME := $(shell uname -sm | sed 's/ /_/' | tr '[:lower:]' '[:upper:]')
export VERSION := $(shell grep "^version" shard.yml | cut -d ' ' -f 2)

SRC_FILES = $(shell find src)
CRYSTAL ?= crystal
CRYSTAL_SPEC_ARGS = --fail-fast

CRYSTAL_ARGS_LOCAL_DARWIN_X86_64 = --progress
CRYSTAL_ARGS_LOCAL_LINUX_X86_64 = --progress
CRYSTAL_ARGS_LOCAL_LINUX_AARCH64 = --progress
CRYSTAL_ARGS_RELEASE_DARWIN_X86_64 = --release --no-debug
CRYSTAL_ARGS_RELEASE_LINUX_X86_64 = --static --release --no-debug
CRYSTAL_ARGS_RELEASE_LINUX_AARCH64 = --static --release --no-debug

DOCKER_IMAGE = 84codes/crystal:1.6.2-alpine
ALPINE_VERSION = $(shell which apk)

EXE_SRC = src/envcat.cr
EXE_BASENAME = envcat-$(VERSION)

.PHONY: init release

lint_and_test: lint test

test:
$(CRYSTAL) spec $(CRYSTAL_SPEC_ARGS)

lint:
bin/ameba

clean:
rm -f build/*

build: build/$(EXE_BASENAME).$(UNAME)

release: test
$(MAKE) build/$(EXE_BASENAME).$(UNAME) BUILD_MODE=RELEASE
# mkdir -p build && date >build/$(EXE_BASENAME).$(UNAME)
rm -f build/*.dwarf

release_linux:
$(MAKE) release UNAME=linux-x86_64

ci:
shards install --without-development

init:
@mkdir -p build

tag:
git tag v$(VERSION)

version:
@echo $(VERSION)

prepare_alpine:
ifeq ($(shell [[ ! -z "$(ALPINE_VERSION)" ]] && echo true),true)
apk add yaml-static
endif

# Static linux release build inside alpine
build/$(EXE_BASENAME).linux-x86_64: $(SRC_FILES) | init prepare_alpine
ifeq ($(shell [[ -z "$(ALPINE_VERSION)" && "$(BUILD_MODE)" == "RELEASE" ]] && echo true),true)
time docker run --rm -it -w /src -v `pwd`:/src --entrypoint make $(DOCKER_IMAGE) $@ BUILD_MODE=RELEASE
else
$(CRYSTAL) build $(CRYSTAL_ARGS_$(or $(BUILD_MODE),LOCAL)_$(MAKE_UNAME)) -o $@ ${EXE_SRC}
@ldd $@ 2>/dev/null && { echo "ERROR: Compiler did not produce a static executable - see http://bit.ly/3jnS5yV"; exit 1; } || true
endif

build/$(EXE_BASENAME).%: $(SRC_FILES) | init prepare_alpine
time $(CRYSTAL) build $(CRYSTAL_ARGS_$(or $(BUILD_MODE),LOCAL)_$(MAKE_UNAME)) -o $@ ${EXE_SRC}

README.md: docs/templates/README.md.j2 build/$(EXE_BASENAME).$(UNAME)
HELP_SCREEN=$$(build/$(EXE_BASENAME).$(UNAME) --help 2>&1 | tac | tail -n +3 | tac | tail -n +2 | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g') build/$(EXE_BASENAME).$(UNAME) -f j2 HELP_SCREEN VERSION <$^ >$@

README: README.md
readme: README.md
Loading

0 comments on commit a803574

Please sign in to comment.