<commit_before>
$buildDir = [System.IO.Path]::GetFullPath($args[0])
$chefPackageName = $args[1]
$sourceDir = [System.IO.Path]::GetFullPath($args[2])
$machineOS = $args[3]
$machineArch = $args[4]
$chefVersion = $args[5]

function Chef-Get-ScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}

$scriptDir = Chef-Get-ScriptDirectory

$chefExtensionRoot = [System.IO.Path]::GetFullPath("$scriptDir\\..")

$installerDir= "$chefExtensionRoot\\ChefExtensionHandler\\installer"

function Download-Chef-Client-Pkg
{
  if ( !(Test-Path $installerDir) ) {
    write-host "Chef Installer directory not found, creating $installerDir."
    mkdir $installerDir
  } else {
    # TODO: If chef installer already exist, and its version and user specified version both are same then don't download/remove existance installer
    Remove-Item $installerDir\\*
  }

  $localPath = "$installerDir\\chef-client-latest.msi"

  $remoteUrl="https://www.opscode.com/chef/download?p=windows&pv=$machineOS&m=$machineArch"

  if ( $chefVersion -ne $null ) {
    $remoteUrl += "&v=$chefVersion"
  }


  $webClient = new-object System.Net.WebClient
  write-host "remoteUrl::::$remoteUrl::::localPath:::$localPath"
  $webClient.DownloadFile($remoteUrl, $localPath)
  if ( $? -eq $True ) {
    write-host "Download via PowerShell succeeded"
  } else {
    write-host "Failed to download $remoteUrl. with status code $LASTEXITCODE"
    exit 1
  }
}

# Courtesy http://blogs.msdn.com/b/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx
function Add-Zip
{
  param([string]$zipfilename)

  if(-not (test-path($zipfilename)))
  {
    echo "Creating zip file $zipfilename..."
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false
  }

  $shellApplication = new-object -com shell.application
  $zipPackage = $shellApplication.NameSpace($zipfilename)

  foreach($file in $input)
  {
    echo "Adding $file to the zip"
    $zipPackage.CopyHere($file.FullName)

    while($zipPackage.Items().Item($file.Name) -Eq $null) {
        start-sleep -seconds 1
        write-host "." -nonewline
    }
  }
  echo "Created zip successfully."
}

write-host "Downloading chef client package..."
Download-Chef-Client-Pkg

# Main
if ( !(Test-Path $buildDir) ) {
  echo "Build directory not found, creating $buildDir."
  mkdir $buildDir
}

echo "Creating package from $sourceDir..."
Get-ChildItem "$sourceDir" | Add-Zip "$buildDir\$chefPackageName"<commit_msg>Added changes to check existing chef-client installer version during  rake build task<commit_after>
$buildDir = [System.IO.Path]::GetFullPath($args[0])
$chefPackageName = $args[1]
$sourceDir = [System.IO.Path]::GetFullPath($args[2])
$machineOS = $args[3]
$machineArch = $args[4]

if ($args[5] -ne $null) {
  $chefVersion = $args[5]
  $chefVersion = ($chefVersion -split '\.|\-')
  $chefVersion = ($chefVersion[0..2] -join("."))+"-$($chefVersion[3])"
}

function Chef-Get-ScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}

$scriptDir = Chef-Get-ScriptDirectory

$chefExtensionRoot = [System.IO.Path]::GetFullPath("$scriptDir\\..")

$installerDir= "$chefExtensionRoot\\ChefExtensionHandler\\installer"

function Get-Property ($Object, $PropertyName, [object[]]$ArgumentList) {
  return $Object.GetType().InvokeMember($PropertyName, 'Public, Instance, GetProperty', $null, $Object, $ArgumentList)
}

function Invoke-Method ($Object, $MethodName, $ArgumentList) {
  return $Object.GetType().InvokeMember($MethodName, 'Public, Instance, InvokeMethod', $null, $Object, $ArgumentList)
}

function Get-Chef-Installer-Version($Path) {
  $msiOpenDatabaseModeReadOnly = 0
  $installer = New-Object -ComObject WindowsInstaller.Installer
  $database = Invoke-Method $installer OpenDatabase  @($Path, $msiOpenDatabaseModeReadOnly)
  $view = Invoke-Method $database OpenView @("SELECT Value FROM Property WHERE Property='ProductVersion'")
  Invoke-Method $view Execute
  $record = Invoke-Method $view Fetch
  $productVersion = $null

  if ($record) {
    $productVersion = (Get-Property $record StringData 1)
  }

  Invoke-Method $view Close @()
  Remove-Variable -Name record, view, database, installer

  $productVersion
}

function Download-Chef-Client-Pkg
{
  $localPath = "$installerDir\\chef-client-latest.msi"
  $remoteUrl="https://www.opscode.com/chef/download?p=windows&pv=$machineOS&m=$machineArch"

  if ( !(Test-Path $installerDir) ) {
    Write-Host "Chef Installer directory not found, creating $installerDir."
    mkdir $installerDir
  }

  if ( (Test-Path $localPath) -and $chefVersion -ne $null -and (Get-Chef-Installer-Version $localPath) -eq ($chefVersion -replace("-", ".")) ) {
    Write-Host "Chef Client installer already exist, skipping downloading..."
  } else {

    # TODO: Fix issue: Unable to remove existing file because another process using it
    Remove-Item -Force -path "$installerDir\\*"

    if ( $chefVersion -ne $null ) {
      $remoteUrl += "&v=$chefVersion"
    }

    $webClient = new-object System.Net.WebClient
    $webClient.DownloadFile($remoteUrl, $localPath)

    if ( $? -eq $True ) {
      Write-Host "Download via PowerShell succeeded"
    } else {
      Write-Host "Failed to download $remoteUrl. with status code $LASTEXITCODE"
      exit 1
    }
  }
}

# Courtesy http://blogs.msdn.com/b/daiken/archive/2007/02/12/compress-files-with-windows-powershell-then-package-a-windows-vista-sidebar-gadget.aspx
function Add-Zip
{
  param([string]$zipfilename)

  if(-not (test-path($zipfilename)))
  {
    echo "Creating zip file $zipfilename..."
    set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
    (dir $zipfilename).IsReadOnly = $false
  }

  $shellApplication = new-object -com shell.application
  $zipPackage = $shellApplication.NameSpace($zipfilename)

  foreach($file in $input)
  {
    echo "Adding $file to the zip"
    $zipPackage.CopyHere($file.FullName)

    while($zipPackage.Items().Item($file.Name) -Eq $null) {
        start-sleep -seconds 1
        Write-Host "." -nonewline
    }
  }
  echo "Created zip successfully."
}

Write-Host "Downloading chef client package..."
Download-Chef-Client-Pkg

# Main
if ( !(Test-Path $buildDir) ) {
  echo "Build directory not found, creating $buildDir."
  mkdir $buildDir
}

echo "Creating package from $sourceDir..."
Get-ChildItem "$sourceDir" | Add-Zip "$buildDir\$chefPackageName"
{'fraction_non_alphanumeric': 0.1502361724820966, 'fraction_numerical': 0.007618467164406522, 'mean_word_length': 4.193037974683544, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5769333', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2154, 'n_words': 471}