Skip to content

Commit

Permalink
#63 - Fixed & refactored code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris committed Dec 20, 2020
1 parent 64ef1e1 commit ce575ef
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 166 deletions.
16 changes: 1 addition & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<!-- START Common stuff, can be copied and changed for other plugins -->
<groupId>io.github.chris2011.netbeans.minifierbeans</groupId>
<artifactId>Minifierbeans</artifactId>
<version>3.3.0</version>
<version>3.4.0</version>
<packaging>nbm</packaging>
<name>Minifierbeans</name>
<url>https://github.com/Chris2011/minifierbeans</url>
Expand Down Expand Up @@ -85,20 +85,6 @@
</archive>
</configuration>
</plugin>
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>-->
</plugins>
</build>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.netbeans.api.progress.ProgressHandle;
import org.netbeans.api.progress.ProgressHandleFactory;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import io.github.chris2011.netbeans.minifierbeans.ui.MinifyProperty;
Expand Down Expand Up @@ -62,19 +61,22 @@ public CSSMinify(DataObject context) {

@Override
public void actionPerformed(ActionEvent ev) {
execute(context, null, true);
execute(context, null, null, true);
}

public static void execute(final DataObject context, final String content, final boolean notify) {
public static void execute(final DataObject context, final FileObject file, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
cssMinify(context, content, notify);
cssMinify(context, file, content, notify);
}
};

final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandleFactory.createHandle("Minifying CSS " + context.getPrimaryFile().getName(), theTask);

String foString = context == null ? file.getName() : context.getPrimaryFile().getName();

final ProgressHandle ph = ProgressHandle.createHandle("Minifying CSS " + foString, theTask);

theTask.addTaskListener(new TaskListener() {
@Override
Expand All @@ -87,20 +89,26 @@ public void taskFinished(org.openide.util.Task task) {
theTask.schedule(0);
}

private static void cssMinify(DataObject context, String content, boolean notify) {
private static void cssMinify(DataObject context, FileObject file, String content, boolean notify) {
Project project = TopComponent.getRegistry().getActivated().getLookup().lookup(Project.class);
FileObject file = context.getPrimaryFile();
FileObject primaryFile = null;

if (context != null) {
primaryFile = context.getPrimaryFile();
} else {
primaryFile = file;
}

MinifyProperty minifyProperty = MinifyProperty.getInstance();
MinifyUtil util = new MinifyUtil();
MinifyFileResult minifyFileResult = new MinifyFileResult();

if (!util.isMinifiedFile(file.getName(), minifyProperty.getPreExtensionCSS())) {
String inputFilePath = file.getPath();
if (!util.isMinifiedFile(primaryFile.getName(), minifyProperty.getPreExtensionCSS())) {
String inputFilePath = primaryFile.getPath();
String outputFilePath;

if (minifyProperty.isNewCSSFile() && minifyProperty.getPreExtensionCSS() != null && !minifyProperty.getPreExtensionCSS().trim().isEmpty()) {
outputFilePath = file.getParent().getPath() + "/" + file.getName() + minifyProperty.getPreExtensionCSS() + "." + file.getExt();
outputFilePath = primaryFile.getParent().getPath() + "/" + primaryFile.getName() + minifyProperty.getPreExtensionCSS() + "." + primaryFile.getExt();
} else {
outputFilePath = inputFilePath;
}
Expand All @@ -114,7 +122,7 @@ private static void cssMinify(DataObject context, String content, boolean notify
outputWriter.flush();

if (project == null) {
project = FileOwnerQuery.getOwner(file);
project = FileOwnerQuery.getOwner(primaryFile);
}

PostCssCliExecutable postCssCliExecutable = PostCssCliExecutable.getDefault(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,31 @@
})
@Messages("CTL_JSMinify=Minify JS")
public final class JSMinify implements ActionListener {

private final DataObject context;
private final static RequestProcessor RP = new RequestProcessor("JSMinify", 1, true);

public JSMinify(DataObject context) {
this.context = context;
}
private final static RequestProcessor RP = new RequestProcessor("JSMinify", 1, true);

@Override
public void actionPerformed(ActionEvent ev) {
execute(context, null, true);
execute(context, null, null, true);
}

public static void execute(final DataObject context, final String content, final boolean notify) {
public static void execute(final DataObject context, final FileObject file, final String content, final boolean notify) {
Runnable runnable = new Runnable() {
@Override
public void run() {
jsMinify(context, content, notify);
jsMinify(context, file, content, notify);
}
};

final RequestProcessor.Task theTask = RP.create(runnable);
final ProgressHandle ph = ProgressHandle.createHandle("Minifying JS " + context.getPrimaryFile().getName(), theTask);

String foString = context == null ? file.getName() : context.getPrimaryFile().getName();

final ProgressHandle ph = ProgressHandle.createHandle("Minifying JS " + foString, theTask);

theTask.addTaskListener(new TaskListener() {
@Override
Expand All @@ -87,20 +89,26 @@ public void taskFinished(org.openide.util.Task task) {
theTask.schedule(0);
}

private static void jsMinify(DataObject context, String content, boolean notify) {
private static void jsMinify(DataObject context, FileObject file, String content, boolean notify) {
Project project = TopComponent.getRegistry().getActivated().getLookup().lookup(Project.class);
FileObject file = context.getPrimaryFile();
FileObject primaryFile = null;

if (context != null) {
primaryFile = context.getPrimaryFile();
} else {
primaryFile = file;
}

MinifyProperty minifyProperty = MinifyProperty.getInstance();
MinifyUtil util = new MinifyUtil();
MinifyFileResult minifyFileResult = new MinifyFileResult();

if (!util.isMinifiedFile(file.getName(), minifyProperty.getPreExtensionJS())) {
String inputFilePath = file.getPath();
if (!util.isMinifiedFile(primaryFile.getName(), minifyProperty.getPreExtensionJS())) {
String inputFilePath = primaryFile.getPath();
String outputFilePath;

if (minifyProperty.isNewJSFile() && minifyProperty.getPreExtensionJS() != null && !minifyProperty.getPreExtensionJS().trim().isEmpty()) {
outputFilePath = file.getParent().getPath() + "/" + file.getName() + minifyProperty.getPreExtensionJS() + "." + file.getExt();
outputFilePath = primaryFile.getParent().getPath() + "/" + primaryFile.getName() + minifyProperty.getPreExtensionJS() + "." + primaryFile.getExt();
} else {
outputFilePath = inputFilePath;
}
Expand All @@ -114,7 +122,7 @@ private static void jsMinify(DataObject context, String content, boolean notify)
outputWriter.flush();

if (project == null) {
project = FileOwnerQuery.getOwner(file);
project = FileOwnerQuery.getOwner(primaryFile);
}

GoogleClosureCompilerCliExecutable googleClosureCompilerCliExecutable = GoogleClosureCompilerCliExecutable.getDefault(project);
Expand All @@ -135,6 +143,7 @@ private static void jsMinify(DataObject context, String content, boolean notify)
// } else {
// minifyFileResult = util.compress(inputFilePath, "text/javascript", outputFilePath, minifyProperty);
// }

if (minifyProperty.isEnableOutputLogAlert() && notify) {
NotificationDisplayer.getDefault().notify("Successful JS minification",
NotificationDisplayer.Priority.NORMAL.getIcon(), String.format(
Expand All @@ -145,4 +154,4 @@ private static void jsMinify(DataObject context, String content, boolean notify)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -334,27 +334,4 @@ void fireChange() {
public void stateChanged(ChangeEvent e) {
fireChange();
}

// private final class DefaultDocumentListener implements DocumentListener {
//
// @Override
// public void insertUpdate(DocumentEvent e) {
// processUpdate();
// }
//
// @Override
// public void removeUpdate(DocumentEvent e) {
// processUpdate();
// }
//
// @Override
// public void changedUpdate(DocumentEvent e) {
// processUpdate();
// }
//
// private void processUpdate() {
// fireChange();
// }
//
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.github.chris2011.netbeans.minifierbeans.task;


import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import io.github.chris2011.netbeans.minifierbeans.ui.MinifyProperty;
Expand All @@ -30,7 +29,6 @@
import org.openide.util.Exceptions;

public class EditorSaveTask implements OnSaveTask {

private final Context context;

public EditorSaveTask(Context ctx) {
Expand All @@ -39,23 +37,23 @@ public EditorSaveTask(Context ctx) {

@Override
public void performTask() {
try {
try {

Document document = context.getDocument();
String content = document.getText(0, document.getLength());
DataObject dataObject = NbEditorUtilities.getDataObject(context.getDocument());
if(dataObject.getPrimaryFile().getMIMEType().equals("text/html") && MinifyProperty.getInstance().isAutoMinifyHTML()){
HTMLMinify.execute(dataObject,content,false);
} else if(dataObject.getPrimaryFile().getMIMEType().equals("text/javascript") && MinifyProperty.getInstance().isAutoMinifyJS()){
JSMinify.execute(dataObject,content,false);
} else if(dataObject.getPrimaryFile().getMIMEType().equals("text/css") && MinifyProperty.getInstance().isAutoMinifyCSS()){
CSSMinify.execute(dataObject,content,false);
} else if(dataObject.getPrimaryFile().getMIMEType().equals("text/x-json") && MinifyProperty.getInstance().isAutoMinifyJSON()){
JSONMinify.execute(dataObject,content,false);
} else if(dataObject.getPrimaryFile().getMIMEType().equals("text/xml-mime") && MinifyProperty.getInstance().isAutoMinifyXML()){
XMLMinify.execute(dataObject,content,false);
}

DataObject dataObject = NbEditorUtilities.getDataObject(context.getDocument());
if (dataObject.getPrimaryFile().getMIMEType().equals("text/html") && MinifyProperty.getInstance().isAutoMinifyHTML()) {
HTMLMinify.execute(dataObject, content, false);
} else if (dataObject.getPrimaryFile().getMIMEType().equals("text/javascript") && MinifyProperty.getInstance().isAutoMinifyJS()) {
JSMinify.execute(dataObject, null, content, false);
} else if (dataObject.getPrimaryFile().getMIMEType().equals("text/css") && MinifyProperty.getInstance().isAutoMinifyCSS()) {
CSSMinify.execute(dataObject, null, content, false);
} else if (dataObject.getPrimaryFile().getMIMEType().equals("text/x-json") && MinifyProperty.getInstance().isAutoMinifyJSON()) {
JSONMinify.execute(dataObject, content, false);
} else if (dataObject.getPrimaryFile().getMIMEType().equals("text/xml-mime") && MinifyProperty.getInstance().isAutoMinifyXML()) {
XMLMinify.execute(dataObject, content, false);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
Expand All @@ -71,5 +69,4 @@ public boolean cancel() {
return true;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@MimeRegistration(mimeType = "text/javascript", service = OnSaveTask.Factory.class),
@MimeRegistration(mimeType = "text/css", service = OnSaveTask.Factory.class),
@MimeRegistration(mimeType = "text/x-json", service = OnSaveTask.Factory.class),
@MimeRegistration(mimeType = "text/xml-mime", service = OnSaveTask.Factory.class)
@MimeRegistration(mimeType = "text/xml", service = OnSaveTask.Factory.class)
})
public class EditorSaveTaskFactory implements OnSaveTask.Factory {

Expand Down
Loading

0 comments on commit ce575ef

Please sign in to comment.