1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Resource, ResourceKind } from "./resource";
6 import { safeLoad } from 'js-yaml';
8 export interface WorkflowResource extends Resource {
9 kind: ResourceKind.WORKFLOW;
14 export interface WorkflowResoruceDefinition {
16 $graph: Array<Workflow | CommandLineTool>;
18 export interface Workflow {
22 inputs: CommandInputParameter[];
27 export interface CommandLineTool {
28 class: 'CommandLineTool';
30 inputs: CommandInputParameter[];
34 export type CommandInputParameter =
35 BooleanCommandInputParameter |
36 IntCommandInputParameter |
37 LongCommandInputParameter |
38 FloatCommandInputParameter |
39 DoubleCommandInputParameter |
40 StringCommandInputParameter |
41 FileCommandInputParameter |
42 DirectoryCommandInputParameter |
43 StringArrayCommandInputParameter |
44 IntArrayCommandInputParameter |
45 FloatArrayCommandInputParameter |
46 FileArrayCommandInputParameter |
47 DirectoryArrayCommandInputParameter |
48 EnumCommandInputParameter;
59 DIRECTORY = 'Directory',
62 export interface CommandInputEnumSchema {
69 export interface CommandInputArraySchema<ItemType> {
75 export interface File {
82 export interface Directory {
83 class: CWLType.DIRECTORY;
89 export interface GenericCommandInputParameter<Type, Value> {
92 doc?: string | string[];
94 type?: Type | Array<Type | CWLType.NULL>;
98 export type GenericArrayCommandInputParameter<Type, Value> = GenericCommandInputParameter<CommandInputArraySchema<Type>, Value[]>;
100 export type BooleanCommandInputParameter = GenericCommandInputParameter<CWLType.BOOLEAN, boolean>;
101 export type IntCommandInputParameter = GenericCommandInputParameter<CWLType.INT, number>;
102 export type LongCommandInputParameter = GenericCommandInputParameter<CWLType.LONG, number>;
103 export type FloatCommandInputParameter = GenericCommandInputParameter<CWLType.FLOAT, number>;
104 export type DoubleCommandInputParameter = GenericCommandInputParameter<CWLType.DOUBLE, number>;
105 export type StringCommandInputParameter = GenericCommandInputParameter<CWLType.STRING, string>;
106 export type FileCommandInputParameter = GenericCommandInputParameter<CWLType.FILE, File>;
107 export type DirectoryCommandInputParameter = GenericCommandInputParameter<CWLType.DIRECTORY, Directory>;
108 export type EnumCommandInputParameter = GenericCommandInputParameter<CommandInputEnumSchema, string>;
110 export type StringArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.STRING, string>;
111 export type IntArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.INT, string>;
112 export type FloatArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.FLOAT, string>;
113 export type FileArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.FILE, File>;
114 export type DirectoryArrayCommandInputParameter = GenericArrayCommandInputParameter<CWLType.DIRECTORY, Directory>;
116 export type WorkflowInputsData = {
117 [key: string]: boolean | number | string | File | Directory;
119 export const parseWorkflowDefinition = (workflow: WorkflowResource): WorkflowResoruceDefinition => {
120 const definition = safeLoad(workflow.definition);
124 export const getWorkflowInputs = (workflowDefinition: WorkflowResoruceDefinition) => {
125 const mainWorkflow = workflowDefinition.$graph.find(item => item.class === 'Workflow' && item.id === '#main');
127 ? mainWorkflow.inputs
130 export const getInputLabel = (input: CommandInputParameter) => {
131 return `${input.label || input.id}`;
134 export const isRequiredInput = ({ type }: CommandInputParameter) => {
135 if (type instanceof Array) {
136 for (const t of type) {
137 if (t === CWLType.NULL) {
145 export const isPrimitiveOfType = (input: GenericCommandInputParameter<any, any>, type: CWLType) =>
146 input.type instanceof Array
147 ? input.type.indexOf(type) > -1
148 : input.type === type;
150 export const isArrayOfType = (input: GenericCommandInputParameter<any, any>, type: CWLType) =>
151 typeof input.type === 'object' &&
152 input.type.type === 'array'
153 ? input.type.items === type
156 export const stringifyInputType = ({ type }: CommandInputParameter) => {
157 if (typeof type === 'string') {
159 } else if (type instanceof Array) {
160 return type.join(' | ');
161 } else if (typeof type === 'object') {
162 if (type.type === 'enum') {
164 } else if (type.type === 'array') {
165 return `${type.items}[]`;