diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 9324250..0000000 --- a/src/main.ts +++ /dev/null @@ -1,144 +0,0 @@ -import {info, getInput, setFailed, setOutput} from "@actions/core" -import { Octokit } from "@octokit/action"; -const { createActionAuth } = require("@octokit/auth-action"); -import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods"; -import { downloadRelease } from "@terascope/fetch-github-release"; -import {RestEndpointMethodTypes} from "@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types"; -import * as cache from "@actions/cache" -import * as path from "path" -import * as fse from "fs-extra" - -const ACTION_NAME = "Setup PMMP PHPStan Environment"; -const ACTION_VERSION = "1"; -const ACTION_OUT_PREFIX = `[${ACTION_NAME}]`; - -const GITHUB_REPO_OWNER = "pmmp"; -const GITHUB_REPO = "PocketMine-MP"; -const GITHUB_RELEASE_ASSET_NAME = "PocketMine-MP.phar"; - -/** - * Find a PHPStan release given a version. - * - * @param gitHubApi - * @param target - */ -async function findVersion(gitHubApi: Octokit, target: string): Promise { - if (target.match(/^\d+\.\d+\.\d+/)) { - const response = await gitHubApi.rest.repos.getReleaseByTag({ - owner: GITHUB_REPO_OWNER, - repo: GITHUB_REPO, - tag: target - }); - - if(response.status !== 200) { - throw new Error(`Could not find a ${GITHUB_REPO_OWNER}/${GITHUB_REPO} release with tag '${target}'`); - } - - return response.data; - } - - if(target.toLowerCase() === "latest") { - const releases = await gitHubApi.rest.repos.listReleases({owner: GITHUB_REPO_OWNER, repo: GITHUB_REPO}); - - if(releases.status !== 200) { - throw new Error(`Could not find releases for ${GITHUB_REPO_OWNER}/${GITHUB_REPO}`); - } - - let latestRelease; - for (const release of releases.data.reverse()) { - if(!release.tag_name.toLowerCase().includes("alpha")) { - latestRelease = release; - } - } - - return latestRelease; - } - - throw new Error("Invalid version target " + target); -} - -type ReleaseAsset = RestEndpointMethodTypes["repos"]["getLatestRelease"]["response"]["data"]["assets"] extends (infer U)[] ? U : never; - -/** - * Find a single asset from a release given a file name. - * - * @param assets - * @param file - */ -function findAsset(assets: RestEndpointMethodTypes["repos"]["getLatestRelease"]["response"]["data"]["assets"], file: string) : ReleaseAsset { - const found = assets.find((asset, index) => { - return asset.name === file; - }); - - if(found === undefined) { - throw new Error(`Could not find ${file} asset in release`); - } - - return found; -} - -/** - * Download or restore a cached phpstan.phar executable. - * - * @param releaseId - * @param asset - * @param restorePath - */ -async function install(releaseId: number, asset: ReleaseAsset, restorePath: string, cacheKey: string) : Promise { - const hitKey = await cache.restoreCache([restorePath], cacheKey); - - if (hitKey === undefined) { - await downloadRelease(GITHUB_REPO_OWNER, GITHUB_REPO, restorePath, (release) : boolean => { - return release.id === releaseId; - }, (releaseAsset) : boolean => { - return releaseAsset.id === asset.id; - }, false, false); - - await cache.saveCache([restorePath], cacheKey); - info(`${ACTION_OUT_PREFIX} Downloaded ${GITHUB_RELEASE_ASSET_NAME} to ${restorePath}`); - } else { - info(`${ACTION_OUT_PREFIX} Using cached ${GITHUB_RELEASE_ASSET_NAME}, restored to ${restorePath}`); - } - - return restorePath; -} - -/** - * Download PMMP phar to the given path, taking care of caching. - */ -export async function run(): Promise { - const RestOctokit = Octokit.plugin(restEndpointMethods); - const auth = createActionAuth(); - const gitHubApi = new RestOctokit({ auth: await auth() }); - - const release = await findVersion(gitHubApi, getInput("pmmp-version")); - const asset = findAsset(release.assets, GITHUB_RELEASE_ASSET_NAME); - info(`${ACTION_OUT_PREFIX} Using target version ${release.tag_name} released @ ${release.published_at}`); - - const restorePath = path.resolve(getInput("install-path")); - const cacheKey = `setup-pmmp-phpstan-env-action-v${ACTION_VERSION}-${release.tag_name}-${asset.id}-${restorePath.replace(/\//g, "-")}-${GITHUB_RELEASE_ASSET_NAME}`; - - await fse.mkdir(restorePath); - let pmmpPhar = await install(release.id, asset, restorePath, cacheKey) + GITHUB_RELEASE_ASSET_NAME; - - setOutput("pmmp-phar", pmmpPhar); - - await installDefaultConfigs(restorePath); -} - -/** - * Copy default PHPStan configs into the workspace. - */ -async function installDefaultConfigs(installPath: string) : Promise { - const sourcePath = path.join(__dirname, "../config"); - const configInstallPath = path.join(installPath, "phpstan"); - - info(`${ACTION_OUT_PREFIX} Installing default PHPStan configs to ${configInstallPath}`); - - await fse.mkdir(configInstallPath); - await fse.copy(sourcePath, configInstallPath); -} - -;(async () => { - await run(); -})().catch(setFailed);