1
0
Fork 0
mirror of https://code.forgejo.org/actions/cache.git synced 2025-05-10 19:19:54 +02:00

Add initial minimatch support

This commit is contained in:
Ethan Dennis 2020-03-05 09:15:35 -08:00
parent 84cead4a82
commit 1e233443e8
No known key found for this signature in database
GPG key ID: 32E74B75DB4065DD
14 changed files with 202 additions and 108 deletions

View file

@ -19,11 +19,6 @@ async function run(): Promise<void> {
return;
}
// const cachePath = utils.resolvePath(
// core.getInput(Inputs.Path, { required: true })
// );
// core.debug(`Cache Path: ${cachePath}`);
const primaryKey = core.getInput(Inputs.Key, { required: true });
core.saveState(State.CacheKey, primaryKey);

View file

@ -4,6 +4,7 @@ import * as cacheHttpClient from "./cacheHttpClient";
import { Events, Inputs, State } from "./constants";
import { createTar } from "./tar";
import * as utils from "./utils/actionUtils";
import * as pathUtils from "./utils/pathUtils";
async function run(): Promise<void> {
try {
@ -43,11 +44,12 @@ async function run(): Promise<void> {
return;
}
core.debug(`Cache ID: ${cacheId}`);
const cachePaths = core
.getInput(Inputs.Path)
.split("\n")
.filter(x => x !== "")
.map(x => utils.resolvePath(x));
const cachePaths = pathUtils.expandPaths(
core
.getInput(Inputs.Path)
.split("\n")
.filter(x => x !== "")
);
core.debug("Cache Paths:");
core.debug(`${JSON.stringify(cachePaths)}`);

View file

@ -44,6 +44,7 @@ export async function createTar(
archivePath: string,
sourceDirectories: string[]
): Promise<void> {
// TODO: will want to stream sourceDirectories into tar
const workingDirectory = getWorkingDirectory();
const args = [
"-cz",

View file

@ -1,7 +1,6 @@
import * as core from "@actions/core";
import * as io from "@actions/io";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import * as uuidV4 from "uuid/v4";
@ -82,18 +81,6 @@ export function logWarning(message: string): void {
core.info(`${warningPrefix}${message}`);
}
export function resolvePath(filePath: string): string {
if (filePath[0] === "~") {
const home = os.homedir();
if (!home) {
throw new Error("Unable to resolve `~` to HOME");
}
return path.join(home, filePath.slice(1));
}
return path.resolve(filePath);
}
export function getSupportedEvents(): string[] {
return [Events.Push, Events.PullRequest];
}

63
src/utils/pathUtils.ts Normal file
View file

@ -0,0 +1,63 @@
import * as os from "os";
import * as path from "path";
import { readdirSync } from "fs";
import { IOptions, Minimatch } from "minimatch";
const globCharacters: string[] = ["*", "?", "[", "]"];
const options: IOptions = {
nocase: true,
dot: true,
nobrace: true
};
// export function resolvePath(filePath: string): string {
// if (filePath[0] === "~") {
// const home = os.homedir();
// if (!home) {
// throw new Error("Unable to resolve `~` to HOME");
// }
// return path.join(home, filePath.slice(1));
// }
// return path.resolve(filePath);
// }
export function isMinimatchPattern(pattern: string): boolean {
if (globCharacters.some(x => pattern.includes(x))) {
return true;
}
return false;
}
export function matchDirectories(pattern: string, workspace: string): string[] {
const directories = readdirSync(workspace, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
const minimatch = new Minimatch(pattern, options);
const matches = directories.filter(x => minimatch.match(x));
return matches;
}
export function expandPaths(filePaths: string[]): string[] {
const paths: string[] = [];
const workspace = process.env["GITHUB_WORKSPACE"] ?? process.cwd();
for (const filePath of filePaths) {
if (isMinimatchPattern(filePath)) {
paths.push(...matchDirectories(filePath, workspace));
} else if (filePath[0] === "~") {
const home = os.homedir();
if (!home) {
throw new Error("Unable to resolve `~` to HOME");
}
paths.push(path.join(home, filePath.slice(1)));
} else {
paths.push(path.resolve(filePath));
}
}
return paths;
}