Compare commits

...

1 Commits

Author SHA1 Message Date
Ruben Fiszel
6b2385886d all 2025-12-12 09:05:59 +00:00

View File

@@ -27,23 +27,29 @@ export class LockfileGenerationError extends Error {
export async function generateAllMetadata() {} export async function generateAllMetadata() {}
export async function getRawWorkspaceDependencies(): Promise<Record<string, string>> { export async function getRawWorkspaceDependencies(): Promise<
Record<string, string>
> {
const rawWorkspaceDeps: Record<string, string> = {}; const rawWorkspaceDeps: Record<string, string> = {};
try { try {
for await (const entry of Deno.readDir("dependencies")) { for await (const entry of Deno.readDir("dependencies")) {
if (entry.isDirectory) continue; if (entry.isDirectory) continue;
const filePath = `dependencies/${entry.name}`; const filePath = `dependencies/${entry.name}`;
const content = await Deno.readTextFile(filePath); const content = await Deno.readTextFile(filePath);
// Find matching language // Find matching language
for (const lang of workspaceDependenciesLanguages) { for (const lang of workspaceDependenciesLanguages) {
if (entry.name.endsWith(lang.filename)) { if (entry.name.endsWith(lang.filename)) {
// Check if out of sync // Check if out of sync
const contentHash = await generateHash(content + filePath); const contentHash = await generateHash(content + filePath);
const isUpToDate = await checkifMetadataUptodate(filePath, contentHash, undefined); const isUpToDate = await checkifMetadataUptodate(
filePath,
contentHash,
undefined
);
if (!isUpToDate) { if (!isUpToDate) {
rawWorkspaceDeps[filePath] = content; rawWorkspaceDeps[filePath] = content;
} }
@@ -54,19 +60,24 @@ export async function getRawWorkspaceDependencies(): Promise<Record<string, stri
} catch { } catch {
// dependencies directory doesn't exist // dependencies directory doesn't exist
} }
return rawWorkspaceDeps; return rawWorkspaceDeps;
} }
export function workspaceDependenciesPathToLanguageAndFilename(path: string): { name: string | undefined, language: ScriptLanguage } | undefined { export function workspaceDependenciesPathToLanguageAndFilename(
const relativePath = path.replace("dependencies/", ""); path: string
for (const { filename, language } of workspaceDependenciesLanguages) { ): { name: string | undefined; language: ScriptLanguage } | undefined {
if (relativePath.endsWith(filename)) { const relativePath = path.replace("dependencies/", "");
return { for (const { filename, language } of workspaceDependenciesLanguages) {
name: relativePath === filename ? undefined : relativePath.replace("." + filename, ""), if (relativePath.endsWith(filename)) {
language return {
}; name:
} relativePath === filename
? undefined
: relativePath.replace("." + filename, ""),
language,
};
} }
}
} }
// on windows, when using powershell, blue is not readable // on windows, when using powershell, blue is not readable
@@ -97,24 +108,27 @@ export async function generateScriptMetadataInternal(
// Filter workspace dependencies to only include those matching the script's language // Filter workspace dependencies to only include those matching the script's language
const filteredRawWorkspaceDependencies: Record<string, string> = {}; const filteredRawWorkspaceDependencies: Record<string, string> = {};
for (const [depPath, depContent] of Object.entries(rawWorkspaceDependencies)) { for (const [depPath, depContent] of Object.entries(
rawWorkspaceDependencies
)) {
const depInfo = workspaceDependenciesPathToLanguageAndFilename(depPath); const depInfo = workspaceDependenciesPathToLanguageAndFilename(depPath);
if (depInfo && depInfo.language === language) { if (depInfo && depInfo.language === language) {
filteredRawWorkspaceDependencies[depPath] = depContent; filteredRawWorkspaceDependencies[depPath] = depContent;
} }
} }
const metadataWithType = await parseMetadataFile( const metadataWithType = await parseMetadataFile(remotePath, undefined);
remotePath,
undefined,
);
// read script content // read script content
const scriptContent = await Deno.readTextFile(scriptPath); const scriptContent = await Deno.readTextFile(scriptPath);
const metadataContent = await Deno.readTextFile(metadataWithType.path); const metadataContent = await Deno.readTextFile(metadataWithType.path);
// Note: rawWorkspaceDependencies are now passed in as parameter instead of being searched hierarchically // Note: rawWorkspaceDependencies are now passed in as parameter instead of being searched hierarchically
let hash = await generateScriptHash(filteredRawWorkspaceDependencies, scriptContent, metadataContent); let hash = await generateScriptHash(
filteredRawWorkspaceDependencies,
scriptContent,
metadataContent
);
if (await checkifMetadataUptodate(remotePath, hash, undefined)) { if (await checkifMetadataUptodate(remotePath, hash, undefined)) {
if (!noStaleMessage) { if (!noStaleMessage) {
@@ -160,7 +174,7 @@ export async function generateScriptMetadataInternal(
} else { } else {
metadataParsedContent.lock = ""; metadataParsedContent.lock = "";
} }
} else { } else if (!opts.schemaOnly || metadataParsedContent.lock != "") {
metadataParsedContent.lock = metadataParsedContent.lock =
"!inline " + remotePath.replaceAll(SEP, "/") + ".script.lock"; "!inline " + remotePath.replaceAll(SEP, "/") + ".script.lock";
} }
@@ -230,12 +244,13 @@ async function updateScriptLock(
return; return;
} }
if (Object.keys(rawWorkspaceDependencies).length > 0) { if (Object.keys(rawWorkspaceDependencies).length > 0) {
const dependencyPaths = Object.keys(rawWorkspaceDependencies).join(', '); const dependencyPaths = Object.keys(rawWorkspaceDependencies).join(", ");
log.info(`Generating script lock for ${remotePath} with raw workspace dependencies: ${dependencyPaths}`); log.info(
`Generating script lock for ${remotePath} with raw workspace dependencies: ${dependencyPaths}`
);
} }
// generate the script lock running a dependency job in Windmill and update it inplace // generate the script lock running a dependency job in Windmill and update it inplace
// TODO: update this once the client is released // TODO: update this once the client is released
const extraHeaders = getHeaders(); const extraHeaders = getHeaders();
@@ -256,8 +271,10 @@ async function updateScriptLock(
script_path: remotePath, script_path: remotePath,
}, },
], ],
raw_workspace_dependencies: Object.keys(rawWorkspaceDependencies).length > 0 raw_workspace_dependencies:
? rawWorkspaceDependencies : null, Object.keys(rawWorkspaceDependencies).length > 0
? rawWorkspaceDependencies
: null,
entrypoint: remotePath, entrypoint: remotePath,
}), }),
} }
@@ -543,7 +560,7 @@ export async function parseMetadataFile(
workspaceRemote: Workspace; workspaceRemote: Workspace;
schemaOnly?: boolean; schemaOnly?: boolean;
rawWorkspaceDependencies: Record<string, string>; rawWorkspaceDependencies: Record<string, string>;
codebases: SyncCodebase[] codebases: SyncCodebase[];
}) })
| undefined | undefined
): Promise<{ isJson: boolean; payload: any; path: string }> { ): Promise<{ isJson: boolean; payload: any; path: string }> {
@@ -577,7 +594,11 @@ export async function parseMetadataFile(
metadataFilePath = scriptPath + ".script.yaml"; metadataFilePath = scriptPath + ".script.yaml";
let scriptInitialMetadata = defaultScriptMetadata(); let scriptInitialMetadata = defaultScriptMetadata();
const lockPath = scriptPath + ".script.lock"; const lockPath = scriptPath + ".script.lock";
scriptInitialMetadata.lock = "!inline " + lockPath;
if (generateMetadataIfMissing && !generateMetadataIfMissing?.schemaOnly) {
log.info(`Using inline lock for ${metadataFilePath}`);
scriptInitialMetadata.lock = "!inline " + lockPath;
}
const scriptInitialMetadataYaml = yamlStringify( const scriptInitialMetadataYaml = yamlStringify(
scriptInitialMetadata as Record<string, any>, scriptInitialMetadata as Record<string, any>,
yamlOptions yamlOptions
@@ -586,11 +607,14 @@ export async function parseMetadataFile(
await Deno.writeTextFile(metadataFilePath, scriptInitialMetadataYaml, { await Deno.writeTextFile(metadataFilePath, scriptInitialMetadataYaml, {
createNew: true, createNew: true,
}); });
await Deno.writeTextFile(lockPath, "", {
createNew: true,
});
if (generateMetadataIfMissing) { if (generateMetadataIfMissing) {
if (!generateMetadataIfMissing.schemaOnly) {
await Deno.writeTextFile(lockPath, "", {
createNew: true,
});
}
log.info( log.info(
(await blueColor())( (await blueColor())(
`Generating lockfile and schema for ${metadataFilePath}` `Generating lockfile and schema for ${metadataFilePath}`
@@ -690,7 +714,9 @@ export async function generateScriptHash(
newMetadataContent: string newMetadataContent: string
) { ) {
return await generateHash( return await generateHash(
JSON.stringify(rawWorkspaceDependencies) + scriptContent + newMetadataContent JSON.stringify(rawWorkspaceDependencies) +
scriptContent +
newMetadataContent
); );
} }