| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.apache.lucene.gradle; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.nio.charset.StandardCharsets; |
| 21 |
import java.nio.file.Files; |
| 22 |
import java.nio.file.Path; |
| 23 |
import java.nio.file.Paths; |
| 24 |
import java.nio.file.StandardOpenOption; |
| 25 |
import java.util.Map; |
| 26 |
|
| 27 |
/** |
| 28 |
* Standalone class that generates a populated gradle.properties from a template. |
| 29 |
* |
| 30 |
* <p>Has no dependencies outside of standard java libraries |
| 31 |
*/ |
| 32 |
public class GradlePropertiesGenerator { |
| 33 |
public static void main(String[] args) { |
| 34 |
if (args.length != 2) { |
| 35 |
System.err.println("Usage: java GradlePropertiesGenerator.java <source> <destination>"); |
| 36 |
System.exit(2); |
| 37 |
} |
| 38 |
|
| 39 |
try { |
| 40 |
new GradlePropertiesGenerator().run(Paths.get(args[0]), Paths.get(args[1])); |
| 41 |
} catch (Exception e) { |
| 42 |
System.err.println("ERROR: " + e.getMessage()); |
| 43 |
System.exit(3); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public void run(Path source, Path destination) throws IOException { |
| 48 |
if (!Files.exists(source)) { |
| 49 |
throw new IOException("template file not found: " + source); |
| 50 |
} |
| 51 |
if (Files.exists(destination)) { |
| 52 |
System.out.println(destination + " already exists, skipping generation."); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Approximate a common-sense default for running gradle/tests with parallel |
| 57 |
// workers: half the count of available cpus but not more than 12. |
| 58 |
var cpus = Runtime.getRuntime().availableProcessors(); |
| 59 |
var maxWorkers = (int) Math.max(1d, Math.min(cpus * 0.5d, 12)); |
| 60 |
var testsJvms = (int) Math.max(1d, Math.min(cpus * 0.5d, 12)); |
| 61 |
|
| 62 |
var replacements = Map.of("@MAX_WORKERS@", maxWorkers, "@TEST_JVMS@", testsJvms); |
| 63 |
|
| 64 |
System.out.println("Generating gradle.properties"); |
| 65 |
String fileContent = Files.readString(source, StandardCharsets.UTF_8); |
| 66 |
for (var entry : replacements.entrySet()) { |
| 67 |
fileContent = fileContent.replace(entry.getKey(), String.valueOf(entry.getValue())); |
| 68 |
} |
| 69 |
Files.writeString( |
| 70 |
destination, fileContent, StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW); |
| 71 |
} |
| 72 |
} |