Skip to content

Commit

Permalink
Better error reporting when internet is not accessible, added support…
Browse files Browse the repository at this point in the history
… for offline mode that will not access the internet for any reason.
  • Loading branch information
gbevin committed Jul 18, 2024
1 parent 8e02a3a commit d904fd2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 58 deletions.
Binary file modified lib/bld/bld-wrapper.jar
Binary file not shown.
24 changes: 20 additions & 4 deletions src/main/java/rife/bld/BaseProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,12 @@ public void compile()
@BuildCommand(value = "dependency-tree", help = DependencyTreeHelp.class)
public void dependencyTree()
throws Exception {
dependencyTreeOperation().executeOnce(() -> dependencyTreeOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: dependency-tree is disabled");
}
else {
dependencyTreeOperation().executeOnce(() -> dependencyTreeOperation().fromProject(this));
}
}

/**
Expand All @@ -496,7 +501,12 @@ public void dependencyTree()
@BuildCommand(help = DownloadHelp.class)
public void download()
throws Exception {
downloadOperation().executeOnce(() -> downloadOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: download is disabled");
}
else {
downloadOperation().executeOnce(() -> downloadOperation().fromProject(this));
}
}

/**
Expand All @@ -507,7 +517,12 @@ public void download()
@BuildCommand(help = PurgeHelp.class)
public void purge()
throws Exception {
purgeOperation().executeOnce(() -> purgeOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: purge is disabled");
}
else {
purgeOperation().executeOnce(() -> purgeOperation().fromProject(this));
}
}

/**
Expand Down Expand Up @@ -1674,7 +1689,8 @@ private void writeHash(File hashFile, String hash) {

@Override
public int execute(String[] arguments) {
if (autoDownloadPurge()) {
if (!isOffline() &&
autoDownloadPurge()) {
performAutoDownloadPurge();
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/rife/bld/BuildExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class BuildExecutor {
public static final String BLD_PROPERTIES = "bld.properties";
public static final String LOCAL_PROPERTIES = "local.properties";

private static final String ARG_OFFLINE = "--offline";
private static final String ARG_HELP1 = "--help";
private static final String ARG_HELP2 = "-h";
private static final String ARG_HELP3 = "-?";
Expand All @@ -42,6 +43,7 @@ public class BuildExecutor {

private final HierarchicalProperties properties_;
private List<String> arguments_ = Collections.emptyList();
private boolean offline_ = false;
private Map<String, CommandDefinition> buildCommands_ = null;
private Map<String, String> buildAliases_ = null;
private final AtomicReference<String> currentCommandName_ = new AtomicReference<>();
Expand Down Expand Up @@ -126,7 +128,18 @@ public static HierarchicalProperties setupProperties(File workDirectory) {
}

/**
* Returns the properties uses by this conversation.
* Returns whether the bld execution is intended to be offline.
*
* @return {@code true} if the execution is intended to be offline;
* or {@code false} otherwise
* @since 2.0
*/
public boolean isOffline() {
return offline_;
}

