HowTo: Using Beyond Compare to copy only changed generated code
To compare files between two directories the tool Beyond Compare can be helpful. This tool can be executed from the (Windows) console to compare directories and copy new or changed files. This article shows how Beyond Compare can be integrated in an Ant script.
Motivation
When using a generator to generate source code the generated sources will differ from previously generated sources only if the model or a template has changed. Only a subset of the generated sources has changed in a "relevant" way. E.g. if an attribute has added to one entity all entities will be generated, but only the sources for the changed entity will be different. Therefore all generated sources will be copied to the source directory. When using a source repository like CVS also all sources will be checked in. A commit message will only be useful for the changed sources and the revision history will grow unnessecary for any other sources. As a conclusion it is desirable to recognize relevant changes to copy only these files.With Beyond Compare it is possible to define rules that help to recognize irrelevant changes, depending on file types.
Import / Export of Beyond Compare Settings
Beyond Compare allows importing and exporting of settings. This is helpful when working in a team, so that common settings can be used from each developer. Settings can be imported / exported in the Extras menu. The wizards are self-explaining. Settings are stored in XML files or in a ZIP file when exporting all settings (preferences, sessions and rules).Beyond Compare Scripting
Beyond Compare can be controlled by scripts. Therefore the tool has a set of script commands. These commands and the usage of the scripts can be read in the BC online help.To execute a script the script filename is passed as the first argument to BC2.exe with the Prefix '@':
BC2.exe @<Filename>
Scripts can use parameters which are referenced by %1 - %9 like in Windows shell scripts. Environment variables can be accessed by %VAR%.
Script example for comparing and copying files
This section shows a script which can be used to recognize relevant changes and copy these files to a source directory. The requirements for this script are as follows:- Two directories including subdirectories are compared. The left directory is the one with the generator output. The right contains the (previously generated) project sources. The structure of both directories are identical.
- All relevant changed files of the left directory should be selected, i.e. files that have changed or exist only in the left directory.
- Selected files are copied from left to right directory.
# INPUT:
# %1: Generator output directory
# %2: Source directory (workspace)
# Turn logging on
log normal "compare.log"
# Load Session and change settings
load "ALLIS_VS"
# Load base folders
load "%1" "%2"
# Include all subdirectories
expand all
# Compare the files by rules
compare rules-based
# Select newer, different generated files
select lt.newer.files lt.orphan.files
# Copy generated files to source directory
copy lt->rt
Integration in Ant script
The following code example demonstrates an Ant target which calls Beyond Compare to achieve the requirements mentioned above. The required properties are:- BEYONDCOMPARE_SCRIPT: Path to the BC script (see above)
- BEYONDCOMPARE_HOME: Beyond Compare install dir
- OUTPUT_PATH: Path to generator output ("left" directory)
- WORKSPACE_PATH: Source path, in this case an Eclipse workspace ("right" directory)
<target name="copy_generated_beyondcompare" if="BEYONDCOMPARE_HOME">
<exec executable="${BEYONDCOMPARE_HOME}/bc2.exe" dir="${basedir}">
<arg value="@${BEYONDCOMPARE_SCRIPT}"/>
<arg value="${OUTPUT_PATH}"/>
<arg value="${WORKSPACE_PATH}"/>
</exec>
</target>

