mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-05 00:36:16 +02:00
Added translate action.
This commit is contained in:
parent
57b3b3f677
commit
2dd11e7f1e
2 changed files with 113 additions and 0 deletions
|
@ -21,5 +21,15 @@
|
|||
version="1.0.0">
|
||||
</action>
|
||||
</extension>
|
||||
<extension
|
||||
point="org.eclipse.equinox.p2.engine.actions">
|
||||
<action
|
||||
class="org.eclipse.cdt.internal.p2.touchpoint.natives.actions.TranslateAction"
|
||||
name="org.eclipse.equinox.p2.touchpoint.natives.translate"
|
||||
touchpointType="org.eclipse.equinox.p2.native"
|
||||
touchpointVersion="1.0.0"
|
||||
version="1.0.0">
|
||||
</action>
|
||||
</extension>
|
||||
|
||||
</plugin>
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2010 Wind River Systems and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Wind River Systems - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.eclipse.cdt.internal.p2.touchpoint.natives.actions;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.eclipse.cdt.internal.p2.Activator;
|
||||
import org.eclipse.core.runtime.IStatus;
|
||||
import org.eclipse.core.runtime.Status;
|
||||
import org.eclipse.equinox.internal.p2.touchpoint.natives.actions.ActionConstants;
|
||||
import org.eclipse.equinox.p2.engine.spi.ProvisioningAction;
|
||||
|
||||
/**
|
||||
* Action for post processing files to replace strings based on the install, such
|
||||
* as install location.
|
||||
*
|
||||
* @author Doug Schaefer
|
||||
*/
|
||||
public class TranslateAction extends ProvisioningAction {
|
||||
|
||||
private static final String PARM_MAP = "map";
|
||||
|
||||
static int n;
|
||||
|
||||
@Override
|
||||
public IStatus execute(Map<String, Object> parameters) {
|
||||
// The file to process
|
||||
String targetFileName = (String)parameters.get(ActionConstants.PARM_TARGET_FILE);
|
||||
File targetFile = new File(targetFileName);
|
||||
if (!targetFile.exists())
|
||||
return new Status(IStatus.WARNING, Activator.PLUGIN_ID, targetFileName + " not found");
|
||||
|
||||
// The replacement map
|
||||
String mapString = (String)parameters.get(PARM_MAP);
|
||||
Activator.getDefault().getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID,
|
||||
"mapString:" + mapString));
|
||||
String[] mapStrings = mapString.split("!");
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
for (int i = 0; i < mapStrings.length; i += 2) {
|
||||
if (i == mapStrings.length - 1)
|
||||
// Odd number of strings
|
||||
break;
|
||||
map.put(mapStrings[i], mapStrings[i + 1]);
|
||||
Activator.getDefault().getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID,
|
||||
"map:" + mapStrings[i] + ":" + mapStrings[i + 1]));
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader(targetFile));
|
||||
File tmpFile = new File(targetFile.getParentFile(), "translate" + (n++));
|
||||
FileWriter writer = new FileWriter(tmpFile);
|
||||
|
||||
Pattern pattern = Pattern.compile("!(.*)!");
|
||||
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
||||
Matcher matcher = pattern.matcher(line);
|
||||
while (matcher.find()) {
|
||||
Activator.getDefault().getLog().log(new Status(IStatus.INFO, Activator.PLUGIN_ID,
|
||||
"match:" + matcher.group() + ":" + matcher.group(1)));
|
||||
String value = map.get(matcher.group(1));
|
||||
if (value != null) {
|
||||
line = line.replace(matcher.group(), value);
|
||||
matcher.reset(line);
|
||||
}
|
||||
}
|
||||
writer.write(line);
|
||||
writer.write('\n');
|
||||
}
|
||||
|
||||
reader.close();
|
||||
writer.close();
|
||||
|
||||
targetFile.delete();
|
||||
tmpFile.renameTo(targetFile);
|
||||
} catch (IOException e) {
|
||||
return new Status(IStatus.WARNING, Activator.PLUGIN_ID, e.getLocalizedMessage(), e);
|
||||
}
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public IStatus undo(Map<String, Object> parameters) {
|
||||
// No real undo since the file will likely be deleted.
|
||||
return Status.OK_STATUS;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue