show used nuget packages on server
authorEugene Petrenko <eugene.petrenko@gmail.com>
Tue, 19 Jul 2011 11:59:53 +0000 (15:59 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Tue, 19 Jul 2011 11:59:53 +0000 (15:59 +0400)
nuget-agent/src/jetbrains/buildServer/nuget/agent/install/impl/PackagesInfoUploader.java
nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependenciesStore.java
nuget-common/src/jetbrains/buildServer/nuget/common/PackagesConstants.java
nuget-server/resources/NuGetBuild.jsp [deleted file]
nuget-server/resources/show/showPackages.jsp [new file with mode: 0644]
nuget-server/src/META-INF/build-server-plugin-nuget.xml
nuget-server/src/jetbrains/buildServer/nuget/server/NuGetDownloadedPackagesTab.java [deleted file]
nuget-server/src/jetbrains/buildServer/nuget/server/show/NuGetDownloadedPackagesTab.java [new file with mode: 0644]

index 3bea681faf23fb0ccdcb902c9732576ae54e5c58..10c2819e2c8d036904d342a28570b98a284a6b47 100644 (file)
@@ -26,6 +26,9 @@ import org.jetbrains.annotations.NotNull;
 import java.io.File;\r
 import java.io.IOException;\r
 \r
+import static jetbrains.buildServer.nuget.common.PackagesConstants.NUGET_USED_PACKAGES_DIR;\r
+import static jetbrains.buildServer.nuget.common.PackagesConstants.NUGET_USED_PACKAGES_FILE;\r
+\r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 18.07.11 23:02\r
@@ -43,8 +46,8 @@ public class PackagesInfoUploader {
   public void uploadDepectedPackages(@NotNull final AgentRunningBuild build,\r
                                      @NotNull final PackageDependencies infos) throws IOException {\r
     File tmp = FileUtil.createTempDirectory("nuget", "packages", build.getBuildTempDirectory());\r
-    File content = new File(tmp, "nuget.xml");\r
+    File content = new File(tmp, NUGET_USED_PACKAGES_FILE);\r
     myStore.save(infos, content);\r
-    myPublisher.addNewArtifactsPath(content.getPath() + " => .teamcity/nuget");\r
+    myPublisher.addNewArtifactsPath(content.getPath() + " => " + NUGET_USED_PACKAGES_DIR);\r
   }\r
 }\r
index fbe7a357b0e8eb359b642c8a1fe653b5091fb0b1..1b7514ac2adfb2b77c406e507e73b13e423324a5 100644 (file)
 package jetbrains.buildServer.nuget.common;\r
 \r
 import jetbrains.buildServer.util.FileUtil;\r
+import jetbrains.buildServer.util.StringUtil;\r
 import jetbrains.buildServer.util.XmlUtil;\r
-import jetbrains.buildServer.util.XmlXppAbstractParser;\r
 import org.jdom.Content;\r
 import org.jdom.Document;\r
 import org.jdom.Element;\r
+import org.jdom.JDOMException;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
 import java.io.*;\r
 import java.util.ArrayList;\r
-import java.util.Arrays;\r
 import java.util.List;\r
 \r
 /**\r
@@ -34,47 +34,47 @@ import java.util.List;
  * Date: 19.07.11 11:39\r
  */\r
 public class PackageDependenciesStore {\r
-  public PackageDependencies load(@NotNull final File file) throws IOException {\r
+\r
+  public PackageDependencies load(@NotNull final InputStream is) throws IOException {\r
+    Element element = null;\r
+    try {\r
+      element = FileUtil.parseDocument(is, false);\r
+    } catch (final JDOMException e) {\r
+      throw new IOException("Failed to parse stream." + e.getMessage()){{initCause(e);}};\r
+    }\r
+\r
     final List<String> sources = new ArrayList<String>();\r
     final List<PackageInfo> infos = new ArrayList<PackageInfo>();\r
 \r
-    new XmlXppAbstractParser(){\r
-      @Override\r
-      protected List<XmlHandler> getRootHandlers() {\r
-        return Arrays.asList(elementsPath(new Handler() {\r
-          public XmlReturn processElement(@NotNull XmlElementInfo xmlElementInfo) {\r
-            return xmlElementInfo.visitChildren(\r
-                    elementsPath(\r
-                            new Handler() {\r
-                              public XmlReturn processElement(@NotNull XmlElementInfo xmlElementInfo) {\r
-                                final String id = xmlElementInfo.getAttribute("id");\r
-                                final String version = xmlElementInfo.getAttribute("version");\r
-                                if (id != null && version != null) {\r
-                                  infos.add(new PackageInfo(id, version));\r
-                                }\r
-                                return xmlElementInfo.noDeep();\r
-                              }\r
-                            }, "packages", "package"),\r
-                    elementsPath(\r
-                            new Handler() {\r
-                              public XmlReturn processElement(@NotNull XmlElementInfo xmlElementInfo) {\r
-                                xmlElementInfo.visitText(new TextHandler() {\r
-                                  public void setText(@NotNull String s) {\r
-                                    sources.add(s);\r
-                                  }\r
-                                });\r
-                                return null;\r
-                              }\r
-                            }, "sources", "source")\r
-            );\r
-          }\r
-        }, "nuget-dependencies"));\r
+    Element packagesElement = element.getChild("packages");\r
+    if (packagesElement != null) {\r
+      for (Object pkg : packagesElement.getChildren("package")) {\r
+        Element el = (Element) pkg;\r
+        final String id = el.getAttributeValue("id");\r
+        final String version = el.getAttributeValue("version");\r
+        if (id != null && version != null) {\r
+          infos.add(new PackageInfo(id, version));\r
+        }\r
       }\r
-    }.parse(file);\r
+    }\r
 \r
+    Element sourcesElement = element.getChild("sources");\r
+    if (sourcesElement != null) {\r
+      for (Object o : sourcesElement.getChildren("source")) {\r
+        Element source = (Element) o;\r
+        String text = source.getTextTrim();\r
+        if (!StringUtil.isEmptyOrSpaces(text)) {\r
+          sources.add(text);\r
+        }\r
+      }\r
+    }\r
     return new PackageDependencies(sources, infos);\r
   }\r
 \r
+  public PackageDependencies load(@NotNull final File file) throws IOException {\r
+    return load(new BufferedInputStream(new FileInputStream(file)));\r
+  }\r
+\r
   public void save(@NotNull final PackageDependencies deps,\r
                    @NotNull final File file) throws IOException {\r
     Element root = new Element("nuget-dependencies");\r
@@ -87,16 +87,16 @@ public class PackageDependenciesStore {
       pkgs.addContent((Content) pkg);\r
     }\r
 \r
-    root.addContent((Content)pkgs);\r
+    root.addContent((Content) pkgs);\r
 \r
     Element sources = new Element("sources");\r
     for (String source : deps.getSources()) {\r
       Element src = new Element("source");\r
       src.setText(source);\r
-      sources.addContent((Content)src);\r
+      sources.addContent((Content) src);\r
     }\r
 \r
-    root.addContent((Content)sources);\r
+    root.addContent((Content) sources);\r
 \r
     Document doc = new Document(root);\r
 \r
index 404819d6d2e80f553aa7e6740a16d7b357c45604..29b929fb2dc8e6940a6b4c5eb407ebafce9a00be 100644 (file)
@@ -16,6 +16,8 @@
 \r
 package jetbrains.buildServer.nuget.common;\r
 \r
+import jetbrains.buildServer.ArtifactsConstants;\r
+\r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 07.07.11 13:56\r
@@ -34,4 +36,7 @@ public interface PackagesConstants {
   public static final String NUGET_UPDATE_MODE = "nuget.updatePackages.mode";\r
 \r
   public static final String SLN_PATH = "sln.path";\r
+\r
+  public static final String NUGET_USED_PACKAGES_DIR = ArtifactsConstants.TEAMCITY_ARTIFACTS_DIR + "/nuget";\r
+  public static final String NUGET_USED_PACKAGES_FILE = "nuget.xml";\r
 }\r
diff --git a/nuget-server/resources/NuGetBuild.jsp b/nuget-server/resources/NuGetBuild.jsp
deleted file mode 100644 (file)
index 6cf7398..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<%@ taglib prefix="forms" tagdir="/WEB-INF/tags/forms" %>\r
-<%@ taglib prefix="props" tagdir="/WEB-INF/tags/props" %>\r
-<%@ taglib prefix="l" tagdir="/WEB-INF/tags/layout" %>\r
-<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>\r
-<jsp:useBean id="nugetPackages" scope="request" type="java.util.Map<java.lang.String, java.lang.String>"/>\r
-\r
-This is the list of all NuGet packages that were downloaded for the build.\r
-\r
-<table class="runnerFormTable" style="width:40em">\r
-  <thead>\r
-    <tr>\r
-      <th>Package Name</th>\r
-      <th>Package Version</th>\r
-    </tr>\r
-  </thead>\r
-<c:forEach var="it" items="${nugetPackages}">\r
-  <tr>\r
-    <td><a href="http://somewhere">${it.key}</a></td>\r
-    <td>${it.value}</td>\r
-  </tr>\r
-</c:forEach>\r
-</table>
\ No newline at end of file
diff --git a/nuget-server/resources/show/showPackages.jsp b/nuget-server/resources/show/showPackages.jsp
new file mode 100644 (file)
index 0000000..9b4dca7
--- /dev/null
@@ -0,0 +1,31 @@
+<%@ taglib prefix="forms" tagdir="/WEB-INF/tags/forms" %>\r
+<%@ taglib prefix="props" tagdir="/WEB-INF/tags/props" %>\r
+<%@ taglib prefix="l" tagdir="/WEB-INF/tags/layout" %>\r
+<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>\r
+<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>\r
+<jsp:useBean id="nugetPackages" scope="request" type="java.util.Map<java.lang.String, java.lang.String>"/>\r
+\r
+This is the list of all NuGet packages that were downloaded for the build.\r
+\r
+<c:choose>\r
+  <c:when test="${fn:length(nugetPackages) eq 0}">\r
+    No packages were reported\r
+  </c:when>\r
+  <c:otherwise>\r
+    <table class="runnerFormTable" style="width:40em">\r
+      <thead>\r
+      <tr>\r
+        <th>Package Name</th>\r
+        <th>Package Version</th>\r
+      </tr>\r
+      </thead>\r
+      <c:forEach var="it" items="${nugetPackages}">\r
+        <tr>\r
+          <td><c:out value="${it.key}"/></td>\r
+          <td><c:out value="${it.value}"/></td>\r
+        </tr>\r
+      </c:forEach>\r
+    </table>\r
+  </c:otherwise>\r
+</c:choose>\r
+\r
index a1ba186b065bb3e69ca5d14363bd48f89c0c72d7..c98d536bdc5ff4acdc8b22483afd0acabe056614 100644 (file)
@@ -6,7 +6,7 @@
 \r
 <!--\r
   <bean class="jetbrains.buildServer.nuget.server.NuGetPublishRunType"/>\r
-  <bean class="jetbrains.buildServer.nuget.server.NuGetDownloadedPackagesTab"/>\r
+  <bean class="jetbrains.buildServer.nuget.server.show.NuGetDownloadedPackagesTab"/>\r
   <bean class="jetbrains.buildServer.nuget.server.NuGetDownloadFeature"/>\r
 \r
 -->\r
@@ -22,4 +22,7 @@
   <bean class="jetbrains.buildServer.nuget.server.trigger.ThreadedBuildTriggetFactoryImpl"/>\r
   <bean class="jetbrains.buildServer.nuget.server.trigger.NamedPackagesUpdateChecker"/>\r
   <bean class="jetbrains.buildServer.nuget.server.trigger.NuGetSimpleTrigger"/>\r
+\r
+\r
+  <bean class="jetbrains.buildServer.nuget.server.show.NuGetDownloadedPackagesTab"/>\r
 </beans>
\ No newline at end of file
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/NuGetDownloadedPackagesTab.java b/nuget-server/src/jetbrains/buildServer/nuget/server/NuGetDownloadedPackagesTab.java
deleted file mode 100644 (file)
index aed2548..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-package jetbrains.buildServer.nuget.server;\r
-\r
-import java.util.Map;\r
-import java.util.TreeMap;\r
-import javax.servlet.http.HttpServletRequest;\r
-import jetbrains.buildServer.serverSide.SBuild;\r
-import jetbrains.buildServer.serverSide.SBuildServer;\r
-import jetbrains.buildServer.web.openapi.PagePlaces;\r
-import jetbrains.buildServer.web.openapi.PluginDescriptor;\r
-import jetbrains.buildServer.web.openapi.ViewLogTab;\r
-import org.jetbrains.annotations.NotNull;\r
-import org.jetbrains.annotations.Nullable;\r
-\r
-/**\r
- * Created Eugene Petrenko (eugene.petrenko@gmail.com)\r
- * date: 28.04.11\r
- */\r
-public class NuGetDownloadedPackagesTab extends ViewLogTab {\r
-  public NuGetDownloadedPackagesTab(@NotNull final PagePlaces pagePlaces,\r
-                                    @NotNull final SBuildServer server,\r
-                                    @NotNull final PluginDescriptor descriptor) {\r
-    super("NuGet Packages", "xxx", pagePlaces, server);\r
-    setIncludeUrl(descriptor.getPluginResourcesPath("NuGetBuild.jsp"));\r
-    register();\r
-  }\r
-\r
-  @Override\r
-  public boolean isAvailable(@NotNull final HttpServletRequest request) {\r
-    final SBuild build = getBuild(request);\r
-    return super.isAvailable(request) && build != null && build.getParametersProvider().get("nuget.packages") != null;\r
-  }\r
-\r
-  @Override\r
-  protected void fillModel(final Map model, final HttpServletRequest request, @Nullable final SBuild build) {\r
-    final Map<String, String> packages = new TreeMap<String, String>();\r
-    packages.put("ELMAH", "1.2.0");\r
-    packages.put("Antlr", "4.0.0");\r
-    packages.put("EasyHTTP", "1.1");\r
-    packages.put("JsonFX", "2.0");\r
-    packages.put("structuremap", "2.6.2");\r
-    packages.put("Awsome", "1.5.0");\r
-\r
-    //noinspection unchecked\r
-    model.put("nugetPackages", packages);\r
-  }\r
-}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/show/NuGetDownloadedPackagesTab.java b/nuget-server/src/jetbrains/buildServer/nuget/server/show/NuGetDownloadedPackagesTab.java
new file mode 100644 (file)
index 0000000..b2a98e3
--- /dev/null
@@ -0,0 +1,96 @@
+package jetbrains.buildServer.nuget.server.show;\r
+\r
+import com.intellij.openapi.diagnostic.Logger;\r
+import jetbrains.buildServer.nuget.common.PackageDependencies;\r
+import jetbrains.buildServer.nuget.common.PackageDependenciesStore;\r
+import jetbrains.buildServer.nuget.common.PackageInfo;\r
+import jetbrains.buildServer.serverSide.SBuild;\r
+import jetbrains.buildServer.serverSide.SBuildServer;\r
+import jetbrains.buildServer.serverSide.artifacts.BuildArtifact;\r
+import jetbrains.buildServer.serverSide.artifacts.BuildArtifactsViewMode;\r
+import jetbrains.buildServer.util.FileUtil;\r
+import jetbrains.buildServer.web.openapi.PagePlaces;\r
+import jetbrains.buildServer.web.openapi.PluginDescriptor;\r
+import jetbrains.buildServer.web.openapi.ViewLogTab;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.jetbrains.annotations.Nullable;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.util.Map;\r
+import java.util.TreeMap;\r
+\r
+import static jetbrains.buildServer.nuget.common.PackagesConstants.NUGET_USED_PACKAGES_DIR;\r
+import static jetbrains.buildServer.nuget.common.PackagesConstants.NUGET_USED_PACKAGES_FILE;\r
+\r
+/**\r
+ * Created Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * date: 28.04.11\r
+ */\r
+public class NuGetDownloadedPackagesTab extends ViewLogTab {\r
+  private static final String DEPS_FILE = NUGET_USED_PACKAGES_DIR + "/" + NUGET_USED_PACKAGES_FILE;\r
+  private static final Logger LOG = Logger.getInstance(NuGetDownloadedPackagesTab.class.getName());\r
+\r
+\r
+  private final PackageDependenciesStore myStore;\r
+\r
+  public NuGetDownloadedPackagesTab(@NotNull final PagePlaces pagePlaces,\r
+                                    @NotNull final SBuildServer server,\r
+                                    @NotNull final PluginDescriptor descriptor,\r
+                                    @NotNull final PackageDependenciesStore store) {\r
+    super("NuGet Packages", "nugetPackagesBuildTab", pagePlaces, server);\r
+    myStore = store;\r
+    setIncludeUrl(descriptor.getPluginResourcesPath("show/showPackages.jsp"));\r
+    register();\r
+  }\r
+\r
+  @Override\r
+  public boolean isAvailable(@NotNull final HttpServletRequest request) {\r
+    final SBuild build = getBuild(request);\r
+    return super.isAvailable(request) && getPackagesFile(build) != null;\r
+  }\r
+\r
+  @Nullable\r
+  private BuildArtifact getPackagesFile(@Nullable final SBuild build) {\r
+    if (build == null) return null;\r
+    BuildArtifact file = build.getArtifacts(BuildArtifactsViewMode.VIEW_ALL).getArtifact(DEPS_FILE);\r
+    if (file == null) return null;\r
+    if (file.isDirectory()) return null;\r
+    return file;\r
+  }\r
+\r
+\r
+  @Override\r
+  protected void fillModel(final Map model, final HttpServletRequest request, @Nullable final SBuild build) {\r
+    final Map<String, String> packages = new TreeMap<String, String>();\r
+    //noinspection unchecked\r
+    model.put("nugetPackages", packages);\r
+\r
+    if (build == null) return;\r
+    PackageDependencies deps = loadDependencies(build);\r
+\r
+    if (deps != null) {\r
+      for (PackageInfo info : deps.getPackages()) {\r
+        packages.put(info.getId(), info.getVersion());\r
+      }\r
+    }\r
+  }\r
+\r
+  @Nullable\r
+  private PackageDependencies loadDependencies(@NotNull final SBuild build) {\r
+    final BuildArtifact file = getPackagesFile(build);\r
+    if (file != null) {\r
+      InputStream inputStream = null;\r
+      try {\r
+        inputStream = file.getInputStream();\r
+        return myStore.load(inputStream);\r
+      } catch (IOException e) {\r
+        LOG.warn("Failed to read used packages build artifacts of build id=" + build.getBuildId());\r
+      } finally {\r
+        FileUtil.close(inputStream);\r
+      }\r
+    }\r
+    return null;\r
+  }\r
+}\r