use of java.util.Optional in project buck by facebook.
the class BuildCommandTest method setUp.
@Before
public void setUp() {
BuildRuleResolver ruleResolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
resolver = new SourcePathResolver(new SourcePathRuleFinder(ruleResolver));
LinkedHashMap<BuildRule, Optional<BuildResult>> ruleToResult = new LinkedHashMap<>();
FakeBuildRule rule1 = new FakeBuildRule(BuildTargetFactory.newInstance("//fake:rule1"), resolver);
rule1.setOutputFile("buck-out/gen/fake/rule1.txt");
ruleResolver.addToIndex(rule1);
ruleToResult.put(rule1, Optional.of(BuildResult.success(rule1, BUILT_LOCALLY, CacheResult.miss())));
BuildRule rule2 = new FakeBuildRule(BuildTargetFactory.newInstance("//fake:rule2"), resolver);
BuildResult rule2Failure = BuildResult.failure(rule2, new RuntimeException("some"));
ruleToResult.put(rule2, Optional.of(rule2Failure));
ruleResolver.addToIndex(rule2);
BuildRule rule3 = new FakeBuildRule(BuildTargetFactory.newInstance("//fake:rule3"), resolver);
ruleToResult.put(rule3, Optional.of(BuildResult.success(rule3, FETCHED_FROM_CACHE, CacheResult.hit("dir"))));
ruleResolver.addToIndex(rule3);
BuildRule rule4 = new FakeBuildRule(BuildTargetFactory.newInstance("//fake:rule4"), resolver);
ruleToResult.put(rule4, Optional.empty());
ruleResolver.addToIndex(rule4);
buildExecutionResult = BuildExecutionResult.builder().setResults(ruleToResult).setFailures(ImmutableSet.of(rule2Failure)).build();
}
use of java.util.Optional in project che by eclipse.
the class CheEnvironmentEngineTest method machineStopShouldFireEvents.
@Test
public void machineStopShouldFireEvents() throws Exception {
// given
List<Instance> instances = startEnv();
Optional<Instance> instanceOpt = instances.stream().filter(machine -> !machine.getConfig().isDev()).findAny();
assertTrue(instanceOpt.isPresent(), "Required for test non-dev machine is not found");
Instance instance = instanceOpt.get();
// when
engine.stopMachine(instance.getWorkspaceId(), instance.getId());
// then
verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.CREATING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
verify(eventService).publish(newDto(MachineStatusEvent.class).withEventType(MachineStatusEvent.EventType.RUNNING).withDev(instance.getConfig().isDev()).withMachineName(instance.getConfig().getName()).withMachineId(instance.getId()).withWorkspaceId(instance.getWorkspaceId()));
}
use of java.util.Optional in project jetty.project by eclipse.
the class Modules method enable.
private void enable(Set<String> newlyEnabled, Module module, String enabledFrom, boolean transitive) {
StartLog.debug("enable %s from %s transitive=%b", module, enabledFrom, transitive);
if (newlyEnabled.contains(module.getName())) {
StartLog.debug("Cycle at %s", module);
return;
}
// Check that this is not already provided by another module!
for (String name : module.getProvides()) {
Set<Module> providers = _provided.get(name);
if (providers != null) {
for (Module p : providers) {
if (p != module && p.isEnabled()) {
// If the already enabled module is transitive and this enable is not
if (p.isTransitive() && !transitive)
p.clearTransitiveEnable();
else
throw new UsageException("Module %s provides %s, which is already provided by %s enabled in %s", module.getName(), name, p.getName(), p.getEnableSources());
}
}
;
}
}
// Enable the module
if (module.enable(enabledFrom, transitive)) {
StartLog.debug("enabled %s", module.getName());
newlyEnabled.add(module.getName());
// Expand module properties
module.expandDependencies(_args.getProperties());
// Apply default configuration
if (module.hasDefaultConfig()) {
for (String line : module.getDefaultConfig()) _args.parse(line, module.getName() + "[ini]");
for (Module m : _modules) m.expandDependencies(_args.getProperties());
}
}
// Process module dependencies (always processed as may be dynamic)
for (String dependsOn : module.getDepends()) {
// Look for modules that provide that dependency
Set<Module> providers = getAvailableProviders(dependsOn);
StartLog.debug("Module %s depends on %s provided by ", module, dependsOn, providers);
// If there are no known providers of the module
if (providers.isEmpty()) {
// look for a dynamic module
if (dependsOn.contains("/")) {
Path file = _baseHome.getPath("modules/" + dependsOn + ".mod");
registerModule(file).expandDependencies(_args.getProperties());
providers = _provided.get(dependsOn);
if (providers == null || providers.isEmpty())
throw new UsageException("Module %s does not provide %s", _baseHome.toShortForm(file), dependsOn);
enable(newlyEnabled, providers.stream().findFirst().get(), "dynamic dependency of " + module.getName(), true);
continue;
}
throw new UsageException("No module found to provide %s for %s", dependsOn, module);
}
// If a provider is already enabled, then add a transitive enable
if (providers.stream().filter(Module::isEnabled).count() != 0)
providers.stream().filter(m -> m.isEnabled() && m != module).forEach(m -> enable(newlyEnabled, m, "transitive provider of " + dependsOn + " for " + module.getName(), true));
else {
// Is there an obvious default?
Optional<Module> dftProvider = (providers.size() == 1) ? providers.stream().findFirst() : providers.stream().filter(m -> m.getName().equals(dependsOn)).findFirst();
if (dftProvider.isPresent())
enable(newlyEnabled, dftProvider.get(), "transitive provider of " + dependsOn + " for " + module.getName(), true);
else if (StartLog.isDebugEnabled())
StartLog.debug("Module %s requires a %s implementation from one of %s", module, dependsOn, providers);
}
}
}
use of java.util.Optional in project che by eclipse.
the class JGitConnection method branchList.
@Override
public List<Branch> branchList(BranchListMode listMode) throws GitException {
ListBranchCommand listBranchCommand = getGit().branchList();
if (LIST_ALL == listMode || listMode == null) {
listBranchCommand.setListMode(ListMode.ALL);
} else if (LIST_REMOTE == listMode) {
listBranchCommand.setListMode(ListMode.REMOTE);
}
List<Ref> refs;
String currentRef;
try {
refs = listBranchCommand.call();
String headBranch = getRepository().getBranch();
Optional<Ref> currentTag = getGit().tagList().call().stream().filter(tag -> tag.getObjectId().getName().equals(headBranch)).findFirst();
if (currentTag.isPresent()) {
currentRef = currentTag.get().getName();
} else {
currentRef = "refs/heads/" + headBranch;
}
} catch (GitAPIException | IOException exception) {
throw new GitException(exception.getMessage(), exception);
}
List<Branch> branches = new ArrayList<>();
for (Ref ref : refs) {
String refName = ref.getName();
boolean isCommitOrTag = Constants.HEAD.equals(refName);
String branchName = isCommitOrTag ? currentRef : refName;
String branchDisplayName;
if (isCommitOrTag) {
branchDisplayName = "(detached from " + Repository.shortenRefName(currentRef) + ")";
} else {
branchDisplayName = Repository.shortenRefName(refName);
}
Branch branch = newDto(Branch.class).withName(branchName).withActive(isCommitOrTag || refName.equals(currentRef)).withDisplayName(branchDisplayName).withRemote(refName.startsWith("refs/remotes"));
branches.add(branch);
}
return branches;
}
use of java.util.Optional in project che by eclipse.
the class JGitConnection method checkout.
@Override
public void checkout(CheckoutParams params) throws GitException {
CheckoutCommand checkoutCommand = getGit().checkout();
String startPoint = params.getStartPoint();
String name = params.getName();
String trackBranch = params.getTrackBranch();
// checkout files?
List<String> files = params.getFiles();
boolean shouldCheckoutToFile = name != null && new File(getWorkingDir(), name).exists();
if (shouldCheckoutToFile || !files.isEmpty()) {
if (shouldCheckoutToFile) {
checkoutCommand.addPath(params.getName());
} else {
files.forEach(checkoutCommand::addPath);
}
} else {
// checkout branch
if (startPoint != null && trackBranch != null) {
throw new GitException("Start point and track branch can not be used together.");
}
if (params.isCreateNew() && name == null) {
throw new GitException("Branch name must be set when createNew equals to true.");
}
if (startPoint != null) {
checkoutCommand.setStartPoint(startPoint);
}
if (params.isCreateNew()) {
checkoutCommand.setCreateBranch(true);
checkoutCommand.setName(name);
} else if (name != null) {
checkoutCommand.setName(name);
List<String> localBranches = branchList(LIST_LOCAL).stream().map(Branch::getDisplayName).collect(Collectors.toList());
if (!localBranches.contains(name)) {
Optional<Branch> remoteBranch = branchList(LIST_REMOTE).stream().filter(branch -> branch.getName().contains(name)).findFirst();
if (remoteBranch.isPresent()) {
checkoutCommand.setCreateBranch(true);
checkoutCommand.setStartPoint(remoteBranch.get().getName());
}
}
}
if (trackBranch != null) {
if (name == null) {
checkoutCommand.setName(cleanRemoteName(trackBranch));
}
checkoutCommand.setCreateBranch(true);
checkoutCommand.setStartPoint(trackBranch);
}
checkoutCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
}
try {
checkoutCommand.call();
} catch (CheckoutConflictException exception) {
throw new GitConflictException(exception.getMessage(), exception.getConflictingPaths());
} catch (RefAlreadyExistsException exception) {
throw new GitRefAlreadyExistsException(exception.getMessage());
} catch (RefNotFoundException exception) {
throw new GitRefNotFoundException(exception.getMessage());
} catch (InvalidRefNameException exception) {
throw new GitInvalidRefNameException(exception.getMessage());
} catch (GitAPIException exception) {
if (exception.getMessage().endsWith("already exists")) {
throw new GitException(format(ERROR_CHECKOUT_BRANCH_NAME_EXISTS, name != null ? name : cleanRemoteName(trackBranch)));
}
throw new GitException(exception.getMessage(), exception);
}
}
Aggregations