add NuGet TeamCity.List command in java
authorEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 14 Jul 2011 09:26:15 +0000 (13:26 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Thu, 14 Jul 2011 09:26:15 +0000 (13:26 +0400)
README
nuget-server/fake-teamcity-server-plugin-context.xml
nuget-server/nuget-server.iml
nuget-server/src/META-INF/build-server-plugin-nuget.xml
nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommand.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandProcessor.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutor.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutorImpl.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetOutputProcessor.java [new file with mode: 0644]
nuget-server/src/jetbrains/buildServer/nuget/server/exec/PackageInfo.java [new file with mode: 0644]

diff --git a/README b/README
index 375eb32e113e83765fa205c0f26d7db5659a9ae0..4d5e47823187fd740bf320501de2185dead6f83a 100644 (file)
--- a/README
+++ b/README
@@ -11,4 +11,5 @@ This plugin will be compatible with at least TeamCity 6.5
 API gaps:\r
  - Reuse ComposideBuildProcess and DelegatingBuildProcess\r
  - Use XmlXppAbstractParser#parse(InputSream)\r
- - Consider reuseing of Maven build trigger/project models
\ No newline at end of file
+ - Consider reuseing of Maven build trigger/project models\r
+ - Make server-side PluginDescription contain getPluginRoot() to avoid cast to PluginInfo
\ No newline at end of file
index 9623ea367fe90a73c9e383b89329f7735ff10254..888f5d40b46a4c0960608e9bd066928d1ccb8e3b 100644 (file)
@@ -6,4 +6,5 @@
        default-autowire="constructor">\r
   <!-- this is a fake spring context xml to make IDEA know all implicit beans that are available for plugin -->\r
   <bean class="jetbrains.buildServer.web.openapi.PluginDescriptor"/>\r
+  <bean class="jetbrains.buildServer.plugins.bean.PluginInfo"/>\r
 </beans>
\ No newline at end of file
index a6d6bdd2f6bba80a89a5ed3f2c341106fb3a1f67..b4b1c6417b7dd739b2dfe151616a1d7bbfa1f682 100644 (file)
@@ -34,6 +34,7 @@
     <orderEntry type="library" name="Idea-OpenApi" level="project" />\r
     <orderEntry type="library" name="Servlet Api" level="project" />\r
     <orderEntry type="module" module-name="nuget-common" />\r
+    <orderEntry type="library" name="Common-Impl" level="project" />\r
   </component>\r
 </module>\r
 \r
index 8bcb497381c7152557fae9622696aaadcfc20719..f39587144238152d40c0d867d4cd65b93922bff8 100644 (file)
@@ -15,5 +15,7 @@
   <bean class="jetbrains.buildServer.nuget.server.install.PackagesInstallerRunType"/>\r
 \r
 \r
+  <bean class="jetbrains.buildServer.nuget.server.exec.NuGetExecutorImpl"/>\r
+\r
   <bean class="jetbrains.buildServer.nuget.server.trigger.NuGetSimpleTrigger"/>\r
 </beans>
\ No newline at end of file
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommand.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommand.java
new file mode 100644 (file)
index 0000000..281eec3
--- /dev/null
@@ -0,0 +1,61 @@
+/*\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.server.exec;\r
+\r
+import com.intellij.openapi.diagnostic.Logger;\r
+import jetbrains.buildServer.util.StringUtil;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.jetbrains.annotations.Nullable;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 13:10\r
+ */\r
+public class ListPackagesCommand {\r
+  private static final Logger LOG = Logger.getInstance(ListPackagesCommand.class.getName());\r
+\r
+  private NuGetExecutor myExec;\r
+\r
+  public ListPackagesCommand(NuGetExecutor exec) {\r
+    myExec = exec;\r
+  }\r
+\r
+  public Collection<PackageInfo> checkForChanges(\r
+          @NotNull final String source,\r
+          @NotNull final String packageId,\r
+          @Nullable final String versionSpec) {\r
+    List<String> cmd = new ArrayList<String>();\r
+\r
+    cmd.add("TeamCity.List");\r
+    cmd.add("-Source");\r
+    cmd.add(source);\r
+    cmd.add("-Id");\r
+    cmd.add(packageId);\r
+\r
+    if (!StringUtil.isEmptyOrSpaces(versionSpec)) {\r
+      cmd.add("-Version");\r
+      cmd.add(versionSpec);\r
+    }\r
+\r
+    return myExec.executeNuGet(cmd, new ListPackagesCommandProcessor(source));\r
+  }\r
+\r
+}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandProcessor.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/ListPackagesCommandProcessor.java
new file mode 100644 (file)
index 0000000..2a853f4
--- /dev/null
@@ -0,0 +1,84 @@
+/*\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.server.exec;\r
+\r
+import com.intellij.openapi.diagnostic.Logger;\r
+import jetbrains.buildServer.messages.serviceMessages.ServiceMessage;\r
+import jetbrains.buildServer.messages.serviceMessages.ServiceMessageParserCallback;\r
+import jetbrains.buildServer.util.StringUtil;\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+import java.text.ParseException;\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+/**\r
+* Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+* Date: 14.07.11 13:23\r
+*/\r
+public class ListPackagesCommandProcessor implements NuGetOutputProcessor<Collection<PackageInfo>> {\r
+  private static final Logger LOG = Logger.getInstance(ListPackagesCommandProcessor.class.getName());\r
+  private final String mySource;\r
+  private final List<PackageInfo> myPackages = new ArrayList<PackageInfo>();\r
+\r
+  ListPackagesCommandProcessor(@NotNull final String source) {\r
+    mySource = source;\r
+  }\r
+\r
+  public void onStdOutput(String text) {\r
+    if (LOG.isDebugEnabled()) {\r
+      LOG.debug(text);\r
+    }\r
+\r
+    ServiceMessage.parse(text, new ServiceMessageParserCallback() {\r
+      public void regularText(@NotNull String s) {\r
+      }\r
+\r
+      public void serviceMessage(@NotNull ServiceMessage serviceMessage) {\r
+        if (!"nuget-package".equals(serviceMessage.getMessageName())) return;\r
+        final String id = serviceMessage.getAttributes().get("Id");\r
+        final String version = serviceMessage.getAttributes().get("Version");\r
+\r
+        if (StringUtil.isEmptyOrSpaces(id)) return;\r
+        if (StringUtil.isEmptyOrSpaces(version)) return;\r
+\r
+        myPackages.add(new PackageInfo(mySource, id, version));\r
+\r
+      }\r
+\r
+      public void parseException(@NotNull ParseException e, @NotNull String s) {\r
+      }\r
+    });\r
+  }\r
+\r
+  public void onStdError(String text) {\r
+    LOG.warn(text);\r
+  }\r
+\r
+  public void onFinished(int exitCode) {\r
+    if (LOG.isDebugEnabled()) {\r
+      LOG.debug("NuGet TeamCity.List command exited with " + exitCode);\r
+    }\r
+  }\r
+\r
+  @NotNull\r
+  public Collection<PackageInfo> getResult() {\r
+    return Collections.unmodifiableList(myPackages);\r
+  }\r
+}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutor.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutor.java
new file mode 100644 (file)
index 0000000..964fb40
--- /dev/null
@@ -0,0 +1,14 @@
+package jetbrains.buildServer.nuget.server.exec;\r
+\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 13:23\r
+ */\r
+public interface NuGetExecutor {\r
+  <T> T executeNuGet(@NotNull List<String> arguments,\r
+                     @NotNull NuGetOutputProcessor<T> listener);\r
+}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutorImpl.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetExecutorImpl.java
new file mode 100644 (file)
index 0000000..4f1b694
--- /dev/null
@@ -0,0 +1,72 @@
+/*\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.server.exec;\r
+\r
+import com.intellij.execution.configurations.GeneralCommandLine;\r
+import com.intellij.openapi.diagnostic.Logger;\r
+import jetbrains.buildServer.ExecResult;\r
+import jetbrains.buildServer.SimpleCommandLineProcessRunner;\r
+import jetbrains.buildServer.plugins.bean.PluginInfo;\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+import java.io.File;\r
+import java.util.List;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 12:48\r
+ */\r
+public class NuGetExecutorImpl implements NuGetExecutor {\r
+  private static final Logger LOG = Logger.getInstance(NuGetExecutorImpl.class.getName());\r
+\r
+  private final PluginInfo myPluginInfo;\r
+\r
+  public NuGetExecutorImpl(PluginInfo pluginInfo) {\r
+    myPluginInfo = pluginInfo;\r
+  }\r
+\r
+  @NotNull\r
+  private File getNuGetRunnerPath() {\r
+    return new File(myPluginInfo.getPluginRoot(), "bin/JetBrains.TeamCity.NuGetRunner.exe");\r
+  }\r
+\r
+  public <T> T executeNuGet(@NotNull final List<String> arguments,\r
+                            @NotNull final NuGetOutputProcessor<T> listener) {\r
+\r
+    GeneralCommandLine cmd = new GeneralCommandLine();\r
+    cmd.setExePath(getNuGetRunnerPath().getPath());\r
+    cmd.addParameters(arguments);\r
+\r
+    if (LOG.isDebugEnabled()) {\r
+      LOG.debug("Starting: " + cmd.getCommandLineString());\r
+    }\r
+\r
+    final ExecResult result = SimpleCommandLineProcessRunner.runCommand(cmd, new byte[0]);\r
+\r
+    if (LOG.isDebugEnabled()) {\r
+      LOG.debug("Exited with code: " + result.getExitCode());\r
+      LOG.debug("Output: " + result.getStdout());\r
+      LOG.error("Error: " + result.getStderr());\r
+    }\r
+\r
+    listener.onStdOutput(result.getStdout());\r
+    listener.onStdError(result.getStderr());\r
+    listener.onFinished(result.getExitCode());\r
+\r
+    return listener.getResult();\r
+  }\r
+}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetOutputProcessor.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/NuGetOutputProcessor.java
new file mode 100644 (file)
index 0000000..1e0bb61
--- /dev/null
@@ -0,0 +1,32 @@
+/*\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.server.exec;\r
+\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+/**\r
+* Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+* Date: 14.07.11 13:23\r
+*/\r
+public interface NuGetOutputProcessor<T> {\r
+  void onStdOutput(String text);\r
+  void onStdError(String text);\r
+  void onFinished(int exitCode);\r
+\r
+  @NotNull\r
+  T getResult();\r
+}\r
diff --git a/nuget-server/src/jetbrains/buildServer/nuget/server/exec/PackageInfo.java b/nuget-server/src/jetbrains/buildServer/nuget/server/exec/PackageInfo.java
new file mode 100644 (file)
index 0000000..7e51996
--- /dev/null
@@ -0,0 +1,61 @@
+/*\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.server.exec;\r
+\r
+import org.jetbrains.annotations.NotNull;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 14.07.11 13:17\r
+ */\r
+public class PackageInfo {\r
+  private final String mySource;\r
+  private final String myPackageId;\r
+  private final String myVersion;\r
+\r
+  public PackageInfo(@NotNull final String source,\r
+                     @NotNull final String packageId,\r
+                     @NotNull final String version) {\r
+    mySource = source;\r
+    myPackageId = packageId;\r
+    myVersion = version;\r
+  }\r
+\r
+  @NotNull\r
+  public String getSource() {\r
+    return mySource;\r
+  }\r
+\r
+  @NotNull\r
+  public String getPackageId() {\r
+    return myPackageId;\r
+  }\r
+\r
+  @NotNull\r
+  public String getVersion() {\r
+    return myVersion;\r
+  }\r
+\r
+  @Override\r
+  public String toString() {\r
+    return "PackageInfo{" +\r
+            "mySource='" + mySource + '\'' +\r
+            ", myPackageId='" + myPackageId + '\'' +\r
+            ", myVersion='" + myVersion + '\'' +\r
+            '}';\r
+  }\r
+}\r