blob: 8e97a7a2671db0a8ef9b79c2fe89559b922a8c7e [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;
Bharat saraswal594bc6d2016-02-22 22:15:21 +053030import org.apache.commons.io.FileUtils;
Bharat saraswal870c56f2016-02-20 21:57:16 +053031
32import static org.slf4j.LoggerFactory.getLogger;
33import org.slf4j.Logger;
34
35/**
36 * Provides common utility functionalities for code generation.
37 */
38public final class YangIoUtils {
39
40 private static final Logger log = getLogger(YangIoUtils.class);
41
42 /**
43 * Default constructor.
44 */
45 private YangIoUtils() {
46 }
47
48 /**
49 * Creates the directory structure.
50 *
51 * @param path directory path
52 * @return directory structure
53 */
54 public static File createDirectories(String path) {
55
b.janani68c55e12016-02-24 12:23:03 +053056 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053057 generatedDir.mkdirs();
58 return generatedDir;
59 }
60
61 /**
62 * Adds package info file for the created directory.
63 *
64 * @param path directory path
65 * @param classInfo class info for the package
66 * @param pack package of the directory
67 * @throws IOException when fails to create package info file.
68 */
69 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
70
b.janani68c55e12016-02-24 12:23:03 +053071 if (pack.contains(UtilConstants.YANG_GEN_DIR)) {
72 String[] strArray = pack.split(UtilConstants.YANG_GEN_DIR + UtilConstants.SLASH);
73 pack = strArray[1];
74 }
75
Bharat saraswal870c56f2016-02-20 21:57:16 +053076 try {
77
78 File packageInfo = new File(path + File.separator + "package-info.java");
79 packageInfo.createNewFile();
b.janani68c55e12016-02-24 12:23:03 +053080 FileWriter fileWriter = null;
81 BufferedWriter bufferedWriter = null;
82 fileWriter = new FileWriter(packageInfo);
83 bufferedWriter = new BufferedWriter(fileWriter);
84 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
85 bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo));
86 bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
87 bufferedWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 } catch (IOException e) {
89 throw new IOException("Exception occured while creating package info file.");
90 }
91 }
92
93 /**
94 * Cleans the generated directory if already exist in source folder.
95 *
96 * @param baseDir generated directory in previous build.
97 */
98 public static void clean(String baseDir) {
99 File generatedDirectory = new File(baseDir + File.separator + UtilConstants.YANG_GEN_DIR);
100 if (generatedDirectory.exists()) {
101 List<String> javafiles;
102 try {
103 javafiles = YangFileScanner.getJavaFiles(generatedDirectory.toString());
104 for (String file : javafiles) {
105 File currentFile = new File(file);
106 currentFile.delete();
107 }
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530108 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530109 } catch (IOException e) {
110 log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
111 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530112 }
113 }
114
115 /**
116 * Adds generated source directory to the compilation root.
117 *
118 * @param source directory
119 * @param project current maven project
120 * @param context current build context
121 */
122 public static void addToSource(String source, MavenProject project, BuildContext context) {
123
124 project.addCompileSourceRoot(source);
125 Resource rsc = new Resource();
126 rsc.setDirectory(source);
127 project.addResource(rsc);
128 context.refresh(project.getBasedir());
129 log.info("Source directory added to compilation root: " + source);
130 }
131
132}