Merge pull request #3001 from reitermarkus/cask-conflicts-with
Implement `conflicts_with :cask`.
This commit is contained in:
		
						commit
						4b34ca5b35
					
				@ -10,25 +10,20 @@ module Hbc
 | 
			
		||||
        :java,
 | 
			
		||||
      ]
 | 
			
		||||
 | 
			
		||||
      attr_accessor(*VALID_KEYS)
 | 
			
		||||
      attr_accessor :pairs
 | 
			
		||||
      attr_reader *VALID_KEYS
 | 
			
		||||
 | 
			
		||||
      def initialize(pairs = {})
 | 
			
		||||
        @pairs = pairs
 | 
			
		||||
 | 
			
		||||
        VALID_KEYS.each do |key|
 | 
			
		||||
          instance_variable_set("@#{key}", Set.new)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        pairs.each do |key, value|
 | 
			
		||||
          raise "invalid conflicts_with key: '#{key.inspect}'" unless VALID_KEYS.include?(key)
 | 
			
		||||
          writer_method = "#{key}=".to_sym
 | 
			
		||||
          send(writer_method, value)
 | 
			
		||||
          instance_variable_set("@#{key}", instance_variable_get("@#{key}").merge([*value]))
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def to_yaml
 | 
			
		||||
        @pairs.to_yaml
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def to_s
 | 
			
		||||
        @pairs.inspect
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -17,6 +17,19 @@ module Hbc
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class CaskConflictError < AbstractCaskErrorWithToken
 | 
			
		||||
    attr_reader :conflicting_cask
 | 
			
		||||
 | 
			
		||||
    def initialize(token, conflicting_cask)
 | 
			
		||||
      super(token)
 | 
			
		||||
      @conflicting_cask = conflicting_cask
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def to_s
 | 
			
		||||
      "Cask '#{token}' conflicts with '#{conflicting_cask}'."
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  class CaskUnavailableError < AbstractCaskErrorWithToken
 | 
			
		||||
    def to_s
 | 
			
		||||
      "Cask '#{token}' is unavailable" << (reason.empty? ? "." : ": #{reason}")
 | 
			
		||||
 | 
			
		||||
@ -86,6 +86,8 @@ module Hbc
 | 
			
		||||
        raise CaskAlreadyInstalledError, @cask
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      check_conflicts
 | 
			
		||||
 | 
			
		||||
      print_caveats
 | 
			
		||||
      fetch
 | 
			
		||||
      uninstall_existing_cask if @reinstall
 | 
			
		||||
@ -98,6 +100,21 @@ module Hbc
 | 
			
		||||
      puts summary
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def check_conflicts
 | 
			
		||||
      return unless @cask.conflicts_with
 | 
			
		||||
 | 
			
		||||
      @cask.conflicts_with.cask.each do |conflicting_cask|
 | 
			
		||||
        begin
 | 
			
		||||
          conflicting_cask = CaskLoader.load(conflicting_cask)
 | 
			
		||||
          if conflicting_cask.installed?
 | 
			
		||||
            raise CaskConflictError.new(@cask, conflicting_cask)
 | 
			
		||||
          end
 | 
			
		||||
        rescue CaskUnavailableError
 | 
			
		||||
          next # Ignore conflicting Casks that do not exist.
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def reinstall
 | 
			
		||||
      odebug "Hbc::Installer#reinstall"
 | 
			
		||||
      @reinstall = true
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										23
									
								
								Library/Homebrew/test/cask/conflicts_with_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Library/Homebrew/test/cask/conflicts_with_spec.rb
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,23 @@
 | 
			
		||||
describe "conflicts_with", :cask do
 | 
			
		||||
  describe "conflicts_with cask" do
 | 
			
		||||
    let(:local_caffeine) {
 | 
			
		||||
      Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/local-caffeine.rb")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let(:with_conflicts_with) {
 | 
			
		||||
      Hbc::CaskLoader.load_from_file(TEST_FIXTURE_DIR/"cask/Casks/with-conflicts-with.rb")
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    it "installs the dependency of a Cask and the Cask itself", :focus do
 | 
			
		||||
      Hbc::Installer.new(local_caffeine).install
 | 
			
		||||
 | 
			
		||||
      expect(local_caffeine).to be_installed
 | 
			
		||||
 | 
			
		||||
      expect {
 | 
			
		||||
        Hbc::Installer.new(with_conflicts_with).install
 | 
			
		||||
      }.to raise_error(Hbc::CaskConflictError, "Cask 'with-conflicts-with' conflicts with 'local-caffeine'.")
 | 
			
		||||
 | 
			
		||||
      expect(with_conflicts_with).not_to be_installed
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -5,7 +5,7 @@ cask 'with-conflicts-with' do
 | 
			
		||||
  url "file://#{TEST_FIXTURE_DIR}/cask/caffeine.zip"
 | 
			
		||||
  homepage 'http://example.com/with-conflicts-with'
 | 
			
		||||
 | 
			
		||||
  conflicts_with formula: 'unar'
 | 
			
		||||
  conflicts_with cask: 'local-caffeine'
 | 
			
		||||
 | 
			
		||||
  app 'Caffeine.app'
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user