From 02477414c487b6b4c0d3aa6c93c81055721823f5 Mon Sep 17 00:00:00 2001 From: khadijahazward Date: Thu, 21 Mar 2024 15:40:04 +0530 Subject: [PATCH] Update copy doc-ui function --- .../docgen/docs/BallerinaDocGenerator.java | 88 +++++++++---------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java b/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java index 02ce0c0e11f9..429aff8be65c 100644 --- a/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java +++ b/misc/docerina/src/main/java/org/ballerinalang/docgen/docs/BallerinaDocGenerator.java @@ -50,12 +50,29 @@ import okhttp3.Response; import okhttp3.ResponseBody; -import java.io.*; +import java.io.BufferedReader; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Properties; import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -251,14 +268,15 @@ private static void writeAPIDocs(ModuleLibrary moduleLib, Path output, boolean i } private static void copyDocerinaUI(Path output) { + int retryCount = 0; Path docsDirPath = ProjectUtils.createAndGetHomeReposPath().resolve(DOCS_FOLDER_NAME); + Path sha256FilePath = docsDirPath.resolve(SHA256_HASH_FILE_NAME); + Path zipFilePath = docsDirPath.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME); OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .get() .url(SOURCE) .build(); - - int retryCount = 0; while (retryCount < MAX_RETRIES) { try (Response response = client.newCall(request).execute()) { if (response.code() == HTTP_OK && response.body() != null) { @@ -266,45 +284,34 @@ private static void copyDocerinaUI(Path output) { JsonObject jsonResponse = JsonParser.parseReader(responseBody.charStream()).getAsJsonObject(); String sha256HashValue = jsonResponse.get(JSON_KEY_HASH_VALUE).getAsString(); String zipFileURL = jsonResponse.get(JSON_KEY_FILE_URL).getAsString(); - - if (docsDirPath.toFile().exists() && Objects.requireNonNull(docsDirPath.toFile().list()).length > 0) { - Path sha256FilePath = docsDirPath.resolve(SHA256_HASH_FILE_NAME); - if(Files.exists(sha256FilePath)) { - String hashValueInCache = Files.readString(sha256FilePath).trim(); - if (sha256HashValue.equals(hashValueInCache)) { - copyDocUIToProjectDir(output, docsDirPath.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME)); - }else { - FileUtils.cleanDirectory(docsDirPath.toFile()); - writeFileInCache(docsDirPath, zipFileURL, sha256HashValue); - copyDocUIToProjectDir(output, docsDirPath.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME)); - } - } else { - //idk how to handle this, typically this should not happen unless the user manually deletes it - log.error("sha256 hash file does not exist in the .ballerina/docs folder."); - } - }else { + if (!Files.exists(sha256FilePath) || !Files.exists(zipFilePath)) { if (!docsDirPath.toFile().exists()) { Files.createDirectories(docsDirPath); } - writeFileInCache(docsDirPath, zipFileURL, sha256HashValue); - copyDocUIToProjectDir(output, docsDirPath.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME)); + writeFileInCache(zipFileURL, sha256HashValue, zipFilePath, sha256FilePath); + } else { + String hashValueInCache = Files.readString(sha256FilePath).trim(); + if (!sha256HashValue.equals(hashValueInCache)) { + writeFileInCache(zipFileURL, sha256HashValue, zipFilePath, sha256FilePath); + } } + copyDocUIToProjectDir(output, zipFilePath); break; - }else{ + } else { throw new IOException("Response failed with status code: " + response.code()); } } catch (IOException e) { retryCount++; if (retryCount == MAX_RETRIES) { - if (docsDirPath.toFile().exists() && Objects.requireNonNull(docsDirPath.toFile().list()).length > 0) { - String warning = String.format("********************************************************************%n" + - "* WARNING: The document is built using an outdated version of the UI. *%n" + - "********************************************************************%n%n"); + if (Files.exists(zipFilePath)) { + String warning = """ + WARNING: Unable to fetch the latest UI from the central. + This document is built using an existing version of the UI. + """; out.println(warning); - copyDocUIToProjectDir(output, docsDirPath.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME)); + copyDocUIToProjectDir(output, zipFilePath); } else { - //should be a better way to handle this?! - log.error("Failed to copy the doc UI"); + log.error("Failed to copy the doc UI", e); } } else { try { @@ -320,23 +327,17 @@ private static void copyDocerinaUI(Path output) { } } - private static void writeFileInCache(Path path, String fileURL, String hashValue) { + private static void writeFileInCache(String fileURL, String hashValue, Path zipFilePath, Path hashFilePath) throws IOException { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().get().url(fileURL).build(); try (Response response = client.newCall(request).execute()) { - if (response.code() == HTTP_OK) { + if (response.code() == HTTP_OK && response.body() != null) { ResponseBody responseBody = response.body(); - if (responseBody != null) { - Path zipFilePath = path.resolve(BALLERINA_DOC_UI_ZIP_FILE_NAME); - Files.write(zipFilePath, responseBody.bytes()); - Path hashFilePath = path.resolve(SHA256_HASH_FILE_NAME); - Files.write(hashFilePath, hashValue.getBytes()); - } else { - log.error("Unable to download doc-ui zip file: Response Body is empty."); - } + Files.write(zipFilePath, responseBody.bytes()); + Files.write(hashFilePath, hashValue.getBytes()); + } else { + throw new IOException("Failed to download doc-ui zip file: Request failed."); } - } catch (IOException e) { - log.error("Unable to download doc-ui zip file: " + e.getMessage()); } } @@ -352,7 +353,6 @@ private static void copyDocUIToProjectDir(Path output, Path zipFilePath) { try (OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(docUIFilePath.toFile()))) { byte[] buffer = new byte[1024]; int length; - while ((length = zipInputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); }