-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.gradle; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import static com.gradle.Utils.execAndGetStdOut; | ||
|
||
class GitMetadata { | ||
|
||
private final String[] args; | ||
private final boolean isGitInstalled; | ||
private final Supplier<Optional<String>> fromEnv; | ||
|
||
private GitMetadata(GitMetadataBuilder metadataBuilder) { | ||
this.args = metadataBuilder.args; | ||
this.isGitInstalled = metadataBuilder.isGitInstalled; | ||
this.fromEnv = metadataBuilder.fromEnv; | ||
} | ||
|
||
Optional<String> resolve() { | ||
if (fromEnv != null) { | ||
return fromEnv.get(); | ||
} | ||
if (isGitInstalled && args != null) { | ||
return execAndGetStdOut(args); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static class GitMetadataBuilder { | ||
private String[] args; | ||
private final boolean isGitInstalled; | ||
private Supplier<Optional<String>> fromEnv; | ||
|
||
GitMetadataBuilder(boolean isGitInstalled) { | ||
this.isGitInstalled = isGitInstalled; | ||
} | ||
|
||
GitMetadataBuilder fromCmd(String... args) { | ||
this.args = args; | ||
return this; | ||
} | ||
|
||
GitMetadataBuilder fromEnv(Supplier<Optional<String>> fromEnv) { | ||
this.fromEnv = fromEnv; | ||
return this; | ||
} | ||
|
||
GitMetadata build() { | ||
return new GitMetadata(this); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters