extend nuget-common to support nuget packages info read/write
authorEugene Petrenko <eugene.petrenko@gmail.com>
Tue, 19 Jul 2011 09:23:24 +0000 (13:23 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Tue, 19 Jul 2011 09:23:24 +0000 (13:23 +0400)
nuget-agent/src/META-INF/build-agent-plugin-nuget.xml
nuget-agent/src/jetbrains/buildServer/nuget/agent/install/NuGetPackagesCollector.java
nuget-agent/src/jetbrains/buildServer/nuget/agent/install/impl/NuGetPackagesCollectorImpl.java
nuget-agent/src/jetbrains/buildServer/nuget/agent/install/impl/PackagesInfoUploader.java
nuget-agent/src/jetbrains/buildServer/nuget/agent/install/impl/PackagesWatcher.java
nuget-common/nuget-common.iml
nuget-common/src/META-INF/spring-nuget-common.xml [new file with mode: 0644]
nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependencies.java [new file with mode: 0644]
nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependenciesStore.java [new file with mode: 0644]
nuget-tests/src/jetbrains/buildServer/nuget/tests/agent/NuGetPackagesConfigParserTest.java

index 665cd9abd4746b6e70edc49ee0cbc67ab7477e75..433fa93abba7e11a69623ebc0483bfc9f3eb3ded 100644 (file)
@@ -6,6 +6,8 @@
                            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"\r
        default-autowire="constructor">\r
 \r
+  <import resource="classpath*:META-INF/spring-nuget-common.xml"/>\r
+\r
   <bean class="jetbrains.buildServer.nuget.agent.util.impl.CommandlineBuildProcessFactoryImpl"/>\r
 \r
   <bean class="jetbrains.buildServer.nuget.agent.install.PackagesInstallerRunner"/>\r
index dcb813f5920376d725b29a074460da3ab2b9277a..a561ff2ddfb968a9b80de6c5cd816f8cf18c269e 100644 (file)
 \r
 package jetbrains.buildServer.nuget.agent.install;\r
 \r
-import jetbrains.buildServer.nuget.common.PackageInfo;\r
+import jetbrains.buildServer.nuget.common.PackageDependencies;\r
 import org.jetbrains.annotations.NotNull;\r
 import org.jetbrains.annotations.Nullable;\r
 \r
-import java.util.Collection;\r
-\r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 18.07.11 22:43\r
@@ -36,5 +34,5 @@ public interface NuGetPackagesCollector {
    * @return sorted list of packages that were registered\r
    */\r
   @NotNull\r
-  public Collection<PackageInfo> getPackages();\r
+  public PackageDependencies getPackages();\r
 }\r
index 3803fc508203ececdba48260478600189259f7bc..a798400836ed3c63c77059ac9c395948b3202a60 100644 (file)
 \r
 package jetbrains.buildServer.nuget.agent.install.impl;\r
 \r
+import jetbrains.buildServer.nuget.common.PackageDependencies;\r
 import jetbrains.buildServer.nuget.common.PackageInfo;\r
 import org.jetbrains.annotations.NotNull;\r
 import org.jetbrains.annotations.Nullable;\r
 \r
-import java.util.Collection;\r
 import java.util.Collections;\r
 import java.util.TreeSet;\r
 \r
@@ -37,8 +37,10 @@ public class NuGetPackagesCollectorImpl implements NuGetPackagesCollectorEx {
   }\r
 \r
   @NotNull\r
-  public Collection<PackageInfo> getPackages() {\r
-    return Collections.unmodifiableCollection(myPackages);\r
+  public PackageDependencies getPackages() {\r
+    return new PackageDependencies(\r
+            Collections.<String>emptyList(),\r
+            myPackages);\r
   }\r
 \r
   public void removeAllPackages() {\r
index 4196d2e3f3e6fcbce1a28f3fa5037d89463aa9e7..3bea681faf23fb0ccdcb902c9732576ae54e5c58 100644 (file)
@@ -18,16 +18,13 @@ package jetbrains.buildServer.nuget.agent.install.impl;
 \r
 import jetbrains.buildServer.agent.AgentRunningBuild;\r
 import jetbrains.buildServer.agent.artifacts.ArtifactsWatcher;\r
-import jetbrains.buildServer.nuget.common.PackageInfo;\r
+import jetbrains.buildServer.nuget.common.PackageDependencies;\r
+import jetbrains.buildServer.nuget.common.PackageDependenciesStore;\r
 import jetbrains.buildServer.util.FileUtil;\r
-import jetbrains.buildServer.util.XmlUtil;\r
-import org.jdom.Content;\r
-import org.jdom.Document;\r
-import org.jdom.Element;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
-import java.io.*;\r
-import java.util.Collection;\r
+import java.io.File;\r
+import java.io.IOException;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
@@ -35,32 +32,19 @@ import java.util.Collection;
  */\r
 public class PackagesInfoUploader {\r
   private final ArtifactsWatcher myPublisher;\r
+  private final PackageDependenciesStore myStore;\r
 \r
-  public PackagesInfoUploader(@NotNull final ArtifactsWatcher publisher) {\r
+  public PackagesInfoUploader(@NotNull final ArtifactsWatcher publisher,\r
+                              @NotNull final PackageDependenciesStore store) {\r
     myPublisher = publisher;\r
+    myStore = store;\r
   }\r
 \r
   public void uploadDepectedPackages(@NotNull final AgentRunningBuild build,\r
-                                     @NotNull final Collection<PackageInfo> infos) throws IOException {\r
+                                     @NotNull final PackageDependencies infos) throws IOException {\r
     File tmp = FileUtil.createTempDirectory("nuget", "packages", build.getBuildTempDirectory());\r
-\r
-    Element root = new Element("packages");\r
-    for (PackageInfo info : infos) {\r
-      Element pkg = new Element("package");\r
-      pkg.setAttribute("id", info.getId());\r
-      pkg.setAttribute("version", info.getVersion());\r
-      root.addContent((Content)pkg);\r
-    }\r
-    Document doc = new Document(root);\r
-\r
-    final File content = new File(tmp, "nuget-packages.xml");\r
-    OutputStream os = new BufferedOutputStream(new FileOutputStream(content));\r
-    try {\r
-      XmlUtil.saveDocument(doc, os);\r
-    } finally {\r
-      FileUtil.close(os);\r
-    }\r
-\r
+    File content = new File(tmp, "nuget.xml");\r
+    myStore.save(infos, content);\r
     myPublisher.addNewArtifactsPath(content.getPath() + " => .teamcity/nuget");\r
   }\r
 }\r
index 01e7d789f5c671a12e84ce5a86deff0d223955df..69e16cc84663fc4b8447dd549a9e715ccbfc089b 100644 (file)
@@ -21,6 +21,7 @@ import jetbrains.buildServer.agent.AgentLifeCycleAdapter;
 import jetbrains.buildServer.agent.AgentLifeCycleListener;\r
 import jetbrains.buildServer.agent.AgentRunningBuild;\r
 import jetbrains.buildServer.agent.BuildFinishedStatus;\r
+import jetbrains.buildServer.nuget.common.PackageDependencies;\r
 import jetbrains.buildServer.nuget.common.PackageInfo;\r
 import jetbrains.buildServer.util.EventDispatcher;\r
 import org.jetbrains.annotations.NotNull;\r
@@ -47,7 +48,7 @@ public class PackagesWatcher {
 \r
       @Override\r
       public void beforeBuildFinish(@NotNull AgentRunningBuild build, @NotNull BuildFinishedStatus buildStatus) {\r
-        Collection<PackageInfo> packages = new ArrayList<PackageInfo>(collector.getPackages());\r
+        PackageDependencies packages = collector.getPackages();\r
         if (packages.isEmpty()) return;\r
 \r
         try {\r
index b2eeaf1bc3750f59dcf7854214ed64a431a826c9..f160c04bffc35a61a08b3683d7a2c97db6692b59 100644 (file)
@@ -8,6 +8,9 @@
     <orderEntry type="inheritedJdk" />\r
     <orderEntry type="sourceFolder" forTests="false" />\r
     <orderEntry type="library" name="idea annotations.jar" level="project" />\r
+    <orderEntry type="library" name="Idea-OpenApi" level="project" />\r
+    <orderEntry type="library" name="Common-Api" level="project" />\r
+    <orderEntry type="library" name="Common-Impl" level="project" />\r
   </component>\r
 </module>\r
 \r
diff --git a/nuget-common/src/META-INF/spring-nuget-common.xml b/nuget-common/src/META-INF/spring-nuget-common.xml
new file mode 100644 (file)
index 0000000..dd90a7e
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<!--suppress AutowiredDependenciesInspection -->\r
+<beans xmlns="http://www.springframework.org/schema/beans"\r
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\r
+       xsi:schemaLocation="http://www.springframework.org/schema/beans\r
+                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"\r
+       default-autowire="constructor">\r
+\r
+  <bean class="jetbrains.buildServer.nuget.common.PackageDependenciesStore"/>\r
+\r
+</beans>\r
diff --git a/nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependencies.java b/nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependencies.java
new file mode 100644 (file)
index 0000000..e8ac099
--- /dev/null
@@ -0,0 +1,60 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.common;\r
+\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.TreeSet;\r
+\r
+/**\r
+ * Represent list of downloaded dependecies of a build\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 19.07.11 11:41\r
+ */\r
+public class PackageDependencies {\r
+  private final Collection<String> mySources;\r
+  private final Collection<PackageInfo> myPackages;\r
+\r
+  public PackageDependencies(@NotNull final Collection<String> sources,\r
+                             @NotNull final Collection<PackageInfo> packages) {\r
+    mySources = Collections.unmodifiableCollection(new TreeSet<String>(sources));\r
+    myPackages = Collections.unmodifiableCollection(new TreeSet<PackageInfo>(packages));\r
+  }\r
+\r
+  /**\r
+   * @return NuGet sources configured from web\r
+   */\r
+  @NotNull\r
+  public Collection<String> getSources() {\r
+    return mySources;\r
+  }\r
+\r
+  /**\r
+   *\r
+   * @return sorted list of packages that were used in project\r
+   */\r
+  @NotNull\r
+  public Collection<PackageInfo> getPackages() {\r
+    return myPackages;\r
+  }\r
+\r
+  public boolean isEmpty() {\r
+    return getPackages().isEmpty();\r
+  }\r
+}\r
diff --git a/nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependenciesStore.java b/nuget-common/src/jetbrains/buildServer/nuget/common/PackageDependenciesStore.java
new file mode 100644 (file)
index 0000000..a05589f
--- /dev/null
@@ -0,0 +1,109 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.common;\r
+\r
+import jetbrains.buildServer.util.FileUtil;\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.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
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 19.07.11 11:39\r
+ */\r
+public class PackageDependenciesStore {\r
+  public PackageDependencies load(@NotNull final File file) throws IOException {\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
+      }\r
+    }.parse(file);\r
+\r
+    return new PackageDependencies(sources, infos);\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
+\r
+    Element pkgs = new Element("packages");\r
+    for (PackageInfo info : deps.getPackages()) {\r
+      Element pkg = new Element("package");\r
+      pkg.setAttribute("id", info.getId());\r
+      pkg.setAttribute("version", info.getVersion());\r
+      pkgs.addContent((Content) pkg);\r
+    }\r
+\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
+    }\r
+\r
+    root.addContent((Content)sources);\r
+\r
+    Document doc = new Document(pkgs);\r
+\r
+    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));\r
+    try {\r
+      XmlUtil.saveDocument(doc, os);\r
+    } finally {\r
+      FileUtil.close(os);\r
+    }\r
+  }\r
+}\r
index 20ad7cd875ac1f3586210f7d84cc0d41777ce841..036d789d623392cc77e3015e39cb2a9d4afcb2ed 100644 (file)
@@ -67,10 +67,12 @@ public class NuGetPackagesConfigParserTest extends BaseTestCase {
     NuGetPackagesCollectorImpl i = new NuGetPackagesCollectorImpl();\r
     p.parseNuGetPackages(Paths.getTestDataPath("config/" + testData), i);\r
 \r
-    if (packages.length != i.getPackages().size()) {\r
+    if (packages.length != i.getPackages().getPackages().size()) {\r
       System.out.println(i.getPackages());\r
     }\r
 \r
-    Assert.assertEquals(i.getPackages(), new TreeSet<PackageInfo>(Arrays.asList(packages)));\r
+    Assert.assertEquals(\r
+            new TreeSet<PackageInfo>(i.getPackages().getPackages()),\r
+            new TreeSet<PackageInfo>(Arrays.asList(packages)));\r
   }\r
 }\r