package jetbrains.mps.build.util;
/*Generated by MPS */
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.baseLanguage.tuples.runtime.Tuples;
import jetbrains.mps.baseLanguage.tuples.runtime.MultiTuple;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.build.behavior.BuildSourcePath__BehaviorDescriptor;
import jetbrains.mps.build.behavior.BuildLayout_Node__BehaviorDescriptor;
public class ArtifactLookup {
private final DependenciesHelper myDependencyHelper;
private final VisibleArtifacts myArtifacts;
public ArtifactLookup(@NotNull VisibleArtifacts artifacts) {
myArtifacts = artifacts;
myDependencyHelper = null;
}
public ArtifactLookup(@NotNull VisibleArtifacts artifacts, @Nullable DependenciesHelper helper) {
myArtifacts = artifacts;
myDependencyHelper = helper;
}
public SNode toOriginalNode(SNode node) {
return (myDependencyHelper == null ? node : myDependencyHelper.getOriginalNode(node));
}
public Tuples._2<SNode, String> getResource(SNode path) {
SNode result = findArtifact(path);
if (result != null) {
return MultiTuple.<SNode,String>from(result, "");
}
StringBuilder suffix = new StringBuilder();
SNode current = SNodeOperations.as(path, MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x65997a657729f6fbL, "jetbrains.mps.build.structure.BuildRelativePath"));
if (current != null) {
suffix.append("/").append(BuildSourcePath__BehaviorDescriptor.getLastSegment_id1bWeed$oUb5.invoke(path, null));
current = SNodeOperations.as(BuildSourcePath__BehaviorDescriptor.getParent_id7wpYgMyTXsR.invoke(current), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x65997a657729f6fbL, "jetbrains.mps.build.structure.BuildRelativePath"));
}
SNode containingRoot = SNodeOperations.getContainingRoot(path);
while (current != null) {
result = findArtifact(new LocalSourcePathArtifact(containingRoot, BuildSourcePath__BehaviorDescriptor.getRelativePath_id4Kip2_918YF.invoke(current), true));
if (result != null) {
return MultiTuple.<SNode,String>from(result, suffix.toString());
}
suffix.insert(0, BuildSourcePath__BehaviorDescriptor.getLastSegment_id1bWeed$oUb5.invoke(current, null)).insert(0, "/");
current = SNodeOperations.as(BuildSourcePath__BehaviorDescriptor.getParent_id7wpYgMyTXsR.invoke(current), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x65997a657729f6fbL, "jetbrains.mps.build.structure.BuildRelativePath"));
}
return MultiTuple.<SNode,String>from((SNode) null, (String) null);
}
public SNode findArtifact(Object id) {
if (id instanceof SNode) {
SNode node = (SNode) id;
if (SNodeOperations.isInstanceOf(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x62ec2ed0f87da183L, "jetbrains.mps.build.structure.BuildLayout_PathElement")) && myArtifacts.parent(SNodeOperations.as(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x62ec2ed0f87da183L, "jetbrains.mps.build.structure.BuildLayout_PathElement"))) != null) {
return SNodeOperations.cast(node, MetaAdapterFactory.getInterfaceConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x62ec2ed0f87da183L, "jetbrains.mps.build.structure.BuildLayout_PathElement"));
}
SNode rv = doFind(id);
if (rv == null) {
SNode originalId = toOriginalNode((SNode) id);
if (originalId == id) {
return null;
}
// try with original node
return doFind(originalId);
}
}
return doFind(id);
}
private SNode doFind(Object id) {
if (id == null) {
return null;
}
SNode result = cached(id);
if (result != null) {
return result;
}
for (SNode artifact : myArtifacts.getArtifacts()) {
if ((boolean) BuildLayout_Node__BehaviorDescriptor.exports_id5FtnUVJQES1.invoke(artifact, id)) {
cache(id, artifact);
return artifact;
}
}
return null;
}
protected SNode cached(Object id) {
if (myDependencyHelper == null) {
return null;
}
return SNodeOperations.as(myDependencyHelper.artifacts().get(id), MetaAdapterFactory.getConcept(0x798100da4f0a421aL, 0xb99171f8c50ce5d2L, 0x668c6cfbafac4c85L, "jetbrains.mps.build.structure.BuildLayout_Node"));
}
protected void cache(Object id, SNode element) {
if (myDependencyHelper == null) {
return;
}
if (id instanceof String) {
myDependencyHelper.putArtifact(as_arca2u_a0a0a0b0q(id, String.class), element);
} else if (id instanceof LocalSourcePathArtifact) {
myDependencyHelper.putArtifact(as_arca2u_a0a0a0a1a61(id, LocalSourcePathArtifact.class), element);
} else if (id instanceof SNode) {
myDependencyHelper.putArtifact(as_arca2u_a0a0a0b1a61(id, SNode.class), element);
} else {
throw new IllegalStateException("Unexpected way to identify artifacts:" + String.valueOf(id));
}
}
private static <T> T as_arca2u_a0a0a0b0q(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
private static <T> T as_arca2u_a0a0a0a1a61(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
private static <T> T as_arca2u_a0a0a0b1a61(Object o, Class<T> type) {
return (type.isInstance(o) ? (T) o : null);
}
}