]> git.basschouten.com Git - openhab-addons.git/blob
e72b6a53356bdedf18c2828d5dc867a1338f3804
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jrubyscripting.internal.watch;
14
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;
18
19 import java.nio.file.Path;
20 import java.nio.file.WatchEvent;
21 import java.util.Arrays;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.service.AbstractWatchService;
26
27 /**
28  * Watches a gem home
29  *
30  * @author Cody Cutrer - Initial contribution
31  */
32 @NonNullByDefault
33 public class JRubyGemWatchService extends AbstractWatchService {
34
35     private static final String GEMSPEC = ".gemspec";
36
37     private JRubyDependencyTracker dependencyTracker;
38
39     JRubyGemWatchService(String path, JRubyDependencyTracker dependencyTracker) {
40         super(path);
41         this.dependencyTracker = dependencyTracker;
42     }
43
44     @Override
45     protected boolean watchSubDirectories() {
46         return true;
47     }
48
49     @Override
50     protected WatchEvent.Kind<?> @Nullable [] getWatchEventKinds(Path path) {
51         return new WatchEvent.Kind<?>[] { ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY };
52     }
53
54     @Override
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)));
71             }
72         }
73     }
74 }