Wednesday, October 15, 2014

Ax 2012 TFS Auto Synchronization Script

A script to synchronize objects from TFS server

static void SysVersionControlSynchronization(Args _args)
{
    SysVersionControlSynchronization SysVersionControlSynchronization;
    SysVersionControlSyncParameters syncParm;
    SysVersionControllable controllable;
    SysVersionControlSystemFileBased sysVersionControlSystem;
    SysVersionControlTmpChange processedSyncElements;
    SysVersionControlSynchronizeBatchNum batchNum;
    Filename filename;
    SysVersionControlSynchronizeCommand command;
    boolean noInfo = false;
    Set folderSetSelected;
    Set folderSet;
    SetEnumerator folderSetEnum;
    Map resultMap;
    MapEnumerator mapEnum;
    boolean isVCSDefFile = true;
    SysTreeNodeContainer contControllable;
    RecordInsertList syncEntries;
    SysVersionControlFilebasedBackEnd backend;
    SysVersionControlRepositoryFolder   folder;
    int         i;
    container   folderCon = ["CUS"]; // model names to be syn
    ;

    SysVersionControlSynchronization = SysVersionControlSynchronization::construct();

    if (!SysVersionControlParameters::isVCSEnabled())
    {
        throw error("@SYS135983" + ' ' + strFmt("@SYS120745", "@SYS85914"), '', SysInfoAction_Formrun::newFormname(formStr(SysVersionControlParametersDev)));
    }

    if (versioncontrol &&
        versioncontrol.parmSysVersionControlSystem() &&
        !versioncontrol.parmSysVersionControlSystem().supportSynchronization())
    {
        throw error("@SYS112528");
    }

    // one batchNum for the whole syn
    batchNum = SysVersionControlSynchronizeLog::nextBatchNum();

    // When client synchronizes the VCSDef must be synchronized as well for getting latest
    // updates of models.
    versioncontrol.init();

    // Synchronize files for each folder
    for (i = 1; i<= conLen(folderCon); i++)
    {
        sysVersionControlSystem = versioncontrol.parmSysVersionControlSystem();

        if (sysVersionControlSystem)
        {
            // folder in the TFS client side
            folder = SysVersionControlParameters::find().RepositoryFolder + "\\" + SysVersionControlParameters::find().AppRoot + "\\" + conPeek(folderCon,i);

            syncParm = SysVersionControlSyncParameters::construct();
            syncParm.parmSilent(false);
            syncParm.parmForce(false); // To do , change here if "force" is required
            syncParm.parmDeleteLocalObjects(false);

            syncParm.parmSyncAll(true);

            // Evaluate if controllable is special for setting additional parameters.
            if (controllable && isVCSDefFile)
            {
                syncParm.parmSilent(true);
            }

            if (syncParm.parmSyncAll())
            {
                // Union the folders for models and additional folders
                folderSet = new Set(Types::String);
                folderSet.add(folder);
                backend = sysVersionControlSystem.parmBackend();
                // download the xpo files
                resultMap = backend.folderSetSynchronize(folderSet, syncParm);
            }

            // Process synchronized files
            if (resultMap) //Sync succeeded
            {
                mapEnum = resultMap.getEnumerator();
                syncEntries = new RecordInsertList(tableNum(SysVersionControlSynchronizeLog));

                while (mapEnum.moveNext())
                {
                    filename = mapEnum.currentKey();
                    command = mapEnum.currentValue();

                    controllable = SysVersionControlTmpItem::newControllable(sysVersionControlSystem.filename2ItemPath(filename), filename);

                    syncEntries.add(SysVersionControlSynchronizeLog::initLogEntry(
                            filename,
                            controllable,
                            command,
                            batchNum,
                            folder));

                    SysVersionControlTmpItem::releaseControllable(controllable);
                }

                // Commit sync-entries
                syncEntries.insertDatabase();

                if (resultMap.elements() && !syncParm.parmSkipImport())
                {
                    //
                    // Something to update
                    // Import xpos into AOT and then compile and synchornize
                    //
                    SysVersionControlSynchronizeLog::processBatchNum(batchNum);
                }

            }
            else
            {
                throw error("@SYS85688");
            }
        }
    }
}