Skip to content

Commit

Permalink
Refactor executable IDs to use strings $262
Browse files Browse the repository at this point in the history
This commit unifies executable ID structure across categories and
scripts, paving the way for more complex ID solutions for $262.
It also refactors related code to adapt to the changes.

Key changes:

- Change numeric IDs to string IDs for categories
- Use named types for string IDs to improve code clarity
- Add unit tests to verify ID uniqueness

Other supporting changes:

- Separate concerns in entities for data access and executables by using
  separate abstractions (`Identifiable` and `RepositoryEntity`)
- Simplify usage and construction of entities.
- Remove `BaseEntity` for simplicity.
- Move creation of categories/scripts to domain layer
- Refactor CategoryCollection for better validation logic isolation
- Rename some categories to keep the names (used as pseudo-IDs) unique
  on Windows.
  • Loading branch information
undergroundwires committed Aug 3, 2024
1 parent 6fbc816 commit ad2e1a0
Show file tree
Hide file tree
Showing 115 changed files with 2,232 additions and 1,285 deletions.
2 changes: 2 additions & 0 deletions docs/collection-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Related documentation:

### Executables

They represent independently executable actions with documentation and reversibility.

An Executable is a logical entity that can

- execute once compiled,
Expand Down
2 changes: 1 addition & 1 deletion src/application/Context/ApplicationContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IApplication } from '@/domain/IApplication';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { EventSource } from '@/infrastructure/Events/EventSource';
import { assertInRange } from '@/application/Common/Enum';
import { CategoryCollectionState } from './State/CategoryCollectionState';
Expand Down
2 changes: 1 addition & 1 deletion src/application/Context/State/CategoryCollectionState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { OperatingSystem } from '@/domain/OperatingSystem';
import { AdaptiveFilterContext } from './Filter/AdaptiveFilterContext';
import { ApplicationCode } from './Code/ApplicationCode';
Expand Down
4 changes: 2 additions & 2 deletions src/application/Context/State/Code/Event/CodeChangedEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export class CodeChangedEvent implements ICodeChangedEvent {
}

public getScriptPositionInCode(script: Script): ICodePosition {
return this.getPositionById(script.id);
return this.getPositionById(script.executableId);
}

private getPositionById(scriptId: string): ICodePosition {
const position = [...this.scripts.entries()]
.filter(([s]) => s.id === scriptId)
.filter(([s]) => s.executableId === scriptId)
.map(([, pos]) => pos)
.at(0);
if (!position) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventSource } from '@/infrastructure/Events/EventSource';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { FilterChange } from './Event/FilterChange';
import { LinearFilterStrategy } from './Strategy/LinearFilterStrategy';
import type { FilterResult } from './Result/FilterResult';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import type { FilterResult } from '../Result/FilterResult';

export interface FilterStrategy {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Category } from '@/domain/Executables/Category/Category';
import type { ScriptCode } from '@/domain/Executables/Script/Code/ScriptCode';
import type { Documentable } from '@/domain/Executables/Documentable';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import type { Script } from '@/domain/Executables/Script/Script';
import { AppliedFilterResult } from '../Result/AppliedFilterResult';
import type { FilterStrategy } from './FilterStrategy';
Expand Down
2 changes: 1 addition & 1 deletion src/application/Context/State/ICategoryCollectionState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { IApplicationCode } from './Code/IApplicationCode';
import type { ReadonlyFilterContext, FilterContext } from './Filter/FilterContext';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ExecutableId } from '@/domain/Executables/Identifiable';

type CategorySelectionStatus = {
readonly isSelected: true;
readonly isReverted: boolean;
Expand All @@ -6,7 +8,7 @@ type CategorySelectionStatus = {
};

export interface CategorySelectionChange {
readonly categoryId: number;
readonly categoryId: ExecutableId;
readonly newStatus: CategorySelectionStatus;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Category } from '@/domain/Executables/Category/Category';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import type { CategorySelectionChange, CategorySelectionChangeCommand } from './CategorySelectionChange';
import type { CategorySelection } from './CategorySelection';
import type { ScriptSelection } from '../Script/ScriptSelection';
Expand All @@ -23,7 +23,7 @@ export class ScriptToCategorySelectionMapper implements CategorySelection {
return false;
}
return scripts.every(
(script) => selectedScripts.some((selected) => selected.id === script.id),
(script) => selectedScripts.some((selected) => selected.id === script.executableId),
);
}

