Generating .url files for each snippet, so the website can link to the hg web
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 17 Aug 2008 17:15:52 +0200
changeset 274e1a7420cea38
parent 273 cfc5a3fd5ae8
child 275 ee4c87a79dba
Generating .url files for each snippet, so the website can link to the hg web
java/ant/src/org/apidesign/infra/ant/GrepCopy.java
java/ant/src/org/apidesign/infra/ant/GrepFilter.java
java/ant/test/org/apidesign/infra/ant/GrepCopyTest.java
java/ant/test/org/apidesign/infra/ant/copy.xml
samples/build.xml
samples/libs/build.xml.orig
     1.1 --- a/java/ant/src/org/apidesign/infra/ant/GrepCopy.java	Tue Aug 12 13:47:15 2008 +0200
     1.2 +++ b/java/ant/src/org/apidesign/infra/ant/GrepCopy.java	Sun Aug 17 17:15:52 2008 +0200
     1.3 @@ -3,6 +3,7 @@
     1.4  import java.io.File;
     1.5  import java.io.FileWriter;
     1.6  import java.io.IOException;
     1.7 +import java.net.URL;
     1.8  import java.util.Map.Entry;
     1.9  import org.apache.tools.ant.BuildException;
    1.10  import org.apache.tools.ant.Task;
    1.11 @@ -12,6 +13,7 @@
    1.12  public class GrepCopy extends Task {
    1.13      private GrepFilter filter = new GrepFilter();
    1.14      private File dir;
    1.15 +    private URL url;
    1.16      
    1.17      public GrepCopy() {
    1.18      }
    1.19 @@ -23,6 +25,10 @@
    1.20      public void setTarget(File dir) {
    1.21          this.dir = dir;
    1.22      }
    1.23 +    
    1.24 +    public void setBaseURL(URL url) {
    1.25 +        this.url = url;
    1.26 +    }
    1.27  
    1.28      @Override
    1.29      public void execute() throws BuildException {
    1.30 @@ -35,11 +41,24 @@
    1.31              FileWriter w = null;
    1.32              try {
    1.33                  Entry en = (Entry) object;
    1.34 -                File to = new File(dir, (String) en.getKey());
    1.35 -                to.getParentFile().mkdirs();
    1.36 -                w = new FileWriter(to);
    1.37 -                w.write((String) en.getValue());
    1.38 -                w.close();
    1.39 +                String key = (String)en.getKey();
    1.40 +                {
    1.41 +                    File to = new File(dir, key);
    1.42 +                    to.getParentFile().mkdirs();
    1.43 +                    w = new FileWriter(to);
    1.44 +                    w.write((String) en.getValue());
    1.45 +                    w.close();
    1.46 +                }
    1.47 +                
    1.48 +                if (url != null) {
    1.49 +                    URL u = filter.getPath(url, key);
    1.50 +                    File to = new File(dir, key + ".url");
    1.51 +                    to.getParentFile().mkdirs();
    1.52 +                    w = new FileWriter(to);
    1.53 +                    w.write(u.toExternalForm());
    1.54 +                    w.close();
    1.55 +                }
    1.56 +                
    1.57              } catch (IOException ex) {
    1.58                  throw new BuildException(ex);
    1.59              }
     2.1 --- a/java/ant/src/org/apidesign/infra/ant/GrepFilter.java	Tue Aug 12 13:47:15 2008 +0200
     2.2 +++ b/java/ant/src/org/apidesign/infra/ant/GrepFilter.java	Sun Aug 17 17:15:52 2008 +0200
     2.3 @@ -4,6 +4,8 @@
     2.4  import java.io.File;
     2.5  import java.io.FileReader;
     2.6  import java.io.IOException;
     2.7 +import java.net.MalformedURLException;
     2.8 +import java.net.URL;
     2.9  import java.util.HashMap;
    2.10  import java.util.Map;
    2.11  import java.util.Stack;
    2.12 @@ -26,6 +28,7 @@
    2.13      private Pattern begin = Pattern.compile(".* BEGIN: *(\\p{Graph}+)[-\\> ]*");
    2.14      private Pattern end = Pattern.compile(".* (END|FINISH): *(\\p{Graph}+)[-\\> ]*");
    2.15      private boolean openoffice;
    2.16 +    private Map<String,String> paths = new HashMap<String, String>();
    2.17      
    2.18      
    2.19      public FileSet createFileSet() {
    2.20 @@ -111,6 +114,7 @@
    2.21                          throw new BuildException("Not closed section " + entry.getKey() + " in " + file);
    2.22                      }
    2.23                      entry.setValue(v.toString());
    2.24 +                    paths.put(entry.getKey(), path);
    2.25                  }
    2.26              }
    2.27              
    2.28 @@ -136,6 +140,10 @@
    2.29          FilterSet filter = createFilterSet();
    2.30          getProject().addReference(id, filter);
    2.31      }
    2.32 +
    2.33 +    final URL getPath(URL root, String key) throws MalformedURLException {
    2.34 +        return new URL(root, paths.get(key));
    2.35 +    }
    2.36      
    2.37      private String linize(String input) {
    2.38          if (!openoffice) {
     3.1 --- a/java/ant/test/org/apidesign/infra/ant/GrepCopyTest.java	Tue Aug 12 13:47:15 2008 +0200
     3.2 +++ b/java/ant/test/org/apidesign/infra/ant/GrepCopyTest.java	Sun Aug 17 17:15:52 2008 +0200
     3.3 @@ -81,6 +81,31 @@
     3.4          assertEquals("public interface I {\n}\n", r);
     3.5      }
     3.6      
     3.7 +    public void testURLGenerated() throws Exception {
     3.8 +        String c1 =
     3.9 +            "package ahoj;\n" +
    3.10 +            "// BEGIN: xyz\n" +
    3.11 +            "public interface I {\n" +
    3.12 +            "// FINISH: xyz\n" +
    3.13 +            "  public void get();\n" +
    3.14 +            "}" +
    3.15 +            "";
    3.16 +        File src = createFile(1, "I.java", c1);
    3.17 +        
    3.18 +        
    3.19 +        String c2 =
    3.20 +            "@xyz@";
    3.21 +        File txt = createFile(2, "in.txt", c2);
    3.22 +        
    3.23 +        execute(1, 2, "-Dfile1=" + txt, "-Dinclude1=*.java", "-Dout.url=http://xyz/", "url");
    3.24 +        
    3.25 +        String r = readFile("xyz");
    3.26 +        assertEquals("public interface I {\n}\n", r);
    3.27 +
    3.28 +        String u = readFile("xyz.url");
    3.29 +        assertEquals("http://xyz/I.java", u);
    3.30 +    }
    3.31 +    
    3.32      public void testSpacesAtBeginingAreStripped() throws Exception {
    3.33          String c1 =
    3.34              "package ahoj;\n" +
     4.1 --- a/java/ant/test/org/apidesign/infra/ant/copy.xml	Tue Aug 12 13:47:15 2008 +0200
     4.2 +++ b/java/ant/test/org/apidesign/infra/ant/copy.xml	Sun Aug 17 17:15:52 2008 +0200
     4.3 @@ -1,9 +1,9 @@
     4.4  <?xml version="1.0" encoding="UTF-8"?>
     4.5  <project name="testing build script" default="all" basedir=".">
     4.6 +    <taskdef name="grepcopy" classname="org.apidesign.infra.ant.GrepCopy"/>
     4.7 +    <fail unless="out.dir"/>
     4.8 +    
     4.9      <target name="all">
    4.10 -        <taskdef name="grepcopy" classname="org.apidesign.infra.ant.GrepCopy"/>
    4.11 -        <fail unless="out.dir"/>
    4.12 -
    4.13          <grepcopy target="${out.dir}">
    4.14              <fileset dir="${dir1}">
    4.15                  <include name="${include1}"/>
    4.16 @@ -11,4 +11,11 @@
    4.17          </grepcopy>
    4.18      </target>
    4.19  
    4.20 +    <target name="url">
    4.21 +        <grepcopy target="${out.dir}" baseurl="${out.url}">
    4.22 +            <fileset dir="${dir1}">
    4.23 +                <include name="${include1}"/>
    4.24 +            </fileset>    
    4.25 +        </grepcopy>
    4.26 +    </target>
    4.27  </project>
     5.1 --- a/samples/build.xml	Tue Aug 12 13:47:15 2008 +0200
     5.2 +++ b/samples/build.xml	Sun Aug 17 17:15:52 2008 +0200
     5.3 @@ -55,7 +55,7 @@
     5.4          <property name="snippets.dir" location="build/snippets"/>
     5.5          <ant dir="../java/ant"/>
     5.6          <taskdef name="grepcopy" classname="org.apidesign.infra.ant.GrepCopy" classpath="../java/ant/dist/apidesign-ant-tasks.jar"/>
     5.7 -        <grepcopy target="${snippets.dir}">
     5.8 +        <grepcopy target="${snippets.dir}" baseurl="http://source.apidesign.org/hg/apidesign/file/tip/samples/">
     5.9              <fileset dir=".">
    5.10                  <include name="**/*"/>
    5.11                  <exclude name="**/build/**/*"/>
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/samples/libs/build.xml.orig	Sun Aug 17 17:15:52 2008 +0200
     6.3 @@ -0,0 +1,80 @@
     6.4 +<?xml version="1.0" encoding="UTF-8"?>
     6.5 +<project name="Downloads necessary libraries" default="libraries" basedir=".">
     6.6 +       <target name="libraries">
     6.7 +        <antcall target="-library">
     6.8 +            <param name="library" value="junit-4.4"/>
     6.9 +            <param name="url" value="http://switch.dl.sourceforge.net/sourceforge/junit/junit-4.4.jar"/>
    6.10 +        </antcall>
    6.11 +        <antcall target="-library-from-zip">
    6.12 +            <param name="library" value="org-openide-util"/>
    6.13 +            <param name="library.include" value="**/org-openide-util.jar"/>
    6.14 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/platform9/org-openide-util.nbm"/>
    6.15 +        </antcall>
    6.16 +        <antcall target="-library-from-zip">
    6.17 +            <param name="library" value="org-openide-filesystems"/>
    6.18 +            <param name="library.include" value="**/org-openide-filesystems.jar"/>
    6.19 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/platform9/org-openide-filesystems.nbm"/>
    6.20 +        </antcall>
    6.21 +        <antcall target="-library-from-zip">
    6.22 +            <param name="library" value="org-netbeans-modules-nbjunit"/>
    6.23 +            <param name="library.include" value="**/org-netbeans-modules-nbjunit.jar"/>
    6.24 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/testtools/org-netbeans-modules-nbjunit.nbm"/>
    6.25 +        </antcall>
    6.26 +        <antcall target="-library-from-zip">
    6.27 +            <param name="library" value="org-netbeans-insane"/>
    6.28 +            <param name="library.include" value="**/org-netbeans-insane.jar"/>
    6.29 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/ide10/org-netbeans-insane.nbm"/>
    6.30 +        </antcall>
    6.31 +        <antcall target="-library-from-zip">
    6.32 +            <param name="library" value="spring-2.5"/>
    6.33 +            <param name="library.include" value="**/*spring*.jar"/>
    6.34 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/java2/org-netbeans-libs-springframework.nbm"/>
    6.35 +        </antcall>
    6.36 +        <antcall target="-library-from-zip">
    6.37 +            <param name="library" value="commons-logging-1.1"/>
    6.38 +            <param name="library.include" value="**/*commons*logg*.jar"/>
    6.39 +            <param name="url" value="http://deadlock.netbeans.org/hudson/job/javadoc-nbms/lastSuccessfulBuild/artifact/nbbuild/nbms/ide9/org-netbeans-libs-commons_logging.nbm"/>
    6.40 +        </antcall>
    6.41 +    </target>
    6.42 +    
    6.43 +    <!-- support methods -->
    6.44 +    
    6.45 +    <target name="-library-check">
    6.46 +        <fail message="You need to specify library name" unless="library"/>
    6.47 +        <fail message="You need to specify url" unless="url"/>
    6.48 +        
    6.49 +        <property name="library.jar" location="dist/${library}.jar"/>
    6.50 +        <property name="library.zip" location="dist/${library}.zip"/>
    6.51 +        
    6.52 +        <echo message="Checking for ${library.jar}"/>
    6.53 +        <available file="${library.jar}" property="library.available"/>
    6.54 +    </target>
    6.55 +    <target name="-library" depends="-library-check" unless="library.available">
    6.56 +        <mkdir dir="dist"/>
    6.57 +        <get dest="${library.jar}" src="${url}"/>
    6.58 +        
    6.59 +        <fail message="Now the library ${library} should be downloaded">
    6.60 +            <condition>
    6.61 +                <not><available file="${library.jar}"/></not>
    6.62 +            </condition>
    6.63 +        </fail>
    6.64 +    </target>
    6.65 +    <target name="-library-from-zip" depends="-library-check" unless="library.available">
    6.66 +        <mkdir dir="dist"/>
    6.67 +        <get dest="${library.zip}" src="${url}"/>
    6.68 +        <unzip dest="dist" src="${library.zip}">
    6.69 +            <patternset includes="${library.include}"/>
    6.70 +            <mapper type="flatten"/>
    6.71 +        </unzip>
    6.72 +        
    6.73 +        <fail message="Now the library ${library} should be downloaded">
    6.74 +            <condition>
    6.75 +                <not><available file="${library.jar}"/></not>
    6.76 +            </condition>
    6.77 +        </fail>
    6.78 +    </target>
    6.79 +    
    6.80 +    <target name="clean">
    6.81 +        <delete dir="dist"/>
    6.82 +    </target>
    6.83 +</project>