Ability to specify the project markers, cleaning up the hudson plugin a bit.
authorJan Lahoda <jlahoda@netbeans.org>
Mon, 04 Jul 2011 14:12:37 +0200
changeset 622726459d70a59
parent 621 6181870b7ba7
child 623 2b29633e523d
Ability to specify the project markers, cleaning up the hudson plugin a bit.
remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java
remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly
     1.1 --- a/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Mon Jul 04 12:48:44 2011 +0200
     1.2 +++ b/remoting/server/hudson/src/main/java/org/netbeans/modules/jackpot30/hudson/IndexingBuilder.java	Mon Jul 04 14:12:37 2011 +0200
     1.3 @@ -50,20 +50,12 @@
     1.4  import hudson.model.Descriptor.FormException;
     1.5  import hudson.model.Hudson;
     1.6  import hudson.remoting.VirtualChannel;
     1.7 -import hudson.scm.ChangeLogSet.AffectedFile;
     1.8 -import hudson.scm.ChangeLogSet.Entry;
     1.9 -import hudson.scm.EditType;
    1.10  import hudson.tasks.Builder;
    1.11  import hudson.util.ArgumentListBuilder;
    1.12 -import java.io.BufferedReader;
    1.13 -import java.io.BufferedWriter;
    1.14  import java.io.File;
    1.15 -import java.io.FileInputStream;
    1.16  import java.io.FileOutputStream;
    1.17  import java.io.IOException;
    1.18 -import java.io.InputStreamReader;
    1.19  import java.io.OutputStreamWriter;
    1.20 -import java.io.Reader;
    1.21  import java.io.Serializable;
    1.22  import java.io.Writer;
    1.23  import java.net.URL;
    1.24 @@ -72,8 +64,8 @@
    1.25  import java.util.HashSet;
    1.26  import java.util.List;
    1.27  import java.util.Set;
    1.28 -import java.util.logging.Level;
    1.29 -import java.util.logging.Logger;
    1.30 +import java.util.regex.Matcher;
    1.31 +import java.util.regex.Pattern;
    1.32  import net.sf.json.JSONObject;
    1.33  import org.kohsuke.stapler.DataBoundConstructor;
    1.34  import org.kohsuke.stapler.StaplerRequest;
    1.35 @@ -113,91 +105,12 @@
    1.36  
    1.37      @Override
    1.38      public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
    1.39 -        Set<String> addedFiles = new HashSet<String>();
    1.40 -        Set<String> removedFiles = new HashSet<String>();
    1.41 -
    1.42 -        for (Entry e : build.getChangeSet()) {
    1.43 -            for (AffectedFile f : e.getAffectedFiles()) {
    1.44 -                if (f.getEditType() == EditType.DELETE) {
    1.45 -                    removedFiles.add(stripLeadingSlash(f.getPath()));
    1.46 -                } else {
    1.47 -                    addedFiles.add(stripLeadingSlash(f.getPath()));
    1.48 -                }
    1.49 -            }
    1.50 -        }
    1.51 -
    1.52 -        boolean success = doIndex(getDescriptor().getCacheDir(), build, launcher, listener, addedFiles, removedFiles);
    1.53 -
    1.54 -//        //XXX:
    1.55 -//        File info = new File(Cache.findCache("jackpot30", 1002).findCacheRoot(build.getWorkspace().toURI().toURL()), "info");
    1.56 -//        String jsonContent = readFully(info);
    1.57 -//        JSONObject json = JSONObject.fromObject(jsonContent);
    1.58 -//
    1.59 -//        String prjName = projectName;
    1.60 -//
    1.61 -//        if (prjName == null || prjName.isEmpty()) {
    1.62 -//            prjName = build.getParent().getDisplayName();
    1.63 -//        }
    1.64 -//
    1.65 -//        if (!prjName.equals(json.get("displayName"))) {
    1.66 -//            json.put("displayName", prjName);
    1.67 -//            write(info, JSONSerializer.toJSON(json).toString());
    1.68 -//        }
    1.69 +        boolean success = doIndex(getDescriptor().getCacheDir(), build, launcher, listener);
    1.70  
    1.71          return success;
    1.72      }
    1.73  
    1.74 -    private static String readFully(File file) throws IOException {
    1.75 -        Reader in = null;
    1.76 -        StringBuilder result = new StringBuilder();
    1.77 -
    1.78 -        try {
    1.79 -            in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
    1.80 -
    1.81 -            int read;
    1.82 -
    1.83 -            while ((read = in.read()) != (-1)) {
    1.84 -                result.append((char) read);
    1.85 -            }
    1.86 -        } finally {
    1.87 -            if (in != null) {
    1.88 -                try {
    1.89 -                    in.close();
    1.90 -                } catch (IOException ex) {
    1.91 -                    Logger.getLogger(IndexingBuilder.class.getName()).log(Level.SEVERE, null, ex);
    1.92 -                }
    1.93 -            }
    1.94 -        }
    1.95 -
    1.96 -        return result.toString();
    1.97 -    }
    1.98 -
    1.99 -    private static void write(File file, String content) throws IOException {
   1.100 -        Writer out = null;
   1.101 -
   1.102 -        try {
   1.103 -            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
   1.104 -            out.write(content);
   1.105 -        } finally {
   1.106 -            if (out != null) {
   1.107 -                try {
   1.108 -                    out.close();
   1.109 -                } catch (IOException ex) {
   1.110 -                    Logger.getLogger(IndexingBuilder.class.getName()).log(Level.SEVERE, null, ex);
   1.111 -                }
   1.112 -            }
   1.113 -        }
   1.114 -    }
   1.115 -
   1.116 -    private String stripLeadingSlash(String path) {
   1.117 -        if (path.length() > 0 && path.charAt(0) == '/') {
   1.118 -            return path.substring(1);
   1.119 -        }
   1.120 -
   1.121 -        return path;
   1.122 -    }
   1.123 -
   1.124 -    protected/*tests*/ boolean doIndex(File cacheDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener, Set<String> addedOrModified, Set<String> removed) throws IOException, InterruptedException {
   1.125 +    protected/*tests*/ boolean doIndex(File cacheDir, AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
   1.126          IndexingTool t = findSelectedTool();
   1.127  
   1.128          if (t == null) {
   1.129 @@ -207,7 +120,7 @@
   1.130  
   1.131          t = t.forNode(build.getBuiltOn(), listener);
   1.132  
   1.133 -        RemoteResult res = build.getWorkspace().act(new FindProjects());
   1.134 +        RemoteResult res = build.getWorkspace().act(new FindProjects(getDescriptor().getProjectMarkers()));
   1.135  
   1.136          listener.getLogger().println("Running: " + toolName + " on projects: " + res);
   1.137  
   1.138 @@ -269,17 +182,14 @@
   1.139          return null;
   1.140      }
   1.141  
   1.142 -    private static final String[] PROJECT_MARKERS = new String[] {
   1.143 -        "nbproject/project.xml",
   1.144 -        "pom.xml"
   1.145 -    };
   1.146 -
   1.147 -    private static void findProjects(File root, Collection<String> result, StringBuilder relPath) {
   1.148 +    private static void findProjects(File root, Collection<String> result, Pattern markers, StringBuilder relPath) {
   1.149          int len = relPath.length();
   1.150          boolean first = relPath.length() == 0;
   1.151  
   1.152 -        for (String marker : PROJECT_MARKERS) {
   1.153 -            if (new File(root, marker).canRead()) result.add(relPath.toString());
   1.154 +        Matcher m = markers.matcher(relPath);
   1.155 +
   1.156 +        if (m.matches()) {
   1.157 +            result.add(m.group(1));
   1.158          }
   1.159  
   1.160          File[] children = root.listFiles();
   1.161 @@ -289,7 +199,7 @@
   1.162                  if (!first)
   1.163                      relPath.append("/");
   1.164                  relPath.append(c.getName());
   1.165 -                findProjects(c, result, relPath);
   1.166 +                findProjects(c, result, markers, relPath);
   1.167                  relPath.delete(len, relPath.length());
   1.168              }
   1.169          }
   1.170 @@ -308,6 +218,8 @@
   1.171      public static final class DescriptorImpl extends Descriptor<Builder> {
   1.172  
   1.173          private File cacheDir;
   1.174 +        private static final String DEFAULT_PROJECT_MARKERS = "(.*)/(nbproject/project.xml|pom.xml)";
   1.175 +        private String projectMarkers = DEFAULT_PROJECT_MARKERS;
   1.176  
   1.177          public DescriptorImpl() {
   1.178              Cache.setStandaloneCacheRoot(cacheDir = new File(Hudson.getInstance().getRootDir(), "index").getAbsoluteFile());
   1.179 @@ -317,12 +229,28 @@
   1.180              return cacheDir;
   1.181          }
   1.182  
   1.183 +        public String getProjectMarkers() {
   1.184 +            return projectMarkers;
   1.185 +        }
   1.186 +
   1.187 +        public  void setProjectMarkers(String projectMarkers) {
   1.188 +            this.projectMarkers = projectMarkers;
   1.189 +        }
   1.190 +
   1.191          @Override
   1.192          public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormException {
   1.193              return new IndexingBuilder(req, formData);
   1.194          }
   1.195  
   1.196          @Override
   1.197 +        public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
   1.198 +            cacheDir = new File(json.getString("cacheDir"));
   1.199 +            projectMarkers = json.optString("projectMarkers", DEFAULT_PROJECT_MARKERS);
   1.200 +            
   1.201 +            return super.configure(req, json);
   1.202 +        }
   1.203 +
   1.204 +        @Override
   1.205          public String getDisplayName() {
   1.206              return "Run Indexers";
   1.207          }
   1.208 @@ -337,10 +265,14 @@
   1.209      }
   1.210  
   1.211      private static class FindProjects implements FileCallable<RemoteResult> {
   1.212 +        private final String markers;
   1.213 +        public FindProjects(String markers) {
   1.214 +            this.markers = markers;
   1.215 +        }
   1.216          public RemoteResult invoke(File file, VirtualChannel vc) throws IOException, InterruptedException {
   1.217              Set<String> projects = new HashSet<String>();
   1.218  
   1.219 -            findProjects(file, projects, new StringBuilder());
   1.220 +            findProjects(file, projects, Pattern.compile(markers), new StringBuilder());
   1.221  
   1.222              return new RemoteResult(projects, file.getCanonicalPath()/*XXX: will resolve symlinks!!!*/);
   1.223          }
     2.1 --- a/remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly	Mon Jul 04 12:48:44 2011 +0200
     2.2 +++ b/remoting/server/hudson/src/main/resources/org/netbeans/modules/jackpot30/hudson/IndexingBuilder/global.jelly	Mon Jul 04 14:12:37 2011 +0200
     2.3 @@ -4,6 +4,11 @@
     2.4          <f:entry title="Cache directory:" field="cacheDir">
     2.5              <f:textbox />
     2.6          </f:entry>
     2.7 +        <f:advanced>
     2.8 +        <f:entry title="Project markers:" field="projectMarkers">
     2.9 +            <f:textbox />
    2.10 +        </f:entry>
    2.11 +        </f:advanced>
    2.12      </f:section>
    2.13      
    2.14  </j:jelly>