Skip to content

Commit

Permalink
Merge pull request #104 from chef/jfm/moved_powershell_here
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmccrae authored Jan 13, 2022
2 parents be36dc9 + c587fac commit 510e5b5
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 41 deletions.
5 changes: 5 additions & 0 deletions chef-powershell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# hide vendor files
[Vv]endor/

# No gem files either!
*.gem
22 changes: 11 additions & 11 deletions chef-powershell/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
PATH
remote: .
specs:
chef-powershell (1.0.5)
chef-powershell (1.0.10)
ffi (~> 1.15)
ffi-yajl (~> 2.4)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
chefstyle (2.1.2)
rubocop (= 1.22.3)
chefstyle (2.2.0)
rubocop (= 1.24.1)
coderay (1.1.3)
diff-lcs (1.4.4)
diff-lcs (1.5.0)
fauxhai-ng (9.1.0)
net-ssh
ffi (1.15.4-x64-mingw32)
ffi (1.15.5-x64-mingw32)
ffi-yajl (2.4.0)
libyajl2 (>= 1.2)
libyajl2 (2.1.0)
method_source (1.0.0)
net-ssh (6.1.0)
parallel (1.21.0)
parser (3.0.2.0)
parser (3.1.0.0)
ast (~> 2.4.1)
pry (0.14.1)
coderay (~> 1.1)
method_source (~> 1.0)
rainbow (3.0.0)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.1.1)
regexp_parser (2.2.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand All @@ -44,16 +44,16 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.3)
rubocop (1.22.3)
rubocop (1.24.1)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.12.0, < 2.0)
rubocop-ast (>= 1.15.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.13.0)
rubocop-ast (1.15.1)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
unicode-display_width (2.1.0)
Expand Down
6 changes: 3 additions & 3 deletions chef-powershell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ The Chef-PowerShell gem provides in-process access to the PowerShell engine.
`powershell_exec` is initialized with a string that should be set to the script
to run and also takes an optional interpreter argument which must be either
:powershell (Windows PowerShell which is the default) or :pwsh (PowerShell
Core). It will return a Chef_PowerShell::PowerShell object that provides 5 methods:
Core). It will return a ChefPowerShell::PowerShell object that provides 5 methods:

.result - returns a hash representing the results returned by executing the
PowerShell script block
Expand All @@ -37,7 +37,7 @@ Core). It will return a Chef_PowerShell::PowerShell object that provides 5 metho
.error? - returns true if there were error messages written to the PowerShell
error stream during execution

.error! - raise Chef_PowerShell::PowerShellExceptions::PowerShellCommandFailed if there was an error
.error! - raise ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed if there was an error

Some examples of usage:

