import { CvsSupportProvider } from "./cvsprovider" ; import { TYPES } from "../bll/utils/constants" ; import { CheckInInfo } from "../bll/entities/checkininfo" ; import { Uri , window , workspace } from "vscode" ; import { inject , injectable } from "inversify" ; import { Logger } from "../bll/utils/logger" ; import { MessageConstants } from "../bll/utils/messageconstants" ; import { Utils } from "../bll/utils/utils" ; import { GitProviderActivator } from "./git/GitProviderActivator" ; import { Settings } from "../bll/entities/settings" ; import { Context } from "../view/Context" ; import { GitProvider } from "./git/GitProvider" ; import { TfvcProviderActivator } from "./tfs/TfvcProviderActivator" ; @ injectable ( ) export class CvsProviderProxy { private actualProviders : CvsSupportProvider [ ] = [ ] ; private readonly isGitSupported : boolean ; constructor ( @ inject ( TYPES . GitProviderActivator ) private readonly gitProviderActivator , @ inject ( TYPES . TfvcProviderActivator ) private readonly tfvcProviderActivator , @ inject ( TYPES . Settings ) private readonly mySettings , @ inject ( TYPES . Context ) context ) { const rootPaths = this . collectAllRootPaths ( ) ; this . isGitSupported = mySettings . isGitSupported ( ) ; if ( this . isGitSupported ) { Logger . logWarning ( "Experimental git support is enabled." ) ; } else { Logger . logInfo ( "Experimental git support is disabled." ) ; } this . detectCvsProviders ( rootPaths ) . then ( ( detectedCvsProviders ) => { this . actualProviders = detectedCvsProviders ; if ( this . actualProviders . length > 0 ) { context . setShowRemoteRunButton ( true ) ; } } ) ; } private collectAllRootPaths ( ) { const rootPaths = [ ] ; const workspaceFolders = workspace . workspaceFolders ; if ( workspaceFolders && workspaceFolders . length !== 0 ) { workspaceFolders . forEach ( ( workspaceFolder ) => { rootPaths . push ( workspaceFolder . uri ) ; } ) ; } return rootPaths ; } private async detectCvsProviders ( rootPaths ) { const providers = [ ] ; for ( let i = 0 ; i < rootPaths . length ; i ++ ) { providers . push . apply ( providers , await this . detectProvidersInDirectory ( rootPaths [ i ] ) ) ; } return providers ; } private async detectProvidersInDirectory ( rootPath ) { const providers = [ ] ; const tfvcProvider = await this . tfvcProviderActivator . tryActivateInPath ( rootPath ) ; if ( tfvcProvider ) { providers . push ( tfvcProvider ) ; Logger . logInfo ( `Tfvc provider was activated for ${ rootPath . fsPath }` ) ; } else { Logger . logWarning ( `Could not activate tfvc provider for ${ rootPath . fsPath }` ) ; } if ( ! this . isGitSupported ) { return providers ; } const gitProvider = await this . gitProviderActivator . tryActivateInPath ( rootPath ) ; if ( gitProvider ) { providers . push ( gitProvider ) ; Logger . logInfo ( `Git provider was activated for ${ rootPath . fsPath }` ) ; } else { Logger . logWarning ( `Could not activate git provider for ${ rootPath . fsPath }` ) ; } return providers ; } public async getFormattedFileNames ( checkInArray ) { const formattedFileNames = [ ] ; if ( ! checkInArray ) { return [ ] ; } checkInArray . forEach ( ( checkInInfo ) => { checkInInfo . getFormattedFileNames ( ) . forEach ( ( fileName ) => { formattedFileNames . push ( fileName ) ; } ) ; } ) ; return formattedFileNames ; } public async getRequiredCheckInInfo ( ) { const result = [ ] ; this . ensureProvidersExist ( ) ; for ( let i = 0 ; i < this . actualProviders . length ; i ++ ) { const provider = this . actualProviders [ i ] ; try { const checkInInfo = await provider . getRequiredCheckInInfo ( ) ; if ( checkInInfo . cvsLocalResources . length > 0 ) { result . push ( checkInInfo ) ; } } catch ( err ) { Logger . logError ( Utils . formatErrorMessage ( err ) ) ; } } return result ; } public async requestForPostCommit ( checkInArray ) { const commitMessage = await CvsProviderProxy . getUpdatedCommitMessages ( checkInArray ) ; this . setUpdatedCommitMessages ( checkInArray , commitMessage ) ; checkInArray . forEach ( ( checkInInfo ) => { checkInInfo . getCvsProvider ( ) . commit ( checkInInfo ) ; } ) ; } public hasGitProvider ( ) { for ( const provider of this . actualProviders ) { if ( provider instanceof GitProvider ) { return true ; } } return false ; } public async getGitBranch ( ) { const results = [ ] ; for ( const provider of this . actualProviders ) { if ( provider instanceof GitProvider ) { results . push ( await provider . getRepoBranchName ( ) ) ; } } if ( results . length === 0 ) { throw new Error ( "No branch name was collected!" ) ; } else if ( results . length === 1 || this . isAllElementsTheSame ( results ) ) { return results [ 0 ] ; } else { throw new Error ( "There are more then one git branch, this case is currently unsupported." ) ; } } private isAllElementsTheSame ( anyArray ) { return ! ! anyArray . reduce ( ( a , b ) => { return ( a === b ) ? a : NaN ; } ) ; } private static async getUpdatedCommitMessages ( checkInArray ) { let commitMessage ; if ( checkInArray . length > 0 ) { const previousMessage = checkInArray [ 0 ] . message ; commitMessage = await window . showInputBox ( { prompt : MessageConstants . PROVIDE_MESSAGE_FOR_COMMIT , value : previousMessage } ) ; } return commitMessage || "" ; } private setUpdatedCommitMessages ( checkInArray , commitMessage ) { checkInArray . forEach ( ( checkInInfo ) => { checkInInfo . message = commitMessage ; } ) ; } private ensureProvidersExist ( ) { if ( this . actualProviders . length === 0 ) { throw new Error ( "No cvs provider found" ) ; } } }