|
| 1 | +package org.openqa.selenium; |
| 2 | + |
| 3 | +import static com.google.common.base.StandardSystemProperty.LINE_SEPARATOR; |
| 4 | +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; |
| 5 | +import static org.openqa.selenium.Platform.WINDOWS; |
| 6 | +import static org.openqa.selenium.testing.DevMode.isInDevMode; |
| 7 | + |
| 8 | +import com.google.common.base.Preconditions; |
| 9 | +import com.google.common.base.Splitter; |
| 10 | +import com.google.common.base.StandardSystemProperty; |
| 11 | +import com.google.common.collect.ImmutableList; |
| 12 | +import com.google.common.hash.HashCode; |
| 13 | +import com.google.common.hash.Hashing; |
| 14 | + |
| 15 | +import org.openqa.selenium.os.CommandLine; |
| 16 | +import org.openqa.selenium.testing.InProject; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.HttpURLConnection; |
| 20 | +import java.net.URL; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.util.List; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +public class BuckBuild { |
| 28 | + private static Logger log = Logger.getLogger(BuckBuild.class.getName()); |
| 29 | + |
| 30 | + private String target; |
| 31 | + |
| 32 | + public BuckBuild of(String target) { |
| 33 | + this.target = target; |
| 34 | + return this; |
| 35 | + } |
| 36 | + |
| 37 | + public Path go() throws IOException { |
| 38 | + Path projectRoot = InProject.locate("Rakefile").toPath().getParent(); |
| 39 | + |
| 40 | + if (!isInDevMode()) { |
| 41 | + // we should only need to do this when we're in dev mode |
| 42 | + // when running in a test suite, our dependencies should already |
| 43 | + // be listed. |
| 44 | + log.info("Not in dev mode. Ignoring attempt to build: " + target); |
| 45 | + return findOutput(projectRoot); |
| 46 | + } |
| 47 | + |
| 48 | + if (target == null || "".equals(target)) { |
| 49 | + throw new IllegalStateException("No targets specified"); |
| 50 | + } |
| 51 | + System.out.println("\nBuilding " + target + " ..."); |
| 52 | + |
| 53 | + ImmutableList.Builder<String> builder = ImmutableList.builder(); |
| 54 | + findBuck(projectRoot, builder); |
| 55 | + builder.add("build"); |
| 56 | + builder.add(target); |
| 57 | + |
| 58 | + ImmutableList<String> command = builder.build(); |
| 59 | + CommandLine commandLine = new CommandLine(command.toArray(new String[command.size()])); |
| 60 | + commandLine.copyOutputTo(System.err); |
| 61 | + commandLine.execute(); |
| 62 | + |
| 63 | + if (!commandLine.isSuccessful()) { |
| 64 | + throw new WebDriverException("Build failed! " + target); |
| 65 | + } |
| 66 | + |
| 67 | + return findOutput(projectRoot); |
| 68 | + } |
| 69 | + |
| 70 | + private Path findOutput(Path projectRoot) throws IOException { |
| 71 | + ImmutableList.Builder<String> builder = ImmutableList.builder(); |
| 72 | + findBuck(projectRoot, builder); |
| 73 | + builder.add("targets", "--show-output", target); |
| 74 | + |
| 75 | + ImmutableList<String> command = builder.build(); |
| 76 | + CommandLine commandLine = new CommandLine(command.toArray(new String[command.size()])); |
| 77 | + commandLine.copyOutputTo(System.err); |
| 78 | + commandLine.execute(); |
| 79 | + |
| 80 | + if (!commandLine.isSuccessful()) { |
| 81 | + throw new WebDriverException("Unable to find output! " + target); |
| 82 | + } |
| 83 | + |
| 84 | + String[] allLines = commandLine.getStdOut().split(LINE_SEPARATOR.value()); |
| 85 | + String lastLine = allLines[allLines.length -1 ]; |
| 86 | + |
| 87 | + List<String> outputs = Splitter.on(' ').limit(2).splitToList(lastLine); |
| 88 | + if (outputs.size() != 2) { |
| 89 | + throw new WebDriverException( |
| 90 | + String.format("Unable to find output! %s, %s", target, lastLine)); |
| 91 | + } |
| 92 | + |
| 93 | + Path output = projectRoot.resolve(outputs.get(1)); |
| 94 | + |
| 95 | + if (!Files.exists(output)) { |
| 96 | + throw new WebDriverException( |
| 97 | + String.format("Found output, but it does not exist: %s, %s", target, output)); |
| 98 | + } |
| 99 | + |
| 100 | + return output; |
| 101 | + } |
| 102 | + |
| 103 | + private void findBuck(Path projectRoot, ImmutableList.Builder<String> builder) throws IOException { |
| 104 | + Path noBuckCheck = projectRoot.resolve(".nobuckcheck"); |
| 105 | + |
| 106 | + // If there's a .nobuckcheck in the root of the file, and we can execute "buck", then assume |
| 107 | + // that the developer knows what they're doing. Ha! Ahaha! Ahahahaha! |
| 108 | + if (Files.exists(noBuckCheck)) { |
| 109 | + String buckCommand = CommandLine.find("buck"); |
| 110 | + if (buckCommand != null) { |
| 111 | + builder.add(buckCommand); |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + downloadBuckPexIfNecessary(builder); |
| 117 | + } |
| 118 | + |
| 119 | + private void downloadBuckPexIfNecessary(ImmutableList.Builder<String> builder) |
| 120 | + throws IOException { |
| 121 | + String buckVersion = new String(Files.readAllBytes(Paths.get(".buckversion"))).trim(); |
| 122 | + |
| 123 | + Path pex = Paths.get( |
| 124 | + StandardSystemProperty.USER_HOME.value(), ".crazyfun", "buck", buckVersion, "buck.pex"); |
| 125 | + |
| 126 | + String expectedHash = new String(Files.readAllBytes(Paths.get(".buckhash"))).trim(); |
| 127 | + HashCode md5 = Files.exists(pex) ? |
| 128 | + Hashing.md5().hashBytes(Files.readAllBytes(pex)) : |
| 129 | + HashCode.fromString("aa"); // So we have a non-null value |
| 130 | + |
| 131 | + if (!Files.exists(pex) || !expectedHash.equals(md5.toString())) { |
| 132 | + log.warning("Downloading PEX"); |
| 133 | + |
| 134 | + if (!Files.exists(pex.getParent())) { |
| 135 | + Files.createDirectories(pex.getParent()); |
| 136 | + } |
| 137 | + |
| 138 | + URL url = new URL(String.format( |
| 139 | + "https://github.com/SeleniumHQ/buck/releases/download/buck-release-%s/buck.pex", |
| 140 | + buckVersion)); |
| 141 | + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
| 142 | + connection.setInstanceFollowRedirects(true); |
| 143 | + Files.copy(connection.getInputStream(), pex, REPLACE_EXISTING); |
| 144 | + // Do our best to make this executable |
| 145 | + pex.toFile().setExecutable(true); |
| 146 | + } |
| 147 | + |
| 148 | + md5 = Hashing.md5().hashBytes(Files.readAllBytes(pex)); |
| 149 | + if (!expectedHash.equals(md5.toString())) { |
| 150 | + throw new WebDriverException("Unable to confirm that download is valid"); |
| 151 | + } |
| 152 | + |
| 153 | + if (Platform.getCurrent().is(WINDOWS)) { |
| 154 | + String python = CommandLine.find("python2"); |
| 155 | + if (python == null) { |
| 156 | + python = CommandLine.find("python"); |
| 157 | + } |
| 158 | + Preconditions.checkNotNull(python, "Unable to find python executable"); |
| 159 | + builder.add(python); |
| 160 | + } |
| 161 | + |
| 162 | + builder.add(pex.toAbsolutePath().toString()); |
| 163 | + } |
| 164 | + |
| 165 | + public static void main(String[] args) throws IOException { |
| 166 | + new BuckBuild().of("se3-server").go(); |
| 167 | + } |
| 168 | +} |
0 commit comments