1e37925139a2079d27a876d529d99c4b9b853185
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangProvider.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang2sources.plugin;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.Collection;
13 import org.apache.maven.model.Resource;
14 import org.apache.maven.plugin.MojoFailureException;
15 import org.apache.maven.project.MavenProject;
16 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 class YangProvider {
21     private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
22
23     void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
24             final Collection<File> excludedFiles) throws MojoFailureException {
25
26         // copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
27
28         File generatedYangDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_GENERATED_DIR);
29         addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, generatedYangDir);
30
31         // Also copy to the actual build output dir if different than "target". When running in
32         // Eclipse this can differ (eg "target-ide").
33
34         File actualGeneratedYangDir = new File(project.getBuild().getDirectory(),
35                 CodeGeneratorArg.YANG_GENERATED_DIR.replace("target" + File.separator, ""));
36         if (!actualGeneratedYangDir.equals(generatedYangDir)) {
37             addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, actualGeneratedYangDir);
38         }
39     }
40
41     private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
42             final Collection<File> excludedFiles, final File generatedYangDir) throws MojoFailureException {
43
44         File withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
45         withMetaInf.mkdirs();
46
47         try {
48             Collection<File> files = Util.listFiles(yangFilesRootDir, excludedFiles);
49             for (File file : files) {
50                 org.apache.commons.io.FileUtils.copyFile(file, new File(withMetaInf, file.getName()));
51             }
52         } catch (IOException e) {
53             LOG.warn("Failed to generate files into root {}", yangFilesRootDir, e);
54             throw new MojoFailureException("Unable to list yang files into resource folder", e);
55         }
56
57         setResource(generatedYangDir, project);
58
59         LOG.debug("{} Yang files from: {} marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, yangFilesRootDir,
60                 YangToSourcesProcessor.META_INF_YANG_STRING_JAR);
61     }
62
63     static void setResource(final File targetYangDir, final MavenProject project) {
64         Resource res = new Resource();
65         res.setDirectory(targetYangDir.getPath());
66         project.addResource(res);
67     }
68 }