From a66c53641e3fa4c7edefe6fd6961ada9db038158 Mon Sep 17 00:00:00 2001 From: John McCrae Date: Tue, 18 Jan 2022 12:59:26 -0800 Subject: [PATCH] Updated the gem to call FFI_yajl directly for json support. Signed-off-by: John McCrae --- .expeditor/manual_gem_release.ps1 | 16 ++--- VERSION | 2 +- .../lib/chef-powershell/json_compat.rb | 63 ------------------- .../lib/chef-powershell/powershell.rb | 7 +-- .../lib/chef-powershell/version.rb | 2 +- 5 files changed, 13 insertions(+), 77 deletions(-) delete mode 100644 chef-powershell/lib/chef-powershell/json_compat.rb diff --git a/.expeditor/manual_gem_release.ps1 b/.expeditor/manual_gem_release.ps1 index 4675cf93..d4c5061f 100644 --- a/.expeditor/manual_gem_release.ps1 +++ b/.expeditor/manual_gem_release.ps1 @@ -16,12 +16,19 @@ $ErrorActionPreference = "Stop" $project_name = "chef-powershell" +Write-Output "--- Cleaning up old Hab directories for a minty fresh build experience" +# Is there a c:\hab directory? If so, nuke it. +if (Test-Path -Path c:\hab) { + Remove-Item -LiteralPath c:\hab -Recurse -Force #-ErrorAction SilentlyContinue +} +Write-Output "`r" + Write-Output "--- Making sure we're in the correct spot" $project_root = (Get-ChildItem c:\ -Recurse | Where-Object { $_.PSIsContainer -and $_.Name.EndsWith($("$project_name-shim")) } | Select-Object -First 1).FullName Set-Location -Path $project_root Write-Output "`r" -Write-Output "-- Is Habitat actually installed?" +Write-Output "--- Is Habitat actually installed?" # Is hab installed? if (-not (Get-Command -Name Hab -ErrorAction SilentlyContinue)) { Write-Output "--- No, Installing Habitat via Choco" @@ -31,13 +38,6 @@ if (-not (Get-Command -Name Hab -ErrorAction SilentlyContinue)) { } Write-Output "`r" -Write-Output "--- Cleaning up old Hab directories for a minty fresh build experience" -# Is there a c:\hab directory? If so, nuke it. -if (Test-Path -Path c:\hab) { - Remove-Item -LiteralPath c:\hab -Recurse -Force -ErrorAction SilentlyContinue -} -Write-Output "`r" - Write-Output "--- Comparing local version to published version, updating the local version as appropriate" try { $file = (Get-Content $("$project_root\chef-powershell\lib\chef-powershell\version.rb")) diff --git a/VERSION b/VERSION index ece61c60..492b167a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.6 \ No newline at end of file +1.0.12 \ No newline at end of file diff --git a/chef-powershell/lib/chef-powershell/json_compat.rb b/chef-powershell/lib/chef-powershell/json_compat.rb deleted file mode 100644 index 4ae677d4..00000000 --- a/chef-powershell/lib/chef-powershell/json_compat.rb +++ /dev/null @@ -1,63 +0,0 @@ -# -# Author:: Tim Hinderliter () -# Copyright:: Copyright (c) Chef Software Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Wrapper class for interacting with JSON. - -autoload :FFI_Yajl, "ffi_yajl" -require_relative "exceptions" -# We're requiring this to prevent breaking consumers using Hash.to_json -require "json" unless defined?(JSON) - -class Chef - class JSONCompat - - class << self - - def parse(source, opts = {}) - FFI_Yajl::Parser.parse(source, opts) - rescue FFI_Yajl::ParseError => e - raise ChefPowerShell::PowerShellExceptions::JSON::ParseError, e.message - end - - def from_json(source, opts = {}) - obj = parse(source, opts) - - # JSON gem requires top level object to be a Hash or Array (otherwise - # you get the "must contain two octets" error). Yajl doesn't impose the - # same limitation. For compatibility, we re-impose this condition. - unless obj.is_a?(Hash) || obj.is_a?(Array) - raise ChefPowerShell::PowerShellExceptions::JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})" - end - - obj - end - - def to_json(obj, opts = nil) - FFI_Yajl::Encoder.encode(obj, opts) - rescue FFI_Yajl::EncodeError => e - raise ChefPowerShell::PowerShellExceptions::JSON::EncodeError, e.message - end - - def to_json_pretty(obj, opts = nil) - options_map = { pretty: true } - options_map[:indent] = opts[:indent] if opts.respond_to?(:key?) && opts.key?(:indent) - to_json(obj, options_map).chomp - end - - end - end -end diff --git a/chef-powershell/lib/chef-powershell/powershell.rb b/chef-powershell/lib/chef-powershell/powershell.rb index 713790f3..cf4a0eb4 100644 --- a/chef-powershell/lib/chef-powershell/powershell.rb +++ b/chef-powershell/lib/chef-powershell/powershell.rb @@ -16,10 +16,9 @@ # limitations under the License. require "ffi" unless defined?(FFI) -require_relative "json_compat" +autoload :FFI_Yajl, "ffi_yajl" require_relative "exceptions" require_relative "unicode" -# require "chef-powershell" class ChefPowerShell class PowerShell @@ -98,8 +97,8 @@ def exec(script, timeout: -1) PowerMod.set_ps_command(script) execution = PowerMod.do_work output = execution.read_utf16string - hashed_outcome = Chef::JSONCompat.parse(output) - @result = Chef::JSONCompat.parse(hashed_outcome["result"]) + hashed_outcome = FFI_Yajl::Parser.parse(output) + @result = FFI_Yajl::Parser.parse(hashed_outcome["result"]) @errors = hashed_outcome["errors"] @verbose = hashed_outcome["verbose"] end diff --git a/chef-powershell/lib/chef-powershell/version.rb b/chef-powershell/lib/chef-powershell/version.rb index dde15310..6b6001c9 100644 --- a/chef-powershell/lib/chef-powershell/version.rb +++ b/chef-powershell/lib/chef-powershell/version.rb @@ -16,5 +16,5 @@ module ChefPowerShellModule CHEFPOWERSHELL_ROOT = File.expand_path("..", __dir__) - VERSION = "1.0.10" + VERSION = "1.0.12" end