1 | /*
|
2 | * Copyright © 2018 Atomist, Inc.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | import {
|
18 | Maker,
|
19 | } from "@atomist/automation-client";
|
20 | import { HandleCommand } from "@atomist/automation-client/lib/HandleCommand";
|
21 | import { isCommandHandlerMetadata } from "@atomist/automation-client/lib/internal/metadata/metadata";
|
22 | import { CommandHandlerMetadata } from "@atomist/automation-client/lib/metadata/automationMetadata";
|
23 | import { toFactory } from "@atomist/automation-client/lib/util/constructionUtils";
|
24 | import { FunctionalUnit } from "@atomist/sdm";
|
25 |
|
26 | export interface HandlerInfo {
|
27 | maker: Maker<HandleCommand<any>>;
|
28 | instance: HandleCommand<any> & CommandHandlerMetadata;
|
29 | }
|
30 |
|
31 | /**
|
32 | * Return command handlers with a given tag.
|
33 | * Note this may not find all, but it will find those that know their
|
34 | * own metadata, which is true of all those returned by generatorCommand
|
35 | * and the underlying commandHandlerFrom
|
36 | * @param {FunctionalUnit} unit
|
37 | * @param {string} tag
|
38 | */
|
39 | export function commandHandlersWithTag(unit: FunctionalUnit, tag: string): HandlerInfo[] {
|
40 | return selfDescribingHandlers(unit)
|
41 | .filter(hi => hi.instance.tags.some(t => t.name === tag));
|
42 | }
|
43 |
|
44 | /**
|
45 | * Return command handlers along with their metadata
|
46 | * Note this may not find all, but it will find those that know their
|
47 | * own metadata
|
48 | * @param {FunctionalUnit} unit
|
49 | */
|
50 | export function selfDescribingHandlers(unit: FunctionalUnit): HandlerInfo[] {
|
51 | return unit.commandHandlers
|
52 | .map(maker => ({maker, instance: toFactory(maker)()}))
|
53 | .filter(hi => isCommandHandlerMetadata(hi.instance)) as HandlerInfo[];
|
54 | }
|