Skip to content

Commit

Permalink
new build pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
ranm8 committed Jan 17, 2023
1 parent e42b7da commit 5252520
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 32 deletions.
7 changes: 3 additions & 4 deletions scopes/pipelines/builder/builder-env-type.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { EnvHandler } from "@teambit/envs";
import { Pipeline } from "./pipeline";

export interface BuilderEnv {
/**
* return a build pipeline instance.
*/
build(): EnvHandler<Pipeline>;
build(): Pipeline;
/**
* return a snap pipeline instance.
*/
snap(): EnvHandler<Pipeline>;
snap(): Pipeline;
/**
* return a tag pipeline instance.
*/
tag(): EnvHandler<Pipeline>;
tag(): Pipeline;
}
7 changes: 3 additions & 4 deletions scopes/pipelines/builder/builder.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,25 +145,24 @@ export class BuilderService implements EnvService<BuildServiceResults, BuilderDe
}

transform(env: Env, envContext: EnvContext): BuilderTransformationMap | undefined {
// Old env
if (!env?.build) return undefined;
return {
getBuildPipe: () => {
// TODO: refactor after defining for an env property
const pipeline = env.build()(envContext);
if (!pipeline || !pipeline.compute) return [];
return pipeline?.compute();
return pipeline?.compute(envContext);
},
getTagPipe: () => {
// TODO: refactor after defining for an env property
const pipeline = env.snap()(envContext);
if (!pipeline || !pipeline.compute) return [];
return pipeline?.compute();
return pipeline?.compute(envContext);
},
getSnapPipe: () => {
const pipeline = env.tag()(envContext);
if (!pipeline || !pipeline.compute) return [];
return pipeline?.compute();
return pipeline?.compute(envContext);
},
}
}
Expand Down
3 changes: 2 additions & 1 deletion scopes/pipelines/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ export {
} from './build-task';
export type { PipeName } from './builder.service';
export type { BuilderMain, RawBuilderData, BuilderData, OnTagOpts } from './builder.main.runtime';
export { Pipeline, Task } from './pipeline';
export { Pipeline, TaskHandler } from './pipeline';
export type { PipelineReport } from './build-pipeline-result-list';
export type { BuilderEnv } from './builder-env-type';
export { BuilderAspect } from './builder.aspect';
export { WholeArtifactStorageResolver, FileStorageResolver, ArtifactStorageResolver } from './storage';
export { Artifact, ArtifactList, ArtifactFactory, ArtifactDefinition, ArtifactModelDefinition } from './artifact';
export { Task } from './task';
export { TaskResultsList } from './task-results-list';
export { ArtifactVinyl } from '@teambit/legacy/dist/consumer/component/sources/artifact';
48 changes: 25 additions & 23 deletions scopes/pipelines/builder/pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { BuildTask } from "@teambit/builder";
import { EnvContext, EnvHandler, reduceServiceHandlersFactories } from "@teambit/envs";
import { Task } from './task';

export type Task = EnvHandler<BuildTask>;
export type TaskHandler = EnvHandler<Task>;

/**
* create and maintain build pipelines for component
* dev environments.
*/
export class Pipeline {
constructor(
private _tasks: BuildTask[],
private context: EnvContext
private _tasks: TaskHandler[],
) {}

/**
Expand All @@ -20,18 +20,26 @@ export class Pipeline {
return this._tasks;
}

private initiateTasks(tasks: Task[]) {
return tasks.map((task) => {
return task(this.context);
private initiateTasks(tasks: TaskHandler[], context: EnvContext, envId: string) {
const _tasks = tasks.map((task) => {
return task(context);
});

return _tasks.map((task) => {
const buildTask: BuildTask = {
aspectId: envId,
...task
};

return buildTask;
});
}

/**
* add a build task to the pipeline.
*/
add(tasks: Task[]) {
const buildTasks = this.initiateTasks(tasks);
this._tasks = this._tasks.concat(buildTasks);
add(tasks: TaskHandler[]) {
this._tasks = this._tasks.concat(tasks);
return this;
}

Expand All @@ -47,9 +55,8 @@ export class Pipeline {
/**
* replace a build task in the pipeline.
*/
replace(tasks: Task[]) {
const buildTasks = this.initiateTasks(tasks);
this.remove(buildTasks.map((task) => task.name));
replace(tasks: TaskHandler[]) {
this.remove(tasks.map((task) => task.name));
this.add(tasks);
return this;
}
Expand All @@ -60,24 +67,19 @@ export class Pipeline {
* @returns
*/
concat(pipeline: Pipeline) {
return new Pipeline(this._tasks.concat(pipeline.tasks), this.context);
return new Pipeline(this._tasks.concat(pipeline.tasks));
}

/**
* compute the pipeline.
*/
compute() {
return this._tasks;
compute(context: EnvContext, envId: string): BuildTask[] {
const buildTasks = this.initiateTasks(this._tasks, context, envId);
return buildTasks;
}

static from(tasks: Task[]) {
return (context: EnvContext) => {
const buildTasks = tasks.map((taskFn) => {
return taskFn(context);
});

return new Pipeline(buildTasks, context);
}
static from(tasks: TaskHandler[]) {
return new Pipeline(tasks);
}

static concat(...pipelines: EnvHandler<Pipeline>[]) {
Expand Down
50 changes: 50 additions & 0 deletions scopes/pipelines/builder/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BuildContext, BuiltTaskResult } from "./build-task";
import { TaskResultsList } from "./task-results-list";

/**
* this is the external interface for task. please make
* sure to use only this interface outside of this builder
* aspect.
*/
export interface Task {
/**
* names ideally with dashes 'typescript'
*/
name: string;

/**
* description of what the task does.
*/
description?: string;

/**
* execute a task in a build context
*/
execute(context: BuildContext): Promise<BuiltTaskResult>;

/**
* run before the build pipeline has started. this is useful when some preparation are needed to
* be done on all envs before the build starts.
* e.g. typescript compiler needs to write the tsconfig file. doing it during the task, will
* cause dependencies from other envs to get this tsconfig written.
*/
preBuild?(context: BuildContext): Promise<void>;

/**
* run after the build pipeline completed for all envs. useful for doing some cleanup on the
* capsules before the deployment starts.
*/
postBuild?(context: BuildContext, tasksResults: TaskResultsList): Promise<void>;

/**
* needed if you want the task to be running only after the dependencies were completed
* for *all* envs.
* normally this is not needed because the build-pipeline runs the tasks in the same order
* they're located in the `getBuildPipe()` array and according to the task.location.
* the case where this is useful is when a task not only needs to be after another task, but also
* after all environments were running that task.
* a dependency is task.aspectId. if an aspect has multiple tasks, to be more specific, use
* "aspectId:name", e.g. "teambit.compilation/compiler:TypescriptCompiler".
*/
dependencies?: string[];
}

0 comments on commit 5252520

Please sign in to comment.