Using XMLDIff with Visual Studio Code
Introduction
This document describes how to use XMLDiff with Visual Studio Code.
Prerequisites
- Visual Studio Code
- XMLDiff.exe and RunXMLDiff.bat from this repository
- VSCode extension Run on Save by emeraldwalk
Steps
- Prepare a folders structure like the following:
RootExtensionFolder
│XMLDiffAndPatch
││XMLDiff.exe
│RunXMLDiff.bat
│aiscripts
│aiscripts.modified
│aiscripts.original
│md
│md.modified
│md.original
- You can skip creation *.original folders, if you will use the extracted files folder.
- Copy the RunXMLDiff.bat file to the root of your project.
- Copy the XMLDiff.exe file to the XMLDiffAndPatch folder in the root of your project.
- Install the VSCode extension Run on Save by emeraldwalk
- Open the settings of the extension by pressing Ctrl + , and search for runOnSave.commands
- Add the following configuration to the settings:
- In case of not to use the "Extracted" files folder, i.e. with *.original folders:
"commands": [
{
"match": "\\.modified\\\\.+?\\.xml$",
"cmd": "${workspaceFolder}\\RunXMLDiff.bat ${file} ${workspaceFolder}\\XMLDiffAndPatch\\XMLDiff.exe
}
]
- With the extracted folder, which is outside the project folder:
"commands": [
{
"match": "\\.modified\\\\.+?\\.xml$",
"cmd": "${workspaceFolder}\\RunXMLDiff.bat ${file} ${workspaceFolder}\\XMLDiffAndPatch\\XMLDiff.exe" ${workspaceFolder}\\..\\extracted"
}
]
Take in account that with the extracted folder you still can use the *.original folders for original files. It will be used first, if they exist, otherwise the script will use the extracted folder.
- In case of not to use the "Extracted" files folder, i.e. with *.original folders:
- Save the settings and close the settings tab.
Now you can modify the *.modified files and the RunXMLDiff.bat will be executed automatically on save.
You can check an output in the OUTPUT tab in the VSCode, selecting the Run on Save item in the dropdown list.
Description of the RunXMLDiff.bat
RunXMLDiff.bat is a batch script that compares a modified XML file with its original version using the XMLDiff.exe utility. The script takes the paths to the modified file, the XMLDiff utility, and optionally the path to the original files as input. It then constructs the appropriate paths, checks for the existence of necessary files and directories, and runs the XMLDiff utility to generate a diff file.
Usage
RunXMLDiff.bat <ModifiedFilePath> <XMLDiffPath> [OriginalFilesPath]
Parameters
- <ModifiedFilePath>: The full path to the modified XML file. This parameter is mandatory.
- <XMLDiffPath>: The full path to the XMLDiff.exe utility. This parameter is mandatory.
- [OriginalFilesPath]: The full path to the directory containing the original XML files. This parameter is optional.
Example
RunXMLDiff.bat "C:\path\to\md.modified\file.xml" "C:\path\to\XMLDiff.exe" "C:\path\to\original\files"
How It Works
- Extract Type and Filename: The script extracts the type (e.g., aiscript or md) and filename from the modified file path.
- Determine Target File Path: The script constructs the target file path by removing .modified from the modified file path.
- Determine Original File Path:
- If the OriginalFilesPath is provided, the original file path is constructed as OriginalFilesPath\type\filename.
- If the OriginalFilesPath is not provided, the original file path is constructed by replacing .modified with .original in the modified file path.
- Check Existence:
- The script checks for the existence of XMLDiff.exe, the modified file, the original file, and the target file directory.
- Run XMLDiff: If all checks pass, the script runs XMLDiff.exe with the appropriate parameters to generate the diff file.
Error Handling
The script will output error messages and exit with a non-zero status code if any of the following conditions are met:
- XMLDiff.exe is not found at the specified path.
- The modified file is not found at the specified path.
- The original file is not found at the constructed path.
- The target file directory does not exist.
Output
If the script runs successfully, it will output a message indicating that the XMLDiff operation completed successfully and provide the path to the generated diff file.
Notes
- Ensure that the XMLDiff.exe utility is accessible and has the necessary permissions to execute.
- The script assumes that the modified file path follows the convention where the last folder in the path is of the form type.modified.
- If you have some beautification plugins for XML use the alternative save (Ctrl + K S, i.e. "Save without Formatting), which will not involve them, to get the diff as small as possible.
Demo
Small demo video
License
This script is provided "as-is" without any warranty. Use it at your own risk.
0 comments