]> git.openstreetmap.org Git - chef.git/blob - cookbooks/nodejs/providers/package.rb
Qualify property access
[chef.git] / cookbooks / nodejs / providers / package.rb
1 #
2 # Cookbook Name:: nodejs
3 # Provider:: package
4 #
5 # Copyright 2013, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #     http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 require "chef/mixin/shell_out"
21 require "json"
22
23 include Chef::Mixin::ShellOut
24
25 use_inline_resources
26
27 def load_current_resource
28   @packages = JSON.parse(shell_out("npm list --global --json").stdout)["dependencies"] || {}
29
30   @current_resource = Chef::Resource::NodejsPackage.new(new_resource.name)
31   @current_resource.package_name(new_resource.package_name)
32   if (package = @packages[@current_resource.package_name])
33     @current_resource.version(package["version"])
34   end
35   @current_resource
36 end
37
38 action :install do
39   package_name = if new_resource.version
40                    "#{new_resource.package_name}@#{new_resource.version}"
41                  else
42                    new_resource.package_name
43                  end
44
45   if !@packages.include?(new_resource.package_name)
46     converge_by "install #{package_name}" do
47       shell_out!("npm install --global #{package_name}")
48     end
49   elsif new_resource.version &&
50         new_resource.version != @current_resource.version
51     converge_by "update #{package_name}" do
52       shell_out!("npm install --global #{package_name}")
53     end
54   end
55 end
56
57 action :upgrade do
58   if @packages.include?(new_resource.package_name)
59     converge_by "update #{new_resource.package_name}" do
60       shell_out!("npm update --global #{new_resource.package_name}")
61     end
62   end
63 end
64
65 action :remove do
66   if @packages.include?(new_resource.package_name)
67     converge_by "remove #{new_resource.package_name}" do
68       shell_out!("npm remove --global #{new_resource.package_name}")
69     end
70   end
71 end