Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress tracker and message for CLI while cloning a template #1888

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/spotbugs/filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,10 @@
<Bug pattern="SF_SWITCH_FALLTHROUGH"/>
</Match>

<!-- It is not worth catching if the closing task of a progress bar fails to execute -->
<Match>
<Class name="software.amazon.smithy.cli.ProgressTracker"/>
<Bug pattern="DE_MIGHT_IGNORE"/>
</Match>

</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.cli;

import software.amazon.smithy.utils.StringUtils;


public interface ProgressStyle {
hpmellema marked this conversation as resolved.
Show resolved Hide resolved

void updateAction(Command.Env env);

void closeAction(Command.Env env);

static ColorBuffer getBuffer(Command.Env env) {
return ColorBuffer.of(env.colors(), env.stdout());
}

static ProgressStyle dots(String progressMessage, String closeMessage) {
return new ProgressStyle() {
private static final String PROGRESS_CHAR = ".";
private static final int TICKER_LENGTH = 3;

private int totalTicks = 0;
private final long startTimeMillis = System.currentTimeMillis();

@Override
public void updateAction(Command.Env env) {
int tickNumber = totalTicks % (TICKER_LENGTH + 1);
String loadStr = StringUtils.repeat(PROGRESS_CHAR, tickNumber)
+ StringUtils.repeat(" ", TICKER_LENGTH - tickNumber);
try (ColorBuffer buffer = getBuffer(env)) {
buffer.print("\r")
.print(progressMessage, ColorTheme.NOTE)
.print(loadStr, ColorTheme.NOTE);
}
totalTicks += 1;
}

@Override
public void closeAction(Command.Env env) {
try (ColorBuffer buffer = getBuffer(env)) {
buffer.print("\r")
.print(closeMessage, ColorTheme.SUCCESS)
.print(" [", ColorTheme.MUTED)
.print((System.currentTimeMillis() - startTimeMillis) / 1000.0 + "s",
ColorTheme.NOTE)
.print("]", ColorTheme.MUTED)
.println();
}
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.cli;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class ProgressTracker implements AutoCloseable {
hpmellema marked this conversation as resolved.
Show resolved Hide resolved
private static final ScheduledThreadPoolExecutor EXECUTOR = new ScheduledThreadPoolExecutor(1, runnable -> {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
thread.setDaemon(true);
return thread;
});
private static final long INTERVAL_MILLIS = 400L;
private final ScheduledFuture<?> task;
private final ProgressStyle style;
private final Command.Env env;
private final boolean quiet;

public ProgressTracker(Command.Env env, ProgressStyle style, boolean quiet) {
this.env = env;
this.style = style;
this.quiet = quiet;
// Do not print a progress bar if the quiet setting is enabled
if (quiet) {
task = null;
} else {
task = EXECUTOR.scheduleAtFixedRate(this::write, 0, INTERVAL_MILLIS, TimeUnit.MILLISECONDS);
}
}

@Override
public void close() {
if (!quiet) {
task.cancel(false);
try {
EXECUTOR.schedule(this::executeClose, 0, TimeUnit.NANOSECONDS).get();
} catch (ExecutionException | InterruptedException e) { /* ignored */ }
}
}

private void write() {
style.updateAction(env);
// Flush so the output is written immediately
env.stdout().flush();
}

private void executeClose() {
style.closeAction(env);
// Flush so the output is written immediately
env.stdout().flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import software.amazon.smithy.cli.ColorTheme;
import software.amazon.smithy.cli.Command;
import software.amazon.smithy.cli.HelpPrinter;
import software.amazon.smithy.cli.ProgressStyle;
import software.amazon.smithy.cli.ProgressTracker;
import software.amazon.smithy.cli.StandardOptions;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
Expand Down Expand Up @@ -188,13 +190,18 @@ private void cloneTemplate(Path temp, ObjectNode smithyTemplatesNode, Options op
final String templatePath = getTemplatePath(templateNode, template);
List<String> includedFiles = getIncludedFiles(templateNode);

// Specify the subdirectory to download
exec(ListUtils.of("git", "sparse-checkout", "set", "--no-cone", templatePath), temp);
// add any additional files that should be included
for (String includedFile : includedFiles) {
exec(ListUtils.of("git", "sparse-checkout", "add", "--no-cone", includedFile), temp);
try (ProgressTracker t = new ProgressTracker(env,
ProgressStyle.dots("cloning template", "template cloned"),
standardOptions.quiet()
)) {
// Specify the subdirectory to download
exec(ListUtils.of("git", "sparse-checkout", "set", "--no-cone", templatePath), temp);
// add any additional files that should be included
for (String includedFile : includedFiles) {
exec(ListUtils.of("git", "sparse-checkout", "add", "--no-cone", includedFile), temp);
}
exec(ListUtils.of("git", "checkout"), temp);
}
exec(ListUtils.of("git", "checkout"), temp);



Expand Down