annotate lib/mod/src/de/masters_of_disaster/ant/tasks/calculatesize/CalculateSize.java @ 33:3d86f0391168

Work on improving the build system.
author David Barts <davidb@stashtea.com>
date Fri, 24 Apr 2020 19:45:57 -0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
1 package de.masters_of_disaster.ant.tasks.calculatesize;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
2
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
3 import java.io.File;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
4 import java.util.Enumeration;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
5 import java.util.Vector;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
6 import org.apache.tools.ant.BuildException;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
7 import org.apache.tools.ant.taskdefs.MatchingTask;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
8 import org.apache.tools.ant.types.FileSet;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
9
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
10 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
11 * Calculates the "Installed-Size" of a deb package for the "control"-file.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
12 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
13 * @ant.task category="packaging"
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
14 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
15 public class CalculateSize extends MatchingTask {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
16 String realSizeProperty = null;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
17 String diskSizeProperty = null;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
18 Vector fileSets = new Vector();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
19 File baseDir;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
20
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
21 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
22 * Add a new fileset
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
23 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
24 * @return the fileset to be used as the nested element.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
25 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
26 public FileSet createFileSet() {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
27 FileSet fileSet = new FileSet();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
28 fileSets.addElement(fileSet);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
29 return fileSet;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
30 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
31
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
32 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
33 * This is the base directory to look in for things to include.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
34 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
35 * @param baseDir the base directory.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
36 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
37 public void setBaseDir(File baseDir) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
38 this.baseDir = baseDir;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
39 fileset.setDir(baseDir);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
40 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
41
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
42 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
43 * This is the property to set to the real size.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
44 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
45 * @param realSizeProperty The property to set to the real size
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
46 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
47 public void setRealSizeProperty(String realSizeProperty) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
48 this.realSizeProperty = realSizeProperty;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
49 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
50
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
51 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
52 * This is the property to set to the disk size.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
53 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
54 * @param diskSizeProperty The property to set to the disk size
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
55 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
56 public void setDiskSizeProperty(String diskSizeProperty) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
57 this.diskSizeProperty = diskSizeProperty;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
58 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
59
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
60 /**
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
61 * do the business
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
62 *
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
63 * @throws BuildException on error
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
64 */
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
65 public void execute() throws BuildException {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
66 if ((null == realSizeProperty) && (null == diskSizeProperty)) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
67 throw new BuildException("realSizeProperty or diskSizeProperty must be set for <CalculateSize>");
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
68 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
69
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
70 if (null != baseDir) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
71 // add the main fileset to the list of filesets to process.
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
72 fileSets.addElement(fileset);
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
73 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
74
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
75 long realSize = 0;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
76 long diskSize = 0;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
77 for (Enumeration e=fileSets.elements() ; e.hasMoreElements() ; ) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
78 FileSet fileSet = (FileSet)e.nextElement();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
79 String[] files = fileSet.getDirectoryScanner(getProject()).getIncludedFiles();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
80 File fileSetDir = fileSet.getDir(getProject());
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
81 for (int i=0, c=files.length ; i<c ; i++) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
82 long fileLength = new File(fileSetDir,files[i]).length();
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
83 realSize += fileLength / 1024;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
84 diskSize += (fileLength / 4096 + 1) * 4;
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
85 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
86 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
87 if (null != realSizeProperty) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
88 getProject().setNewProperty(realSizeProperty,Long.toString(realSize));
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
89 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
90 if (null != diskSizeProperty) {
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
91 getProject().setNewProperty(diskSizeProperty,Long.toString(diskSize));
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
92 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
93 }
3d86f0391168 Work on improving the build system.
David Barts <davidb@stashtea.com>
parents:
diff changeset
94 }