2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.jrubyscripting.internal.watch;
15 import static java.nio.file.StandardWatchEventKinds.ENTRY_CREATE;
16 import static java.nio.file.StandardWatchEventKinds.ENTRY_DELETE;
17 import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
19 import java.nio.file.Path;
20 import java.nio.file.WatchEvent;
21 import java.util.Arrays;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.service.AbstractWatchService;
30 * @author Cody Cutrer - Initial contribution
33 public class JRubyGemWatchService extends AbstractWatchService {
35 private static final String GEMSPEC = ".gemspec";
37 private JRubyDependencyTracker dependencyTracker;
39 JRubyGemWatchService(String path, JRubyDependencyTracker dependencyTracker) {
41 this.dependencyTracker = dependencyTracker;
45 protected boolean watchSubDirectories() {
50 protected WatchEvent.Kind<?> @Nullable [] getWatchEventKinds(Path path) {
51 return new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY };
55 protected void processWatchEvent(WatchEvent<?> watchEvent, WatchEvent.Kind<?> kind, Path path) {
56 String file = path.toFile().getName();
57 if (file.endsWith(GEMSPEC)) {
58 // This seems really lazy, but you can't definitively tell the name
59 // of a gem from the gemspec's filename. It's simply too ambiguous with version
60 // numbers and platforms allowed to have `-` and `_` characters as well. RubyGems
61 // doesn't do it either - it either has the name already, and searches for
62 // `<name>-*.gemspec`, or it completely lists the all files on disk. Either way
63 // it then executes the gemspec to get full details. We can't do that here in
64 // pure Java and without a JRubyEngine available. So just punt and invalidate
65 // _all_ subsets of hyphens. Worst case we invalidate a "parent" gem that didn't
66 // need to be invalidated, but oh well, that just means a script reloads sometimes
67 // when it didn't absolutely need to.
68 String[] parts = file.split("-");
69 for (int i = 0; i < parts.length - 1; ++i) {
70 dependencyTracker.dependencyChanged("gem:" + String.join("-", Arrays.copyOf(parts, i + 1)));