Expand Down Expand Up @@ -85,7 +85,7 @@ WIN32OLERuntimeError: (in OLE method `ExecuteScript': )
```ruby
require "chef-powershell"
...
include Chef_PowerShell::ChefPowerShell::PowerShellExec
include ChefPowerShell::ChefPowerShell::PowerShellExec
...
def join_command
cmd = ""
Expand Down
2 changes: 1 addition & 1 deletion chef-powershell/chef-powershell.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require "chef-powershell/version"

Gem::Specification.new do |spec|
spec.name = "chef-powershell"
spec.version = ChefPowerShell::VERSION
spec.version = ChefPowerShellModule::VERSION
spec.authors = ["Chef Software, Inc"]
spec.email = ["[email protected]"]

Expand Down
4 changes: 2 additions & 2 deletions chef-powershell/lib/chef-powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

require_relative "chef-powershell/powershell_exec"

module ChefPowerShell
include Chef_PowerShell::ChefPowerShell::PowerShellExec
module ChefPowerShellModule
include ChefPowerShell::ChefPowerShellModule::PowerShellExec
end
2 changes: 1 addition & 1 deletion chef-powershell/lib/chef-powershell/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

class Chef_PowerShell
class ChefPowerShell
class PowerShellExceptions
class PowerShellCommandFailed < RuntimeError; end

Expand Down
6 changes: 3 additions & 3 deletions chef-powershell/lib/chef-powershell/json_compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class << self
def parse(source, opts = {})
FFI_Yajl::Parser.parse(source, opts)
rescue FFI_Yajl::ParseError => e
raise Chef_PowerShell::PowerShellExceptions::JSON::ParseError, e.message
raise ChefPowerShell::PowerShellExceptions::JSON::ParseError, e.message
end

def from_json(source, opts = {})
Expand All @@ -40,7 +40,7 @@ def from_json(source, opts = {})
# 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 Chef_PowerShell::PowerShellExceptions::JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
raise ChefPowerShell::PowerShellExceptions::JSON::ParseError, "Top level JSON object must be a Hash or Array. (actual: #{obj.class})"
end

obj
Expand All @@ -49,7 +49,7 @@ def from_json(source, opts = {})
def to_json(obj, opts = nil)
FFI_Yajl::Encoder.encode(obj, opts)
rescue FFI_Yajl::EncodeError => e
raise Chef_PowerShell::PowerShellExceptions::JSON::EncodeError, e.message
raise ChefPowerShell::PowerShellExceptions::JSON::EncodeError, e.message
end

def to_json_pretty(obj, opts = nil)
Expand Down
6 changes: 3 additions & 3 deletions chef-powershell/lib/chef-powershell/powershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
require_relative "unicode"
# require "chef-powershell"

class Chef_PowerShell
class ChefPowerShell
class PowerShell

attr_reader :result
Expand Down Expand Up @@ -58,10 +58,10 @@ def error?
end

#
# @raise [Chef_PowerShell::PowerShellExceptions::PowerShellCommandFailed] raise if the command failed
# @raise [ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed] raise if the command failed
#
def error!
raise Chef_PowerShell::PowerShellExceptions::PowerShellCommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
raise ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
end

module PowerMod
Expand Down
12 changes: 6 additions & 6 deletions chef-powershell/lib/chef-powershell/powershell_exec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# powershell_exec is initialized with a string that should be set to the script
# to run and also takes an optional interpreter argument which must be either
# :powershell (Windows PowerShell which is the default) or :pwsh (PowerShell
# Core). It will return a Chef_PowerShell::PowerShell object that provides 5 methods:
# Core). It will return a ChefPowerShell::PowerShell object that provides 5 methods:
#
# .result - returns a hash representing the results returned by executing the
# PowerShell script block
Expand Down Expand Up @@ -95,8 +95,8 @@
# credentials of the user running Chef Client are used.
#

class Chef_PowerShell
module ChefPowerShell
class ChefPowerShell
module ChefPowerShellModule
module PowerShellExec
# The Chef.PowerShell.Wrapper.dll file looks in the same folder as ruby.exe OR in the folder specified by the environment variable CHEF_POWERSHELL_BIN for other chef powershell dll's
# We don't want to move files around so we're setting the variable here to keep everything tidy.
Expand All @@ -113,16 +113,16 @@ module PowerShellExec
def powershell_exec(script, interpreter = :powershell, timeout: -1)
case interpreter
when :powershell
Chef_PowerShell::PowerShell.new(script, timeout: timeout)
ChefPowerShell::PowerShell.new(script, timeout: timeout)
when :pwsh
Chef_PowerShell::Pwsh.new(script, timeout: timeout)
ChefPowerShell::Pwsh.new(script, timeout: timeout)
else
raise ArgumentError, "Expected interpreter of :powershell or :pwsh"
end
end

# The same as the #powershell_exec method except this will raise
# Chef_PowerShell::PowerShellExceptions::PowerShellCommandFailed if the command fails
# ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed if the command fails
def powershell_exec!(script, interpreter = :powershell, **options)
cmd = powershell_exec(script, interpreter, **options)
cmd.error!
Expand Down
4 changes: 2 additions & 2 deletions chef-powershell/lib/chef-powershell/pwsh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

class Chef_PowerShell
class Pwsh < Chef_PowerShell::PowerShell
class ChefPowerShell
class Pwsh < ChefPowerShell::PowerShell
# Run a command under pwsh (powershell core) via FFI
# This implementation requires the managed dll, native wrapper and a
# published, self contained dotnet core directory tree to exist in the
Expand Down
4 changes: 2 additions & 2 deletions chef-powershell/lib/chef-powershell/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

module ChefPowerShell
module ChefPowerShellModule
CHEFPOWERSHELL_ROOT = File.expand_path("..", __dir__)
VERSION = "1.0.5"
VERSION = "1.0.10"
end
14 changes: 7 additions & 7 deletions chef-powershell/spec/unit/powershell_exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

require "chef-powershell"

describe Chef_PowerShell::ChefPowerShell::PowerShellExec, :windows_only do
let(:powershell_mixin) { Class.new { include Chef_PowerShell::ChefPowerShell::PowerShellExec } }
describe ChefPowerShell::ChefPowerShellModule::PowerShellExec, :windows_only do
let(:powershell_mixin) { Class.new { include ChefPowerShell::ChefPowerShellModule::PowerShellExec } }
subject(:object) { powershell_mixin.new }

before do
Expand All @@ -30,7 +30,7 @@
describe "#powershell_exec" do
context "not specifying an interpreter" do
it "runs a basic command and returns a Chef::PowerShell object" do
expect(object.powershell_exec("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(Chef_PowerShell::PowerShell)
expect(object.powershell_exec("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(ChefPowerShell::PowerShell)
end

it "uses less than version 7" do
Expand All @@ -41,7 +41,7 @@

context "using pwsh interpreter" do
it "runs a basic command and returns a Chef::PowerShell object" do
expect(object.powershell_exec("$PSVersionTable", :pwsh, timeout: -1)).to be_kind_of(Chef_PowerShell::Pwsh)
expect(object.powershell_exec("$PSVersionTable", :pwsh, timeout: -1)).to be_kind_of(ChefPowerShell::Pwsh)
end

it "uses greater than version 6" do
Expand All @@ -52,7 +52,7 @@

context "using powershell interpreter" do
it "runs a basic command and returns a Chef::PowerShell object" do
expect(object.powershell_exec("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(Chef_PowerShell::PowerShell)
expect(object.powershell_exec("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(ChefPowerShell::PowerShell)
end

it "uses less than version 6" do
Expand Down Expand Up @@ -80,11 +80,11 @@

describe "#powershell_exec!" do
it "runs a basic command and returns a Chef::PowerShell object" do
expect(object.powershell_exec!("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(Chef_PowerShell::PowerShell)
expect(object.powershell_exec!("$PSVersionTable", :powershell, timeout: -1)).to be_kind_of(ChefPowerShell::PowerShell)
end

it "raises an error if the command fails" do
expect { object.powershell_exec!("this-should-error") }.to raise_error(Chef_PowerShell::PowerShellExceptions::PowerShellCommandFailed)
expect { object.powershell_exec!("this-should-error") }.to raise_error(ChefPowerShell::PowerShellExceptions::PowerShellCommandFailed)
end

it "raises an error if the interpreter is invalid" do
Expand Down

0 comments on commit 510e5b5

Please sign in to comment.