Skip to content

Commit

Permalink
feat: add bun task
Browse files Browse the repository at this point in the history
  • Loading branch information
teletha committed Dec 17, 2024
1 parent 210f41e commit 28607bd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/bee/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
*/
public final class Platform {

/** OS */
public static final String OSName = System.getProperty("os.name");

/** OS */
public static final String OSVersion = System.getProperty("os.version");

/** OS */
public static final String OSArch = System.getProperty("os.arch");

/** The encoding. */
public static final Charset Encoding = Charset.forName(System.getProperty("sun.jnu.encoding"));

Expand Down Expand Up @@ -166,6 +175,15 @@ public static boolean isLinux() {
return isLinux;
}

/**
* Check whether the current platform is Linux like OS or not.
*
* @return Result
*/
public static boolean isMac() {
return OSName.contains("Mac") || OSName.contains("Darwin");
}

/**
* Check whether the current platform is Github Action or not.
*
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/bee/task/Bun.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2024 Nameless Production Committee
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://opensource.org/licenses/mit-license.php
*/
package bee.task;

import bee.Platform;
import bee.Task;
import bee.api.Command;
import bee.util.Process;

public class Bun extends Task {

@Command("Install bun.")
public void install() {
if (Process.isAvailable("bun")) {
ui.info("Bun [", Process.with().read("bun -v"), "] is already installed.");
} else {
Process.with()
.inheritIO()
.when(Platform::isWindows, "powershell -c \"irm bun.sh/install.ps1|iex\"")
.run("curl -fsSL https://bun.sh/install | bash");
}
}

@Command("Launch development server.")
public void dev() {
Process.with().inheritIO().run("bun run dev");
}
}
72 changes: 69 additions & 3 deletions src/main/java/bee/util/Process.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.stream.Collectors;

import bee.Platform;
Expand All @@ -35,16 +36,26 @@ public class Process {
/** The flag. */
private boolean showOutput = true;

/** The flag. */
private boolean inheritIO = false;

/** The exit code. */
private int exit;

/** The logger. */
private boolean verbose;

/** The overridden command for specific platform. */
private List<String> overriden;

/**
* Hide Constructor.
* Test command.
*
* @param command
* @return
*/
private Process() {
public static boolean isAvailable(String command) {
return Process.with().ignoreOutput().run(Platform.isWindows() ? "where" : "which", command) == 0;
}

/**
Expand Down Expand Up @@ -74,6 +85,12 @@ public static String readWith(Object... commands) {
return with().read(commands);
}

/**
* Hide Constructor.
*/
private Process() {
}

/**
* Set the working directory of the sub process.
*
Expand Down Expand Up @@ -135,6 +152,38 @@ public Process verbose() {
return this;
}

/**
* Configure user IO.
*
* @return
*/
public Process inheritIO() {
inheritIO = true;
return this;
}

/**
* Configure the platform specific command.
*
* @param command
* @return
*/
public Process when(BooleanSupplier condition, String command) {
if (condition.getAsBoolean()) {
overriden = List.of(command.split(" "));
}
return this;
}

/**
* Execute sub process.
*
* @param commands
*/
public int run(String commands) {
return run(List.of(commands.split(" ")));
}

/**
* Execute sub process.
*
Expand All @@ -160,6 +209,15 @@ public int run(List<String> command) {
return exit;
}

/**
* Execute sub process and accept its result.
*
* @param commands
*/
public String read(String commands) {
return read(List.of(commands.split(" ")));
}

/**
* Execute sub process and accept its result.
*
Expand Down Expand Up @@ -191,6 +249,10 @@ public String read(List<String> command) {
* @return
*/
private String run(List<String> command, boolean userOutput) {
if (overriden != null) {
command = overriden;
}

try {
ProcessBuilder builder = new ProcessBuilder();

Expand All @@ -207,7 +269,11 @@ private String run(List<String> command, boolean userOutput) {
builder.directory(directory.asJavaFile());
}

builder.redirectErrorStream(true);
if (inheritIO) {
builder.inheritIO();
} else {
builder.redirectErrorStream(true);
}
builder.command(command);

if (verbose) {
Expand Down

0 comments on commit 28607bd

Please sign in to comment.