-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't write locks if both the locks and props change (#67)
Don't write locks if both the locks and props change
- Loading branch information
Showing
6 changed files
with
196 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
type: fix | ||
fix: | ||
description: Don't write locks if both the locks and props change | ||
links: | ||
- https://github.com/palantir/gradle-consistent-versions-idea-plugin/pull/67 |
67 changes: 67 additions & 0 deletions
67
...ugin/src/main/java/com/palantir/gradle/versions/intellij/DebouncingAsyncFileListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 com.palantir.gradle.versions.intellij; | ||
|
||
import com.intellij.openapi.Disposable; | ||
import com.intellij.openapi.vfs.AsyncFileListener; | ||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent; | ||
import com.intellij.util.Alarm; | ||
import com.intellij.util.SingleAlarm; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public final class DebouncingAsyncFileListener implements AsyncFileListener { | ||
private static final Logger log = LoggerFactory.getLogger(DebouncingAsyncFileListener.class); | ||
|
||
private final AsyncFileListener delegate; | ||
private final SingleAlarm alarm; | ||
private final BlockingQueue<VFileEvent> bufferedEvents = new LinkedBlockingQueue<>(); | ||
|
||
DebouncingAsyncFileListener(AsyncFileListener delegate, int debounceDelayMillis, Disposable parentDisposable) { | ||
this.delegate = delegate; | ||
this.alarm = new SingleAlarm( | ||
this::processEvents, debounceDelayMillis, parentDisposable, Alarm.ThreadToUse.POOLED_THREAD); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ChangeApplier prepareChange(List<? extends VFileEvent> events) { | ||
log.debug("Received events: {}", events); | ||
bufferedEvents.addAll(events); | ||
alarm.request(); | ||
return null; | ||
} | ||
|
||
private void processEvents() { | ||
List<VFileEvent> eventsToProcess = new ArrayList<>(); | ||
int drained = bufferedEvents.drainTo(eventsToProcess); | ||
if (drained == 0) { | ||
return; | ||
} | ||
|
||
log.debug("Processing debounced events: {}", eventsToProcess); | ||
AsyncFileListener.ChangeApplier applier = delegate.prepareChange(eventsToProcess); | ||
if (applier != null) { | ||
applier.afterVfsChange(); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...lugin/src/main/java/com/palantir/gradle/versions/intellij/FilteringAsyncFileListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 com.palantir.gradle.versions.intellij; | ||
|
||
import com.intellij.openapi.vfs.AsyncFileListener; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class FilteringAsyncFileListener implements AsyncFileListener { | ||
private static final Logger log = LoggerFactory.getLogger(FilteringAsyncFileListener.class); | ||
|
||
private final AsyncFileListener delegate; | ||
private final Predicate<VirtualFile> filter; | ||
|
||
FilteringAsyncFileListener(AsyncFileListener delegate, Predicate<VirtualFile> filter) { | ||
this.delegate = delegate; | ||
this.filter = filter; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public final ChangeApplier prepareChange(List<? extends VFileEvent> events) { | ||
List<? extends VFileEvent> filteredEvents = events.stream() | ||
.filter(event -> { | ||
VirtualFile file = event.getFile(); | ||
return file != null && filter.test(file); | ||
}) | ||
.toList(); | ||
|
||
log.debug("Events after filtering {}", filteredEvents); | ||
|
||
if (filteredEvents.isEmpty()) { | ||
return null; | ||
} | ||
|
||
return delegate.prepareChange(filteredEvents); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...in/src/main/java/com/palantir/gradle/versions/intellij/VersionPropsListenerRegistrar.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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 com.palantir.gradle.versions.intellij; | ||
|
||
import com.intellij.openapi.Disposable; | ||
import com.intellij.openapi.vfs.AsyncFileListener; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.openapi.vfs.newvfs.events.VFileEvent; | ||
import java.util.List; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public final class VersionPropsListenerRegistrar implements AsyncFileListener, Disposable { | ||
|
||
private final FilteringAsyncFileListener changeListener; | ||
|
||
VersionPropsListenerRegistrar() { | ||
this.changeListener = new FilteringAsyncFileListener( | ||
new DebouncingAsyncFileListener(new VersionPropsFileListener(), 250, this), this::isRelevantFile); | ||
} | ||
|
||
private boolean isRelevantFile(VirtualFile virtualFile) { | ||
String fileName = virtualFile.getName(); | ||
return "versions.props".equals(fileName) || "versions.lock".equals(fileName); | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public ChangeApplier prepareChange(List<? extends VFileEvent> events) { | ||
return changeListener.prepareChange(events); | ||
} | ||
|
||
@Override | ||
public void dispose() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters