blob: efd9be1d4ef01fbc25b333d26879df77838f710c [file] [log] [blame]
Brian O'Connore8468b52016-07-25 13:42:36 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.onosjar;
17
18import com.facebook.buck.io.ProjectFilesystem;
19import com.facebook.buck.rules.SourcePathResolver;
20import com.facebook.buck.step.AbstractExecutionStep;
21import com.facebook.buck.step.ExecutionContext;
22import com.facebook.buck.step.StepExecutionResult;
23import com.google.common.collect.ImmutableSortedSet;
24
25import java.io.File;
26import java.io.IOException;
27import java.nio.file.Path;
28import java.util.List;
29import java.util.stream.Collectors;
30
31/**
32 * Buck build step to trigger SwaggerGenerator.
33 */
34public class SwaggerStep extends AbstractExecutionStep {
35
36 private final ProjectFilesystem filesystem;
37
38 private final ImmutableSortedSet<Path> srcs;
39 private final ImmutableSortedSet<Path> resources;
40 private final Path genSourcesOutput;
41 private final Path genResourcesOutput;
42
43 private final String webContext;
44 private final String apiTitle;
45 private final String apiVersion;
46 private final String apiPackage;
47 private final String apiDescription;
48
49
50 public SwaggerStep(ProjectFilesystem filesystem,
51 ImmutableSortedSet<Path> srcs,
52 ImmutableSortedSet<Path> resources,
53 Path genSourcesOutput, Path genResourcesOutput,
54 String webContext, String apiTitle, String apiVersion,
55 String apiPackage, String apiDescription) {
56 super("swagger");
57 this.filesystem = filesystem;
58 this.srcs = srcs;
59 this.resources = resources;
60 this.genSourcesOutput = genSourcesOutput;
61 this.genResourcesOutput = genResourcesOutput;
62 this.webContext = webContext;
63 this.apiTitle = apiTitle;
64 this.apiVersion = apiVersion;
65 this.apiPackage = apiPackage;
66 this.apiDescription = apiDescription;
67 }
68
69 @Override
70 public StepExecutionResult execute(ExecutionContext executionContext)
71 throws IOException, InterruptedException {
72 try {
73 List<File> srcFiles = srcs.stream()
74 .map(src -> filesystem.resolve(src).toFile())
75 .collect(Collectors.toList());
76 List<File> resourceFiles = resources.stream()
77 .map(rsrc -> filesystem.resolve(rsrc).toFile())
78 .collect(Collectors.toList());
79 new SwaggerGenerator(srcFiles, resourceFiles, null, null,
80 filesystem.resolve(genSourcesOutput).toFile(),
81 filesystem.resolve(genResourcesOutput).toFile(),
82 webContext,
83 apiTitle,
84 apiVersion,
85 apiPackage,
86 apiDescription).execute();
87
88 return StepExecutionResult.SUCCESS;
89 } catch (Exception e) {
90 e.printStackTrace();
91 // FIXME print the exception
92 return StepExecutionResult.ERROR;
93 }
94 }
95
96 Path apiRegistratorPath() {
97 return genSourcesOutput.resolve(SwaggerGenerator.apiRegistratorPath(apiPackage));
98 }
99}