Skip to content

Commit

Permalink
Merge master into feature/#63-minify-folder-switch-to-node
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore
	custom-packages/package.json
	custom-packages/yarn.lock
	pom.xml
	src/main/java/io/github/chris2011/netbeans/minifierbeans/javascript/JSMinify.java
  • Loading branch information
Chris committed Jul 7, 2021
2 parents f90df7b + 3b8f17a commit 9e6c379
Show file tree
Hide file tree
Showing 16 changed files with 799 additions and 963 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/nbproject
/build/
/target/
/custom-packages/node_modules
/custom-packages/node
/custom-packages/nbproject
/custom-packages/nbproject/
/nbproject/
/custom-packages.zip
/nb-configuration.xml
12 changes: 12 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## Update in 3.3.1

### General
1 - Added more error handling.
2 - Updated npm dependencies.

### Fixes
1 - [#72](https://github.com/Chris2011/minifierbeans/issues/72): Installation in Netbeans 12.3 on OpenJDK 11 with Minifierbeans-3.3.0 not working.
2 - [#75](https://github.com/Chris2011/minifierbeans/issues/75): Change --language_out from STABLE to other spec.
3 - [#82](https://github.com/Chris2011/minifierbeans/issues/82): Exception while unzipping.


## Update in 3.3.0

### General
Expand Down
13 changes: 6 additions & 7 deletions custom-packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
"version": "1.4.0",
"description": "CLIs to minify JS, CSS, HTML, XML and JSON.",
"devDependencies": {
"cssnano": "^4.1.10",
"google-closure-compiler": "^20210202.0.0",
"html-minifier-terser": "^5.1.1",
"minify-xml": "^2.5.0",
"postcss": "^8.2.6",
"postcss-cli": "^8.3.1",
"yargs": "^16.2.0"
"cssnano": "5.0.6",
"google-closure-compiler": "20210601.0.0",
"html-minifier-terser": "5.1.1",
"minify-xml": "2.5.0",
"postcss": "8.3.5",
"postcss-cli": "8.3.1"
}
}
1,186 changes: 436 additions & 750 deletions custom-packages/yarn.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -157,7 +157,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
<version>2.7</version>
<type>jar</type>
</dependency>
<dependency>
Expand Down Expand Up @@ -267,4 +267,4 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<netbeans.version>RELEASE90</netbeans.version>
</properties>
</project>
</project>
163 changes: 13 additions & 150 deletions src/main/java/io/github/chris2011/netbeans/minifierbeans/Installer.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
package io.github.chris2011.netbeans.minifierbeans;

import com.vdurmont.semver4j.Semver;
import java.io.BufferedInputStream;
import io.github.chris2011.netbeans.minifierbeans.util.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.apache.commons.io.*;
import org.json.JSONException;
import org.json.JSONObject;
import org.netbeans.api.progress.ProgressHandle;
import org.openide.awt.NotificationDisplayer;
import org.openide.modules.ModuleInstall;
import org.openide.util.Exceptions;
import org.openide.util.RequestProcessor;
import org.openide.windows.OnShowing;

@OnShowing
public class Installer extends ModuleInstall implements Runnable {

private final int BUFFER_SIZE = 4096;
private final String DOWNLOAD_URL = "https://github.com/Chris2011/minifierbeans/releases/download/1.0.0-cp/custom-packages.zip";
private static final RequestProcessor RP = new RequestProcessor(Installer.class);

@Override
Expand All @@ -48,20 +36,20 @@ public void run() {
String remoteVersion = getVersionFromRemotePackageJson();
String localVersion = getVersionFromLocalPackageJson();

if (localVersion.equals("") && deleteDirectory(customPackagesFolder.toFile())) {
downloadFile(DOWNLOAD_URL, System.getProperty("user.home"));
if (localVersion.equals("") && FileUtils.deleteDirectory(customPackagesFolder.toFile())) {
FileUtils.downloadFile(System.getProperty("user.home"));

return;
}

Semver remoteSemVersion = new Semver(remoteVersion);
Semver localSemVersion = new Semver(localVersion);

if (remoteSemVersion.isGreaterThan(localSemVersion) && deleteDirectory(customPackagesFolder.toFile())) {
downloadFile(DOWNLOAD_URL, System.getProperty("user.home"));
if (remoteSemVersion.isGreaterThan(localSemVersion) && FileUtils.deleteDirectory(customPackagesFolder.toFile())) {
FileUtils.downloadFile(System.getProperty("user.home"));
}
} else {
downloadFile(DOWNLOAD_URL, System.getProperty("user.home"));
FileUtils.downloadFile(System.getProperty("user.home"));
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
Expand Down Expand Up @@ -89,7 +77,7 @@ private String getVersionFromRemotePackageJson() {
return version.isEmpty() ? "" : version;
}

private String getVersionFromLocalPackageJson() {
private String getVersionFromLocalPackageJson() throws IOException {
String version = "";

try {
Expand All @@ -99,14 +87,15 @@ private String getVersionFromLocalPackageJson() {

if (f.exists()) {
InputStream is = new FileInputStream(packageJsonPath);

JSONObject json = new JSONObject(IOUtils.toString(is, "UTF-8"));
if(json.has("version")) {

if (json.has("version")) {
version = json.getString("version");
}

}

} catch (JSONException ex) {
FileUtils.downloadFile(System.getProperty("user.home"));
} catch (MalformedURLException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Expand All @@ -115,130 +104,4 @@ private String getVersionFromLocalPackageJson() {

return version.isEmpty() ? "" : version;
}

/**
* Downloads a file from a URL
*
* @param fileURL HTTP URL of the file to be downloaded
* @param saveDir path of the directory to save the file
* @throws IOException
*/
private void downloadFile(String fileURL, String saveDir)
throws IOException {
final ProgressHandle handle = ProgressHandle.createHandle("Downloading minifierbeans archive");
handle.start(0);

URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();

// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "custom-packages.zip";

// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();

final File theFile = new File(saveDir + "/.netbeans/minifierbeans");
theFile.mkdirs();

final String saveFilePath = saveDir + "/.netbeans/minifierbeans/" + fileName;

// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);

int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}

outputStream.close();
inputStream.close();
handle.finish();

RP.post(new Runnable() {
@Override
public void run() {
try {
extractArchive(saveFilePath, theFile.getAbsolutePath() + "/");
} catch (IOException ex) {
handle.finish();
Exceptions.printStackTrace(ex);
}
}
});
} else {
handle.finish();
NotificationDisplayer.getDefault().notify("Error while downloading", NotificationDisplayer.Priority.HIGH.getIcon(), "No file to download. Server replied HTTP code: " + responseCode, null);
}

httpConn.disconnect();
}

private boolean deleteDirectory(File directoryToBeDeleted) {
File[] allContents = directoryToBeDeleted.listFiles();

if(allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}

return directoryToBeDeleted.delete();
}

private void extractArchive(String fileZip, String destDir) throws FileNotFoundException, IOException {
ProgressHandle handle = ProgressHandle.createHandle("Extracting minifierbeans archive");

//Open the file
try ( ZipFile file = new ZipFile(fileZip)) {
FileSystem fileSystem = FileSystems.getDefault();
//Get file entries
Enumeration<? extends ZipEntry> entries = file.entries();

int bytesRead;
int nread = 0;
int length = file.size();

handle.start(0);

//We will unzip files in this folder
//Iterate over entries
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
//If directory then create a new directory in uncompressed folder
if (entry.isDirectory()) {
Files.createDirectories(fileSystem.getPath(destDir + entry.getName()));
} //Else create the file
else {
InputStream is = file.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
String uncompressedFileName = destDir + entry.getName();
Path uncompressedFilePath = fileSystem.getPath(uncompressedFileName);
Files.createFile(uncompressedFilePath);

try (FileOutputStream fileOutput = new FileOutputStream(uncompressedFileName)) {
while (bis.available() > 0) {
bytesRead = bis.read();
nread += bytesRead;

fileOutput.write(bytesRead);
}

handle.progress(nread);
} catch (Exception e) {
handle.finish();
NotificationDisplayer.getDefault().notify("Error while extracting", NotificationDisplayer.Priority.HIGH.getIcon(), e.getLocalizedMessage(), null);
}
}
}

handle.finish();
NotificationDisplayer.getDefault().notify("Extracting successful", NotificationDisplayer.Priority.NORMAL.getIcon(), "Done extracting", null);
} catch (IOException e) {
handle.finish();
NotificationDisplayer.getDefault().notify("Error while extracting", NotificationDisplayer.Priority.HIGH.getIcon(), e.getLocalizedMessage(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import io.github.chris2011.netbeans.minifierbeans.ui.MinifyProperty;
import io.github.chris2011.netbeans.minifierbeans.util.FileUtils;
import io.github.chris2011.netbeans.minifierbeans.util.source.minify.MinifyFileResult;
import io.github.chris2011.netbeans.minifierbeans.util.source.minify.MinifyUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.openide.loaders.DataObject;

import org.openide.awt.ActionRegistration;
Expand Down Expand Up @@ -132,20 +137,42 @@ private static void cssMinify(DataObject context, FileObject file, String conten
task.get(1, TimeUnit.MINUTES);

minifyFileResult.setOutputFileSize(outputFile.length());

if (minifyProperty.isEnableOutputLogAlert() && notify) {
NotificationDisplayer.getDefault().notify("Successful CSS minification",
NotificationDisplayer.Priority.NORMAL.getIcon(), String.format(
"Input CSS Files Size: %s Bytes \n"
+ "CSS Minified Completed Successfully\n"
+ "After Minifying CSS Files Size: %s Bytes \n"
+ "CSS Space Saved %s%%", minifyFileResult.getInputFileSize(), minifyFileResult.getOutputFileSize(), minifyFileResult.getSavedPercentage()), null);
}
} catch (NullPointerException ex) {
ex.printStackTrace();

NotificationDisplayer.getDefault().notify("Error on CLI execution", NotificationDisplayer.Priority.NORMAL.getIcon(), "Something went wrong. Please click this link to download and extract the binaries again.", new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
RP.post(new Runnable() {
@Override
public void run() {
try {
Path customPackagesFolder = Paths.get(System.getProperty("user.home") + "/.netbeans/minifierbeans/custom-packages");

if (Files.exists(customPackagesFolder) && FileUtils.deleteDirectory(customPackagesFolder.toFile())) {
FileUtils.downloadFile(System.getProperty("user.home"));
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
});
}
});
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (ExecutionException | TimeoutException ex) {
Exceptions.printStackTrace(ex);
}

if (minifyProperty.isEnableOutputLogAlert() && notify) {
NotificationDisplayer.getDefault().notify("Successful CSS minification",
NotificationDisplayer.Priority.NORMAL.getIcon(), String.format(
"Input CSS Files Size: %s Bytes \n"
+ "CSS Minified Completed Successfully\n"
+ "After Minifying CSS Files Size: %s Bytes \n"
+ "CSS Space Saved %s%%", minifyFileResult.getInputFileSize(), minifyFileResult.getOutputFileSize(), minifyFileResult.getSavedPercentage()), null);
}
}
}
}
}
Loading

0 comments on commit 9e6c379

Please sign in to comment.