-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
162 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { deep_eql } from "../deps/common.ts"; | ||
import getLogger from "../utils/logger.ts"; | ||
|
||
const logger = getLogger(import.meta); | ||
|
||
type Var = | ||
| { kind: "static"; value: string; parentName: string } | ||
| { kind: "dynamic"; taskId: string; parentName: string }; | ||
|
||
export class ParentEnvs { | ||
#childName: string; | ||
#vars: Map<string, Var> = new Map(); | ||
#installs: Set<string> = new Set(); | ||
#onEnterHooks: string[] = []; | ||
#onExitHooks: string[] = []; | ||
#allowedBuildDeps: Map<string, [string, string]> = new Map(); | ||
|
||
constructor(childName: string) { | ||
this.#childName = childName; | ||
} | ||
|
||
addHooks(onEnterHooks: string[], onExitHooks: string[]) { | ||
this.#onEnterHooks.push(...onEnterHooks); | ||
this.#onExitHooks.push(...onExitHooks); | ||
} | ||
|
||
mergeVars(parentName: string, vars: Record<string, string>) { | ||
for (const [key, value] of Object.entries(vars)) { | ||
const conflict = this.#vars.get(key); | ||
|
||
if ( | ||
conflict && !(conflict.kind === "static" && conflict.value === value) | ||
) { | ||
logger.warn( | ||
"environment variable conflict on multiple env inheritance, parent 2 was chosen", | ||
{ | ||
child: this.#childName, | ||
parent1: conflict.parentName, | ||
parent2: parentName, | ||
variable: key, | ||
}, | ||
); | ||
} | ||
|
||
this.#vars.set(key, { kind: "static", value, parentName }); | ||
} | ||
} | ||
|
||
mergeDynVars(parentName: string, dynVars: Record<string, string>) { | ||
for (const [key, taskId] of Object.entries(dynVars)) { | ||
const conflict = this.#vars.get(key); | ||
|
||
if ( | ||
conflict && !(conflict.kind === "dynamic" && conflict.taskId === taskId) | ||
) { | ||
logger.warn( | ||
"dynamic environment variable conflict on multiple env inheritance, parent 2 was chosen", | ||
{ | ||
child: this.#childName, | ||
parent1: conflict.parentName, | ||
parent2: parentName, | ||
variable: key, | ||
}, | ||
); | ||
} | ||
|
||
this.#vars.set(key, { kind: "dynamic", taskId, parentName }); | ||
} | ||
} | ||
|
||
mergeInstalls( | ||
parentName: string, | ||
installs: Set<string>, | ||
allowedBuildDeps: Record<string, string>, | ||
) { | ||
this.#installs = this.#installs.union(installs); | ||
|
||
for (const [key, val] of Object.entries(allowedBuildDeps)) { | ||
const conflict = this.#allowedBuildDeps.get(key); | ||
if (conflict && !deep_eql(val, conflict[0])) { | ||
logger.warn( | ||
"allowedBuildDeps conflict on multiple env inheritance, parent 2 was chosen", | ||
{ | ||
child: this.#childName, | ||
parent1: conflict[1], | ||
parent2: parentName, | ||
variable: key, | ||
}, | ||
); | ||
} | ||
|
||
this.#allowedBuildDeps.set(key, [val, parentName]); | ||
} | ||
} | ||
|
||
finalize() { | ||
const vars: Record<string, string> = {}; | ||
const dynVars: Record<string, string> = {}; | ||
|
||
for (const [key, value] of this.#vars) { | ||
if (value.kind === "static") { | ||
vars[key] = value.value; | ||
} else { | ||
dynVars[key] = value.taskId; | ||
} | ||
} | ||
|
||
return { | ||
installSet: { | ||
installs: this.#installs, | ||
allowedBuildDeps: Object.fromEntries( | ||
[...this.#allowedBuildDeps.entries()].map(( | ||
[key, [val]], | ||
) => [key, val]), | ||
), | ||
}, | ||
onEnterHookTasks: this.#onEnterHooks, | ||
onExitHookTasks: this.#onExitHooks, | ||
vars, | ||
dynVars, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters