Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autoformat and reconcile to save ant task #1013

7 changes: 2 additions & 5 deletions plugins/org.eclipse.fordiac.ide.ant/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ Require-Bundle: org.eclipse.core.resources,
org.eclipse.ant.core,
org.eclipse.fordiac.ide.systemmanagement,
org.eclipse.fordiac.ide.model,
org.eclipse.fordiac.ide.model.eval,
org.eclipse.fordiac.ide.export.ui,
org.eclipse.fordiac.ide.export.forte_ng,
org.eclipse.fordiac.ide.export,
org.eclipse.fordiac.ide.typemanagement,
org.eclipse.fordiac.ide.export.xmi,
org.eclipse.fordiac.ide.library,
org.eclipse.fordiac.ide.library.model,
org.eclipse.fordiac.ide.hierarchymanager.model,
org.eclipse.fordiac.ide.hierarchymanager.ui,
org.eclipse.fordiac.ide.library
org.eclipse.fordiac.ide.hierarchymanager.ui
Bundle-Vendor: Eclipse 4diac
Bundle-ActivationPolicy: lazy
Import-Package: org.eclipse.xtext.resource;version="[2.35.0,3.0.0)"
8 changes: 8 additions & 0 deletions plugins/org.eclipse.fordiac.ide.ant/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@
name="fordiac.saveBlock">
</antTask>
</extension>
<extension
point="org.eclipse.ant.core.antTasks">
<antTask
class="org.eclipse.fordiac.ide.ant.ant.ResaveTypelib"
library="ant_tasks/ant-ant.jar"
name="fordiac.resaveTypeLib">
</antTask>
</extension>
<extension
point="org.eclipse.ant.core.antTasks">
<antTask
Expand Down
26 changes: 17 additions & 9 deletions plugins/org.eclipse.fordiac.ide.ant/scripts/buildExtraJAR.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,26 @@
<dirset dir="${basedir}/..">
<include name="org.eclipse.fordiac.ide.ant/bin"/>
<include name="org.eclipse.fordiac.ide.model/bin"/>
<include name="org.eclipse.fordiac.ide.library/bin"/>
<include name="org.eclipse.fordiac.ide.hierarchymanager/bin"/>
<include name="org.eclipse.fordiac.ide.library.model/bin"/>
<include name="org.eclipse.fordiac.ide.systemmanagement/bin"/>
<include name="org.eclipse.fordiac.ide.export/bin"/>
<include name="org.eclipse.fordiac.ide.export.forte_ng/bin"/>
<include name="org.eclipse.fordiac.ide.export.xmi/bin"/>
</dirset>
<fileset dir="${target.home}">
<include name="plugins/eclipse.fordiac.ide.ant*.jar"/>
<include name="plugins/eclipse.fordiac.ide.model*.jar"/>
<include name="plugins/org.eclipse.core.resources*.jar"/>
<include name="plugins/org.eclipse.core.runtime*.jar"/>
<include name="plugins/org.eclipse.equinox.common*.jar"/>
<include name="plugins/org.eclipse.core.jobs*.jar"/>
<include name="plugins/org.eclipse.osgi*.jar"/>
<include name="plugins/org.eclipse.ant.core*.jar"/>
<include name="plugins/org.apache.ant*/lib/*.jar"/>
<include name="plugins/org.eclipse.emf.common*.jar"/>
<include name="plugins/org.eclipse.emf.ecore*.jar"/>
<include name="plugins/org.eclipse.fordiac.ide.hierarchymanager*.jar"/>
<include name="plugins/org.eclipse.core.resources*.jar"/>
<include name="plugins/org.eclipse.core.runtime*.jar"/>
<include name="plugins/org.eclipse.equinox.common*.jar"/>
<include name="plugins/org.eclipse.core.jobs*.jar"/>
<include name="plugins/org.eclipse.osgi*.jar"/>
<include name="plugins/org.eclipse.ant.core*.jar"/>
<include name="plugins/org.apache.ant*/lib/*.jar"/>
<include name="plugins/org.eclipse.xtext*.jar"/>
</fileset>
</classpath>
<src path="src_ant/"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Primetals Technologies Austria GmbH
* Copyright (c) 2023 - 2025 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -19,6 +19,8 @@
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.fordiac.ide.model.libraryElement.FBType;
import org.eclipse.fordiac.ide.model.typelibrary.FBTypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibrary;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryManager;

