Browse Source

Print Projet help information

Submitted by:	Marcel Schutte <marcel@schutte.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@267937 13f79535-47bb-0310-9956-ffa450edef68
master
Conor MacNeill 25 years ago
parent
commit
21ac4b4b7f
3 changed files with 77 additions and 5 deletions
  1. +62
    -3
      src/main/org/apache/tools/ant/Main.java
  2. +5
    -1
      src/main/org/apache/tools/ant/ProjectHelper.java
  3. +10
    -1
      src/main/org/apache/tools/ant/Target.java

+ 62
- 3
src/main/org/apache/tools/ant/Main.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -107,6 +107,11 @@ public class Main {
*/
private boolean readyToRun = false;

/**
* Indicates we should only parse and display the project help information
*/
private boolean projectHelp = false;
/**
* Command line entry point. This method kicks off the building
* of a project object and executes a build using either a given
@@ -211,6 +216,9 @@ public class Main {
loggerClassname = args[++i];
} else if (arg.equals("-emacs")) {
emacsMode = true;
} else if (arg.equals("-projecthelp")) {
// set the flag to display the targets and quit
projectHelp = true;
} else if (arg.startsWith("-")) {
// we don't have any more args to recognize!
String msg = "Unknown arg: " + arg;
@@ -295,8 +303,12 @@ public class Main {
targets.addElement(project.getDefaultTarget());
}

// actually do some work
project.executeTargets(targets);
if (projectHelp) {
printTargets(project);
} else {
// actually do some work
project.executeTargets(targets);
}
}
catch(RuntimeException exc) {
error = exc;
@@ -369,6 +381,7 @@ public class Main {
msg.append("ant [options] [target]" + lSep);
msg.append("Options: " + lSep);
msg.append(" -help print this message" + lSep);
msg.append(" -projecthelp print project help information" + lSep);
msg.append(" -version print the version information and exit" + lSep);
msg.append(" -quiet be extra quiet" + lSep);
msg.append(" -verbose be extra verbose" + lSep);
@@ -404,4 +417,50 @@ public class Main {
System.err.println("Could not load the version information.");
}
}
/**
* Print out a list of all targets in the current buildfile
*/
private static void printTargets(Project project) {
// find the target with the longest name and
// filter out the targets with no description
int maxLength = 0;
Enumeration ptargets = project.getTargets().elements();
String targetName;
String targetDescription;
Target currentTarget;
Vector names = new Vector();
Vector descriptions = new Vector();

while (ptargets.hasMoreElements()) {
currentTarget = (Target)ptargets.nextElement();
targetName = currentTarget.getName();
targetDescription = currentTarget.getDescription();
if (targetDescription == null) {
targetDescription = "";
}
names.addElement(targetName);
descriptions.addElement(targetDescription);
if (targetName.length() > maxLength) {
maxLength = targetName.length();
}
}

// now, start printing the targets and their descriptions
String lSep = System.getProperty("line.separator");
// got a bit annoyed that I couldn't find a pad function
String spaces = " ";
while (spaces.length()<maxLength) {
spaces += spaces;
}
StringBuffer msg = new StringBuffer();
msg.append("Targets: " + lSep);
for (int i=0; i<names.size(); i++) {
msg.append(" -"+names.elementAt(i));
msg.append(spaces.substring(0, maxLength - ((String)names.elementAt(i)).length() + 2));
msg.append(descriptions.elementAt(i)+lSep);
}
System.out.println(msg.toString());
}
}

+ 5
- 1
src/main/org/apache/tools/ant/ProjectHelper.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -284,6 +284,7 @@ public class ProjectHelper {
String ifCond = null;
String unlessCond = null;
String id = null;
String description = null;

for (int i = 0; i < attrs.getLength(); i++) {
String key = attrs.getName(i);
@@ -299,6 +300,8 @@ public class ProjectHelper {
unlessCond = value;
} else if (key.equals("id")) {
id = value;
} else if (key.equals("description")) {
description = value;
} else {
throw new SAXParseException("Unexpected attribute \"" + key + "\"", locator);
}
@@ -312,6 +315,7 @@ public class ProjectHelper {
target.setName(name);
target.setIf(ifCond);
target.setUnless(unlessCond);
target.setDescription(description);
project.addTarget(name, target);

if (id != null && !id.equals(""))


+ 10
- 1
src/main/org/apache/tools/ant/Target.java View File

@@ -1,7 +1,7 @@
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* Copyright (c) 1999, 2000 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -70,6 +70,7 @@ public class Target {
private Vector dependencies = new Vector(2);
private Vector tasks = new Vector(5);
private Project project;
private String description = null;

public void setProject(Project project) {
this.project = project;
@@ -117,6 +118,14 @@ public class Target {
this.unlessCondition = (property == null) ? "" : property;
}

public void setDescription(String description) {
this.description = description;
}

public String getDescription() {
return description;
}

public String toString() {
return name;
}


Loading…
Cancel
Save