PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/facebook/buck/io/ProjectFilesystemDelegateFactory.java

https://gitlab.com/smartether/buck
Java | 102 lines | 61 code | 11 blank | 30 comment | 9 complexity | f70058ee950d2e952a3af66ac9629788 MD5 | raw file
  1. /*
  2. * Copyright 2016-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations
  14. * under the License.
  15. */
  16. package com.facebook.buck.io;
  17. import com.facebook.buck.eden.EdenClient;
  18. import com.facebook.buck.eden.EdenMount;
  19. import com.facebook.buck.eden.EdenProjectFilesystemDelegate;
  20. import com.facebook.buck.log.Logger;
  21. import com.facebook.buck.util.PrintStreamProcessExecutorFactory;
  22. import com.facebook.buck.util.autosparse.AbstractAutoSparseFactory;
  23. import com.facebook.buck.util.autosparse.AutoSparseConfig;
  24. import com.facebook.buck.util.autosparse.AutoSparseProjectFilesystemDelegate;
  25. import com.facebook.buck.util.autosparse.AutoSparseState;
  26. import com.facebook.buck.util.environment.Platform;
  27. import com.facebook.buck.util.versioncontrol.HgCmdLineInterface;
  28. import com.facebook.eden.thrift.EdenError;
  29. import com.facebook.thrift.TException;
  30. import com.google.common.collect.ImmutableMap;
  31. import java.nio.file.Path;
  32. import java.util.Optional;
  33. /**
  34. * {@link ProjectFilesystemDelegateFactory} mediates the creation of a
  35. * {@link ProjectFilesystemDelegate} for a {@link ProjectFilesystem} root.
  36. */
  37. public final class ProjectFilesystemDelegateFactory {
  38. private static final Logger LOG = Logger.get(ProjectFilesystemDelegateFactory.class);
  39. /** Utility class: do not instantiate. */
  40. private ProjectFilesystemDelegateFactory() {}
  41. /**
  42. * Must always create a new delegate for the specified {@code root}.
  43. */
  44. public static ProjectFilesystemDelegate newInstance(
  45. Path root,
  46. String hgCmd,
  47. AutoSparseConfig autoSparseConfig) {
  48. Optional<EdenClient> client = tryToCreateEdenClient();
  49. if (client.isPresent()) {
  50. try {
  51. EdenMount mount = client.get().getMountFor(root);
  52. if (mount != null) {
  53. return new EdenProjectFilesystemDelegate(mount);
  54. }
  55. } catch (TException | EdenError e) {
  56. // If Eden is running but root is not a mount point, Eden getMountFor() should just return
  57. // null rather than throw an error.
  58. LOG.error(e, "Failed to find Eden client for %s.", root);
  59. }
  60. }
  61. if (autoSparseConfig.enabled()) {
  62. // We can't access BuckConfig because that class requires a
  63. // ProjectFileSystem, which we are in the process of building
  64. // Access the required info from the Config instead
  65. HgCmdLineInterface hgCmdLine = new HgCmdLineInterface(
  66. new PrintStreamProcessExecutorFactory(),
  67. root,
  68. hgCmd,
  69. ImmutableMap.of()
  70. );
  71. AutoSparseState autoSparseState = AbstractAutoSparseFactory.getAutoSparseState(
  72. root,
  73. hgCmdLine,
  74. autoSparseConfig);
  75. if (autoSparseState != null) {
  76. LOG.debug("Autosparse enabled, using AutoSparseProjectFilesystemDelegate");
  77. return new AutoSparseProjectFilesystemDelegate(autoSparseState, root);
  78. }
  79. }
  80. // No Eden or Mercurial info available, use the default
  81. return new DefaultProjectFilesystemDelegate(root);
  82. }
  83. /** @return {@link Optional#empty()} if there is no instance of Eden running. */
  84. private static Optional<EdenClient> tryToCreateEdenClient() {
  85. if (Platform.detect() != Platform.WINDOWS) {
  86. return EdenClient.newInstance();
  87. } else {
  88. return Optional.empty();
  89. }
  90. }
  91. }