1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
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 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.dotnet;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.Task;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.Project;
|
---|
23 | import org.apache.tools.ant.util.FileUtils;
|
---|
24 |
|
---|
25 | import java.io.File;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Import a COM type library into the .NET framework.
|
---|
29 | * <p>
|
---|
30 | *
|
---|
31 | * This task is a wrapper to .NET's tlbimport; it imports a tlb file to a NET assembly
|
---|
32 | * by generating a binary assembly (.dll) that contains all the binding
|
---|
33 | * metadata. It uses date timestamps to minimise rebuilds.
|
---|
34 | * <p>
|
---|
35 | * Example
|
---|
36 | * <pre>
|
---|
37 | * <importtypelib
|
---|
38 | * srcfile="xerces.tlb"
|
---|
39 | * destfile="xerces.dll"
|
---|
40 | * namespace="Apache.Xerces"/>
|
---|
41 | * </pre>
|
---|
42 | * @since Ant 1.6
|
---|
43 | * @ant.task category="dotnet"
|
---|
44 | */
|
---|
45 | public class ImportTypelib extends Task {
|
---|
46 |
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * input file; precedes options
|
---|
50 | */
|
---|
51 | private File srcFile;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * /out:file
|
---|
55 | */
|
---|
56 | private File destFile;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * /namespace:[string]
|
---|
60 | */
|
---|
61 | private String namespace;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * /sysarray
|
---|
65 | */
|
---|
66 | private boolean useSysArray = false;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * /unsafe
|
---|
70 | */
|
---|
71 | private boolean unsafe = false;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * extra commands?
|
---|
75 | */
|
---|
76 | private String extraOptions = null;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * This method names the output file.
|
---|
80 | *
|
---|
81 | * This is an operation which is required to have been performed.
|
---|
82 | * @param destFile
|
---|
83 | */
|
---|
84 | public void setDestFile(File destFile) {
|
---|
85 | this.destFile = destFile;
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * This method sets what namespace the typelib is to be in.
|
---|
90 | * This is an operation which is required to have been performed.
|
---|
91 | * @param namespace
|
---|
92 | */
|
---|
93 | public void setNamespace(String namespace) {
|
---|
94 | this.namespace = namespace;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * This method sets which is the source .tlb file.
|
---|
99 | * This is an operation which is required to have been performed.
|
---|
100 | * @param srcFile
|
---|
101 | */
|
---|
102 | public void setSrcFile(File srcFile) {
|
---|
103 | this.srcFile = srcFile;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * do you want unsafe code.
|
---|
108 | * @param unsafe
|
---|
109 | */
|
---|
110 | public void setUnsafe(boolean unsafe) {
|
---|
111 | this.unsafe = unsafe;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * set this to map a COM SafeArray to the System.Array class
|
---|
116 | * @param useSysArray
|
---|
117 | */
|
---|
118 | public void setUseSysArray(boolean useSysArray) {
|
---|
119 | this.useSysArray = useSysArray;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * set any extra options that are not yet supported by this task.
|
---|
124 | * @param extraOptions
|
---|
125 | */
|
---|
126 | public void setExtraOptions(String extraOptions) {
|
---|
127 | this.extraOptions = extraOptions;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * validation code
|
---|
132 | * @throws BuildException if validation failed
|
---|
133 | */
|
---|
134 | protected void validate()
|
---|
135 | throws BuildException {
|
---|
136 | if (destFile == null) {
|
---|
137 | throw new BuildException("destination file must be specified");
|
---|
138 | }
|
---|
139 | if (destFile.isDirectory()) {
|
---|
140 | throw new BuildException(
|
---|
141 | "destination file is a directory");
|
---|
142 | }
|
---|
143 | if (srcFile == null || !srcFile.exists()) {
|
---|
144 | throw new BuildException(
|
---|
145 | "source file does not exist");
|
---|
146 | }
|
---|
147 | if (srcFile.isDirectory()) {
|
---|
148 | throw new BuildException(
|
---|
149 | "source file is a directory");
|
---|
150 | }
|
---|
151 | if (namespace == null) {
|
---|
152 | throw new BuildException("No namespace");
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Test for disassembly being needed; use existence and granularity
|
---|
158 | * correct date stamps
|
---|
159 | * @return true iff a rebuild is required.
|
---|
160 | */
|
---|
161 | private boolean isExecuteNeeded() {
|
---|
162 | if (!destFile.exists()) {
|
---|
163 | log("Destination file does not exist: a build is required",
|
---|
164 | Project.MSG_VERBOSE);
|
---|
165 | return true;
|
---|
166 | }
|
---|
167 | long sourceTime = srcFile.lastModified();
|
---|
168 | long destTime = destFile.lastModified();
|
---|
169 | if (sourceTime > (destTime + FileUtils.newFileUtils().getFileTimestampGranularity())) {
|
---|
170 | log("Source file is newer than the dest file: a rebuild is required",
|
---|
171 | Project.MSG_VERBOSE);
|
---|
172 | return true;
|
---|
173 | } else {
|
---|
174 | log("The output file is up to date", Project.MSG_VERBOSE);
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 |
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Create a typelib command
|
---|
183 | * @exception BuildException if something goes wrong with the build
|
---|
184 | */
|
---|
185 | public void execute() throws BuildException {
|
---|
186 | validate();
|
---|
187 | log("Importing typelib " + srcFile
|
---|
188 | + " to assembly " + destFile
|
---|
189 | + " in namespace " + namespace, Project.MSG_VERBOSE);
|
---|
190 | //rebuild unless the dest file is newer than the source file
|
---|
191 | if(!isExecuteNeeded()) {
|
---|
192 | return;
|
---|
193 | }
|
---|
194 |
|
---|
195 | NetCommand command = new NetCommand(this, "ImportTypelib", "tlbimp");
|
---|
196 | command.setFailOnError(true);
|
---|
197 | command.addArgument(srcFile.toString());
|
---|
198 | //fill in args
|
---|
199 | command.addArgument("/nologo");
|
---|
200 | command.addArgument("/out:" + destFile);
|
---|
201 | command.addArgument("/namespace:", namespace);
|
---|
202 | if (useSysArray) {
|
---|
203 | command.addArgument("/sysarray");
|
---|
204 | }
|
---|
205 | if (unsafe) {
|
---|
206 | command.addArgument("/unsafe");
|
---|
207 | }
|
---|
208 | command.addArgument(extraOptions);
|
---|
209 | command.runCommand();
|
---|
210 | }
|
---|
211 | }
|
---|