From 74a3ba0689365c957474147576fe099d3c0912e1 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 27 Nov 2024 12:41:26 -0300 Subject: [PATCH] new stackbrew in js --- UPDATE.md | 4 +- generate-stackbrew-library.sh | 92 ----------------------------------- stackbrew.js | 82 +++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 94 deletions(-) delete mode 100755 generate-stackbrew-library.sh create mode 100755 stackbrew.js diff --git a/UPDATE.md b/UPDATE.md index 5ff1643..2604499 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,8 +1,8 @@ -# Instuctions to update +# Instructions to update ``` ./update.sh -./generate-stackbrew-library.sh > ../official-images/library/rocket.chat +node stackbrew.js > ../official-images/library/rocket.chat cd ../official-images #git commit && git push && PR ``` diff --git a/generate-stackbrew-library.sh b/generate-stackbrew-library.sh deleted file mode 100755 index 886a180..0000000 --- a/generate-stackbrew-library.sh +++ /dev/null @@ -1,92 +0,0 @@ -#!/bin/bash -set -eu - -declare -A aliases=( - [6.13]='6' - [7.0]='7 latest' -) - -cd "$(cd "${0%/*}" && pwd -P)" - -sort_versions() { - local versions=("$@") - local sorted - local lines - local line - - IFS=$'\n' - lines="${versions[*]}" - unset IFS - - while IFS='' read -r line; do - sorted+=("${line}") - done <<< "$(echo "${lines}" | grep "^[0-9]" | sort -r)" - - while IFS='' read -r line; do - sorted+=("${line}") - done <<< "$(echo "${lines}" | grep -v "^[0-9]" | sort -r)" - - echo "${sorted[@]}" -} - -versions=( */ ) -IFS=' ' read -ra versions <<< "$(sort_versions "${versions[@]%/}")" - -# get the most recent commit which modified any of "$@" -fileCommit() { - git log -1 --format='format:%H' HEAD -- "$@" -} - -# get the most recent commit which modified "$1/Dockerfile" or any file COPY'd from "$1/Dockerfile" -dirCommit() { - local dir="$1"; shift - ( - cd "$dir" - fileCommit \ - Dockerfile \ - $(git show HEAD:./Dockerfile | awk ' - toupper($1) == "COPY" { - for (i = 2; i < NF; i++) { - print $i - } - } - ') - ) -} - -self="$(basename "${BASH_SOURCE[0]}")" -url='https://github.com/RocketChat/Docker.Official.Image' - -echo "# this file is generated via ${url}/blob/$(fileCommit "${self}")/${self}" -echo -echo "Maintainers: Rocket.Chat Image Team (@RocketChat)" -echo "GitRepo: ${url}.git" -echo - -# prints "$2$1$3$1...$N" -join() { - local sep="$1"; shift - local out; printf -v out "${sep//%/%%}%s" "$@" - echo "${out#$sep}" -} - -for version in "${versions[@]}"; do - # Skip "docs" and other non-docker directories - [ -f "${version}/Dockerfile" ] || continue - - commit="$(dirCommit "$version")" - - fullVersion="$(git show "$commit":"$version/Dockerfile" | awk '$1 == "ENV" && $2 == "RC_VERSION" { gsub(/~/, "-", $3); print $3; exit }')" - - versionAliases=( $fullVersion ) - - versionAliases+=( - $version - ${aliases[$version]:-} - ) - - echo "Tags: $(join ', ' "${versionAliases[@]}")" - echo "GitCommit: ${commit}" - echo "Directory: ${version}" - echo -done diff --git a/stackbrew.js b/stackbrew.js new file mode 100755 index 0000000..36ec411 --- /dev/null +++ b/stackbrew.js @@ -0,0 +1,82 @@ +#!/usr/bin/env node + +const fs = require('fs'); +const path = require('path'); + +// Grab last git commit +function getCommitHasForPath(path) { + return require('child_process') + .execSync(`git log -1 --format=%H HEAD -- ${path}`) + .toString().trim() +} + +const stackbrewPath = path.basename(__filename); + +const url = 'https://github.com/RocketChat/Docker.Official.Image'; + +// Header +let stackbrew = `# this file is generated via ${url}/blob/${getCommitHasForPath(stackbrewPath)}/${stackbrewPath} + +Maintainers: Rocket.Chat Image Team (@RocketChat) +GitRepo: ${url}.git +GitFetch: refs/heads/main\n`; + +// Loop versions +const rcDirRegex = /^\d+\.\d+$/; + +// Returns a list of the child directories in the given path +const getChildDirectories = (parent) => fs.readdirSync(parent, { withFileTypes: true }) + .filter((dirent) => dirent.isDirectory()) + .map(({ name }) => name); + +const getRocketChatVersionDirs = (base) => getChildDirectories(base) + .filter((childPath) => rcDirRegex.test(path.basename(childPath))); + +// versions need to be in order from most recent to older +const versions = getRocketChatVersionDirs(__dirname) + .sort((a,b) => b.localeCompare(a, undefined, { numeric: true, sensitivity: 'base' })); + +let foundCurrent = false; + +const latestMajor = []; + +for (version of versions) { + const isCurrent = !foundCurrent; + foundCurrent = true; + + let fullversion; + + // Get full version from the first Dockerfile + if (!fullversion) { + const dockerfile = fs.readFileSync(path.join(version, 'Dockerfile'), 'utf-8'); + fullversion = dockerfile.match(/ENV RC_VERSION (?\d+)\.(?\d+)\.(?\d+)/) + } + let tags = [ + `${fullversion.groups.major}.${fullversion.groups.minor}.${fullversion.groups.patch}`, + `${fullversion.groups.major}.${fullversion.groups.minor}`, + ]; + + const isLatestMajor = !latestMajor.includes(fullversion.groups.major); + + if (isLatestMajor) { + tags.push(fullversion.groups.major); + + latestMajor.push(fullversion.groups.major); + } + + if (isCurrent) { + tags.push('latest'); + } + + // remove duplicates + tags = tags.filter((x, i, a) => a.indexOf(x) == i); + tags = tags.sort((a, b) => b - a); + + stackbrew += `\nTags: ${tags.join(', ')}\n`; + // stackbrew += `Architectures: ${tbd.join(', ')}\n` + stackbrew += `GitCommit: ${getCommitHasForPath(version)}\n`; + stackbrew += `Directory: ${version}\n`; +} + +// output +console.log(stackbrew)