package jetbrains.mps.ide.make.actions;
/*Generated by MPS */
import jetbrains.mps.project.Project;
import java.util.List;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;
import jetbrains.mps.internal.collections.runtime.IWhereFilter;
import jetbrains.mps.util.SNodeOperations;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.make.resources.IResource;
import jetbrains.mps.internal.collections.runtime.ITranslator2;
import jetbrains.mps.internal.collections.runtime.Sequence;
import java.util.Collections;
import jetbrains.mps.smodel.resources.ModelsToResources;
import jetbrains.mps.smodel.Language;
import jetbrains.mps.smodel.Generator;
public class MakeActionParameters {
private final Project myProject;
private List<SModel> myModels;
private List<SModule> myModules;
private boolean myCleanBuild = false;
public MakeActionParameters(@NotNull Project project) {
myProject = project;
}
public MakeActionParameters models(Iterable<SModel> models) {
myModels = (models != null ? ListSequence.fromListWithValues(new ArrayList<SModel>(), models) : null);
return this;
}
public MakeActionParameters modules(Iterable<SModule> modules) {
myModules = (modules != null ? ListSequence.fromListWithValues(new ArrayList<SModule>(), modules) : null);
return this;
}
/**
* By default, parameters are set for make, not clean build (i.e. <code>cleanMake(false)</code>.
*
* @param clean indicates whether to perform clean build
* @return this for convenience
*/
public MakeActionParameters cleanMake(boolean clean) {
myCleanBuild = clean;
return this;
}
public boolean isCleanMake() {
return myCleanBuild;
}
@NotNull
public Project getProject() {
return myProject;
}
public String actionText() {
String fmt = (myCleanBuild ? "Rebuild %s" : "Make %s");
if (myModels != null && ListSequence.fromList(myModels).isNotEmpty()) {
if (!(ListSequence.fromList(myModels).any(new IWhereFilter<SModel>() {
public boolean accept(SModel md) {
return SNodeOperations.isGeneratable(md);
}
}))) {
return null;
}
if (ListSequence.fromList(myModels).count() > 1) {
return String.format(fmt, "Selected Models");
}
String modelName = NameUtil.compactNamespace(ListSequence.fromList(myModels).first().getModelName());
return String.format(fmt, String.format("Model '%s'", modelName));
}
if (myModules != null && ListSequence.fromList(myModules).isNotEmpty()) {
if (ListSequence.fromList(myModules).any(new IWhereFilter<SModule>() {
public boolean accept(SModule m) {
return m == null;
}
}) || ListSequence.fromList(myModules).all(new IWhereFilter<SModule>() {
public boolean accept(SModule m) {
return m.isReadOnly();
}
})) {
return null;
}
if (ListSequence.fromList(myModules).count() > 1) {
return String.format(fmt, "Selected Modules");
}
SModule module = ListSequence.fromList(myModules).first();
String kindName = NameUtil.shortNameFromLongName(module.getClass().getName().replaceAll("\\$.*", ""));
String moduleName = NameUtil.compactNamespace(module.getModuleName());
return String.format(fmt, String.format("%s '%s'", kindName, moduleName));
}
return null;
}
public Iterable<IResource> collectInput() {
Iterable<SModel> smds;
if (myModels != null && ListSequence.fromList(myModels).isNotEmpty()) {
smds = myModels;
} else if (myModules != null && ListSequence.fromList(myModules).isNotEmpty()) {
smds = ListSequence.fromList(myModules).translate(new ITranslator2<SModule, SModel>() {
public Iterable<SModel> translate(SModule it) {
return allModelsOf(it);
}
});
} else {
smds = Sequence.fromIterable(Collections.<SModel>emptyList());
}
return new ModelsToResources(Sequence.fromIterable(smds).where(new IWhereFilter<SModel>() {
public boolean accept(SModel it) {
return SNodeOperations.isGeneratable(it);
}
})).resources(!(myCleanBuild));
}
private Iterable<SModel> allModelsOf(SModule module) {
Iterable<SModel> models = ((Iterable<SModel>) module.getModels());
if (module instanceof Language) {
Iterable<Generator> generators = ((Language) module).getGenerators();
return Sequence.fromIterable(models).concat(Sequence.fromIterable(generators).translate(new ITranslator2<Generator, SModel>() {
public Iterable<SModel> translate(Generator gen) {
return allModelsOf(gen);
}
}));
}
return models;
}
}