summaryrefslogtreecommitdiff
path: root/lib/chef/resource/homebrew_tap.rb
blob: a4591e9ed8e01eec4eca4bb24e7b5c1ad4b27f3b (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
# Author:: Joshua Timberman (<jtimberman@chef.io>)
# Author:: Graeme Mathieson (<mathie@woss.name>)
#
# Copyright:: 2011-2018, Chef Software, Inc.
#
# 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.
#

require "chef/resource"
require "chef/mixin/homebrew_user"

class Chef
  class Resource
    class HomebrewTap < Chef::Resource
      resource_name :homebrew_tap
      provides(:homebrew_tap) { true }

      description "Use the homebrew_tap resource to add additional formula repositories to the Homebrew package manager."
      introduced "14.0"

      include Chef::Mixin::HomebrewUser

      property :tap_name, String,
               description: "Optional tap name to override the resource name",
               validation_message: "Homebrew tap names must be in the form REPO/TAP format!",
               regex: %r{^[\w-]+(?:\/[\w-]+)+$},
               name_property: true

      property :url, String,
               description: "The URL of the tap."

      property :full, [TrueClass, FalseClass],
               description: "Perform a full clone on the tap, as opposed to a shallow clone.",
               default: false

      property :homebrew_path, String,
               description: "The path to the Homebrew binary.",
               default: "/usr/local/bin/brew"

      property :owner, String,
               description: "The owner of the Homebrew installation.",
               default: lazy { find_homebrew_username }

      action :tap do
        description "Add a Homebrew tap."

        unless tapped?(new_resource.name)
          converge_by("tap #{new_resource.name}") do
            shell_out!("#{new_resource.homebrew_path} tap #{new_resource.full ? '--full' : ''} #{new_resource.name} #{new_resource.url || ''}",
                user: new_resource.owner,
                env:  { "HOME" => ::Dir.home(new_resource.owner), "USER" => new_resource.owner },
                cwd: ::Dir.home(new_resource.owner))
          end
        end
      end

      action :untap do
        description "Remove a Homebrew tap."

        if tapped?(new_resource.name)
          converge_by("untap #{new_resource.name}") do
            shell_out!("#{new_resource.homebrew_path} untap #{new_resource.name}",
                user: new_resource.owner,
                env:  { "HOME" => ::Dir.home(new_resource.owner), "USER" => new_resource.owner },
                cwd: ::Dir.home(new_resource.owner))
          end
        end
      end

      def tapped?(name)
        tap_dir = name.gsub("/", "/homebrew-")
        ::File.directory?("/usr/local/Homebrew/Library/Taps/#{tap_dir}")
      end
    end
  end
end