function getCode()

in Extensions/ExternalTfs/Src/Tasks/DownloadArtifactsTfsVersionControl/downloadTfsVersionControl.js [85:183]


function getCode() {
    var workspaceName = getWorkspaceName();
    var workspaceMappings = getDefaultTfvcMappings();

    var newWorkspace = {
        name: workspaceName,
        mappings: []
    };

    console.log("Try deleting workspace if it already exists");
    return tfvcw.deleteWorkspace(newWorkspace)
        .then(function(retCode) {
            if (retCode === 0) {
                console.log("Successfully deleted workspace");
            }
            if (IsPathExists(downloadPath)) {
                console.log("Cleaning up artifacts download path");
                return utilExec('rm -fr ' + downloadPath)
                    .then(function(ret) {
                        if (ret.code === 0) {
                            console.log("Successfully cleaned up artifacts download path");
                        }
                        return Q(ret.code);
                    });
            } else {
                return Q(0);
            }
        }, function(error) {
            console.log("Warning: Failed to delete Workspace. Ignoring it as this could happen due to non existent workspace. " + error);
            if (IsPathExists(downloadPath)) {
                console.log("Artifacts download path exists. Cleaning up");
                return utilExec('rm -fr ' + downloadPath)
                    .then(function(ret) {
                        if (ret.code === 0) {
                            console.log("Successfully cleaned up artifacts download path");
                        }
                        return Q(ret.code);
                    });
            } else {
                return Q(0);
            }
        })
        .then(function() {
            ensurePathExist(downloadPath);
            shell.cd(downloadPath);
            console.log("Creating new workspace: " + newWorkspace.name);
            return tfvcw.newWorkspace(newWorkspace)
                .then(function(retCode) {
                    if (retCode === 0) {
                        console.log("Successfully created workspace");
                    }
                    return Q(retCode);
                }, function(error) {
                    tl.error("Failed to Create a new Workspace. " + error);
                    tl.exit(1);
                });
        })
        .then(function() {
            // workspace must exist now
            // Sometime the job fails with:
            //   An argument error occurred: Unable to determine the workspace. 
            //   You may be able to correct this by running 'tf workspaces -collection:TeamProjectCollectionUrl'.
            // when getting the source.  Preemptively run this to be safe.
            console.log("List workspaces");
            tfvcw.listWorkspaces();

            console.log("Current working directory: " + process.cwd());
            console.log("Add default workspace mappings: " + JSON.stringify(workspaceMappings));
            return tfvcw.mapFolder(workspaceMappings.serverPath, workspaceMappings.localPath, newWorkspace)
                .then(function(retCode) {
                    if (retCode === 0) {
                        console.log("Successfully added default mapping");
                    }
                    return Q(retCode);
                }, function(error) {
                    tl.error("Failed to add default mapping. " + error);
                    tl.exit(1);
                });
        })
        .then(function() {
            shell.cd(downloadPath);
            console.log("Sync workspace: " + newWorkspace.name);
            
            if (!changesetId) {
                console.log("Getting latest changeset as no changeset is specified");
            }

            return tfvcw.get(changesetId)
                .then(function(retCode) {
                    if (retCode === 0) {
                        console.log("Successfully synced workspace");
                    }
                    return Q(retCode);
                }, function(error) {
                    tl.error("Failed to sync workspace. " + error);
                    tl.exit(1);
                });
        });
};