summaryrefslogtreecommitdiff
path: root/ext/win32-eventlog/Rakefile
blob: a4f032d04e6923cff8e7dd4e29a367a5a5079581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require "rubygems"
require "rake"
require "mkmf"

desc "Building event log dll"

def ensure_present(commands)
  commands.each do |c|
    unless find_executable c
      warn "Could not find #{c}. Windows Event Logging will not correctly function."
    end
  end
end

EVT_MC_FILE = "chef-log.man".freeze
EVT_RC_FILE = "chef-log.rc".freeze
EVT_RESOURCE_OBJECT = "resource.o".freeze
EVT_SHARED_OBJECT = "chef-log.dll".freeze
MC = "windmc".freeze
RC = "windres".freeze
CC = "gcc".freeze

ensure_present [MC, RC, CC]

task build: [EVT_RESOURCE_OBJECT, EVT_SHARED_OBJECT]
task default: %i{build register}

file EVT_RC_FILE => EVT_MC_FILE do
  sh "#{MC} #{EVT_MC_FILE}"
end

file EVT_RESOURCE_OBJECT => EVT_RC_FILE do
  sh "#{RC} -i #{EVT_RC_FILE} -o #{EVT_RESOURCE_OBJECT}"
end

file EVT_SHARED_OBJECT => EVT_RESOURCE_OBJECT do
  sh "#{CC} -o #{EVT_SHARED_OBJECT} -shared #{EVT_RESOURCE_OBJECT}"
end

task register: EVT_SHARED_OBJECT do
  require "win32/eventlog"
  dll_file = File.expand_path(EVT_SHARED_OBJECT)
  begin
    Win32::EventLog.add_event_source(
      source: "Application",
      key_name: "Chef",
      event_message_file: dll_file,
      category_message_file: dll_file
    )
  rescue Errno::EIO => e
    puts "Skipping event log registration due to missing privileges: #{e}"
  end
end