From e58081ea4528beb2694726d928d1826c3dfc3aa5 Mon Sep 17 00:00:00 2001 From: David First Date: Fri, 26 Jan 2024 15:19:38 -0500 Subject: [PATCH 1/2] refactor, move auto-tag from legacy to a new component teambit.workspace/modules/auto-tag --- .bitmap | 8 ++++++++ .../component/merging/merging.main.runtime.ts | 2 +- .../component/snapping/snapping.main.runtime.ts | 2 +- .../component/snapping/tag-model-component.ts | 2 +- .../workspace/modules/auto-tag}/auto-tag.ts | 6 +++--- scopes/workspace/modules/auto-tag/index.ts | 1 + scopes/workspace/workspace/workspace.ts | 5 ++--- src/consumer/consumer.ts | 5 ----- src/scope/component-ops/auto-tag.spec.ts | 17 ----------------- 9 files changed, 17 insertions(+), 31 deletions(-) rename {src/scope/component-ops => scopes/workspace/modules/auto-tag}/auto-tag.ts (95%) create mode 100644 scopes/workspace/modules/auto-tag/index.ts delete mode 100644 src/scope/component-ops/auto-tag.spec.ts diff --git a/.bitmap b/.bitmap index 3c8c82d9d74..8440c6bfe6d 100644 --- a/.bitmap +++ b/.bitmap @@ -863,6 +863,14 @@ "mainFile": "index.ts", "rootDir": "scopes/scope/models/scope-model" }, + "modules/auto-tag": { + "name": "modules/auto-tag", + "scope": "", + "version": "", + "defaultScope": "teambit.workspace", + "mainFile": "index.ts", + "rootDir": "scopes/workspace/modules/auto-tag" + }, "modules/babel-compiler": { "name": "modules/babel-compiler", "scope": "teambit.compilation", diff --git a/scopes/component/merging/merging.main.runtime.ts b/scopes/component/merging/merging.main.runtime.ts index c5954a6f881..4b895f97eef 100644 --- a/scopes/component/merging/merging.main.runtime.ts +++ b/scopes/component/merging/merging.main.runtime.ts @@ -15,7 +15,7 @@ import { ComponentID, ComponentIdList } from '@teambit/component-id'; import { BitError } from '@teambit/bit-error'; import GeneralError from '@teambit/legacy/dist/error/general-error'; import { LaneId } from '@teambit/lane-id'; -import { AutoTagResult } from '@teambit/legacy/dist/scope/component-ops/auto-tag'; +import { AutoTagResult } from '@teambit/workspace.modules.auto-tag'; import { UnmergedComponent } from '@teambit/legacy/dist/scope/lanes/unmerged-components'; import { Lane, ModelComponent } from '@teambit/legacy/dist/scope/models'; import { Ref } from '@teambit/legacy/dist/scope/objects'; diff --git a/scopes/component/snapping/snapping.main.runtime.ts b/scopes/component/snapping/snapping.main.runtime.ts index e663adc3e8b..e92e255b68f 100644 --- a/scopes/component/snapping/snapping.main.runtime.ts +++ b/scopes/component/snapping/snapping.main.runtime.ts @@ -40,7 +40,7 @@ import { getArtifactsFiles, } from '@teambit/legacy/dist/consumer/component/sources/artifact-files'; import { VersionNotFound, ComponentNotFound } from '@teambit/legacy/dist/scope/exceptions'; -import { AutoTagResult } from '@teambit/legacy/dist/scope/component-ops/auto-tag'; +import { AutoTagResult } from '@teambit/workspace.modules.auto-tag'; import DependenciesAspect, { DependenciesMain } from '@teambit/dependencies'; import { SourceFile } from '@teambit/legacy/dist/consumer/component/sources'; import Version, { DepEdge, DepEdgeType, Log } from '@teambit/legacy/dist/scope/models/version'; diff --git a/scopes/component/snapping/tag-model-component.ts b/scopes/component/snapping/tag-model-component.ts index 83d4412f22b..4388c1ab2ec 100644 --- a/scopes/component/snapping/tag-model-component.ts +++ b/scopes/component/snapping/tag-model-component.ts @@ -16,7 +16,7 @@ import { Component } from '@teambit/component'; import deleteComponentsFiles from '@teambit/legacy/dist/consumer/component-ops/delete-component-files'; import logger from '@teambit/legacy/dist/logger/logger'; import { sha1 } from '@teambit/legacy/dist/utils'; -import { AutoTagResult, getAutoTagInfo } from '@teambit/legacy/dist/scope/component-ops/auto-tag'; +import { AutoTagResult, getAutoTagInfo } from '@teambit/workspace.modules.auto-tag'; import { getValidVersionOrReleaseType } from '@teambit/legacy/dist/utils/semver-helper'; import { BuilderMain, OnTagOpts } from '@teambit/builder'; import { Log } from '@teambit/legacy/dist/scope/models/version'; diff --git a/src/scope/component-ops/auto-tag.ts b/scopes/workspace/modules/auto-tag/auto-tag.ts similarity index 95% rename from src/scope/component-ops/auto-tag.ts rename to scopes/workspace/modules/auto-tag/auto-tag.ts index 6a98166ef5e..5d07027ed03 100644 --- a/src/scope/component-ops/auto-tag.ts +++ b/scopes/workspace/modules/auto-tag/auto-tag.ts @@ -2,9 +2,9 @@ import graphlib, { Graph } from 'graphlib'; import semver from 'semver'; import { ComponentIdList } from '@teambit/component-id'; import { isTag } from '@teambit/component-version'; -import { Consumer } from '../../consumer'; -import Component from '../../consumer/component/consumer-component'; -import { Dependency } from '../../consumer/component/dependencies'; +import { Consumer } from '@teambit/legacy/dist/consumer'; +import Component from '@teambit/legacy/dist/consumer/component/consumer-component'; +import { Dependency } from '@teambit/legacy/dist/consumer/component/dependencies'; export async function getAutoTagPending(consumer: Consumer, changedComponents: ComponentIdList): Promise { const autoTagInfo = await getAutoTagInfo(consumer, changedComponents); diff --git a/scopes/workspace/modules/auto-tag/index.ts b/scopes/workspace/modules/auto-tag/index.ts new file mode 100644 index 00000000000..1d3afa44207 --- /dev/null +++ b/scopes/workspace/modules/auto-tag/index.ts @@ -0,0 +1 @@ +export { getAutoTagInfo, getAutoTagPending, AutoTagResult } from './auto-tag'; diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 6f9b3f2e8f2..647eb425296 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -60,6 +60,7 @@ import { Lane, Version } from '@teambit/legacy/dist/scope/models'; import { LaneNotFound } from '@teambit/legacy/dist/api/scope/lib/exceptions/lane-not-found'; import { ScopeNotFoundOrDenied } from '@teambit/legacy/dist/remotes/exceptions/scope-not-found-or-denied'; import { isHash } from '@teambit/component-version'; +import { getAutoTagPending } from '@teambit/workspace.modules.auto-tag'; import { GlobalConfigMain } from '@teambit/global-config'; import { ComponentConfigFile } from './component-config-file'; import { @@ -340,9 +341,7 @@ export class Workspace implements ComponentFactory { const modifiedComponents = (await this.modified()).map((c) => c.id); const newComponents = (await componentsList.listNewComponents()) as ComponentIdList; if (!modifiedComponents || !modifiedComponents.length) return []; - const autoTagPending = await this.consumer.listComponentsForAutoTagging( - ComponentIdList.fromArray(modifiedComponents) - ); + const autoTagPending = await getAutoTagPending(this.consumer, ComponentIdList.fromArray(modifiedComponents)); const comps = autoTagPending.filter((autoTagComp) => !newComponents.has(autoTagComp.componentId)); return comps.map((c) => c.id); } diff --git a/src/consumer/consumer.ts b/src/consumer/consumer.ts index ed36f83e1f5..d567ad2237d 100644 --- a/src/consumer/consumer.ts +++ b/src/consumer/consumer.ts @@ -16,7 +16,6 @@ import { } from '../constants'; import logger from '../logger/logger'; import { Scope } from '../scope'; -import { getAutoTagPending } from '../scope/component-ops/auto-tag'; import { ComponentNotFound } from '../scope/exceptions'; import { Lane, ModelComponent, Version } from '../scope/models'; import { generateRandomStr, sortObject } from '../utils'; @@ -292,10 +291,6 @@ export default class Consumer { return this.componentLoader.loadMany(ids, throwOnFailure, loadOpts); } - async listComponentsForAutoTagging(modifiedComponents: ComponentIdList): Promise { - return getAutoTagPending(this, modifiedComponents); - } - /** * Check whether a model representation and file-system representation of the same component is the same. * The way how it is done is by converting the file-system representation of the component into diff --git a/src/scope/component-ops/auto-tag.spec.ts b/src/scope/component-ops/auto-tag.spec.ts deleted file mode 100644 index ff296938a35..00000000000 --- a/src/scope/component-ops/auto-tag.spec.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { expect } from 'chai'; - -import { getAutoTagPending } from './auto-tag'; - -describe('AutoTag', () => { - describe('getAutoTagPending', () => { - it('should return an empty array when there are no components in the workspace', async () => { - const consumer = { - bitMap: { getAllBitIds: () => [] }, - loadComponents: () => Promise.resolve({ components: [] }), - }; - // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! - const test = await getAutoTagPending(consumer, []); - expect(test).to.be.an('array').and.empty; - }); - }); -}); From d3069a6595b9bfe35df5a10c1b8b7293f34bcfa2 Mon Sep 17 00:00:00 2001 From: David First Date: Fri, 26 Jan 2024 16:33:56 -0500 Subject: [PATCH 2/2] change loadComponents from legacy to harmony --- .../component/snapping/tag-model-component.ts | 15 ++++++++-- scopes/workspace/modules/auto-tag/auto-tag.ts | 29 ++++++++++++------- scopes/workspace/workspace/workspace.ts | 4 +-- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/scopes/component/snapping/tag-model-component.ts b/scopes/component/snapping/tag-model-component.ts index 4388c1ab2ec..bc843dc4e8a 100644 --- a/scopes/component/snapping/tag-model-component.ts +++ b/scopes/component/snapping/tag-model-component.ts @@ -234,9 +234,11 @@ export async function tagModelComponent({ // them as dependencies. const idsToTriggerAutoTag = idsToTag.filter((id) => id.hasVersion()); const autoTagData = - skipAutoTag || !consumer ? [] : await getAutoTagInfo(consumer, ComponentIdList.fromArray(idsToTriggerAutoTag)); + skipAutoTag || !workspace ? [] : await getAutoTagInfo(workspace, ComponentIdList.fromArray(idsToTriggerAutoTag)); const autoTagComponents = autoTagData.map((autoTagItem) => autoTagItem.component); - const autoTagComponentsFiltered = autoTagComponents.filter((c) => !idsToTag.has(c.id)); + const autoTagConsumerComponents = autoTagComponents.map((c) => c.state._consumer) as ConsumerComponent[]; + const autoTagComponentsFiltered = autoTagConsumerComponents.filter((c) => !idsToTag.has(c.id)); + const autoTagIds = ComponentIdList.fromArray(autoTagComponentsFiltered.map((autoTag) => autoTag.id)); const allComponentsToTag = [...componentsToTag, ...autoTagComponentsFiltered]; @@ -296,7 +298,14 @@ export async function tagModelComponent({ // go through all dependencies and update their versions updateDependenciesVersions(allComponentsToTag, dependencyResolver); - await addLogToComponents(componentsToTag, autoTagComponents, persist, message, messagePerId, copyLogFromPreviousSnap); + await addLogToComponents( + componentsToTag, + autoTagConsumerComponents, + persist, + message, + messagePerId, + copyLogFromPreviousSnap + ); // don't move it down. otherwise, it'll be empty and we don't know which components were during merge. // (it's being deleted in snapping.main.runtime - `_addCompToObjects` method) const unmergedComps = workspace ? await workspace.listComponentsDuringMerge() : []; diff --git a/scopes/workspace/modules/auto-tag/auto-tag.ts b/scopes/workspace/modules/auto-tag/auto-tag.ts index 5d07027ed03..dc597b8c785 100644 --- a/scopes/workspace/modules/auto-tag/auto-tag.ts +++ b/scopes/workspace/modules/auto-tag/auto-tag.ts @@ -2,27 +2,34 @@ import graphlib, { Graph } from 'graphlib'; import semver from 'semver'; import { ComponentIdList } from '@teambit/component-id'; import { isTag } from '@teambit/component-version'; -import { Consumer } from '@teambit/legacy/dist/consumer'; -import Component from '@teambit/legacy/dist/consumer/component/consumer-component'; +// import Component from '@teambit/legacy/dist/consumer/component/consumer-component'; import { Dependency } from '@teambit/legacy/dist/consumer/component/dependencies'; +import { Workspace } from '@teambit/workspace'; +import { Component } from '@teambit/component'; -export async function getAutoTagPending(consumer: Consumer, changedComponents: ComponentIdList): Promise { - const autoTagInfo = await getAutoTagInfo(consumer, changedComponents); +export async function getAutoTagPending( + workspace: Workspace, + changedComponents: ComponentIdList +): Promise { + const autoTagInfo = await getAutoTagInfo(workspace, changedComponents); return autoTagInfo.map((a) => a.component); } export type AutoTagResult = { component: Component; triggeredBy: ComponentIdList }; -export async function getAutoTagInfo(consumer: Consumer, changedComponents: ComponentIdList): Promise { +export async function getAutoTagInfo( + workspace: Workspace, + changedComponents: ComponentIdList +): Promise { if (!changedComponents.length) return []; - const potentialComponents = potentialComponentsForAutoTagging(consumer, changedComponents); + const potentialComponents = potentialComponentsForAutoTagging(workspace, changedComponents); const idsToLoad = new ComponentIdList(...potentialComponents, ...changedComponents); - const { components } = await consumer.loadComponents(idsToLoad, false); + const { components } = await workspace.componentLoader.getMany(idsToLoad, undefined, false); const graph = buildGraph(components); const autoTagResults: AutoTagResult[] = []; components.forEach((component) => { - const bitId = component.componentId; + const bitId = component.id; const idStr = bitId.toStringWithoutVersion(); if (!graph.hasNode(idStr)) return; // preorder gets all dependencies and dependencies of dependencies and so on. @@ -70,7 +77,7 @@ function buildGraph(components: Component[]): Graph { components.forEach((component) => { const idStr = component.id.toStringWithoutVersion(); - component.getAllDependencies().forEach((dependency: Dependency) => { + component.state._consumer.getAllDependencies().forEach((dependency: Dependency) => { if (componentsIds.searchWithoutVersion(dependency.id)) { const depId = dependency.id.toStringWithoutVersion(); // save the full ComponentID of a string id to be able to retrieve it later with no confusion @@ -83,8 +90,8 @@ function buildGraph(components: Component[]): Graph { return graph; } -function potentialComponentsForAutoTagging(consumer: Consumer, modifiedComponents: ComponentIdList): ComponentIdList { - const candidateComponentsIds = consumer.bitMap.getAllBitIds(); +function potentialComponentsForAutoTagging(workspace: Workspace, modifiedComponents: ComponentIdList): ComponentIdList { + const candidateComponentsIds = workspace.consumer.bitMap.getAllBitIds(); // if a modified component is in candidates array, remove it from the array as it will be already // tagged with the correct version const idsWithoutModified = candidateComponentsIds.filter( diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 647eb425296..669b5560f3b 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -341,8 +341,8 @@ export class Workspace implements ComponentFactory { const modifiedComponents = (await this.modified()).map((c) => c.id); const newComponents = (await componentsList.listNewComponents()) as ComponentIdList; if (!modifiedComponents || !modifiedComponents.length) return []; - const autoTagPending = await getAutoTagPending(this.consumer, ComponentIdList.fromArray(modifiedComponents)); - const comps = autoTagPending.filter((autoTagComp) => !newComponents.has(autoTagComp.componentId)); + const autoTagPending = await getAutoTagPending(this, ComponentIdList.fromArray(modifiedComponents)); + const comps = autoTagPending.filter((autoTagComp) => !newComponents.has(autoTagComp.id)); return comps.map((c) => c.id); }