Skip to content

Commit

Permalink
fix(cache): take into account custom options for cacheMayBeUsed flag
Browse files Browse the repository at this point in the history
Previously, when a custom options file was used, the internal
cacheMayBeUsed flag would be set. When set, this flag checks that the
repository clone is non-shallow, sets file modification times according
to commit times, and restores any previous artifact, in preparation for
it to be reused by PackSquash.

However, all that work goes to waste if the options file tells
PackSquash to not use previous results anyway. Let's be smarter and set
that flag after parsing the options, taking into account the values that
were set in that file.
  • Loading branch information
AlexTMjugador committed Oct 11, 2022
1 parent 751283c commit 091f0ba
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getInput, info, setFailed } from '@actions/core';
import { generateOptionsFile, getPackDirectory, Options, printOptionsFileContent, shouldUseCache, tweakAndCopyUserOptionsFile } from './options.js';
import { generateOptionsFile, getPackDirectory, Options, printOptionsFileContent, mayCacheBeUsed, tweakAndCopyUserOptionsFile } from './options.js';
import { computeCacheKey, restorePackSquashCache, savePackSquashCache } from './cache';
import { downloadAppImage } from './appimage';
import { printPackSquashVersion, runPackSquash } from './packsquash';
Expand All @@ -17,8 +17,8 @@ async function run() {
const workingDirectory = new WorkingDirectory();
await workingDirectory.rm();
await workingDirectory.mkdir();

const optionsFile = getInput(Options.OptionsFile);
const cacheMayBeUsed = optionsFile || shouldUseCache();
const workspace = getEnvOrThrow('GITHUB_WORKSPACE');

if (optionsFile) {
Expand All @@ -28,7 +28,9 @@ async function run() {
await generateOptionsFile(workingDirectory);
}
await printOptionsFileContent(workingDirectory);

const packDirectory = getPackDirectory();
const cacheMayBeUsed = mayCacheBeUsed();

if (cacheMayBeUsed) {
await checkRepositoryIsNotShallow(packDirectory);
Expand Down
23 changes: 18 additions & 5 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ function getForceIncludeFiles() {
return getMultilineInput(Options.ForceIncludeFiles).map(file => ({ [file]: { force_include: true } }));
}

export function shouldUseCache() {
return !getBooleanInput(Options.NeverStoreSquashTimes) && getInput(Options.ZipSpecConformanceLevel) !== 'pedantic';
}

/**
* Like {@link getInput}, but parses the input value as an integer. If the
* conversion to an integer is not successful, an error is thrown.
Expand Down Expand Up @@ -192,7 +188,10 @@ function getOptionsFileContent(workingDirectory: WorkingDirectory) {
);
}

let packDirectory: string;
let packDirectory: string | undefined;
let neverStoreSquashTimes: boolean | undefined;
let zipSpecConformanceLevel: string | undefined;

/**
* Returns the pack directory. This is equivalent to `getInput(Options.Path)`
* if a custom options file was not processed with `tweakAndCopyUserOptionsFile`;
Expand All @@ -202,6 +201,13 @@ export function getPackDirectory() {
return packDirectory || getInput(Options.Path);
}

export function mayCacheBeUsed() {
const neverStoreTimes = neverStoreSquashTimes !== undefined ? neverStoreSquashTimes : getBooleanInput(Options.NeverStoreSquashTimes);
const zipConformanceLevel = zipSpecConformanceLevel !== undefined ? zipSpecConformanceLevel : getInput(Options.ZipSpecConformanceLevel);

return !neverStoreTimes && zipConformanceLevel !== 'pedantic';
}

export async function generateOptionsFile(workingDirectory: WorkingDirectory) {
await writeFile(workingDirectory.optionsFile, getOptionsFileContent(workingDirectory), 'utf8');
}
Expand All @@ -215,9 +221,16 @@ export async function tweakAndCopyUserOptionsFile(path: string, workingDirectory
warning('The custom options file sets the output_file_path option, but the action will ignore its value. Please remove it from the options file');
}
options.output_file_path = workingDirectory.outputFile;

if (typeof options.pack_directory === 'string') {
packDirectory = options.pack_directory;
}
if (typeof options.never_store_squash_times === 'boolean') {
neverStoreSquashTimes = options.never_store_squash_times;
}
if (typeof options.zip_spec_conformance_level === 'string') {
zipSpecConformanceLevel = options.zip_spec_conformance_level;
}

await writeFile(workingDirectory.optionsFile, TOML.stringify(options), 'utf-8');
}
Expand Down

0 comments on commit 091f0ba

Please sign in to comment.