diff --git a/action.js b/action.js new file mode 100644 index 0000000..35a2dfe --- /dev/null +++ b/action.js @@ -0,0 +1,57 @@ +import { execSync } from "child_process"; +import { readFileSync, mkdirSync, writeFileSync, readdirSync } from "fs"; + +const libName = process.env.PWD; +const version = readFileSync("version", { encoding: "utf8" }); +const repoBaseUrl = `${process.env.INPUT_REPO_URL}/${libName}/${version}`; +const packageJsonUrls = []; + +const sendFile = async (path, data) => { + await fetch(`${repoBaseUrl}/${path}`, { + method: "PUT", + headers: { + Authorization: `Basic ${process.env.INPUT_REPO_USER}:${process.env.INPUT_REPO_PASS}`, + }, + body: data, + }); +}; + +// Get system utils +execSync("apt update", { encoding: "utf8" }); +execSync("apt install -y pipx", { encoding: "utf8" }); +execSync("pipx install mpy-cross", { encoding: "utf8" }); + +// Add base lib file to repo +await sendFile(`__init__.mpy`, new ArrayBuffer(0)); + +// Update urls +packageJsonUrls.push([`${libName}/__init__.mpy`, "__init__.mpy"]); + +// Generate module folders/files +readdirSync(".") + .filter((f) => f.slice(-2) == "py") + .forEach(async (moduleFile) => { + const modName = moduleFile.slice(0, -3); + const modInitPath = `${modName}/__init__.mpy`; + const modPyPath = `${modName}/${modName}.mpy`; + + // compile mpy file + execSync(`pipx run mpy-cross ${moduleFile}`, { + encoding: "utf8", + }); + + // Add module files to repo + await sendFile(modInitPath, new ArrayBuffer(0)); + await sendFile(modPyPath, readFileSync(modPyPath).buffer); + + // Update urls + packageJsonUrls.push([`${libName}/${modInitPath}`, modInitPath]); + packageJsonUrls.push([`${libName}/${modPyPath}`, modPyPath]); + }); + +// Add package.json to repo +await sendFile(`package.json`, { + urls: packageJsonUrls, + deps: [], + version: version, +}); diff --git a/action.sh b/action.sh deleted file mode 100755 index 8e0f66e..0000000 --- a/action.sh +++ /dev/null @@ -1,14 +0,0 @@ -NAME=$(basename "$PWD") -VER=$(cat version) -DIR_NAME="$NAME"_"$VER" -TAR_NAME="$NAME".tar.gz -echo $INPUT_REPO_URL -FULL_URL="$INPUT_REPO_URL"/"$NAME"/"$VER"/"$TAR_NAME" - -apt update -apt install -y pipx -pipx install mpy-cross -mkdir "$DIR_NAME" -for f in ./*.py; do pipx run mpy-cross "$f" -o "$DIR_NAME"/$(basename "$f" .py).mpy; done -tar czf "$TAR_NAME" "$DIR_NAME" -curl -u "$INPUT_REPO_USER":"$INPUT_REPO_PASS" -X PUT -T "$TAR_NAME" "$FULL_URL" \ No newline at end of file diff --git a/action.yaml b/action.yaml index 3261837..e5d88a5 100644 --- a/action.yaml +++ b/action.yaml @@ -9,10 +9,10 @@ inputs: description: Password used for authenticating with repo required: true repo_url: - description: Domain for pypi repo + description: Domain for package repo required: true runs: using: "composite" steps: - - run: $GITHUB_ACTION_PATH/action.sh - shell: bash + - run: $GITHUB_ACTION_PATH/action.js + shell: node