public abstract class AbstractBlockModelTask extends Task {
Expand All @@ -34,27 +36,44 @@ public void setBlockname(final String blockname) {
@Override
public final void execute() throws BuildException {

final IProject fordiacProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectname);
if (fordiacProject == null) {
throw new BuildException(MessageFormat.format("Project named {0} not in workspace", projectname));//$NON-NLS-1$
}
final var fordiacProject = getFordiacProject(projectname);
final var tl = getTypeLibrary(fordiacProject, projectname);
final var te = getTypeEntry(tl, blockname);
final FBType fb = getFBType(te, blockname);

final var tl = TypeLibraryManager.INSTANCE.getTypeLibrary(fordiacProject);
if (tl == null) {
throw new BuildException(MessageFormat.format("Can not get TypeLib for {0}", projectname)); //$NON-NLS-1$
modifyBlock(fb);
}

protected static FBType getFBType(final FBTypeEntry t, final String blockname) {
final var fb = t.copyType();
if (fb == null) {
throw new BuildException(MessageFormat.format("Can not get FBType for {0}", blockname)); //$NON-NLS-1$
}
return fb;
}

protected static FBTypeEntry getTypeEntry(final TypeLibrary tl, final String blockname) {
final var t = tl.getFBTypeEntry(blockname);
if (t == null) {
throw new BuildException(MessageFormat.format("Can not get FBTypeEntry for {0}", blockname)); //$NON-NLS-1$
}
return t;
}

final FBType fb = t.getTypeEditable();
if (fb == null) {
throw new BuildException(MessageFormat.format("Can not get FBType for {0}", blockname)); //$NON-NLS-1$
protected static TypeLibrary getTypeLibrary(final IProject fordiacProject, final String projectname) {
final var tl = TypeLibraryManager.INSTANCE.getTypeLibrary(fordiacProject);
if (tl == null) {
throw new BuildException(MessageFormat.format("Can not get TypeLib for {0}", projectname)); //$NON-NLS-1$
}
return tl;
}

modifyBlock(fb);
protected static IProject getFordiacProject(final String projectname) {
final var fordiacProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectname);
if (fordiacProject == null) {
throw new BuildException(MessageFormat.format("Project named {0} not in workspace", projectname));//$NON-NLS-1$
}
return fordiacProject;
}

protected abstract void modifyBlock(final FBType fb);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2025 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Ernst Blecha - initial implementation and/or documentation
*******************************************************************************/
package org.eclipse.fordiac.ide.ant.ant;

import java.text.MessageFormat;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class ResaveTypelib extends Task {

public void setAutoFormat(final boolean enable) {
this.autoformat = enable;
}

public void setProjectname(final String projectname) {
this.projectname = AbstractBlockModelTask.nullCheckString(projectname);
}

@Override
public final void execute() throws BuildException {
final var fordiacProject = AbstractBlockModelTask.getFordiacProject(projectname);
final var tl = AbstractBlockModelTask.getTypeLibrary(fordiacProject, projectname);

log(MessageFormat.format("Save {0}", projectname)); //$NON-NLS-1$
tl.getAllTypes().forEach(te -> SaveBlock.saveBlock(te, autoformat, this::log));
}

protected String projectname = AbstractBlockModelTask.EMPTY_STRING;

private boolean autoformat = false;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2023 Primetals Technologies Austria GmbH
* Copyright (c) 2023 - 2025 Primetals Technologies Austria GmbH
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
Expand All @@ -9,25 +9,110 @@
*
* Contributors:
* Ernst Blecha - initial implementation and/or documentation
* Ernst Blecha - run reconciler and autoformat during saving
*******************************************************************************/
package org.eclipse.fordiac.ide.ant.ant;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.function.Consumer;

import org.apache.tools.ant.BuildException;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.fordiac.ide.model.libraryElement.FBType;
import org.eclipse.fordiac.ide.model.typelibrary.TypeEntry;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryTags;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.resource.IResourceServiceProvider;
import org.eclipse.xtext.resource.SaveOptions;
import org.eclipse.xtext.resource.XtextResourceSet;

public class SaveBlock extends AbstractBlockModelTask {

public void setAutoFormat(final boolean enable) {
this.autoformat = enable;
}

@Override
protected void modifyBlock(final FBType fb) {
log(MessageFormat.format("Save {0}/{1}", projectname, blockname)); //$NON-NLS-1$
saveBlock(fb.getTypeEntry(), autoformat, this::log);
}

private boolean autoformat = false;

public static void saveBlock(final TypeEntry te, final boolean autoformat, final Consumer<String> logger) {
try {
fb.getTypeEntry().save(fb);
log(MessageFormat.format("Save {0}/{1}", projectname, blockname)); //$NON-NLS-1$
} catch (final CoreException e) {
final IFile typeFile = te.getFile();

if (typeFile.isLinked(IResource.CHECK_ANCESTORS)) {
// do not modify files stored outside the project that are only linked in
return;
}

logger.accept(te.getFullTypeName());

final URI sourceUri = URI.createPlatformResourceURI(typeFile.getFullPath().toString(), true);
final Resource resource = getResourceSet(te).getResource(sourceUri, true);

final SaveOptions.Builder optionsBuilder = SaveOptions.newBuilder();
if (autoformat) {
optionsBuilder.format();
}
resource.save(optionsBuilder.getOptions().toOptionsMap());
} catch (final IOException e) {
throw new BuildException(e);
}
}

private static final URI SYNTHETIC_URI_FBT = URI.createURI("__synthetic.stalg"); //$NON-NLS-1$
private static final IResourceServiceProvider SERVICE_PROVIDER_FBT = IResourceServiceProvider.Registry.INSTANCE
.getResourceServiceProvider(SYNTHETIC_URI_FBT);

private static final URI SYNTHETIC_URI_FCT = URI.createURI("__synthetic.stfunc"); //$NON-NLS-1$
private static final IResourceServiceProvider SERVICE_PROVIDER_FCT = IResourceServiceProvider.Registry.INSTANCE
.getResourceServiceProvider(SYNTHETIC_URI_FCT);

private static final URI SYNTHETIC_URI_GCF = URI.createURI("__synthetic.globalconsts"); //$NON-NLS-1$
private static final IResourceServiceProvider SERVICE_PROVIDER_GCF = IResourceServiceProvider.Registry.INSTANCE
.getResourceServiceProvider(SYNTHETIC_URI_GCF);

private static ResourceSet getResourceSet(final TypeEntry te) {
XtextResourceSet resourceSet;

if (te.getFile().getFullPath().getFileExtension().equalsIgnoreCase(TypeLibraryTags.FC_TYPE_FILE_ENDING)) {
resourceSet = SERVICE_PROVIDER_FCT.get(XtextResourceSet.class);
} else if (te.getFile().getFullPath().getFileExtension()
.equalsIgnoreCase(TypeLibraryTags.GLOBAL_CONST_FILE_ENDING)) {
resourceSet = SERVICE_PROVIDER_GCF.get(XtextResourceSet.class);
} else {
resourceSet = SERVICE_PROVIDER_FBT.get(XtextResourceSet.class);
}

resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FB_TYPE_FILE_ENDING.toLowerCase(), //
SERVICE_PROVIDER_FBT.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FB_TYPE_FILE_ENDING, //
SERVICE_PROVIDER_FBT.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FC_TYPE_FILE_ENDING.toLowerCase(), //
SERVICE_PROVIDER_FCT.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FC_TYPE_FILE_ENDING, //
SERVICE_PROVIDER_FCT.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING.toLowerCase(), //
SERVICE_PROVIDER_GCF.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING, //
SERVICE_PROVIDER_GCF.get(IResourceFactory.class));

return resourceSet;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.fordiac.ide.globalconstantseditor.ui.internal.GlobalconstantseditorActivator;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryTags;
import org.eclipse.fordiac.ide.systemmanagement.SystemManager;
import org.eclipse.xtext.resource.IResourceFactory;
import org.eclipse.xtext.ui.resource.IResourceSetInitializer;
Expand All @@ -28,8 +29,12 @@ public class GlobalConstantsResourceSetInitializer implements IResourceSetInitia
public void initialize(final ResourceSet resourceSet, final IProject project) {
if (has4diacProjectNature(project)) {
final IResourceFactory resourceFactory = getInjector().getInstance(IResourceFactory.class);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("gcf", resourceFactory); //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("GCF", resourceFactory); //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING.toLowerCase(), //
resourceFactory);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING, //
resourceFactory);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.fordiac.ide.globalconstantseditor.globalConstants.STGlobalConstsSource;
import org.eclipse.fordiac.ide.globalconstantseditor.resource.GlobalConstantsResource;
import org.eclipse.fordiac.ide.model.libraryElement.GlobalConstants;
import org.eclipse.fordiac.ide.model.typelibrary.TypeLibraryTags;
import org.eclipse.fordiac.ide.structuredtextcore.util.STCoreParseUtil;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.resource.IResourceFactory;
Expand Down Expand Up @@ -60,13 +61,17 @@ private static IParseResult parseInternal(final GlobalConstants type, final List
private static IParseResult parseInternal(final GlobalConstants type, final List<Issue> issues) {
final XtextResourceSet resourceSet = (XtextResourceSet) SERVICE_PROVIDER_GCF.get(ResourceSet.class);
resourceSet.getLoadOptions().put(ResourceDescriptionsProvider.PERSISTED_DESCRIPTIONS, Boolean.TRUE);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("gcf", //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING.toLowerCase(), //
SERVICE_PROVIDER_GCF.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("GCF", //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.GLOBAL_CONST_FILE_ENDING, //
SERVICE_PROVIDER_GCF.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("fct", //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FC_TYPE_FILE_ENDING.toLowerCase(), //
SERVICE_PROVIDER_FCT.get(IResourceFactory.class));
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("FCT", //$NON-NLS-1$
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
TypeLibraryTags.FC_TYPE_FILE_ENDING, //
SERVICE_PROVIDER_FCT.get(IResourceFactory.class));
final GlobalConstantsResource resource = (GlobalConstantsResource) SERVICE_PROVIDER_GCF
.get(XtextResource.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ public class HierachyManagerUpdateListener extends QualNameChangeListener {
private static EObject loadPlantHierachy(final IProject project) {
final Map<String, Object> loadOptions = new HashMap<>();
final ResourceSet hierarchyResouceSet = new ResourceSetImpl();
hierarchyResouceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
.put(PlantHierarchyView.PLANT_HIERARCHY_FILE_NAME_EXTENSION, new HierarchyResourceFactoryImpl());
hierarchyResouceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
PlantHierarchyView.PLANT_HIERARCHY_FILE_NAME_EXTENSION, //
new HierarchyResourceFactoryImpl());
hierarchyResouceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put( //
PlantHierarchyView.PLANT_HIERARCHY_FILE_NAME_EXTENSION.toLowerCase(), //
new HierarchyResourceFactoryImpl());
loadOptions.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
final XMLMapImpl map = new XMLMapImpl();
map.setNoNamespacePackage(HierarchyPackage.eINSTANCE);
Expand Down
Loading
Loading