57
57
import com .google .copybara .util .console .NoPromptConsole ;
58
58
import java .io .ByteArrayInputStream ;
59
59
import java .io .IOException ;
60
+ import java .io .InputStream ;
60
61
import java .nio .charset .StandardCharsets ;
61
62
import java .nio .file .FileSystem ;
62
63
import java .nio .file .FileSystems ;
67
68
import java .util .Arrays ;
68
69
import java .util .Map ;
69
70
import java .util .Optional ;
71
+ import java .util .Properties ;
70
72
import java .util .function .Consumer ;
71
73
import java .util .logging .Level ;
72
74
import java .util .logging .LogManager ;
@@ -83,6 +85,8 @@ public class Main {
83
85
private static final String COPYBARA_NAMESPACE = "com.google.copybara" ;
84
86
85
87
private static final FluentLogger logger = FluentLogger .forEnclosingClass ();
88
+ private static final String BUILD_DATA_PROPERTIES = "/build-data.properties" ;
89
+ public static final String BUILD_LABEL = "Build label" ;
86
90
/**
87
91
* Represents the environment, typically {@code System.getEnv()}. Injected to make easier tests.
88
92
*
@@ -300,15 +304,37 @@ public ImmutableSet<CopybaraCmd> getCommands(ModuleSet moduleSet,
300
304
* Returns a short String representing the version of the binary
301
305
*/
302
306
protected String getVersion () {
303
- return "Unknown version" ;
307
+ String buildLabel = getBuildInfo ().get (BUILD_LABEL );
308
+ return buildLabel == null ? "Unknown version" : buildLabel ;
304
309
}
305
310
311
+ private static ImmutableMap <String , String > getBuildInfo () {
312
+ try (InputStream in = Main .class .getResourceAsStream (BUILD_DATA_PROPERTIES )) {
313
+ if (in == null ) {
314
+ return ImmutableMap .of ();
315
+ }
316
+ Properties props = new Properties ();
317
+ props .load (in );
318
+ ImmutableMap .Builder <String , String > buildData = ImmutableMap .builder ();
319
+ for (Object key : props .keySet ()) {
320
+ String stringKey = key .toString ();
321
+ if (stringKey .startsWith ("build." )) {
322
+ // build.label -> Build label, build.timestamp.as.int -> Build timestamp as int
323
+ String buildDataKey = "B" + stringKey .substring (1 ).replace ('.' , ' ' );
324
+ buildData .put (buildDataKey , props .getProperty (stringKey , "" ));
325
+ }
326
+ }
327
+ return buildData .buildOrThrow ();
328
+ } catch (IOException ignored ) {
329
+ return ImmutableMap .of ();
330
+ }
331
+ }
306
332
/**
307
333
* Returns a String (can be multiline) representing all the information about who and when the
308
334
* Copybara was built.
309
335
*/
310
336
protected String getBinaryInfo () {
311
- return "Unknown version" ;
337
+ return Joiner . on ( " \n " ). withKeyValueSeparator ( ": " ). join ( getBuildInfo ()) ;
312
338
}
313
339
314
340
protected Consumer <Migration > getMigrationRanConsumer () {
0 commit comments