Expand All @@ -50,7 +50,7 @@ export class ScriptToCategorySelectionMapper implements CategorySelection {
const scripts = category.getAllScriptsRecursively();
const scriptsChangesInCategory = scripts
.map((script): ScriptSelectionChange => ({
scriptId: script.id,
scriptId: script.executableId,
newStatus: {
...change.newStatus,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InMemoryRepository } from '@/infrastructure/Repository/InMemoryReposito
import type { Script } from '@/domain/Executables/Script/Script';
import { EventSource } from '@/infrastructure/Events/EventSource';
import type { ReadonlyRepository, Repository } from '@/application/Repository/Repository';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { batchedDebounce } from '@/application/Common/Timing/BatchedDebounce';
import { UserSelectedScript } from './UserSelectedScript';
import type { ScriptSelection } from './ScriptSelection';
Expand All @@ -16,7 +16,7 @@ export type DebounceFunction = typeof batchedDebounce<ScriptSelectionChangeComma
export class DebouncedScriptSelection implements ScriptSelection {
public readonly changed = new EventSource<ReadonlyArray<SelectedScript>>();

private readonly scripts: Repository<string, SelectedScript>;
private readonly scripts: Repository<SelectedScript>;

public readonly processChanges: ScriptSelection['processChanges'];

Expand All @@ -25,7 +25,7 @@ export class DebouncedScriptSelection implements ScriptSelection {
selectedScripts: ReadonlyArray<SelectedScript>,
debounce: DebounceFunction = batchedDebounce,
) {
this.scripts = new InMemoryRepository<string, SelectedScript>();
this.scripts = new InMemoryRepository<SelectedScript>();
for (const script of selectedScripts) {
this.scripts.addItem(script);
}
Expand All @@ -49,7 +49,7 @@ export class DebouncedScriptSelection implements ScriptSelection {
public selectAll(): void {
const scriptsToSelect = this.collection
.getAllScripts()
.filter((script) => !this.scripts.exists(script.id))
.filter((script) => !this.scripts.exists(script.executableId))
.map((script) => new UserSelectedScript(script, false));
if (scriptsToSelect.length === 0) {
return;
Expand Down Expand Up @@ -116,9 +116,9 @@ export class DebouncedScriptSelection implements ScriptSelection {
private applyChange(change: ScriptSelectionChange): number {
const script = this.collection.getScript(change.scriptId);
if (change.newStatus.isSelected) {
return this.addOrUpdateScript(script.id, change.newStatus.isReverted);
return this.addOrUpdateScript(script.executableId, change.newStatus.isReverted);
}
return this.removeScript(script.id);
return this.removeScript(script.executableId);
}

private addOrUpdateScript(scriptId: string, revert: boolean): number {
Expand Down Expand Up @@ -152,24 +152,24 @@ function assertNonEmptyScriptSelection(selectedItems: readonly Script[]) {
}

function getScriptIdsToBeSelected(
existingItems: ReadonlyRepository<string, SelectedScript>,
existingItems: ReadonlyRepository<SelectedScript>,
desiredScripts: readonly Script[],
): string[] {
return desiredScripts
.filter((script) => !existingItems.exists(script.id))
.map((script) => script.id);
.filter((script) => !existingItems.exists(script.executableId))
.map((script) => script.executableId);
}

function getScriptIdsToBeDeselected(
existingItems: ReadonlyRepository<string, SelectedScript>,
existingItems: ReadonlyRepository<SelectedScript>,
desiredScripts: readonly Script[],
): string[] {
return existingItems
.getItems()
.filter((existing) => !desiredScripts.some((script) => existing.id === script.id))
.filter((existing) => !desiredScripts.some((script) => existing.id === script.executableId))
.map((script) => script.id);
}

function equals(a: SelectedScript, b: SelectedScript): boolean {
return a.script.equals(b.script.id) && a.revert === b.revert;
return a.script.executableId === b.script.executableId && a.revert === b.revert;
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { IEntity } from '@/infrastructure/Entity/IEntity';
import type { Script } from '@/domain/Executables/Script/Script';
import type { RepositoryEntity } from '@/application/Repository/RepositoryEntity';

type ScriptId = Script['id'];

export interface SelectedScript extends IEntity<ScriptId> {
export interface SelectedScript extends RepositoryEntity {
readonly script: Script;
readonly revert: boolean;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { BaseEntity } from '@/infrastructure/Entity/BaseEntity';
import type { Script } from '@/domain/Executables/Script/Script';
import type { SelectedScript } from './SelectedScript';
import type { RepositoryEntity } from '@/application/Repository/RepositoryEntity';

type SelectedScriptId = SelectedScript['id'];
export class UserSelectedScript implements RepositoryEntity {
public readonly id: string;

export class UserSelectedScript extends BaseEntity<SelectedScriptId> {
constructor(
public readonly script: Script,
public readonly revert: boolean,
) {
super(script.id);
this.id = script.executableId;
if (revert && !script.canRevert()) {
throw new Error(`The script with ID '${script.id}' is not reversible and cannot be reverted.`);
throw new Error(`The script with ID '${script.executableId}' is not reversible and cannot be reverted.`);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { ScriptToCategorySelectionMapper } from './Category/ScriptToCategorySelectionMapper';
import { DebouncedScriptSelection } from './Script/DebouncedScriptSelection';
import type { CategorySelection } from './Category/CategorySelection';
Expand Down
4 changes: 2 additions & 2 deletions src/application/Parser/CategoryCollectionParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { CollectionData } from '@/application/collections/';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import { CategoryCollection } from '@/domain/CategoryCollection';
import type { ICategoryCollection } from '@/domain/Collection/ICategoryCollection';
import { CategoryCollection } from '@/domain/Collection/CategoryCollection';
import type { ProjectDetails } from '@/domain/Project/ProjectDetails';
import { createEnumParser, type EnumParser } from '../Common/Enum';
import { parseCategory, type CategoryParser } from './Executable/CategoryParser';
Expand Down
12 changes: 3 additions & 9 deletions src/application/Parser/Executable/CategoryParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import type {
} from '@/application/collections/';
import { wrapErrorWithAdditionalContext, type ErrorWithContextWrapper } from '@/application/Parser/Common/ContextualError';
import type { Category } from '@/domain/Executables/Category/Category';
import { CollectionCategory } from '@/domain/Executables/Category/CollectionCategory';
import type { Script } from '@/domain/Executables/Script/Script';
import { createCategory, type CategoryFactory } from '@/domain/Executables/Category/CategoryFactory';
import { parseDocs, type DocsParser } from './DocumentationParser';
import { parseScript, type ScriptParser } from './Script/ScriptParser';
import { createExecutableDataValidator, type ExecutableValidator, type ExecutableValidatorFactory } from './Validation/ExecutableValidator';
import { ExecutableType } from './Validation/ExecutableType';
import type { CategoryCollectionSpecificUtilities } from './CategoryCollectionSpecificUtilities';

let categoryIdCounter = 0;

export const parseCategory: CategoryParser = (
category: CategoryData,
collectionUtilities: CategoryCollectionSpecificUtilities,
Expand Down Expand Up @@ -59,7 +57,7 @@ function parseCategoryRecursively(
}
try {
return context.categoryUtilities.createCategory({
id: categoryIdCounter++,
executableId: context.categoryData.category, // arbitrary ID
name: context.categoryData.category,
docs: context.categoryUtilities.parseDocs(context.categoryData),
subcategories: children.subcategories,
Expand Down Expand Up @@ -166,10 +164,6 @@ function hasProperty(
return Object.prototype.hasOwnProperty.call(object, propertyName);
}

export type CategoryFactory = (
...parameters: ConstructorParameters<typeof CollectionCategory>
) => Category;

interface CategoryParserUtilities {
readonly createCategory: CategoryFactory;
readonly wrapError: ErrorWithContextWrapper;
Expand All @@ -179,7 +173,7 @@ interface CategoryParserUtilities {
}

const DefaultCategoryParserUtilities: CategoryParserUtilities = {
createCategory: (...parameters) => new CollectionCategory(...parameters),
createCategory,
wrapError: wrapErrorWithAdditionalContext,
createValidator: createExecutableDataValidator,
parseScript,
Expand Down
11 changes: 2 additions & 9 deletions src/application/Parser/Executable/Script/ScriptParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ScriptData, CodeScriptData, CallScriptData } from '@/application/collections/';
import { NoEmptyLines } from '@/application/Parser/Executable/Script/Validation/Rules/NoEmptyLines';
import type { ILanguageSyntax } from '@/application/Parser/Executable/Script/Validation/Syntax/ILanguageSyntax';
import { CollectionScript } from '@/domain/Executables/Script/CollectionScript';
import { RecommendationLevel } from '@/domain/Executables/Script/RecommendationLevel';
import type { ScriptCode } from '@/domain/Executables/Script/Code/ScriptCode';
import type { ICodeValidator } from '@/application/Parser/Executable/Script/Validation/ICodeValidator';
Expand All @@ -11,6 +10,7 @@ import { createScriptCode } from '@/domain/Executables/Script/Code/ScriptCodeFac
import type { Script } from '@/domain/Executables/Script/Script';
import { createEnumParser, type EnumParser } from '@/application/Common/Enum';
import { filterEmptyStrings } from '@/application/Common/Text/FilterEmptyStrings';
import { createScript, type ScriptFactory } from '@/domain/Executables/Script/ScriptFactory';
import { parseDocs, type DocsParser } from '../DocumentationParser';
import { ExecutableType } from '../Validation/ExecutableType';
import { createExecutableDataValidator, type ExecutableValidator, type ExecutableValidatorFactory } from '../Validation/ExecutableValidator';
Expand Down Expand Up @@ -38,6 +38,7 @@ export const parseScript: ScriptParser = (
validateScript(data, validator);
try {
const script = scriptUtilities.createScript({
executableId: data.name, // arbitrary ID
name: data.name,
code: parseCode(
data,
Expand Down Expand Up @@ -132,14 +133,6 @@ interface ScriptParserUtilities {
readonly parseDocs: DocsParser;
}

export type ScriptFactory = (
...parameters: ConstructorParameters<typeof CollectionScript>
) => Script;

const createScript: ScriptFactory = (...parameters) => {
return new CollectionScript(...parameters);
};

const DefaultUtilities: ScriptParserUtilities = {
levelParser: createEnumParser(RecommendationLevel),
createScript,
Expand Down
18 changes: 10 additions & 8 deletions src/application/Repository/Repository.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import type { IEntity } from '@/infrastructure/Entity/IEntity';
import type { RepositoryEntity } from './RepositoryEntity';

export interface ReadonlyRepository<TKey, TEntity extends IEntity<TKey>> {
type EntityId = RepositoryEntity['id'];

export interface ReadonlyRepository<TEntity extends RepositoryEntity> {
readonly length: number;
getItems(predicate?: (entity: TEntity) => boolean): readonly TEntity[];
getById(id: TKey): TEntity;
exists(id: TKey): boolean;
getById(id: EntityId): TEntity;
exists(id: EntityId): boolean;
}

export interface MutableRepository<TKey, TEntity extends IEntity<TKey>> {
export interface MutableRepository<TEntity extends RepositoryEntity> {
addItem(item: TEntity): void;
addOrUpdateItem(item: TEntity): void;
removeItem(id: TKey): void;
removeItem(id: EntityId): void;
}

export interface Repository<TKey, TEntity extends IEntity<TKey>>
extends ReadonlyRepository<TKey, TEntity>, MutableRepository<TKey, TEntity> { }
export interface Repository<TEntity extends RepositoryEntity>
extends ReadonlyRepository<TEntity>, MutableRepository<TEntity> { }
6 changes: 6 additions & 0 deletions src/application/Repository/RepositoryEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Aggregate root */
export type RepositoryEntityId = string;

export interface RepositoryEntity {
readonly id: RepositoryEntityId;
}
6 changes: 3 additions & 3 deletions src/application/collections/windows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,7 @@ actions:
parameters:
appCapability: bluetoothSync
-
category: Disable app access to voice activation
category: Disable app voice activation
docs: |- # refactor-with-variable: Same • App Access Caution
This category safeguards against unauthorized app activation via voice commands.

Expand Down Expand Up @@ -15671,7 +15671,7 @@ actions:
data: '1'
deleteOnRevert: 'true' # Missing by default since Windows 10 Pro (≥ 22H2) and Windows 11 Pro (≥ 23H2)
-
category: Minimize CPU usage during scans
category: Disable intensive CPU usage during Defender scans
children:
-
name: Minimize CPU usage during scans
Expand Down Expand Up @@ -15866,7 +15866,7 @@ actions:
category: Disable scanning archive files
children:
-
name: Disable scanning archive files
name: Disable Defender archive file scanning
docs:
- https://admx.help/?Category=Windows_10_2016&Policy=Microsoft.Policies.WindowsDefender::Scan_DisableArchiveScanning
# Managing with MpPreference module:
Expand Down
2 changes: 1 addition & 1 deletion src/domain/Application.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { OperatingSystem } from './OperatingSystem';
import type { IApplication } from './IApplication';
import type { ICategoryCollection } from './ICategoryCollection';
import type { ICategoryCollection } from './Collection/ICategoryCollection';
import type { ProjectDetails } from './Project/ProjectDetails';

export class Application implements IApplication {
Expand Down
Loading

0 comments on commit ad2e1a0

Please sign in to comment.