/*
 * Copyright 2019 the original author or authors.
 *
 * 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.
 */

package org.gradle.internal.vfs;

import net.rubygrapefruit.platform.Native;
import net.rubygrapefruit.platform.internal.jni.OsxFileEventFunctions;
import org.gradle.internal.vfs.impl.WatcherEvent;
import org.gradle.internal.vfs.watch.FileWatcherRegistry;
import org.gradle.internal.vfs.watch.FileWatcherRegistryFactory;
import org.gradle.internal.vfs.watch.WatchRootUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class DarwinFileWatcherRegistry extends AbstractEventDrivenFileWatcherRegistry {
    private static final Logger LOGGER = LoggerFactory.getLogger(DarwinFileWatcherRegistry.class);

    public DarwinFileWatcherRegistry(Set<Path> watchRoots) {
        super(events -> Native.get(OsxFileEventFunctions.class)
            .startWatching(
                watchRoots.stream()
                    .map(Path::toString)
                    .collect(Collectors.toList()),
                // TODO Figure out a good value for this
                300, TimeUnit.MICROSECONDS,
                (type, path) -> events.add(WatcherEvent.createEvent(type, path))
            ));
    }

    public static class Factory implements FileWatcherRegistryFactory {
        @Override
        public FileWatcherRegistry startWatching(SnapshotHierarchy snapshotHierarchy, Predicate<String> watchFilter, Collection<String> mustWatchDirectories) {
            Set<String> directories = WatchRootUtil.resolveDirectoriesToWatch(snapshotHierarchy, watchFilter, mustWatchDirectories);
            Set<Path> watchRoots = WatchRootUtil.resolveRootsToWatch(directories);
            LOGGER.warn("Watching {} directory hierarchies to track changes between builds in {} directories", watchRoots.size(), directories.size());
            return new DarwinFileWatcherRegistry(watchRoots);
        }
    }
}

@@ -644,16 +644,63 @@
 l.vfs;%0A%0A
+import com.google.common.collect.ImmutableSet;%0A
 import n
@@ -1088,16 +1088,37 @@
 ctory;%0A%0A
+import java.io.File;%0A
 import j
@@ -2219,24 +2219,246 @@
 ectories) %7B%0A
+            Set%3CString%3E mustWatchDirectoryPrefixes = ImmutableSet.copyOf(%0A                mustWatchDirectories.stream()%0A                    .map(path -%3E path + File.separator)%0A                    ::iterator%0A            );%0A
             
@@ -2523,16 +2523,33 @@
 ToWatch(
+%0A                
 snapshot
@@ -2558,29 +2558,137 @@
 erarchy,
- watchFilter,
+%0A                path -%3E watchFilter.test(path) %7C%7C startsWithAnyPrefix(path, mustWatchDirectoryPrefixes),%0A               
  mustWat
@@ -3008,16 +3008,298 @@
        %7D
+%0A%0A        private static boolean startsWithAnyPrefix(String path, Set%3CString%3E prefixes) %7B%0A            for (String prefix : prefixes) %7B%0A                if (path.startsWith(prefix)) %7B%0A                    return true;%0A                %7D%0A            %7D%0A            return false;%0A        %7D
 %0A    %7D%0A%7D

Improve watching on macOS a little bit