001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.dotnet;
020:
021: import org.apache.tools.ant.BuildException;
022:
023: /**
024: * This task compiles Visual Basic.NET source into executables or modules.
025: * The task requires vbc.exe on the execute path, unless it or an equivalent
026: * program is specified in the <tt>executable</tt> parameter
027: *
028: * <p>
029: * All parameters are optional: <vbc/> should suffice to produce a debug
030: * build of all *.vb files.
031: *
032: * <p>
033:
034: * The task is a directory based task, so attributes like
035: * <tt>includes="**\/*.vb"</tt> and
036: * <tt>excludes="broken.vb"</tt> can be used to control
037: * the files pulled in. By default,
038: * all *.vb files from the project folder down are included in the command.
039: * When this happens the destFile -if not specified-
040: * is taken as the first file in the list, which may be somewhat hard to control.
041: Specifying the output file with <tt>destfile</tt> is prudent.
042: </p>
043: <p>
044: * Also, dependency checking only works if destfile is set.
045: *
046: * <p>For historical reasons the pattern
047: * <code>**</code><code>/*.vb</code> is preset as includes list and
048: * you can not override it with an explicit includes attribute. Use
049: * nested <code><src></code> elements instead of the basedir
050: * attribute if you need more control.</p>
051: *
052: * As with <csc> nested <tt>src</tt> filesets of source,
053: * reference filesets, definitions and resources can be provided.
054: *
055: * <p>
056: * Example
057: * </p>
058: * <pre><vbc
059: * optimize="true"
060: * debug="false"
061: * warnLevel="4"
062: * targetType="exe"
063: * definitions="RELEASE"
064: * excludes="src/unicode_class.vb"
065: * mainClass = "MainApp"
066: * destFile="NetApp.exe"
067: * optionExplicit="true"
068: * optionCompare="text"
069: * references="System.Xml,System.Web.Xml"
070: * >
071: * <reference file="${testCSC.dll}" />
072: * <define name="RELEASE" />
073: * <define name="DEBUG" if="debug.property"/>
074: * <define name="def3" unless="def2.property"/>
075: * </vbc>
076: </pre>
077: * @ant.task name="vbc" category="dotnet"
078: */
079:
080: public class VisualBasicCompile extends DotnetCompile {
081:
082: /**
083: * Compiler option to remove integer checks. Default: false.
084: */
085: private boolean removeIntChecks = false;
086:
087: /**
088: * Require explicit declaration of variables? Default: false.
089: */
090: private boolean optionExplicit = false;
091:
092: /**
093: * Enforce strict language semantics? Default: false.
094: */
095: private boolean optionStrict = false;
096:
097: /**
098: * Whether to compare strings as "text" or "binary". Default: "binary".
099: */
100: private String optionCompare;
101:
102: /**
103: * Root namespace for all type declarations.
104: */
105: private String rootNamespace;
106:
107: /**
108: * Declare global imports fornamespaces in referenced metadata files.
109: */
110: private String imports;
111:
112: /**
113: * Constructor for VisualBasicCompile.
114: */
115: public VisualBasicCompile() {
116: clear();
117: }
118:
119: /**
120: * reset all contents.
121: */
122: public void clear() {
123: super .clear();
124: imports = null;
125: rootNamespace = null;
126: optionCompare = null;
127: optionExplicit = false;
128: optionStrict = false;
129: removeIntChecks = false;
130: setExecutable("vbc");
131: }
132:
133: /**
134: * get the argument or null for no argument needed
135: * This is overridden from DotnetCompile.java because VBC uses
136: * "/win32resource:" rather than "/win32res:"
137: *
138: *@return The Win32Res Parameter to CSC
139: */
140: protected String getWin32ResParameter() {
141: if (getWin32Res() != null) {
142: return "/win32resource:" + getWin32Res().toString();
143: } else {
144: return null;
145: }
146: }
147:
148: /**
149: * Whether to remove integer checks. Default false.
150: * @param flag on/off flag
151: */
152: public void setRemoveIntChecks(boolean flag) {
153: removeIntChecks = flag;
154: }
155:
156: /**
157: * Get the flag for removing integer checks.
158: * @return true if flag is turned on
159: */
160: public boolean getRemoveIntChecks() {
161: return removeIntChecks;
162: }
163:
164: /**
165: * Form the option string for removeIntChecks.
166: * @return The parameter string.
167: */
168: public String getRemoveIntChecksParameter() {
169: return "/removeintchecks" + (removeIntChecks ? "+" : "-");
170: }
171:
172: /**
173: * Whether to require explicit declaration of variables.
174: * @param flag on/off flag
175: */
176: public void setOptionExplicit(boolean flag) {
177: optionExplicit = flag;
178: }
179:
180: /**
181: * Get the flag for whether to require explicit declaration of variables.
182: *@return true if flag is turned on
183: */
184: public boolean getOptionExplicit() {
185: return optionExplicit;
186: }
187:
188: /**
189: * Form the option string for optionExplicit..
190: * @return The parameter string.
191: */
192: public String getOptionExplicitParameter() {
193: return "/optionexplicit" + (optionExplicit ? "+" : "-");
194: }
195:
196: /**
197: * Enforce strict language semantics.
198: * @param flag on/off flag
199: */
200: public void setOptionStrict(boolean flag) {
201: optionStrict = flag;
202: }
203:
204: /**
205: * Get the flag for whether to enforce strict language semantics.
206: * @return true if flag is turned on
207: */
208: public boolean getOptionStrict() {
209: return optionStrict;
210: }
211:
212: /**
213: * For the option string for optionStrict.
214: * @return The parameter string.
215: */
216: public String getOptionStrictParameter() {
217: return "/optionstrict" + (optionStrict ? "+" : "-");
218: }
219:
220: /**
221: * Specifies the root namespace for all type declarations.
222: * @param rootNamespace a root namespace.
223: */
224: public void setRootNamespace(String rootNamespace) {
225: this .rootNamespace = rootNamespace;
226: }
227:
228: /**
229: * Get the root namespace.
230: * @return the root namespace.
231: */
232: public String getRootNamespace() {
233: return this .rootNamespace;
234: }
235:
236: /**
237: * Form the option string for rootNamespace.
238: * @return the root namespace option string.
239: */
240: protected String getRootNamespaceParameter() {
241: if (rootNamespace != null && rootNamespace.length() != 0) {
242: return "/rootnamespace:" + rootNamespace;
243: } else {
244: return null;
245: }
246: }
247:
248: /**
249: * Declare global imports for namespaces in referenced metadata files.
250: * @param imports the imports string
251: */
252: public void setImports(String imports) {
253: this .imports = imports;
254: }
255:
256: /**
257: * Get global imports for namespaces in referenced metadata files.
258: * @return the imports string.
259: */
260: public String getImports() {
261: return this .imports;
262: }
263:
264: /**
265: * Format the option for imports.
266: * @return the formatted import option.
267: */
268: protected String getImportsParameter() {
269: if (imports != null && imports.length() != 0) {
270: return "/imports:" + imports;
271: } else {
272: return null;
273: }
274: }
275:
276: /**
277: * Specify binary- or text-style string comparisons. Defaults
278: * to "binary"
279: * @param optionCompare the option compare style. "text" | "binary".
280: */
281: public void setOptionCompare(String optionCompare) {
282: if ("text".equalsIgnoreCase(optionCompare)) {
283: this .optionCompare = "text";
284: } else {
285: this .optionCompare = "binary";
286: }
287: }
288:
289: /**
290: * "binary" or "text" for the string-comparison style.
291: * @return the option compare style.
292: */
293: public String getOptionCompare() {
294: return this .optionCompare;
295: }
296:
297: /**
298: * Format the option for string comparison style.
299: * @return The formatted option.
300: */
301: protected String getOptionCompareParameter() {
302: if (optionCompare != null
303: && "text".equalsIgnoreCase(optionCompare)) {
304: return "/optioncompare:text";
305: } else {
306: return "/optioncompare:binary";
307: }
308: }
309:
310: /**
311: * implement VBC commands
312: * @param command the command to set arguements on.
313: */
314: protected void addCompilerSpecificOptions(NetCommand command) {
315: command.addArgument(getRemoveIntChecksParameter());
316: command.addArgument(getImportsParameter());
317: command.addArgument(getOptionExplicitParameter());
318: command.addArgument(getOptionStrictParameter());
319: command.addArgument(getRootNamespaceParameter());
320: command.addArgument(getOptionCompareParameter());
321: }
322:
323: /**
324: * Get the delimiter that the compiler uses between references.
325: * For example, c# will return ";"; VB.NET will return ","
326: * @return The string delimiter for the reference string.
327: */
328: public String getReferenceDelimiter() {
329: return ",";
330: }
331:
332: /**
333: * Get the extension of filenames to compile.
334: * @return The string extension of files to compile.
335: */
336: public String getFileExtension() {
337: return "vb";
338: }
339:
340: /** {@inheritDoc} */
341: protected void createResourceParameter(NetCommand command,
342: DotnetResource resource) {
343: resource.getParameters(getProject(), command, false);
344: }
345:
346: /**
347: * validation code
348: * @throws BuildException if validation failed
349: */
350: protected void validate() throws BuildException {
351: super .validate();
352: if (getDestFile() == null) {
353: throw new BuildException("DestFile was not specified");
354: }
355: }
356: }
|