-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from Unibeautify/dependency-manager
Add support for configuration of dependencies
- Loading branch information
Showing
14 changed files
with
482 additions
and
192 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,47 +1,93 @@ | ||
// tslint:disable:no-reserved-keywords | ||
import { DependencyFactory } from "./DependencyFactory"; | ||
import { Dependency, DependencyOptions } from "./Dependency"; | ||
import { | ||
Dependency, | ||
DependencyDefinition, | ||
DependencyOptions, | ||
} from "./Dependency"; | ||
|
||
export class DependencyManager { | ||
private readonly dependencies: Dependency[]; | ||
private readonly lookup: { | ||
[name: string]: Dependency; | ||
}; | ||
private static registry: DependencyRegistry = {}; | ||
|
||
public static clearRegistry(): void { | ||
this.registry = {}; | ||
} | ||
|
||
constructor( | ||
private beautifierName: string, | ||
private dependencyDefinitions: DependencyDefinition[] = [], | ||
private options: LanguageDependencyOptions = {} | ||
) { | ||
this.initializeDependencies(); | ||
} | ||
|
||
public load(): Promise<boolean> { | ||
return Promise.all( | ||
this.dependencyDefinitions | ||
.map(def => this.get(def.name)) | ||
.map(dep => dep.load()) | ||
).then(() => true); | ||
} | ||
|
||
private initializeDependencies(): void { | ||
const lookup = DependencyManager.registry; | ||
const beautifierLookup = lookup[this.beautifierName] || {}; | ||
lookup[this.beautifierName] = beautifierLookup; | ||
|
||
constructor(dependencies: DependencyOptions[]) { | ||
this.dependencies = dependencies.map(dependency => | ||
new DependencyFactory(dependency).dependency() | ||
); | ||
this.lookup = this.dependencies.reduce( | ||
(lookup, dep) => ({ | ||
...lookup, | ||
[dep.name]: dep, | ||
}), | ||
{} | ||
); | ||
this.dependencyDefinitions.forEach(def => { | ||
const { name: dependencyName } = def; | ||
const options = this.optionsForDependency(dependencyName); | ||
const optionsKey = this.keyForOptions(options); | ||
const depLookup = beautifierLookup[dependencyName] || {}; | ||
beautifierLookup[dependencyName] = depLookup; | ||
depLookup[optionsKey] = | ||
depLookup[optionsKey] || this.createDependency(def, options); | ||
}); | ||
} | ||
|
||
public has(name: string): boolean { | ||
return Boolean(this.get(name)); | ||
} | ||
|
||
public get<T extends Dependency>(name: string): T { | ||
const dep = this.lookup[name] as T | undefined; | ||
public get<T extends Dependency>(dependencyName: string): T { | ||
const options = this.optionsForDependency(dependencyName); | ||
const optionsKey = this.keyForOptions(options); | ||
const lookup = this.registry[dependencyName] || {}; | ||
const dep = lookup[optionsKey] as T | undefined; | ||
if (!dep) { | ||
throw new Error(`Dependency with name ${name} not found.`); | ||
throw new Error(`Dependency with name ${dependencyName} not found.`); | ||
} | ||
return dep; | ||
} | ||
|
||
public load(): Promise<boolean> { | ||
return Promise.all( | ||
this.dependencies.map(dep => { | ||
return dep.load(); | ||
}) | ||
).then(() => true); | ||
private optionsForDependency(dependencyName: string): DependencyOptions { | ||
return this.options[dependencyName]; | ||
} | ||
|
||
protected get registry(): DependencyRegistry[string] { | ||
return DependencyManager.registry[this.beautifierName]; | ||
} | ||
|
||
protected createDependency( | ||
definition: DependencyDefinition, | ||
options: DependencyOptions | ||
): Dependency { | ||
return new DependencyFactory(definition, options).dependency(); | ||
} | ||
|
||
private keyForOptions(options: DependencyOptions = {}): string { | ||
return JSON.stringify(options); | ||
} | ||
} | ||
|
||
export interface DependencyMap { | ||
[name: string]: Dependency; | ||
export interface LanguageDependencyOptions { | ||
[dependencyName: string]: DependencyOptions; | ||
} | ||
|
||
export interface DependencyRegistry { | ||
[beautifierName: string]: { | ||
[dependencyName: string]: { | ||
[optionsKey: string]: Dependency; | ||
}; | ||
}; | ||
} |
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
Oops, something went wrong.