blob: f8ab7622c31fd9d7a1cefc9707f1598ec54b6ad2 [file] [log] [blame]
Richard S. Hall85bafab2009-07-13 13:25:46 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20package org.cauldron.bld.ant;
21
22import java.io.File;
23import java.io.IOException;
24import java.net.URI;
25import java.util.Hashtable;
26import java.util.List;
27import java.util.Properties;
28
29import org.apache.tools.ant.BuildException;
30import org.apache.tools.ant.Project;
31import org.apache.tools.ant.Task;
32import org.apache.tools.ant.types.Path;
33import org.cauldron.bld.bnd.BundleBuilder;
34import org.cauldron.bld.config.BldFactory;
35import org.cauldron.bld.config.IBldProject;
36import org.cauldron.bld.config.IBldProject.IBldBundle;
37
38public class BundleTask extends Task {
39 private File[] classpath;
40 private String destPattern;
41 private boolean force;
42 private String property;
43 private String sigilFile;
44
45 @Override
46 public void execute() throws BuildException {
47 if (classpath == null)
48 throw new BuildException("missing: attribute: classpathref");
49 if (destPattern == null)
50 throw new BuildException("missing attribute: destpattern");
51
52 IBldProject project;
53
54 try {
55 project = BldFactory.getProject(getSigilFileURI());
56
57 } catch (IOException e) {
58 throw new BuildException("failed to get project file: " + e);
59 }
60
61 Properties env = new Properties();
62 @SuppressWarnings("unchecked") Hashtable<String, String> properties = getProject().getProperties();
63 for (String key : properties.keySet()) {
64 if (key.matches("^[a-z].*")) { // avoid props starting with Uppercase - bnd adds them to manifest
65 env.setProperty(key, properties.get(key));
66 }
67 }
68
69 BundleBuilder bb = new BundleBuilder(project, classpath, destPattern, env);
70 boolean anyModified = false;
71
72 for (IBldBundle bundle : project.getBundles()) {
73 String id = bundle.getId();
74 log("creating bundle: " + id);
75 int nWarn = 0;
76 int nErr = 0;
77 String msg = "";
78
79 try {
80 boolean modified = (bb.createBundle(bundle, force, new BundleBuilder.Log() {
81 public void warn(String msg) {
82 log(msg, Project.MSG_WARN);
83 }
84 public void verbose(String msg) {
85 log(msg, Project.MSG_VERBOSE);
86 }
87 }));
88 nWarn = bb.warnings().size();
89 if (modified) {
90 anyModified = true;
91 } else {
92 msg = " (not modified)";
93 }
94 } catch (Exception e) {
95 List<String> errors = bb.errors();
96 if (errors != null) {
97 nErr = errors.size();
98 for (String err : errors) {
99 log(err, Project.MSG_ERR);
100 }
101 }
102 throw new BuildException("failed to create: " + id + ": " + e, e);
103 } finally {
104 log(id + ": " + count(nErr, "error") + ", " + count(nWarn, "warning") + msg);
105 }
106 }
107
108 if (anyModified && property != null) {
109 getProject().setProperty(property, "true");
110 }
111 }
112
113 private URI getSigilFileURI() {
114 File file = sigilFile == null ? new File(getProject().getBaseDir(), IBldProject.PROJECT_FILE) : new File(sigilFile);
115 if ( !file.isFile() ) {
116 throw new BuildException( "File not found " + file.getAbsolutePath() );
117 }
118 return file.toURI();
119 }
120
121 private String count(int count, String msg) {
122 return count + " " + msg + (count == 1 ? "" : "s");
123 }
124
125 public void setDestpattern(String pattern) {
126 this.destPattern = pattern;
127 }
128
129 public void setForce(String force) {
130 this.force = Boolean.parseBoolean(force);
131 }
132
133 public void setProperty(String property) {
134 this.property = property;
135 }
136
137 public void setClasspathref(String value) {
138 Path p = (Path) getProject().getReference(value);
139 if (p == null) {
140 throw new BuildException(value + "is not a path reference.");
141 }
142
143 String[] paths = p.list();
144 classpath = new File[paths.length];
145 for (int i = 0; i < paths.length; ++i) {
146 classpath[i] = new File(paths[i]);
147 }
148 }
149
150 public void setSigilFile(String sigilFile) {
151 this.sigilFile = sigilFile;
152 }
153}