Skip to content

Commit

Permalink
gradle: implement dev server task
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Mar 13, 2024
1 parent 32ae1ab commit 078e9d7
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 5 deletions.
2 changes: 2 additions & 0 deletions tools/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ dependencies {
compileOnly(project(":jso:apis"))

api(project(":core"))
implementation(project(":tools:devserver"))

implementation(libs.commons.io)
implementation(libs.commons.cli)
}

teavmPublish {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 Alexey Andreev.
* Copyright 2024 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.cli;
package org.teavm.tooling.devserver;

import java.util.Arrays;
import org.apache.commons.cli.CommandLine;
Expand Down Expand Up @@ -94,6 +94,10 @@ private static void setupOptions() {
.desc("delegate requests from path")
.longOpt("proxy-path")
.build());
options.addOption(Option.builder()
.desc("don't watch file system changes")
.longOpt("no-watch")
.build());
}

private TeaVMDevServerRunner(CommandLine commandLine) {
Expand Down Expand Up @@ -144,6 +148,9 @@ private void parseArguments() {
if (commandLine.hasOption("proxy-path")) {
devServer.setProxyPath(commandLine.getOptionValue("proxy-path"));
}
if (commandLine.hasOption("no-watch")) {
devServer.setFileSystemWatched(false);
}

String[] args = commandLine.getArgs();
if (args.length != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public void waitForChange(int timeout) throws InterruptedException, IOException
if (!hasChanges()) {
take();
}
while (poll(timeout)) {
// continue polling
if (timeout > 0) {
while (poll(timeout)) {
// continue polling
}
}
while (pollNow()) {
// continue polling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public class CodeServlet extends HttpServlet {
private InMemorySymbolTable fileSymbolTable = new InMemorySymbolTable();
private InMemorySymbolTable variableSymbolTable = new InMemorySymbolTable();
private ReferenceCache referenceCache = new ReferenceCache();
private boolean fileSystemWatched = true;

public CodeServlet(String mainClass, String[] classPath) {
this.mainClass = mainClass;
Expand Down Expand Up @@ -201,6 +202,10 @@ public void setProxyPath(String proxyPath) {
this.proxyPath = normalizePath(proxyPath);
}

public void setFileSystemWatched(boolean fileSystemWatched) {
this.fileSystemWatched = fileSystemWatched;
}

public void addProgressHandler(ProgressHandler handler) {
synchronized (progressHandlers) {
progressHandlers.add(handler);
Expand Down Expand Up @@ -737,7 +742,19 @@ private void runTeaVM() {
synchronized (statusLock) {
waiting = true;
}
watcher.waitForChange(750);
if (fileSystemWatched) {
log.info("Waiting for changes in filesystem");
watcher.waitForChange(750);
} else {
log.info("Waiting for rebuild request");
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
watcher.waitForChange(0);
}
}
}
synchronized (statusLock) {
waiting = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class DevServer {
private boolean indicator;
private boolean deobfuscateStack;
private boolean reloadedAutomatically;
private boolean fileSystemWatched = true;
private TeaVMToolLog log;
private CodeServlet servlet;
private List<DevServerListener> listeners = new ArrayList<>();
Expand Down Expand Up @@ -88,6 +89,10 @@ public void setReloadedAutomatically(boolean reloadedAutomatically) {
this.reloadedAutomatically = reloadedAutomatically;
}

public void setFileSystemWatched(boolean fileSystemWatched) {
this.fileSystemWatched = fileSystemWatched;
}

public void setProxyUrl(String proxyUrl) {
this.proxyUrl = proxyUrl;
}
Expand Down Expand Up @@ -137,6 +142,7 @@ public void start() {
servlet.setDebugPort(debugPort);
servlet.setProxyUrl(proxyUrl);
servlet.setProxyPath(proxyPath);
servlet.setFileSystemWatched(fileSystemWatched);
for (DevServerListener listener : listeners) {
servlet.addListener(listener);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2024 Alexey Andreev.
*
* 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.teavm.gradle.tasks;

import javax.inject.Inject;
import org.gradle.api.DefaultTask;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Classpath;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.internal.logging.progress.ProgressLoggerFactory;

public abstract class CodeServerTask extends DefaultTask {
@Classpath
public abstract ConfigurableFileCollection getClasspath();

@Input
@Optional
public abstract Property<String> getTargetFileName();

@Input
@Optional
public abstract Property<String> getTargetFilePath();

@Input
@Optional
public abstract MapProperty<String, String> getProperties();

@Input
@Optional
public abstract ListProperty<String> getPreservedClasses();

@Input
public abstract Property<String> getMainClass();

@Input
@Optional
public abstract Property<Integer> getProcessMemory();

@Classpath
public abstract ConfigurableFileCollection getServerClasspath();

@Internal
public abstract Property<Integer> getServerDebugPort();

@Inject
protected abstract ProgressLoggerFactory getProgressLoggerFactory();
}

0 comments on commit 078e9d7

Please sign in to comment.