import * as pako from "pako" ; import { Logger } from "./logger" ; import { Change } from "../entities/change" ; import { Constants } from "./constants" ; import { Project } from "../entities/project" ; import { BuildConfig } from "../entities/buildconfig" ; import { CvsResource } from "../entities/cvsresources/cvsresource" ; import * as path from "path" ; export class Utils { param value return public static parseValueColonValue ( value ) { const KEY_SEPARATOR = ":" ; if ( value === undefined || ! value . indexOf ( KEY_SEPARATOR ) ) { Logger . logWarning ( `VsCodeUtils#parseValueColonValue: value ${ value } wasn't parsed` ) ; return undefined ; } const keys = value . split ( KEY_SEPARATOR ) ; return keys . length !== 2 ? undefined : keys ; } public static gzip2Xml ( gzip ) { Logger . logDebug ( `VsCodeUtils#gzip2Str: starts unzipping gzip` ) ; const STEP = 100000 ; const buffer = [ ] ; const inflatedGzip = pako . inflate ( gzip ) ; for ( let i = 0 ; i < inflatedGzip . byteLength ; i = i + STEP ) { const topIndex = Math . min ( i + STEP , inflatedGzip . byteLength ) ; buffer . push ( String . fromCharCode . apply ( null , new Uint16Array ( inflatedGzip . slice ( i , topIndex ) ) ) ) ; } Logger . logDebug ( `VsCodeUtils#gzip2Str: finishes unzipping gzip` ) ; return buffer . join ( "" ) ; } param err public static formatErrorMessage ( err ) { if ( ! err || ! err . message ) { return err || "" ; } let formattedMsg = err . message ; if ( err . stderr ) { formattedMsg = `${ formattedMsg } ${ err . stderr }` ; } return formattedMsg ; } public static formatErrorMessageForLogging ( err ) { if ( ! err ) { return "" ; } let formattedMsg = err . message ? err . message : err ; if ( err . stderr ) { formattedMsg = `${ formattedMsg } ${ err . stderr }` ; } if ( err . stack ) { formattedMsg = `${ formattedMsg } ${ err . stack }` ; } return formattedMsg ; } param arr param fn public static uniqBy < T > ( arr , fn ) { if ( ! arr ) { return [ ] ; } const seen = Object . create ( null ) ; return arr . filter ( ( el ) => { const key = fn ( el ) ; if ( seen [ key ] ) { return false ; } seen [ key ] = true ; return true ; } ) ; } /**
     * This method generates uniq UUID in the format "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".
     */ public static uuidv4 ( ) { return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" . replace ( /[xy]/g , function ( c ) { const r = Math . random ( ) * 16 | 0 ; const v = c === "x" ? r : ( r & 0x3 | 0x8 ) ; return v . toString ( 16 ) ; } ) ; } param ms public static sleep ( ms ) { return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ; } public static clearArray ( array ) { array . length = 0 ; } public static formChangeLabel ( change ) { const descriptionLines = change . myDescription . split ( "\n" ) ; const firstDescriptionLine = descriptionLines [ 0 ] . trim ( ) ; const firstLineCharacters = firstDescriptionLine . substring ( 0 , 30 ) ; const shouldAddDots = ( descriptionLines . length > 1 ) || ( firstDescriptionLine !== firstLineCharacters ) ; let shownDescription = firstLineCharacters + ( shouldAddDots ? "..." : "" ) ; shownDescription = shownDescription ? shownDescription : "<no comment>" ; const myVersionControlName = change . myVersionControlName ; const changesCount = `${ change . myChangesCount } file${ change . myChangesCount === 0 ? "s" : "" }` ; return `${ shownDescription }\n${ myVersionControlName } | ${ changesCount }` ; } public static formChangeTooltip ( change ) { const description = change . myDescription ? change . myDescription : "<no comment>" ; const myVersionControlName = change . myVersionControlName ; const changesCount = `${ change . myChangesCount } file${ change . myChangesCount === 0 ? "s" : "" }` ; const displayVersion = change . isPersonal ? "|" : ( "| " + change . displayVersion + " |" ) ; return `${ description }\n${ myVersionControlName } ${ displayVersion } ${ changesCount }` ; } public static createTargetName ( url , username ) { const encryptedUrl = new Buffer ( url , "utf8" ) . toString ( "hex" ) ; const encryptedUsername = new Buffer ( username , "utf8" ) . toString ( "hex" ) ; return encryptedUrl + Constants . TARGET_NAME_SEPARATOR + encryptedUsername ; } public static tryParseTargetName ( targetName ) { if ( ! targetName ) { return undefined ; } let result ; try { result = this . parseTargetName ( targetName ) ; } catch ( err ) { Logger . logWarning ( `tryParseTargetName: ${ Utils . formatErrorMessage ( err ) }` ) ; result = undefined ; } return result ; } private static parseTargetName ( targetName ) { if ( ! targetName ) { return undefined ; } const segments = targetName . split ( Constants . TARGET_NAME_SEPARATOR ) ; const url = new Buffer ( segments [ 0 ] , "hex" ) . toString ( "utf8" ) ; const username = new Buffer ( segments [ 1 ] , "hex" ) . toString ( "utf8" ) ; return { url : url , username : username } ; } static flattenBuildConfigArray ( projects ) { if ( ! projects ) { return [ ] ; } const result = [ ] ; projects . forEach ( ( project ) => { result . push ( ... this . extractBuildConfigs ( project ) ) ; } ) ; return result ; } private static extractBuildConfigs ( project ) { const result = [ ] ; project . children . forEach ( ( child ) => { if ( child instanceof BuildConfig ) { result . push ( child ) ; } if ( child instanceof Project ) { result . push ( ... this . extractBuildConfigs ( child ) ) ; } } ) ; return result ; } public static getNormalizedRelativePath ( cvsResource , workspaceRootPath ) { const notNormalizedRelativePath = path . relative ( workspaceRootPath , cvsResource . fileAbsPath ) ; return notNormalizedRelativePath . replace ( /\\/g , "/" ) ; } }