Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
*/ |
40 |
package org.netbeans.modules.maven.newproject; |
41 |
|
42 |
import java.io.File; |
43 |
import java.io.IOException; |
44 |
import java.io.InputStream; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collection; |
47 |
import java.util.Collections; |
48 |
import java.util.Enumeration; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import java.util.Properties; |
52 |
import java.util.regex.Pattern; |
53 |
import org.netbeans.api.project.Project; |
54 |
import org.netbeans.api.project.ProjectManager; |
55 |
import org.netbeans.api.templates.CreateDescriptor; |
56 |
import org.netbeans.api.templates.CreateFromTemplateHandler; |
57 |
import org.netbeans.modules.maven.api.archetype.Archetype; |
58 |
import org.netbeans.modules.maven.api.archetype.ArchetypeWizards; |
59 |
import org.netbeans.modules.maven.api.archetype.ProjectInfo; |
60 |
import org.netbeans.spi.project.ActionProvider; |
61 |
import org.netbeans.spi.project.ui.support.CommonProjectActions; |
62 |
import org.openide.filesystems.FileObject; |
63 |
import org.openide.filesystems.FileUtil; |
64 |
import org.openide.util.NbBundle; |
65 |
import org.openide.util.NbCollections; |
66 |
import org.openide.util.lookup.ServiceProvider; |
67 |
|
68 |
@ServiceProvider(service = CreateFromTemplateHandler.class, position = 5000) |
69 |
public final class ArchetypeTemplateHandler extends CreateFromTemplateHandler { |
70 |
public ArchetypeTemplateHandler() { |
71 |
} |
72 |
|
73 |
@Override |
74 |
protected boolean accept(CreateDescriptor desc) { |
75 |
return desc.getTemplate().hasExt("archetype"); // NOI18N |
76 |
} |
77 |
|
78 |
@NbBundle.Messages({ |
79 |
"MSG_NoVersion=No version attribute specified for the Maven project", |
80 |
"MSG_NoArtifactId=No artifactId attribute specified for the Maven project", |
81 |
"MSG_NoGroupId=No groupId attribute specified for the Maven project", |
82 |
}) |
83 |
@Override |
84 |
protected List<FileObject> createFromTemplate(CreateDescriptor desc) throws IOException { |
85 |
Properties p = new Properties(); |
86 |
try (InputStream is = desc.getTemplate().getInputStream()) { |
87 |
p.load(is); |
88 |
} |
89 |
String version = (String)desc.getValue("version"); // NOI18N |
90 |
if (version == null) { |
91 |
throw new IOException(Bundle.MSG_NoVersion()); |
92 |
} |
93 |
String artifactId = (String)desc.getValue("artifactId"); // NOI18N |
94 |
if (version == null) { |
95 |
throw new IOException(Bundle.MSG_NoArtifactId()); |
96 |
} |
97 |
String groupId = (String)desc.getValue("groupId"); // NOI18N |
98 |
if (version == null) { |
99 |
throw new IOException(Bundle.MSG_NoGroupId()); |
100 |
} |
101 |
String packageName = (String)desc.getValue("package"); // NOI18N |
102 |
ProjectInfo pi = new ProjectInfo(groupId, artifactId, version, packageName); |
103 |
Map<?,?> additionalProperties = desc.getValue("wizard"); // NOI18N |
104 |
Archetype arch = new Archetype(); |
105 |
arch.setArtifactId(p.getProperty("artifactId")); // NOI18N |
106 |
arch.setGroupId(p.getProperty("groupId")); // NOI18N |
107 |
arch.setVersion(p.getProperty("version")); // NOI18N |
108 |
File projDir = desc.getValue(CommonProjectActions.PROJECT_PARENT_FOLDER); |
109 |
if (projDir == null && desc.getTarget() != null) { |
110 |
projDir = FileUtil.toFile(desc.getTarget()); |
111 |
} |
112 |
if (projDir == null) { |
113 |
throw new IOException(CommonProjectActions.PROJECT_PARENT_FOLDER + " not specified"); |
114 |
} |
115 |
|
116 |
Map<String, String> filteredProperties = |
117 |
additionalProperties == null ? |
118 |
Collections.emptyMap() |
119 |
: |
120 |
NbCollections.checkedMapByFilter(additionalProperties, String.class, String.class, false); |
121 |
ArchetypeWizards.createFromArchetype(projDir, pi, arch, filteredProperties, true); |
122 |
FileObject fo = FileUtil.toFileObject(projDir); |
123 |
|
124 |
List<FileObject> fos = new ArrayList<>(); |
125 |
collectPomDirs(fo, fos); |
126 |
final String toOpen = p.getProperty("open"); // NOI18N |
127 |
if (toOpen != null) { |
128 |
collectFiles(fo, fos, toOpen.split(",")); // NOI18N |
129 |
} |
130 |
|
131 |
Project prj = ProjectManager.getDefault().findProject(fo); |
132 |
ActionProvider ap = prj == null ? null : prj.getLookup().lookup(ActionProvider.class); |
133 |
if (ap != null) { |
134 |
ap.invokeAction(ActionProvider.COMMAND_BUILD, prj.getLookup()); |
135 |
} |
136 |
return fos; |
137 |
} |
138 |
|
139 |
private static void collectPomDirs(FileObject dir, Collection<? super FileObject> found) { |
140 |
if (dir == null || !dir.isFolder()) { |
141 |
return; |
142 |
} |
143 |
if (dir.getFileObject("pom.xml") == null) { |
144 |
return; |
145 |
} |
146 |
found.add(dir); |
147 |
for (FileObject f : dir.getChildren()) { |
148 |
collectPomDirs(f, found); |
149 |
} |
150 |
} |
151 |
|
152 |
static void collectFiles(FileObject root, Collection<? super FileObject> found, String... includes) { |
153 |
Pattern[] check = new Pattern[includes.length]; |
154 |
for (int i = 0; i < check.length; i++) { |
155 |
check[i] = Pattern.compile(includes[i]); |
156 |
} |
157 |
|
158 |
Enumeration<? extends FileObject> en = root.getChildren(true); |
159 |
while (en.hasMoreElements()) { |
160 |
FileObject fo = en.nextElement(); |
161 |
String relPath = FileUtil.getRelativePath(root, fo); |
162 |
if (relPath == null) { |
163 |
continue; |
164 |
} |
165 |
for (Pattern p : check) { |
166 |
if (p.matcher(relPath).matches()) { |
167 |
found.add(fo); |
168 |
break; |
169 |
} |
170 |
} |
171 |
} |
172 |
} |
173 |
|
174 |
} |