| CODENOTIFIER | HelpYou are not signed inSign in |
Project: RadRails1.0
Revision: 14209
Author: cwilliams
Date: 23 Jun 2008 09:24:51
Changes:apply Geoffrey Longman's patch
Files:| ... | ...@@ -1,979 +1,979 @@ | |
| 1 | package com.aptana.rdt.internal.core.gems; | |
| 2 | ||
| 3 | import java.io.File; | |
| 4 | import java.io.FileNotFoundException; | |
| 5 | import java.io.FileOutputStream; | |
| 6 | import java.io.FileReader; | |
| 7 | import java.io.IOException; | |
| 8 | import java.util.ArrayList; | |
| 9 | import java.util.Collection; | |
| 10 | import java.util.Collections; | |
| 11 | import java.util.HashMap; | |
| 12 | import java.util.HashSet; | |
| 13 | import java.util.List; | |
| 14 | import java.util.Map; | |
| 15 | import java.util.Set; | |
| 16 | import java.util.SortedSet; | |
| 17 | import java.util.TreeSet; | |
| 18 | import java.util.regex.Matcher; | |
| 19 | import java.util.regex.Pattern; | |
| 20 | ||
| 21 | import javax.xml.parsers.FactoryConfigurationError; | |
| 22 | import javax.xml.parsers.ParserConfigurationException; | |
| 23 | import javax.xml.parsers.SAXParserFactory; | |
| 24 | ||
| 25 | import org.eclipse.core.runtime.CoreException; | |
| 26 | import org.eclipse.core.runtime.IPath; | |
| 27 | import org.eclipse.core.runtime.IProgressMonitor; | |
| 28 | import org.eclipse.core.runtime.IStatus; | |
| 29 | import org.eclipse.core.runtime.NullProgressMonitor; | |
| 30 | import org.eclipse.core.runtime.Path; | |
| 31 | import org.eclipse.core.runtime.Status; | |
| 32 | import org.eclipse.core.runtime.jobs.Job; | |
| 33 | import org.eclipse.debug.core.DebugPlugin; | |
| 34 | import org.eclipse.debug.core.ILaunch; | |
| 35 | import org.eclipse.debug.core.ILaunchConfiguration; | |
| 36 | import org.eclipse.debug.core.ILaunchConfigurationType; | |
| 37 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; | |
| 38 | import org.eclipse.debug.core.ILaunchManager; | |
| 39 | import org.eclipse.debug.ui.IDebugUIConstants; | |
| 40 | import org.rubypeople.rdt.launching.IRubyLaunchConfigurationConstants; | |
| 41 | import org.rubypeople.rdt.launching.IVMInstall; | |
| 42 | import org.rubypeople.rdt.launching.IVMInstallChangedListener; | |
| 43 | import org.rubypeople.rdt.launching.PropertyChangeEvent; | |
| 44 | import org.rubypeople.rdt.launching.RubyRuntime; | |
| 45 | import org.xml.sax.InputSource; | |
| 46 | import org.xml.sax.SAXException; | |
| 47 | import org.xml.sax.XMLReader; | |
| 48 | ||
| 49 | import com.aptana.rdt.AptanaRDTPlugin; | |
| 50 | import com.aptana.rdt.IAptanaProxyService; | |
| 51 | import com.aptana.rdt.core.gems.Gem; | |
| 52 | import com.aptana.rdt.core.gems.GemListener; | |
| 53 | import com.aptana.rdt.core.gems.GemRequirement; | |
| 54 | import com.aptana.rdt.core.gems.IGemManager; | |
| 55 | import com.aptana.rdt.core.gems.LogicalGem; | |
| 56 | import com.aptana.rdt.core.gems.Version; | |
| 57 | import com.aptana.rdt.internal.core.SystemPropertyProxyService; | |
| 58 | // XXX If user tries to install a gem that someone has contributed a local copy of, try using the local copy! (Need to worry about dependencies then!) | |
| 59 | public class GemManager implements IGemManager, IVMInstallChangedListener { | |
| 60 | ||
| 61 | private static final String DETAIL_SWITCH = "-d"; | |
| 62 | private static final String SOURCE_SWITCH = "--source"; | |
| 63 | private static final String INCLUDE_DEPENDENCIES_SWITCH = "-y"; | |
| 64 | private static final String LOCAL_SWITCH = "-l"; | |
| 65 | private static final String VERSION_SWITCH = "-v"; | |
| 66 | private static final String REMOTE_SWITCH = "-r"; | |
| 67 | ||
| 68 | private static final String LIST_COMMAND = "list"; | |
| 69 | private static final String INSTALL_COMMAND = "install"; | |
| 70 | private static final String UNINSTALL_COMMAND = "uninstall"; | |
| 71 | private static final String UPDATE_COMMAND = "update"; | |
| 72 | private static final String CLEANUP_COMMAND = "cleanup"; | |
| 73 | private static final String EXECUTABLE = "ruby"; | |
| 74 | ||
| 75 | private static final String LOCAL_GEMS_CACHE_FILE = "local_gems.xml"; | |
| 76 | // private static final String RAILS_GEM_HOST = "http://gems.rubyonrails.org"; | |
| 77 | ||
| 78 | private static GemManager fgInstance; | |
| 79 | ||
| 80 | private Set<Gem> gems; | |
| 81 | private Set<GemListener> listeners; | |
| 82 | private Set<String> urls; | |
| 83 | private List<IPath> fGemInstallPaths; | |
| 84 | private Map<String, Set<Gem>> fRemoteGems = new HashMap<String, Set<Gem>>(); | |
| 85 | ||
| 86 | protected boolean isInitialized; | |
| 87 | private Version fVersion; | |
| 88 | ||
| 89 | private static int seed = 0; // A number we append to the launch configs' name to ensure uniqueness (because the method which is supposed to generate unique names in the LaunchManager doesn't actually do it). | |
| 90 | ||
| 91 | GemManager() { | |
| 92 | urls = new HashSet<String>(); | |
| 93 | gems = new HashSet<Gem>(); | |
| 94 | listeners = new HashSet<GemListener>(); | |
| 95 | } | |
| 96 | ||
| 97 | public boolean isInitialized() { | |
| 98 | return isInitialized; | |
| 99 | } | |
| 100 | ||
| 101 | protected Set<Gem> loadLocalCache(File file) { | |
| 102 | FileReader fileReader = null; | |
| 103 | try { | |
| 104 | fileReader = new FileReader(file); | |
| 105 | XMLReader reader = SAXParserFactory.newInstance().newSAXParser() | |
| 106 | .getXMLReader(); | |
| 107 | GemManagerContentHandler handler = new GemManagerContentHandler(); | |
| 108 | reader.setContentHandler(handler); | |
| 109 | reader.parse(new InputSource(fileReader)); | |
| 110 | ||
| 111 | return handler.getGems(); | |
| 112 | } catch (FileNotFoundException e) { | |
| 113 | // This is okay, will get thrown if no config exists yet | |
| 114 | } catch (SAXException e) { | |
| 115 | AptanaRDTPlugin.log(e); | |
| 116 | } catch (ParserConfigurationException e) { | |
| 117 | AptanaRDTPlugin.log(e); | |
| 118 | } catch (FactoryConfigurationError e) { | |
| 119 | AptanaRDTPlugin.log(e); | |
| 120 | } catch (IOException e) { | |
| 121 | AptanaRDTPlugin.log(e); | |
| 122 | } finally { | |
| 123 | try { | |
| 124 | if (fileReader != null) | |
| 125 | fileReader.close(); | |
| 126 | } catch (IOException e) { | |
| 127 | // ignore | |
| 128 | } | |
| 129 | } | |
| 130 | return new HashSet<Gem>(); | |
| 131 | } | |
| 132 | ||
| 133 | protected void storeGemCache(Set<Gem> gems, File file) { | |
| 134 | XMLWriter out = null; | |
| 135 | try { | |
| 136 | out = new XMLWriter(new FileOutputStream(file)); | |
| 137 | writeXML(gems, out); | |
| 138 | } catch (FileNotFoundException e) { | |
| 139 | AptanaRDTPlugin.log(e); | |
| 140 | } catch (IOException e) { | |
| 141 | AptanaRDTPlugin.log(e); | |
| 142 | } finally { | |
| 143 | if (out != null) | |
| 144 | out.close(); | |
| 145 | } | |
| 146 | } | |
| 147 | ||
| 148 | protected File getConfigFile(String fileName) { | |
| 149 | return AptanaRDTPlugin.getDefault().getStateLocation().append(fileName) | |
| 150 | .toFile(); | |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Writes each server configuration to file in XML format. | |
| 155 | * | |
| 156 | * @param gems | |
| 157 | * | |
| 158 | * @param out | |
| 159 | * the writer to use | |
| 160 | */ | |
| 161 | private void writeXML(Set<Gem> gems, XMLWriter out) { | |
| 162 | out.startTag("gems", null); | |
| 163 | for (Gem gem : gems) { | |
| 164 | out.startTag("gem", null); | |
| 165 | out.printSimpleTag("name", gem.getName()); | |
| 166 | out.printSimpleTag("version", gem.getVersion()); | |
| 167 | out.printSimpleTag("description", gem.getDescription()); | |
| 168 | out.printSimpleTag("platform", gem.getPlatform()); | |
| 169 | out.endTag("gem"); | |
| 170 | } | |
| 171 | out.endTag("gems"); | |
| 172 | out.flush(); | |
| 173 | } | |
| 174 | ||
| 175 | private Set<Gem> loadRemoteGems(String gemIndexUrl, IProgressMonitor monitor) { | |
| 176 | if (!isRubyGemsInstalled()) return new HashSet<Gem>(); | |
| 177 | ||
| 178 | GemParser parser = new GemParser(); | |
| 179 | String output = getRemoteGemsListing(gemIndexUrl); | |
| 180 | return parser.parse(output); | |
| 181 | } | |
| 182 | ||
| 183 | private Set<Gem> loadLocalGems() { | |
| 184 | if (!isRubyGemsInstalled()) return new HashSet<Gem>(); | |
| 185 | ||
| 186 | GemParser parser = new GemParser(); | |
| 187 | String output = getLocalGemsListing(); | |
| 188 | return parser.parse(output); | |
| 189 | } | |
| 190 | ||
| 191 | private String launchInBackgroundAndRead(final ILaunchConfiguration config, final File file) { | |
| 192 | return RubyRuntime.launchInBackgroundAndRead(config, file); | |
| 193 | } | |
| 194 | ||
| 195 | private String launchInBackgroundAndRead(String command, File file) { | |
| 196 | return launchInBackgroundAndRead(createGemLaunchConfiguration(command, false), file); | |
| 197 | } | |
| 198 | ||
| 199 | private Version getVersion() { | |
| 200 | if (fVersion != null) return fVersion; | |
| 201 | int tries = 0; | |
| 202 | while (fVersion == null && tries < 3) { | |
| 203 | String version = launchInBackgroundAndRead("-v", getStateFile("version.txt")); | |
| 204 | try { | |
| 205 | if (version != null && version.trim().length() > 0) | |
| 206 | fVersion = new Version(version.trim()); | |
| 207 | } catch (RuntimeException e) { | |
| 208 | AptanaRDTPlugin.log(e); | |
| 209 | fVersion = null; | |
| 210 | } | |
| 211 | tries++; | |
| 212 | } | |
| 213 | return fVersion; | |
| 214 | } | |
| 215 | ||
| 216 | private String getLocalGemsListing() { | |
| 217 | String command = "query -d"; | |
| 218 | // If we're using RubyGems older than 0.9.3, we need to do a "gem list -l" to get the equivalent of query -d | |
| 219 | if (getVersion() != null && getVersion().isLessThanOrEqualTo("0.9.3")) { | |
| 220 | command = LIST_COMMAND + " " + LOCAL_SWITCH; | |
| 221 | } | |
| 222 | return launchInBackgroundAndRead(command, getGemListingFile()); | |
| 223 | } | |
| 224 | ||
| 225 | private String getRemoteGemsListing(String sourceURL) { | |
| 226 | String command = LIST_COMMAND + " " + DETAIL_SWITCH + " " + REMOTE_SWITCH + " " + SOURCE_SWITCH + " " + sourceURL; | |
| 227 | return launchInBackgroundAndRead(command, getStateFile("remote_listing.txt")); | |
| 228 | } | |
| 229 | ||
| 230 | private File getGemListingFile() { | |
| 231 | return getStateFile("local_listing.txt"); | |
| 232 | } | |
| 233 | ||
| 234 | /* | |
| 235 | * (non-Javadoc) | |
| 236 | * | |
| 237 | * @see com.aptana.rdt.internal.gems.IGemManager#update(com.aptana.rdt.internal.gems.Gem) | |
| 238 | */ | |
| 239 | public boolean update(final Gem gem) { | |
| 240 | if (!isRubyGemsInstalled()) return false; | |
| 241 | try { | |
| 242 | String command = UPDATE_COMMAND + " " + gem.getName(); | |
| 243 | command = addProxy(command); | |
| 244 | ILaunchConfiguration config = createGemLaunchConfiguration(command, true); | |
| 245 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 246 | Job job = new Job("Updating gem " + gem.getName()) { | |
| 247 | ||
| 248 | @Override | |
| 249 | protected IStatus run(IProgressMonitor monitor) { | |
| 250 | while (!launch.isTerminated()) { | |
| 251 | Thread.yield(); | |
| 252 | } | |
| 253 | refresh(); | |
| 254 | return Status.OK_STATUS; | |
| 255 | } | |
| 256 | ||
| 257 | }; | |
| 258 | job.schedule(); | |
| 259 | } catch (CoreException e) { | |
| 260 | AptanaRDTPlugin.log(e); | |
| 261 | return false; | |
| 262 | } | |
| 263 | return true; | |
| 264 | } | |
| 265 | ||
| 266 | private ILaunchConfigurationType getRubyApplicationConfigType() { | |
| 267 | return getLaunchManager().getLaunchConfigurationType( | |
| 268 | IRubyLaunchConfigurationConstants.ID_RUBY_APPLICATION); | |
| 269 | } | |
| 270 | ||
| 271 | private ILaunchManager getLaunchManager() { | |
| 272 | return DebugPlugin.getDefault().getLaunchManager(); | |
| 273 | } | |
| 274 | ||
| 275 | private ILaunchConfiguration createGemLaunchConfiguration(String arguments, boolean isSudo) { | |
| 276 | String gemPath = getGemScriptPath(); | |
| 277 | ILaunchConfiguration config = null; | |
| 278 | try { | |
| 279 | ILaunchConfigurationType configType = getRubyApplicationConfigType(); | |
| 280 | ILaunchConfigurationWorkingCopy wc = configType | |
| 281 | .newInstance(null, getUniqueName(gemPath)); | |
| 282 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_FILE_NAME, | |
| 283 | gemPath); | |
| 284 | wc.setAttribute( | |
| 285 | IRubyLaunchConfigurationConstants.ATTR_VM_INSTALL_NAME, | |
| 286 | RubyRuntime.getDefaultVMInstall().getName()); | |
| 287 | wc.setAttribute( | |
| 288 | IRubyLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE, | |
| 289 | RubyRuntime.getDefaultVMInstall().getVMInstallType() | |
| 290 | .getId()); | |
| 291 | // FIXME Use IProxyService in 3.3 and go through proxy (by passing the necessary values as args) if we need to! | |
| 292 | wc.setAttribute( | |
| 293 | IRubyLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, | |
| 294 | arguments); | |
| 295 | wc.setAttribute( | |
| 296 | IRubyLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, | |
| 297 | ""); | |
| 298 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_IS_SUDO, isSudo); | |
| 299 | if (isSudo) { | |
| 300 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_TERMINAL_COMMAND, "gem " + arguments); | |
| 301 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_USE_TERMINAL, "org.radrails.rails.shell"); // use rails shell if it's available | |
| 302 | } | |
| 303 | Map<String, String> map = new HashMap<String, String>(); | |
| 304 | map.put(IRubyLaunchConfigurationConstants.ATTR_RUBY_COMMAND, | |
| 305 | EXECUTABLE); | |
| 306 | wc | |
| 307 | .setAttribute( | |
| 308 | IRubyLaunchConfigurationConstants.ATTR_VM_INSTALL_TYPE_SPECIFIC_ATTRS_MAP, | |
| 309 | map); | |
| 310 | wc.setAttribute(IDebugUIConstants.ATTR_PRIVATE, true); | |
| 311 | wc.setAttribute(IDebugUIConstants.ATTR_LAUNCH_IN_BACKGROUND, false); | |
| 312 | config = wc.doSave(); | |
| 313 | } catch (CoreException ce) { | |
| 314 | // ignore for now | |
| 315 | } | |
| 316 | return config; | |
| 317 | } | |
| 318 | ||
| 319 | private synchronized String getUniqueName(String name) { | |
| 320 | String unique = getLaunchManager().generateUniqueLaunchConfigurationNameFrom(name) + seed++; | |
| 321 | return unique; | |
| 322 | } | |
| 323 | ||
| 324 | public ILaunchConfiguration run(String args) throws CoreException { | |
| 325 | boolean useSudo = false; | |
| 326 | if (args.contains("install ") || args.contains("update") || args.contains("uninstall ") | |
| 327 | || args.contains("cleanup")) { | |
| 328 | useSudo = true; | |
| 329 | } | |
| 330 | return createGemLaunchConfiguration(args, useSudo); | |
| 331 | } | |
| 332 | ||
| 333 | private static String getGemScriptPath() { | |
| 334 | IVMInstall vm = RubyRuntime.getDefaultVMInstall(); | |
| 335 | if (vm == null) return null; | |
| 336 | File installLocation = vm.getInstallLocation(); | |
| 337 | String path = installLocation.getAbsolutePath(); | |
| 338 | return path + File.separator + "bin" + File.separator + "gem"; | |
| 339 | } | |
| 340 | ||
| 341 | public boolean isRubyGemsInstalled() { | |
| 342 | String path = getGemScriptPath(); | |
| 343 | if (path == null) return false; | |
| 344 | File file = new File(path); | |
| 345 | return file.exists(); | |
| 346 | } | |
| 347 | ||
| 348 | /* | |
| 349 | * (non-Javadoc) | |
| 350 | * @see com.aptana.rdt.core.gems.IGemManager#installGem(com.aptana.rdt.core.gems.Gem) | |
| 351 | */ | |
| 352 | public boolean installGem(final Gem gem) { | |
| 353 | return installGem(gem, true); | |
| 354 | } | |
| 355 | ||
| 356 | /* | |
| 357 | * (non-Javadoc) | |
| 358 | * @see com.aptana.rdt.core.gems.IGemManager#installGem(com.aptana.rdt.core.gems.Gem, boolean) | |
| 359 | */ | |
| 360 | public boolean installGem(final Gem gem, boolean includeDependencies) { | |
| 361 | if (gem.isLocal()) { | |
| 362 | return doLocalInstallGem(gem); | |
| 363 | } | |
| 364 | return installGem(gem, DEFAULT_GEM_HOST, includeDependencies); | |
| 365 | } | |
| 366 | ||
| 367 | /* | |
| 368 | * (non-Javadoc) | |
| 369 | * | |
| 370 | * @see com.aptana.rdt.internal.gems.IGemManager#removeGem(com.aptana.rdt.internal.gems.Gem) | |
| 371 | */ | |
| 372 | public boolean removeGem(final Gem gem) { | |
| 373 | if (!isRubyGemsInstalled()) return false; | |
| 374 | try { | |
| 375 | String command = UNINSTALL_COMMAND + " " + gem.getName(); | |
| 376 | if (gem.getVersion() != null | |
| 377 | && gem.getVersion().trim().length() > 0) { | |
| 378 | command += " " + VERSION_SWITCH + " " + gem.getVersion(); | |
| 379 | } | |
| 380 | ILaunchConfiguration config = createGemLaunchConfiguration(command, true); | |
| 381 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 382 | Job job = new Job("Notifying gem listeners of uninstalled gem") { | |
| 383 | ||
| 384 | @Override | |
| 385 | protected IStatus run(IProgressMonitor monitor) { | |
| 386 | while (!launch.isTerminated()) { | |
| 387 | Thread.yield(); | |
| 388 | } | |
| 389 | refresh(); | |
| 390 | // Need to wait until uninstall is finished | |
| 391 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 392 | listener.gemRemoved(gem); | |
| 393 | } | |
| 394 | return Status.OK_STATUS; | |
| 395 | } | |
| 396 | ||
| 397 | }; | |
| 398 | job.setSystem(true); | |
| 399 | job.schedule(); | |
| 400 | } catch (CoreException e) { | |
| 401 | AptanaRDTPlugin.log(e); | |
| 402 | return false; | |
| 403 | } | |
| 404 | ||
| 405 | return true; | |
| 406 | } | |
| 407 | ||
| 408 | /* | |
| 409 | * (non-Javadoc) | |
| 410 | * | |
| 411 | * @see com.aptana.rdt.internal.gems.IGemManager#getGems() | |
| 412 | */ | |
| 413 | public Set<Gem> getGems() { | |
| 414 | return Collections.unmodifiableSortedSet(new TreeSet<Gem>(gems)); | |
| 415 | } | |
| 416 | ||
| 417 | public static GemManager getInstance() { | |
| 418 | if (fgInstance == null) | |
| 419 | fgInstance = new GemManager(); | |
| 420 | return fgInstance; | |
| 421 | } | |
| 422 | ||
| 423 | /* | |
| 424 | * (non-Javadoc) | |
| 425 | * | |
| 426 | * @see com.aptana.rdt.internal.gems.IGemManager#refresh() | |
| 427 | */ | |
| 428 | public boolean refresh() { | |
| 429 | Set<Gem> newGems = loadLocalGems(); | |
| 430 | gems = newGems; | |
| 431 | storeGemCache(gems, getConfigFile(LOCAL_GEMS_CACHE_FILE)); | |
| 432 | Job job = new Job("notifying Gem Listeners of refresh") { | |
| 433 | ||
| 434 | @Override | |
| 435 | protected IStatus run(IProgressMonitor monitor) { | |
| 436 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 437 | listener.gemsRefreshed(); | |
| 438 | } | |
| 439 | return Status.OK_STATUS; | |
| 440 | } | |
| 441 | ||
| 442 | }; | |
| 443 | job.setSystem(true); | |
| 444 | job.schedule(); | |
| 445 | return true; | |
| 446 | } | |
| 447 | ||
| 448 | /* | |
| 449 | * (non-Javadoc) | |
| 450 | * | |
| 451 | * @see com.aptana.rdt.internal.gems.IGemManager#addGemListener(com.aptana.rdt.internal.gems.GemManager.GemListener) | |
| 452 | */ | |
| 453 | public synchronized void addGemListener(GemListener listener) { | |
| 454 | listeners.add(listener); | |
| 455 | } | |
| 456 | ||
| 457 | /* | |
| 458 | * (non-Javadoc) | |
| 459 | * | |
| 460 | * @see com.aptana.rdt.internal.gems.IGemManager#getRemoteGems() | |
| 461 | */ | |
| 462 | public Set<Gem> getRemoteGems() { | |
| 463 | return getRemoteGems(DEFAULT_GEM_HOST, new NullProgressMonitor()); | |
| 464 | } | |
| 465 | ||
| 466 | public Set<Gem> getRemoteGems(String sourceURL, IProgressMonitor monitor) { | |
| 467 | Set<Gem> remoteGems = new HashSet<Gem>(); | |
| 468 | if (fRemoteGems.containsKey(sourceURL)) { | |
| 469 | // FIXME How long should we be caching this? | |
| 470 | remoteGems = fRemoteGems.get(sourceURL); | |
| 471 | } else{ | |
| 472 | remoteGems = makeLogical(loadRemoteGems(sourceURL, monitor)); | |
| 473 | if (!remoteGems.isEmpty()) { | |
| 474 | addSourceURL(sourceURL); | |
| 475 | fRemoteGems.put(sourceURL, remoteGems); | |
| 476 | } | |
| 477 | } | |
| 478 | return Collections.unmodifiableSortedSet(new TreeSet<Gem>(remoteGems)); | |
| 479 | } | |
| 480 | ||
| 481 | protected void addSourceURL(String sourceURL) { | |
| 482 | if (urls.contains(sourceURL)) return; | |
| 483 | launchInBackgroundAndRead("sources -a " + sourceURL, getConfigFile("add_source.txt")); | |
| 484 | urls.add(sourceURL); | |
| 485 | } | |
| 486 | ||
| 487 | public Set<String> getSourceURLs() { | |
| 488 | return Collections.unmodifiableSet(new TreeSet<String>(urls)); | |
| 489 | } | |
| 490 | ||
| 491 | /* | |
| 492 | * (non-Javadoc) | |
| 493 | * | |
| 494 | * @see com.aptana.rdt.internal.gems.IGemManager#gemInstalled(java.lang.String) | |
| 495 | */ | |
| 496 | public boolean gemInstalled(String gemName) { | |
| 497 | Set<Gem> gems = getGems(); | |
| 498 | for (Gem gem : gems) { | |
| 499 | if (gem.getName().equalsIgnoreCase(gemName)) | |
| 500 | return true; | |
| 501 | } | |
| 502 | return false; | |
| 503 | } | |
| 504 | ||
| 505 | /* | |
| 506 | * (non-Javadoc) | |
| 507 | * | |
| 508 | * @see com.aptana.rdt.internal.gems.IGemManager#removeGemListener(com.aptana.rdt.internal.gems.GemManager.GemListener) | |
| 509 | */ | |
| 510 | public synchronized void removeGemListener(GemListener listener) { | |
| 511 | listeners.remove(listener); | |
| 512 | } | |
| 513 | ||
| 514 | public List<IPath> getGemInstallPaths() { | |
| 515 | if (fGemInstallPaths == null) { | |
| 516 | if (!isRubyGemsInstalled()) return null; | |
| 517 | ILaunchConfiguration config = createGemLaunchConfiguration("", false); | |
| 518 | try { | |
| 519 | ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy(); | |
| 520 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "-r rubygems -e p(Gem.path)"); | |
| 521 | config = wc.doSave(); | |
| 522 | } catch (CoreException e) { | |
| 523 | AptanaRDTPlugin.log(e); | |
| 524 | } | |
| 525 | int tries = 0; | |
| 526 | while(tries < 3) { | |
| 527 | try { | |
| 528 | String output = launchInBackgroundAndRead(config, getGemInstallPathFile()); | |
| 529 | fGemInstallPaths = parseInstallPaths(output); | |
| 530 | break; | |
| 531 | } catch (IllegalArgumentException e) { | |
| 532 | // ignore | |
| 533 | tries++; | |
| 534 | } | |
| 535 | } | |
| 536 | } | |
| 537 | return fGemInstallPaths; | |
| 538 | } | |
| 539 | ||
| 540 | private List<IPath> parseInstallPaths(String output) { | |
| 541 | try { | |
| 542 | if (output == null || output.trim().length() == 0) throw new IllegalArgumentException("Got empty output for gem install paths"); | |
| 543 | output = output.trim(); | |
| 544 | if (!output.startsWith("[") || !output.endsWith("]")) throw new IllegalArgumentException("Expected an array for gem install paths, but was: " + output); | |
| 545 | // toss the array brackets | |
| 546 | output = output.substring(1, output.length() - 1); | |
| 547 | ||
| 548 | String[] paths = output.split(","); | |
| 549 | if (paths == null || paths.length < 1) return null; | |
| 550 | List<IPath> installPaths = new ArrayList<IPath>(); | |
| 551 | for (int i = 0; i < paths.length; i++) { | |
| 552 | String path = paths[i].trim(); | |
| 553 | // toss out the quotes | |
| 554 | path = path.substring(1, path.length() - 1); | |
| 555 | installPaths.add(new Path(path.trim())); | |
| 556 | } | |
| 557 | return installPaths; | |
| 558 | } catch (Exception e) { | |
| 559 | AptanaRDTPlugin.log(e); | |
| 560 | } | |
| 561 | return null; | |
| 562 | } | |
| 563 | ||
| 564 | private File getGemInstallPathFile() { | |
| 565 | return getStateFile("install_path.txt"); | |
| 566 | } | |
| 567 | ||
| 568 | private File getStateFile(String name) { | |
| 569 | String currentVMId = RubyRuntime.getDefaultVMInstall().getId(); | |
| 570 | File file = AptanaRDTPlugin.getDefault().getStateLocation().append("gems").append(currentVMId).append(name).toFile(); | |
| 571 | try { | |
| 572 | file.getParentFile().mkdirs(); | |
| 573 | file.createNewFile(); | |
| 574 | } catch (IOException e) { | |
| 575 | // ignore | |
| 576 | } | |
| 577 | return file; | |
| 578 | } | |
| 579 | ||
| 580 | public IPath getGemPath(String gemName) { | |
| 581 | List<IPath> paths = getGemInstallPaths(); | |
| 582 | if (paths == null) return null; | |
| 583 | List<IPath> matches = new ArrayList<IPath>(); | |
| 584 | for (IPath path : paths) { | |
| 585 | path = path.append("gems"); | |
| 586 | File gemFolder = path.toFile(); | |
| 587 | File[] gems = gemFolder.listFiles(); | |
| 588 | if (gems == null) continue; | |
| 589 | for (int i = 0; i < gems.length; i++) { | |
| 590 | File gem = gems[i]; | |
| 591 | String name = gem.getName(); | |
| 592 | if (name.startsWith(gemName)) | |
| 593 | matches.add(new Path(gem.getAbsolutePath())); | |
| 594 | } | |
| 595 | } | |
| 596 | ||
| 597 | if (matches.isEmpty()) return null; | |
| 598 | if (matches.size() == 1) return matches.get(0).append("lib"); | |
| 599 | // otherwise, find latest version | |
| 600 | List<Version> versions = new ArrayList<Version>(); | |
| 601 | for (IPath match : matches) { | |
| 602 | String name = match.lastSegment(); | |
| 603 | String[] parts = name.split("-"); | |
| 604 | for (int i = parts.length - 1; i >= 0; i--) { | |
| 605 | String version = parts[i]; | |
| 606 | if (!Version.correctFormat(version)) continue; | |
| 607 | try { | |
| 608 | Version duh = new Version(version); | |
| 609 | versions.add(duh); | |
| 610 | break; | |
| 611 | } catch (IllegalArgumentException e) { | |
| 612 | // ignore, that part may not be version for gem | |
| 613 | } | |
| 614 | } | |
| 615 | } | |
| 616 | Collections.sort(versions); | |
| 617 | Version latest = versions.get(versions.size() - 1); | |
| 618 | for (IPath match : matches) { | |
| 619 | String name = match.lastSegment(); | |
| 620 | String[] parts = name.split("-"); | |
| 621 | String version = null; | |
| 622 | for (int i = parts.length - 1; i >= 0; i--) { | |
| 623 | if (!Version.correctFormat(parts[i])) continue; | |
| 624 | version = parts[i]; | |
| 625 | try { | |
| 626 | Version duh = new Version(version); | |
| 627 | versions.add(duh); | |
| 628 | break; | |
| 629 | } catch (IllegalArgumentException e) { | |
| 630 | // ignore, that part may not be version for gem | |
| 631 | } | |
| 632 | } | |
| 633 | if (version.equals(latest.toString())) return match.append("lib"); | |
| 634 | } | |
| 635 | return null; | |
| 636 | } | |
| 637 | ||
| 638 | public IPath getGemPath(String gemName, String version) { | |
| 639 | return getGemPath(gemName + "-" + version); | |
| 640 | } | |
| 641 | ||
| 642 | public boolean updateAll() { | |
| 643 | if (!isRubyGemsInstalled()) return false; | |
| 644 | updateSystem(); | |
| 645 | try { | |
| 646 | ILaunchConfiguration config = createGemLaunchConfiguration(addProxy(UPDATE_COMMAND + " " + INCLUDE_DEPENDENCIES_SWITCH), true); | |
| 647 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 648 | Job job = new Job("Updating gem listing") { | |
| 649 | ||
| 650 | @Override | |
| 651 | protected IStatus run(IProgressMonitor monitor) { | |
| 652 | while (!launch.isTerminated()) { | |
| 653 | Thread.yield(); | |
| 654 | } | |
| 655 | refresh(); | |
| 656 | return Status.OK_STATUS; | |
| 657 | } | |
| 658 | ||
| 659 | }; | |
| 660 | job.schedule(); | |
| 661 | } catch (CoreException e) { | |
| 662 | AptanaRDTPlugin.log(e); | |
| 663 | return false; | |
| 664 | } | |
| 665 | return true; | |
| 666 | } | |
| 667 | ||
| 668 | private boolean updateSystem() { | |
| 669 | if (!isRubyGemsInstalled()) return false; | |
| 670 | if (RubyRuntime.currentVMIsJRuby()) return false; // ROR-307. For now don't let user upgrade rubygems on JRuby until we find a fix. | |
| 671 | try { | |
| 672 | ILaunchConfiguration config = createGemLaunchConfiguration(addProxy(UPDATE_COMMAND + " rubygems-update"), true); | |
| 673 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 674 | while (!launch.isTerminated()) { | |
| 675 | Thread.yield(); | |
| 676 | } | |
| 677 | } catch (CoreException e) { | |
| 678 | AptanaRDTPlugin.log(e); | |
| 679 | return false; | |
| 680 | } | |
| 681 | return true; | |
| 682 | } | |
| 683 | ||
| 684 | public void initialize() { | |
| 685 | RubyRuntime.addVMInstallChangedListener(this); | |
| 686 | scheduleLoadingSources(); | |
| 687 | scheduleLoadingLocalGems(); | |
| 688 | } | |
| 689 | ||
| 690 | private void scheduleLoadingSources() { | |
| 691 | Job job = new Job("Loading Remote Gem Sources") { | |
| 692 | ||
| 693 | @Override | |
| 694 | protected IStatus run(IProgressMonitor monitor) { | |
| 695 | urls = loadSourceURLs(); | |
| 696 | // if (urls.size() < 2) { | |
| 697 | // addSourceURL(RAILS_GEM_HOST); | |
| 698 | // } | |
| 699 | return Status.OK_STATUS; | |
| 700 | } | |
| 701 | ||
| 702 | }; | |
| 703 | job.setPriority(Job.LONG); | |
| 704 | job.setSystem(true); | |
| 705 | job.schedule(); | |
| 706 | } | |
| 707 | ||
| 708 | protected Set<String> loadSourceURLs() { | |
| 709 | Set<String> sources = new HashSet<String>(); | |
| 710 | String output = launchInBackgroundAndRead("sources -l", getConfigFile("sources_list.txt")); | |
| 711 | if (output == null) return sources; | |
| 712 | String[] lines = output.split("\n"); | |
| 713 | if (lines == null) return sources; | |
| 714 | for (int i = 2; i < lines.length; i++) { | |
| 715 | sources.add(lines[i].trim()); | |
| 716 | } | |
| 717 | return sources; | |
| 718 | } | |
| 719 | ||
| 720 | private void scheduleLoadingLocalGems() { | |
| 721 | Job job = new Job(GemsMessages.GemManager_loading_local_gems) { | |
| 722 | ||
| 723 | @Override | |
| 724 | protected IStatus run(IProgressMonitor monitor) { | |
| 725 | try { | |
| 726 | gems = loadLocalCache(getConfigFile(LOCAL_GEMS_CACHE_FILE)); | |
| 727 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 728 | listener.gemsRefreshed(); | |
| 729 | } | |
| 730 | gems = loadLocalGems(); | |
| 731 | int tries = 0; | |
| 732 | while (gems.isEmpty() && tries < 3) { // if we get back an empty list retry up to 3 times | |
| 733 | tries++; | |
| 734 | gems = loadLocalGems(); | |
| 735 | } | |
| 736 | storeGemCache(gems, getConfigFile(LOCAL_GEMS_CACHE_FILE)); | |
| 737 | isInitialized = true; | |
| 738 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 739 | listener.managerInitialized(); | |
| 740 | } | |
| 741 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 742 | listener.gemsRefreshed(); | |
| 743 | } | |
| 744 | } catch (Exception e) { | |
| 745 | AptanaRDTPlugin.log(e); | |
| 746 | return Status.CANCEL_STATUS; | |
| 747 | } | |
| 748 | return Status.OK_STATUS; | |
| 749 | } | |
| 750 | ||
| 751 | }; | |
| 752 | job.setPriority(Job.LONG); | |
| 753 | job.setSystem(true); | |
| 754 | job.schedule(); | |
| 755 | } | |
| 756 | ||
| 757 | protected Set<Gem> makeLogical(Set<Gem> remoteGems) { | |
| 758 | SortedSet<Gem> sorted = new TreeSet<Gem>(remoteGems); | |
| 759 | SortedSet<Gem> logical = new TreeSet<Gem>(); | |
| 760 | String name = null; | |
| 761 | Collection<Gem> temp = new HashSet<Gem>(); | |
| 762 | for (Gem gem : sorted) { | |
| 763 | if (name != null && !gem.getName().equals(name)) { | |
| 764 | logical.add(LogicalGem.create(temp)); | |
| 765 | temp.clear(); | |
| 766 | } | |
| 767 | name = gem.getName(); | |
| 768 | temp.add(gem); | |
| 769 | } | |
| 770 | if (name != null && !temp.isEmpty()) { | |
| 771 | logical.add(LogicalGem.create(temp)); | |
| 772 | temp.clear(); | |
| 773 | } | |
| 774 | return Collections.unmodifiableSortedSet(logical); | |
| 775 | } | |
| 776 | ||
| 777 | public boolean cleanup() { | |
| 778 | if (!isRubyGemsInstalled()) return false; | |
| 779 | try { | |
| 780 | String command = CLEANUP_COMMAND; | |
| 781 | ILaunchConfiguration config = createGemLaunchConfiguration(command, true); | |
| 782 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 783 | Job job = new Job("Cleaning up old versions of gems") { | |
| 784 | ||
| 785 | @Override | |
| 786 | protected IStatus run(IProgressMonitor monitor) { | |
| 787 | while (!launch.isTerminated()) { | |
| 788 | Thread.yield(); | |
| 789 | } | |
| 790 | refresh(); | |
| 791 | return Status.OK_STATUS; | |
| 792 | } | |
| 793 | ||
| 794 | }; | |
| 795 | job.schedule(); | |
| 796 | } catch (CoreException e) { | |
| 797 | AptanaRDTPlugin.log(e); | |
| 798 | return false; | |
| 799 | } | |
| 800 | return true; | |
| 801 | } | |
| 802 | ||
| 803 | public boolean installGem(Gem gem, String sourceURL) { | |
| 804 | return installGem(gem, sourceURL, true); | |
| 805 | } | |
| 806 | ||
| 807 | private boolean doInstallGem(final Gem gem, String command) { | |
| 808 | if (!isRubyGemsInstalled()) return false; | |
| 809 | try { | |
| 810 | ILaunchConfiguration config = createGemLaunchConfiguration(command, true); | |
| 811 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 812 | Job job = new Job("Installing gem " + gem.getName()) { | |
| 813 | ||
| 814 | @Override | |
| 815 | protected IStatus run(IProgressMonitor monitor) { | |
| 816 | while (!launch.isTerminated()) { | |
| 817 | Thread.yield(); | |
| 818 | } | |
| 819 | refresh(); | |
| 820 | // Need to wait until install is finished | |
| 821 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 822 | listener.gemAdded(gem); | |
| 823 | } | |
| 824 | return Status.OK_STATUS; | |
| 825 | } | |
| 826 | ||
| 827 | }; | |
| 828 | job.setSystem(true); | |
| 829 | job.schedule(); | |
| 830 | } catch (CoreException e) { | |
| 831 | AptanaRDTPlugin.log(e); | |
| 832 | return false; | |
| 833 | } | |
| 834 | return true; | |
| 835 | } | |
| 836 | ||
| 837 | private boolean doLocalInstallGem(final Gem gem) { | |
| 838 | if (!isRubyGemsInstalled()) return false; | |
| 839 | try { | |
| 840 | // force working directory to that containing the gem | |
| 841 | String command = INSTALL_COMMAND + " -l " + new File(gem.getAbsolutePath()).getName() + ""; | |
| 842 | ILaunchConfiguration config = createGemLaunchConfiguration(command, true); | |
| 843 | ILaunchConfigurationWorkingCopy wc = config.getWorkingCopy(); | |
| 844 | wc.setAttribute(IRubyLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, new File(gem.getAbsolutePath()).getParent()); | |
| 845 | config = wc.doSave(); | |
| 846 | final ILaunch launch = config.launch(ILaunchManager.RUN_MODE, null); | |
| 847 | Job job = new Job("Installing gem " + gem.getName()) { | |
| 848 | ||
| 849 | @Override | |
| 850 | protected IStatus run(IProgressMonitor monitor) { | |
| 851 | while (!launch.isTerminated()) { | |
| 852 | Thread.yield(); | |
| 853 | } | |
| 854 | refresh(); | |
| 855 | // Need to wait until uninstall is finished | |
| 856 | for (GemListener listener : new ArrayList<GemListener>(listeners)) { | |
| 857 | listener.gemAdded(gem); | |
| 858 | } | |
| 859 | return Status.OK_STATUS; | |
| 860 | } | |
| 861 | ||