blob: af83de4213a57ba54ad750413e267120253ba576 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 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 */
16
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedWriter;
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
23import java.util.List;
24
25import org.sonatype.plexus.build.incremental.BuildContext;
26import org.apache.maven.project.MavenProject;
27import org.apache.maven.model.Resource;
28
29import org.onosproject.yangutils.utils.UtilConstants;
30
31import static org.slf4j.LoggerFactory.getLogger;
32import org.slf4j.Logger;
33
34/**
35 * Provides common utility functionalities for code generation.
36 */
37public final class YangIoUtils {
38
39 private static final Logger log = getLogger(YangIoUtils.class);
40
41 /**
42 * Default constructor.
43 */
44 private YangIoUtils() {
45 }
46
47 /**
48 * Creates the directory structure.
49 *
50 * @param path directory path
51 * @return directory structure
52 */
53 public static File createDirectories(String path) {
54
55 File generatedDir = new File(UtilConstants.YANG_GEN_DIR + File.separator + path);
56 generatedDir.mkdirs();
57 return generatedDir;
58 }
59
60 /**
61 * Adds package info file for the created directory.
62 *
63 * @param path directory path
64 * @param classInfo class info for the package
65 * @param pack package of the directory
66 * @throws IOException when fails to create package info file.
67 */
68 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
69
70 try {
71
72 File packageInfo = new File(path + File.separator + "package-info.java");
73 packageInfo.createNewFile();
74 if (packageInfo.exists()) {
75
76 FileWriter fileWriter = null;
77 BufferedWriter bufferedWriter = null;
78 fileWriter = new FileWriter(packageInfo);
79 bufferedWriter = new BufferedWriter(fileWriter);
80 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
81 bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
82 bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
83 bufferedWriter.close();
84 }
85 } catch (IOException e) {
86 throw new IOException("Exception occured while creating package info file.");
87 }
88 }
89
90 /**
91 * Cleans the generated directory if already exist in source folder.
92 *
93 * @param baseDir generated directory in previous build.
94 */
95 public static void clean(String baseDir) {
96 File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
97 if (generatedDirectory.exists()) {
98 List<String> javafiles;
99 try {
100 javafiles = YangFileScanner.getJavaFiles(generatedDirectory.toString());
101 for (String file : javafiles) {
102 File currentFile = new File(file);
103 currentFile.delete();
104 }
105 } catch (IOException e) {
106 log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
107 }
108 generatedDirectory.delete();
109 }
110 }
111
112 /**
113 * Adds generated source directory to the compilation root.
114 *
115 * @param source directory
116 * @param project current maven project
117 * @param context current build context
118 */
119 public static void addToSource(String source, MavenProject project, BuildContext context) {
120
121 project.addCompileSourceRoot(source);
122 Resource rsc = new Resource();
123 rsc.setDirectory(source);
124 project.addResource(rsc);
125 context.refresh(project.getBasedir());
126 log.info("Source directory added to compilation root: " + source);
127 }
128
129}