<commit_before>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2011 Opscode, 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.
#

action :run do
  begin
    script_file.puts(@new_resource.code)
    script_file.close
    # always set the ExecutionPolicy flag
    # see http://technet.microsoft.com/en-us/library/ee176961.aspx
    # Powershell will hang if stdin is redirected
    # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
    flags = "#{@new_resource.flags} -ExecutionPolicy RemoteSigned -inputformat none -Command".strip

    # cwd hax...shell_out on windows needs to support proper 'cwd'
    # follow CHEF-2357 for more
    cwd = @new_resource.cwd ? "cd #{@new_resource.cwd} & " : ""
    command = "#{cwd}#{@new_resource.interpreter} #{flags} \"#{build_powershell_scriptblock}\""

    execute.command(command)
    execute.creates(@new_resource.creates)
    execute.user(@new_resource.user)
    execute.group(@new_resource.group)
    execute.timeout(@new_resource.timeout)
    execute.returns(@new_resource.returns)
    execute.run_action(:run)
  ensure
    unlink_script_file
  end
end

private

def execute
  @execute ||= Chef::Resource::Execute.new(@new_resource.name, run_context)
end

def script_file
  @script_file ||= Tempfile.open(['chef-script', '.ps1'])
end

def unlink_script_file
  @script_file && @script_file.close!
end

# take advantage of PowerShell scriptblocks
# to pass scoped environment variables to the
# command
def build_powershell_scriptblock
  # environment var hax...shell_out on windows needs to support proper 'environment'
  # follow CHEF-2358 for more
  env_string = if @new_resource.environment
    @new_resource.environment.inject("") {|a, (k,v)| a << "$#{k} = '#{v}'; "; a}
  else
    ""
  end
  "& { #{env_string}#{ensure_windows_friendly_path(script_file.path)} }"
end

def ensure_windows_friendly_path(path)
  if path
    path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)
  else
    path
  end
end<commit_msg>Take advantage of native Win32 support for `cwd` and `environment` in Chef 0.10.8+.<commit_after>#
# Author:: <NAME> (<<EMAIL>>)
# Copyright:: Copyright (c) 2011-2012 Opscode, 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.
#

action :run do
  begin
    # render a tempfile
    script_file.puts(@new_resource.code)
    script_file.close

    # default flags
    flags = [
      # always set the ExecutionPolicy flag
      # see http://technet.microsoft.com/en-us/library/ee176961.aspx
      "-ExecutionPolicy RemoteSigned",
      # Powershell will hang if STDIN is redirected
      # http://connect.microsoft.com/PowerShell/feedback/details/572313/powershell-exe-can-hang-if-stdin-is-redirected
      "-InputFormat none"
    ]

    # user-provided flags
    unless @new_resource.flags.nil? || @new_resource.flags.empty?
      flags << @new_resource.flags.strip
    end

    cwd = ensure_windows_friendly_path(@new_resource.cwd)
    prefix = @new_resource.interpreter
    command = ensure_windows_friendly_path(script_file.path)

    # Chef::Resource::Execute in Chef >= 0.10.8 has first-class Win32 support
    if Gem::Version.create(Chef::VERSION) >= Gem::Version.create("0.10.8")
      execute.cwd(cwd)
      execute.environment(@new_resource.environment)
    else
      # we have to fake `cwd` and `environment` on older versions of Chef
      prefix = "cd #{@new_resource.cwd} & #{prefix}" if @new_resource.cwd
      command = create_env_wrapper(command, @new_resource.environment)
    end

    command = "#{prefix} #{flags.join(' ')} -Command \"#{command}\""

    execute.command(command)
    execute.creates(@new_resource.creates)
    execute.user(@new_resource.user)
    execute.group(@new_resource.group)
    execute.timeout(@new_resource.timeout)
    execute.returns(@new_resource.returns)
    execute.run_action(:run)
  ensure
    unlink_script_file
  end
end

private

def execute
  @execute ||= Chef::Resource::Execute.new(@new_resource.name, run_context)
end

def script_file
  @script_file ||= Tempfile.open(['chef-script', '.ps1'])
end

def unlink_script_file
  @script_file && @script_file.close!
end

# take advantage of PowerShell scriptblocks
# to pass scoped environment variables to the
# command.  This is mainly only useful for versions
# of Chef < 0.10.8 when Chef::Resource::Execute
# did not support the 'environment' attribute.
def create_env_wrapper(command, environment)
  if environment
    env_string = environment.map{ |k,v| "$env:#{k}='#{v}'" }.join('; ')
    "& { #{env_string}; #{command} }"
  else
    command
  end
end

def ensure_windows_friendly_path(path)
  if path
    path.gsub(::File::SEPARATOR, ::File::ALT_SEPARATOR)
  else
    path
  end
end
{'fraction_non_alphanumeric': 0.11633919338159256, 'fraction_numerical': 0.01344364012409514, 'mean_word_length': 4.037326388888889, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '878538', 'n_tokens_mistral': 1871, 'n_tokens_neox': 1801, 'n_words': 500}