/**
* Returns the properties uses for bld execution.
*
* @return the instance of {@code HierarchicalProperties} that is used
* by this build executor
Expand Down Expand Up @@ -275,6 +288,11 @@ private void outputCommandExecutionException(Throwable e) {
* @since 1.5.1
*/
public void start(String[] arguments) {
if (arguments.length > 0 && arguments[0].equals(ARG_OFFLINE)) {
offline_ = true;
arguments = Arrays.copyOfRange(arguments, 1, arguments.length);
}

System.exit(execute(arguments));
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/java/rife/bld/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ public void publish()
jar();
jarSources();
jarJavadoc();
publishOperation().executeOnce(() -> publishOperation().fromProject(this));
if (isOffline()) {
System.out.println("Offline mode: publish is disabled");
}
else {
publishOperation().executeOnce(() -> publishOperation().fromProject(this));
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/rife/bld/operations/HelpOperation.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private void executePrintOverviewHelp(Exception exception) {

System.err.println("""
--offline Work without internet (only as first argument)
--json Output help in JSON format
-?, -h, --help Shows this help message
-D<name>=<value> Set a JVM system property
Expand Down
117 changes: 65 additions & 52 deletions src/main/java/rife/bld/wrapper/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ private enum LaunchMode {

public static final String BUILD_ARGUMENT = "--build";
public static final String JSON_ARGUMENT = "--json";
public static final String OFFLINE_ARGUMENT = "--offline";
public static final String HELP_COMMAND = "help";

static final String MAVEN_CENTRAL = "https://repo1.maven.org/maven2/";
Expand Down Expand Up @@ -79,6 +80,7 @@ private enum LaunchMode {

private File currentDir_ = new File(System.getProperty("user.dir"));
private LaunchMode launchMode_ = LaunchMode.Cli;
private boolean offline_ = false;

private final Properties jvmProperties_ = new Properties();
private final Properties wrapperProperties_ = new Properties();
Expand Down Expand Up @@ -389,6 +391,15 @@ private int installAndLaunch(List<String> arguments) {
arguments.remove(0);
}

// first argument after the --build argument is the main build file
// arguments.get(0)

// check if the next argument enables offline mode
if (arguments.size() >= 2 &&
OFFLINE_ARGUMENT.equals(arguments.get(1))) {
offline_ = true;
}

var help_index = arguments.indexOf(HELP_COMMAND);
if (help_index != -1) {
try {
Expand All @@ -405,12 +416,7 @@ private int installAndLaunch(List<String> arguments) {
try {
extractJvmProperties(arguments);
initWrapperProperties(getVersion());
File distribution;
try {
distribution = installDistribution();
} catch (IOException e) {
return -1;
}
var distribution = installDistribution();
return launchMain(distribution, arguments);
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -513,66 +519,72 @@ private File installDistribution()
var download_version = version;
var is_snapshot = isSnapshot(version);
var is_local = false;
if (is_snapshot) {
var meta_data = "";
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata.xml")));
}
catch (IOException e) {
if (offline_) {
System.out.println("Offline mode: no artifacts will be checked nor downloaded");
System.out.flush();
}
else {
if (is_snapshot) {
var meta_data = "";
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata-local.xml")));
}
catch (IOException e2) {
throw e;
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata.xml")));
} catch (IOException e) {
try {
meta_data = readString(version, new URL(downloadUrl(version, "maven-metadata-local.xml")));
} catch (IOException e2) {
throw e;
}
}
}
var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data);
is_local = local_matcher.find();
if (!is_local) {
var version_matcher = META_DATA_SNAPSHOT_VERSION.matcher(meta_data);
if (version_matcher.find()) {
download_version = version_matcher.group(1);
var local_matcher = META_DATA_LOCAL_COPY.matcher(meta_data);
is_local = local_matcher.find();
if (!is_local) {
var version_matcher = META_DATA_SNAPSHOT_VERSION.matcher(meta_data);
if (version_matcher.find()) {
download_version = version_matcher.group(1);
}
}
}
}

var distribution_file = new File(DISTRIBUTIONS_DIR, bldFileName(version));
var distribution_sources_file = new File(DISTRIBUTIONS_DIR, bldSourcesFileName(version));

// if this is a snapshot and the distribution file exists,
// ensure that it's the latest by comparing hashes
if (is_snapshot && distribution_file.exists()) {
boolean delete_distribution_files = is_local;
if (!delete_distribution_files) {
var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5"));
try {
var digest = MessageDigest.getInstance("MD5");
digest.update(FileUtils.readBytes(distribution_file));
if (!download_md5.equals(encodeHexLower(digest.digest()))) {
delete_distribution_files = true;
if (!offline_) {
// if this is a snapshot and the distribution file exists,
// ensure that it's the latest by comparing hashes
if (is_snapshot && distribution_file.exists()) {
boolean delete_distribution_files = is_local;
if (!delete_distribution_files) {
var download_md5 = readString(version, new URL(downloadUrl(version, bldFileName(download_version)) + ".md5"));
try {
var digest = MessageDigest.getInstance("MD5");
digest.update(FileUtils.readBytes(distribution_file));
if (!download_md5.equals(encodeHexLower(digest.digest()))) {
delete_distribution_files = true;
}
} catch (NoSuchAlgorithmException ignore) {
}
} catch (NoSuchAlgorithmException ignore) {
}
}

if (delete_distribution_files) {
distribution_file.delete();
if (distribution_sources_file.exists()) {
distribution_sources_file.delete();
if (delete_distribution_files) {
distribution_file.delete();
if (distribution_sources_file.exists()) {
distribution_sources_file.delete();
}
}
}

}
}

// download distribution jars if necessary
if (!distribution_file.exists()) {
downloadDistribution(distribution_file, downloadUrl(version, bldFileName(download_version)));
}
if (!distribution_sources_file.exists()) {
try {
downloadDistribution(distribution_sources_file, downloadUrl(version, bldSourcesFileName(download_version)));
} catch (IOException e) {
// this is not critical, ignore
// download distribution jars if necessary
if (!distribution_file.exists()) {
downloadDistribution(distribution_file, downloadUrl(version, bldFileName(download_version)));
}
if (!distribution_sources_file.exists()) {
try {
downloadDistribution(distribution_sources_file, downloadUrl(version, bldSourcesFileName(download_version)));
} catch (IOException e) {
// this is not critical, ignore
}
}
}

Expand Down Expand Up @@ -618,7 +630,8 @@ private void downloadDistribution(File file, String downloadUrl)

private void resolveExtensions() {
if (null == classloader_ ||
null == wrapperPropertiesFile_) {
null == wrapperPropertiesFile_ ||
offline_) {
return;
}

Expand Down

0 comments on commit d904fd2

Please sign in to comment.