diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..975e957 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,28 @@ +name: Upload Release Asset + +on: + push: + branches: + - main + tags: + - 'v*' + +jobs: + release: + name: Create Release and upload .kpz file + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v3 + - name: Version + run: | + VERSION=$(node checkVersionNumber.js version) + FILENAME=$(node checkVersionNumber.js filename) + echo "VERSION=$VERSION" >> $GITHUB_ENV + echo "FILENAME=$FILENAME" >> $GITHUB_ENV + - name: 'Create Release' + run: gh release create ${{ env.VERSION }} ${{ env.FILENAME }} -t ${{ env.VERSION }} -n "" + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Koha/Plugin/Com/PTFSEurope/BDS.pm b/Koha/Plugin/Com/PTFSEurope/BDS.pm index e82661b..f3658c4 100644 --- a/Koha/Plugin/Com/PTFSEurope/BDS.pm +++ b/Koha/Plugin/Com/PTFSEurope/BDS.pm @@ -25,7 +25,7 @@ use Net::FTP; use Data::Dumper; ## Here we set our plugin version -our $VERSION = 1.2; +our $VERSION = 1.5; ## Here is our metadata, some keys are required, some are optional our $metadata = { diff --git a/checkRemotes.js b/checkRemotes.js new file mode 100644 index 0000000..f2e0b8c --- /dev/null +++ b/checkRemotes.js @@ -0,0 +1,59 @@ +const checkType = process.argv[2] + +const getFormattedRemotes = (input) => { + const numberOfRemotes = input.length + const remotes = [] + + for(let i = 0; i < numberOfRemotes; i = i + 3) { + const [remote, url, type] = input.slice(i, i + 3) + remotes.push({ + remote, + url, + type + }) + } + + const validRemotes = remotes.filter(remote => remote.type === '(push)').reduce((result, remote) => { + if(remote.url !== 'git@github.com:PTFS-Europe/koha-plugin-template.git' && + remote.url !== 'https://github.com/PTFS-Europe/koha-plugin-template.git') { + result.push(remote) + } + return result + }, []) + + return validRemotes +} + +if(checkType === 'check') { + const cliInput = process.argv.slice(3) + + const validRemotes = getFormattedRemotes(cliInput) + + if(validRemotes.length === 0) console.log('No valid remotes') + if(validRemotes.length === 1) return console.log(validRemotes[0].remote) + + if(validRemotes.length > 1) console.log('Multiple remotes available') +} +if(checkType === 'validate') { + const response = process.argv.slice(3, 4)[0].split('-')[0] + const remoteList = process.argv.slice(4) + + const validRemotes = getFormattedRemotes(remoteList).map(i => i.remote) + + const checkRemoteIsValid = validRemotes.indexOf(response) + + if(checkRemoteIsValid > -1) return console.log(response) + if(checkRemoteIsValid === -1) return console.log('Invalid') +} +if(checkType === 'provide') { + const cliInput = process.argv.slice(3) + + const validRemotes = getFormattedRemotes(cliInput) + + const remoteStrings = validRemotes.map(remote => { + return `${remote.remote}-${remote.url}` + }) + + console.log(...remoteStrings) +} + diff --git a/checkVersionNumber.js b/checkVersionNumber.js new file mode 100644 index 0000000..b66801c --- /dev/null +++ b/checkVersionNumber.js @@ -0,0 +1,70 @@ +const fs = require('fs') +const path = require('path') + +const identifyPluginFile = (file) => { + const pluginFile = fs.readFileSync(file, 'utf8') + const fileByLine = pluginFile.split(/\r?\n/) + + let pluginIdentified = false + fileByLine.forEach(line => { + if(line.includes("Koha::Plugins::Base")){ + pluginIdentified = true + } + }) + return pluginIdentified +} + +const collectPluginFiles = (fullPath) => { + let files = [] + fs.readdirSync(fullPath).forEach(file => { + const absolutePath = path.join(fullPath, file) + if (fs.statSync(absolutePath).isDirectory()) { + const filesFromNestedFolder = collectPluginFiles(absolutePath) + filesFromNestedFolder && filesFromNestedFolder.forEach(file => { + files.push(file) + }) + } else { + return files.push(absolutePath) + } + }) + return files +} + +const pluginFiles = collectPluginFiles('./Koha') + +let pluginFilePath +pluginFiles.forEach(file => { + const pluginFile = identifyPluginFile(file) + if(pluginFile) { + pluginFilePath = file + } +}) +if(!pluginFilePath) return console.log('No plugin file found') + +const cliInput = process.argv.slice(2)[0] + +if(cliInput === 'filename') { + const filename = pluginFilePath.replace(/^.*[\\\/]/, '').toLowerCase() + const removeFileExtension = path.parse(filename).name + const kpzFileName = `koha-plugin-${removeFileExtension}.kpz` + console.log(kpzFileName) +} +if(cliInput === 'version') { + const pluginFile = fs.readFileSync(pluginFilePath, 'utf8') + const fileByLine = pluginFile.split(/\r?\n/) + + let versionNumberIdentified + fileByLine.forEach((line, index)=> { + if(line.includes("our $VERSION")){ + versionNumberIdentified = index + } + }) + + if(!versionNumberIdentified) return console.log('No version found') + if(versionNumberIdentified) { + const regex = /our \$VERSION\s*=\s*(["'`]?)([\d.]+)\1;/ + const findVersionNumber = regex.exec(fileByLine[versionNumberIdentified]) + const pluginVersion = findVersionNumber[2] + console.log(`v${pluginVersion}`) + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..487688c --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "koha-plugin-template", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "release": "bash ./release_kpz.sh" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/release_kpz.sh b/release_kpz.sh new file mode 100755 index 0000000..2b06bd4 --- /dev/null +++ b/release_kpz.sh @@ -0,0 +1,76 @@ +#!/usr/bin/env bash + +RED="\033[0;31m" +NC="\033[0m" # No colour +GREEN="\033[0;32m" +YELLOW="\033[1;33m" + +echo "Creating and releasing .kpz file" + +FILENAME=$(node checkVersionNumber.js filename) +FILE=./${FILENAME} + +if test -f "$FILE"; then + echo "Plugin .kpz file already exists, previous version deleted" + rm -r "$FILE" +fi +echo "Creating new .kpz file" +zip -r "$FILE" Koha/ + +NEW_VERSION=$(node checkVersionNumber.js version) + +if [ "$NEW_VERSION" = "No plugin file found" ]; then + echo -e "${RED}No plugin file could be identified, have you created one?" + echo -e "A plugin file must contain "use base qw\(Koha::Plugins::Base\);"${NC}" + exit +elif [ "$NEW_VERSION" = "No version found" ]; then + echo -e "${RED}No version could be identified, does your plugin file include one?" + echo -e "A plugin file must contain "our \$VERSION = "version here";"${NC}" + exit +else + NEW_VERSION_NUMBER=${NEW_VERSION:1} +fi + +PREVIOUS_VERSION=$(git log --pretty=oneline | grep -m 1 -o "v[0-9.]\{1,6\}") +if [ -z "$PREVIOUS_VERSION" ]; then + echo -e "${YELLOW}No previous version found. Assuming this is the first release.${NC}" + PREVIOUS_VERSION_NUMBER="v0.0.0" # Assuming 0 as the initial version for comparison +else + PREVIOUS_VERSION_NUMBER=${PREVIOUS_VERSION:1} +fi +echo -e "Previous version: $PREVIOUS_VERSION" +echo -e "New version: $NEW_VERSION" + +# Comparing versions +VERSION_COMPARISON=$(echo -e "$PREVIOUS_VERSION_NUMBER\n$NEW_VERSION_NUMBER" | sort -V | head -n 1) +echo -e "Version comparison: $VERSION_COMPARISON" + +if [ "$VERSION_COMPARISON" != "$PREVIOUS_VERSION_NUMBER" ]; then + echo -e "${GREEN}Version has been updated from $PREVIOUS_VERSION_NUMBER to $NEW_VERSION_NUMBER - checking remotes and starting upload${NC}" + + REMOTES=$(git remote -v) + VALID_REMOTE=$(node checkRemotes.js check $REMOTES) + + if [ "$VALID_REMOTE" = "No valid remotes" ]; then + echo -e "${RED}You have not set a git remote, please set one to push${NC}" + exit + elif [ "$VALID_REMOTE" = "Multiple remotes available" ]; then + REMOTE_LIST=$(node checkRemotes.js provide $REMOTES) + echo -e "${RED}Multiple git remotes identified, which one would you like to select?${NC}\n" + PS3="Select a number:" + select REMOTE in $REMOTE_LIST + do + echo "Selected remote: $REMOTE\n" + break + done + VALID_REMOTE=$(node checkRemotes.js validate $REMOTE $REMOTES) + fi + + git add . + git commit -m "$NEW_VERSION" + git tag "$NEW_VERSION" + git push $VALID_REMOTE + echo -e "${GREEN}Plugin has been pushed to Github and a release is being generated${NC}" +else + echo -e "${RED}WARNING: The Plugin version needs to be updated - please check the .pm file and update the version${NC}" +fi